aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid North <dnorth@apache.org>2015-08-19 11:10:13 +0000
committerDavid North <dnorth@apache.org>2015-08-19 11:10:13 +0000
commit598e148d2d7bd9805ab250c5dc6100bc0a0b0eb5 (patch)
tree5176a1080d0db70bdd2c386a06bc72fb9c934b04
parent255ace6918dbdf7195e4e2a0db08284ed0131993 (diff)
downloadpoi-598e148d2d7bd9805ab250c5dc6100bc0a0b0eb5.tar.gz
poi-598e148d2d7bd9805ab250c5dc6100bc0a0b0eb5.zip
Improve ReadOnlySharedStringsTable to cope gracefully with an empty SST part.
https://bz.apache.org/bugzilla/show_bug.cgi?id=58262 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1696559 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java16
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestReadOnlySharedStringsTable.java24
-rwxr-xr-xtest-data/spreadsheet/noSharedStringTable.xlsxbin0 -> 7820 bytes
3 files changed, 33 insertions, 7 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java
index 5263473bca..24e2c649d3 100644
--- a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java
+++ b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/ReadOnlySharedStringsTable.java
@@ -132,13 +132,15 @@ public class ReadOnlySharedStringsTable extends DefaultHandler {
* @throws ParserConfigurationException
*/
public void readFrom(InputStream is) throws IOException, SAXException {
- InputSource sheetSource = new InputSource(is);
- try {
- XMLReader sheetParser = SAXHelper.newXMLReader();
- sheetParser.setContentHandler(this);
- sheetParser.parse(sheetSource);
- } catch(ParserConfigurationException e) {
- throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());
+ if (is.available() > 0) {
+ InputSource sheetSource = new InputSource(is);
+ try {
+ XMLReader sheetParser = SAXHelper.newXMLReader();
+ sheetParser.setContentHandler(this);
+ sheetParser.parse(sheetSource);
+ } catch(ParserConfigurationException e) {
+ throw new RuntimeException("SAX parser appears to be broken - " + e.getMessage());
+ }
}
}
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestReadOnlySharedStringsTable.java b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestReadOnlySharedStringsTable.java
index c4690369b9..ad8470baba 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestReadOnlySharedStringsTable.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestReadOnlySharedStringsTable.java
@@ -20,12 +20,16 @@
package org.apache.poi.xssf.eventusermodel;
import junit.framework.TestCase;
+
import org.apache.poi.POIDataSamples;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.xssf.model.SharedStringsTable;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
+import org.xml.sax.SAXException;
+import java.io.IOException;
import java.util.List;
import java.util.regex.Pattern;
@@ -55,4 +59,24 @@ public final class TestReadOnlySharedStringsTable extends TestCase {
}
}
+
+ public void testEmptySSTOnPackageObtainedViaWorkbook() throws Exception {
+ XSSFWorkbook wb = new XSSFWorkbook(_ssTests.openResourceAsStream("noSharedStringTable.xlsx"));
+ OPCPackage pkg = wb.getPackage();
+ assertEmptySST(pkg);
+ wb.close();
+ }
+
+ public void testEmptySSTOnPackageDirect() throws Exception {
+ OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("noSharedStringTable.xlsx"));
+ assertEmptySST(pkg);
+ }
+
+ private void assertEmptySST(OPCPackage pkg) throws IOException, SAXException {
+ ReadOnlySharedStringsTable sst = new ReadOnlySharedStringsTable(pkg);
+ assertEquals(0, sst.getCount());
+ assertEquals(0, sst.getUniqueCount());
+ assertNull(sst.getItems()); // same state it's left in if fed a package which has no SST part.
+ }
+
}
diff --git a/test-data/spreadsheet/noSharedStringTable.xlsx b/test-data/spreadsheet/noSharedStringTable.xlsx
new file mode 100755
index 0000000000..49f14658d0
--- /dev/null
+++ b/test-data/spreadsheet/noSharedStringTable.xlsx
Binary files differ