From: Dominik Stadler Date: Sat, 22 Jan 2022 08:57:43 +0000 (+0000) Subject: Don't create an error-log when slightly broken spreadsheets contain an empty value... X-Git-Tag: REL_5_2_1~171 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7f1d84375201a0b5c12203c98638d30e2fa64726;p=poi.git Don't create an error-log when slightly broken spreadsheets contain an empty value for attribute "s" git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1897331 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java index 86c4dcfe78..d6b66c9578 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFSheetXMLHandler.java @@ -67,18 +67,18 @@ public class XSSFSheetXMLHandler extends DefaultHandler { /** * Table with the styles used for formatting */ - private Styles stylesTable; + private final Styles stylesTable; /** * Table with cell comments */ - private Comments comments; + private final Comments comments; /** * Read only access to the shared strings table, for looking * up (most) string cell's contents */ - private SharedStrings sharedStringsTable; + private final SharedStrings sharedStringsTable; /** * Where our text is going @@ -105,12 +105,12 @@ public class XSSFSheetXMLHandler extends DefaultHandler { private int rowNum; private int nextRowNum; // some sheets do not have rowNums, Excel can read them so we should try to handle them correctly as well private String cellRef; - private boolean formulasNotResults; + private final boolean formulasNotResults; // Gathers characters as they are seen. - private StringBuilder value = new StringBuilder(64); - private StringBuilder formula = new StringBuilder(64); - private StringBuilder headerFooter = new StringBuilder(64); + private final StringBuilder value = new StringBuilder(64); + private final StringBuilder formula = new StringBuilder(64); + private final StringBuilder headerFooter = new StringBuilder(64); private Queue commentCellRefs; @@ -413,13 +413,14 @@ public class XSSFSheetXMLHandler extends DefaultHandler { case SST_STRING: String sstIndex = value.toString(); - try { - int idx = Integer.parseInt(sstIndex); - RichTextString rtss = sharedStringsTable.getItemAt(idx); - thisStr = rtss.toString(); - } - catch (NumberFormatException ex) { - LOG.atError().withThrowable(ex).log("Failed to parse SST index '{}'", sstIndex); + if (sstIndex.length() > 0) { + try { + int idx = Integer.parseInt(sstIndex); + RichTextString rtss = sharedStringsTable.getItemAt(idx); + thisStr = rtss.toString(); + } catch (NumberFormatException ex) { + LOG.atError().withThrowable(ex).log("Failed to parse SST index '{}'", sstIndex); + } } break; diff --git a/poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestXSSFSheetXMLHandler.java b/poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestXSSFSheetXMLHandler.java index eaea83d972..0ddf7bf678 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestXSSFSheetXMLHandler.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestXSSFSheetXMLHandler.java @@ -68,4 +68,35 @@ public class TestXSSFSheetXMLHandler { } } } + + @Test + public void testNumber() throws Exception { + try (OPCPackage xlsxPackage = OPCPackage.open(_ssTests.openResourceAsStream("sample.xlsx"))) { + final XSSFReader reader = new XSSFReader(xlsxPackage); + + final Iterator iter = reader.getSheetsData(); + + try (InputStream stream = iter.next()) { + final XMLReader sheetParser = XMLHelper.getSaxParserFactory().newSAXParser().getXMLReader(); + + sheetParser.setContentHandler(new XSSFSheetXMLHandler(reader.getStylesTable(), + new ReadOnlySharedStringsTable(xlsxPackage), new SheetContentsHandler() { + @Override + public void startRow(final int rowNum) { + } + + @Override + public void endRow(final int rowNum) { + } + + @Override + public void cell(final String cellReference, final String formattedValue, + final XSSFComment comment) { + } + }, false)); + + sheetParser.parse(new InputSource(stream)); + } + } + } }