From 598e148d2d7bd9805ab250c5dc6100bc0a0b0eb5 Mon Sep 17 00:00:00 2001 From: David North Date: Wed, 19 Aug 2015 11:10:13 +0000 Subject: [PATCH] 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 --- .../ReadOnlySharedStringsTable.java | 16 +++++++----- .../TestReadOnlySharedStringsTable.java | 24 ++++++++++++++++++ .../spreadsheet/noSharedStringTable.xlsx | Bin 0 -> 7820 bytes 3 files changed, 33 insertions(+), 7 deletions(-) create mode 100755 test-data/spreadsheet/noSharedStringTable.xlsx 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 0000000000000000000000000000000000000000..49f14658d0349191373d616c3dc55be785719f35 GIT binary patch literal 7820 zcmeHMg0LO?*ehwknMB?Xc0lu@JwhL(`-k{Up|!EfAs z_gmN9_b+(&JlFM{XX5@{Gw0m*ua2rBG72#O4S)dv0B8XD8eK!mNB{r^3IIR^z__C? zFH?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