问题描述
我有一个将 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 片段:
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,Workbook workbook) throws IOException {
//Write to outputstream
}
目标是得到类似的东西
wrapHTMLBody(wrapStyle(htmlLayout.getHTML_STYLE()),table)
但我觉得我没有采取正确的方法并且我没有正确理解 SRP。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)