【发布时间】:2021-01-21 22:29:43
【问题描述】:
我有一项将 xls 文件转换为 html 的服务。它工作得很好,但它是一个相当大的方法,不遵循任何 SOLID 原则。因此,我想改进它以至少遵循单一责任原则。但我真的不知道如何应用它并在我的案例中找到抽象级别。
@Service
public class xlsToHtmlImpl implements MultipartFileToHtmlService {
private final HtmlLayout htmlLayout;
@Autowired
public xlsToHtmlImpl(HtmlLayout htmlLayout) {
this.htmlLayout = htmlLayout;
}
@Override
public InputStream multipartFileToHtml(MultipartFile multipartFile, boolean hasOnlyOneSheet, boolean hasBorders) throws IOException {
String fileName = multipartFile.getOriginalFilename();
BufferedInputStream inputStream = new BufferedInputStream(multipartFile.getInputStream());
Workbook workbook;
assert fileName != null;
//Selecting workbook depending on FileType
if (fileName.toLowerCase().endsWith(htmlLayout.FILE_TYPES[0])) {
workbook = new HSSFWorkbook(inputStream);
} else {
workbook = new XSSFWorkbook(inputStream);
}
//Writing content of multipartFile to outputstream
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(htmlLayout.openStyle());
//Selecting style to apply depending on user input
if (hasBorders) {
outputStream.write(htmlLayout.noBordersStyle());
} else {
outputStream.write(htmlLayout.withBordersStyle());
}
outputStream.write(htmlLayout.closeStyle());
outputStream.write(htmlLayout.openNewHtml());
outputStream.write(fileName.getBytes());
//Different algorithm for the content of the body depending on user input
Sheet sheet;
if (hasOnlyOneSheet) {
sheet = workbook.getSheetAt(0);
Iterator<Row> rows = sheet.rowIterator();
while (rows.hasNext()) {
Row row = rows.next();
Iterator<Cell> cells = row.cellIterator();
outputStream.write(htmlLayout.newLine());
outputStream.write(htmlLayout.newRow());
while (cells.hasNext()) {
Cell cell = cells.next();
outputStream.write(htmlLayout.newCell());
outputStream.write(cell.toString().getBytes());
outputStream.write(htmlLayout.closeCell());
}
outputStream.write(htmlLayout.closeRow());
}
} else {
for (int i = 0; i< workbook.getNumberOfSheets(); i++) {
sheet = workbook.getSheetAt(i);
Iterator<Row> rows = sheet.rowIterator();
outputStream.write(htmlLayout.newLine());
outputStream.write(htmlLayout.newRow());
outputStream.write(htmlLayout.closeCell());
outputStream.write(htmlLayout.closeRow());
outputStream.write(htmlLayout.newLine());
while (rows.hasNext()) {
Row row = rows.next();
Iterator<Cell> cells = row.cellIterator();
outputStream.write(htmlLayout.newLine());
outputStream.write(htmlLayout.newRow());
while (cells.hasNext()) {
Cell cell = cells.next();
outputStream.write(htmlLayout.newCell());
outputStream.write(cell.toString().getBytes());
outputStream.write(htmlLayout.closeCell());
}
outputStream.write(htmlLayout.closeRow());
}
}
}
outputStream.write(htmlLayout.newLine());
outputStream.write(htmlLayout.closeHtml());
outputStream.close();
//Returning result as ByteArrayInputStream to controller
return new ByteArrayInputStream(outputStream.toByteArray());
}
其中htmlLayout 包含html sn-p like:
public byte[] closeHtml() {return "</table></body></html>".getBytes();}
根据这篇文章,我尝试创建不同的类,如下所示:
public class HtmlStyleWrapper {
private byte[] style;
public byte[] withBordersStyle() {
return ("table, td{" +
" border: 1px solid black;\n" +
" border-collapse: collapse;\n" +
" padding: 9px;\n" +
"}").getBytes();
}
public byte[] noBordersStyle() {
return ("td {" +
" padding: 9px;\n" +
"}").getBytes();
}
public byte[] openStyle() {
return "</title></head><body><style>".getBytes();
}
public byte[] closeStyle() {
return "</style><table>".getBytes();
}
public void wrapStyle(ByteArrayOutputStream outputStream, boolean hasBorders) throws IOException {
outputStream.write(openStyle());
if (hasBorders) {
outputStream.write(noBordersStyle());
} else {
outputStream.write(withBordersStyle());
}
outputStream.write(closeStyle());
}
public class HtmlBodyWrapper {
private byte[] body;
public byte[] openNewHtml() {
return "<!DOCTYPE html><html><head><title>".getBytes();
}
public byte[] newLine() {
return "\n".getBytes();
}
public byte[] closeHtml() {
return "</table></body></html>".getBytes();
}
public byte[] newRow() {
return "<tr>".getBytes();
}
public byte[] closeRow() {
return "</tr>".getBytes();
}
public byte[] newCell() {
return "<td>".getBytes();
}
public byte[] closeCell() {
return "</td>".getBytes();
}
public void wrapBody(ByteArrayOutputStream outputStream, String fileName, boolean hasOnlyOneSheet, Workbook workbook) throws IOException {
//Write to outputstream
}
目标是得到类似的东西
wrapHTMLBody(wrapStyle(htmlLayout.getHTML_STYLE()), table)
但我觉得我没有采取正确的方法,而且我没有正确理解 SRP。
【问题讨论】:
-
我认为这个问题更适合codereview.stackexchange.com,因为代码工作得很好。
-
@Amongalen 我猜你是对的,我会转移到那里
标签: java design-patterns solid-principles single-responsibility-principle