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
This commit is contained in:
David North 2015-08-19 11:10:13 +00:00
parent 1e45197f59
commit 2af2fa28c2
3 changed files with 33 additions and 7 deletions

View File

@ -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());
}
}
}

View File

@ -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.
}
}

Binary file not shown.