aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDominik Stadler <centic@apache.org>2016-02-27 10:23:46 +0000
committerDominik Stadler <centic@apache.org>2016-02-27 10:23:46 +0000
commite1f1c5c01c5feb7a06aa03db35bbf9e5d6b6bdaa (patch)
treeb91f3654be113727bcfb12e9d4a5fb34cc8c90b9 /src
parentabc5c69c42729e512ca6cea31917c24948c48e42 (diff)
downloadpoi-e1f1c5c01c5feb7a06aa03db35bbf9e5d6b6bdaa.tar.gz
poi-e1f1c5c01c5feb7a06aa03db35bbf9e5d6b6bdaa.zip
Bug 57914: Provide a better error message for OOXML strict format which we do not support yet.
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1732619 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src')
-rw-r--r--src/integrationtest/org/apache/poi/TestAllFiles.java1
-rw-r--r--src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java4
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java12
-rw-r--r--src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java97
4 files changed, 77 insertions, 37 deletions
diff --git a/src/integrationtest/org/apache/poi/TestAllFiles.java b/src/integrationtest/org/apache/poi/TestAllFiles.java
index 1d578fb7f7..e2d26391d7 100644
--- a/src/integrationtest/org/apache/poi/TestAllFiles.java
+++ b/src/integrationtest/org/apache/poi/TestAllFiles.java
@@ -254,6 +254,7 @@ public class TestAllFiles {
EXPECTED_FAILURES.add("spreadsheet/SampleSS.strict.xlsx");
EXPECTED_FAILURES.add("spreadsheet/SimpleStrict.xlsx");
EXPECTED_FAILURES.add("spreadsheet/sample.strict.xlsx");
+ EXPECTED_FAILURES.add("spreadsheet/57914.xlsx");
// non-TNEF files
EXPECTED_FAILURES.add("ddf/Container.dat");
diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java
index 4d01e1ca78..665c51f6c4 100644
--- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java
+++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipCollection.java
@@ -273,13 +273,13 @@ public final class PackageRelationshipCollection implements
if (index < 0 || index > relationshipsByID.values().size())
throw new IllegalArgumentException("index");
- PackageRelationship retRel = null;
int i = 0;
for (PackageRelationship rel : relationshipsByID.values()) {
if (index == i++)
return rel;
}
- return retRel;
+
+ return null;
}
/**
diff --git a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java
index cc5fbb2cd4..2060a870a8 100644
--- a/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java
+++ b/src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java
@@ -69,6 +69,18 @@ public class XSSFReader {
PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType(
PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
+ // strict OOXML likely not fully supported, see #57699
+ // this code is similar to POIXMLDocumentPart.getPartFromOPCPackage(), but I could not combine it
+ // easily due to different return values
+ if(coreDocRelationship == null) {
+ if (this.pkg.getRelationshipsByType(
+ PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0) != null) {
+ throw new POIXMLException("Strict OOXML isn't currently supported, please see bug #57699");
+ }
+
+ throw new POIXMLException("OOXML file structure broken/invalid - no core document found!");
+ }
+
// Get the part that holds the workbook
workbookPart = this.pkg.getPart(coreDocRelationship);
}
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java
index c67447377c..3b5ac58ca7 100644
--- a/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java
+++ b/src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java
@@ -21,8 +21,8 @@ import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
-import junit.framework.TestCase;
-
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.POIXMLException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.XSSFTestDataSamples;
@@ -31,7 +31,8 @@ import org.apache.poi.xssf.model.StylesTable;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
-import org.apache.poi.POIDataSamples;
+
+import junit.framework.TestCase;
/**
* Tests for {@link XSSFReader}
@@ -168,38 +169,64 @@ public final class TestXSSFReader extends TestCase {
stream.close();
}
}
- /**
- * Test text extraction from text box using getShapes()
- * @throws Exception
- */
- public void testShapes() throws Exception{
- OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("WithTextBox.xlsx");
- XSSFReader r = new XSSFReader(pkg);
- XSSFReader.SheetIterator it = (XSSFReader.SheetIterator)r.getSheetsData();
-
- StringBuilder sb = new StringBuilder();
- while(it.hasNext())
- {
- it.next();
- List<XSSFShape> shapes = it.getShapes();
- if (shapes != null){
- for (XSSFShape shape : shapes){
- if (shape instanceof XSSFSimpleShape){
- String t = ((XSSFSimpleShape)shape).getText();
- sb.append(t).append('\n');
- }
- }
- }
- }
- String text = sb.toString();
- assertTrue(text.indexOf("Line 1") > -1);
- assertTrue(text.indexOf("Line 2") > -1);
- assertTrue(text.indexOf("Line 3") > -1);
- }
-
+ /**
+ * Test text extraction from text box using getShapes()
+ *
+ * @throws Exception
+ */
+ public void testShapes() throws Exception {
+ OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("WithTextBox.xlsx");
+ XSSFReader r = new XSSFReader(pkg);
+ XSSFReader.SheetIterator it = (XSSFReader.SheetIterator) r.getSheetsData();
+
+ String text = getShapesString(it);
+ assertTrue(text.contains("Line 1"));
+ assertTrue(text.contains("Line 2"));
+ assertTrue(text.contains("Line 3"));
+ }
+
+ private String getShapesString(XSSFReader.SheetIterator it) {
+ StringBuilder sb = new StringBuilder();
+ while (it.hasNext()) {
+ it.next();
+ List<XSSFShape> shapes = it.getShapes();
+ if (shapes != null) {
+ for (XSSFShape shape : shapes) {
+ if (shape instanceof XSSFSimpleShape) {
+ String t = ((XSSFSimpleShape) shape).getText();
+ sb.append(t).append('\n');
+ }
+ }
+ }
+ }
+ return sb.toString();
+ }
+
+ public void testBug57914() throws Exception {
+ OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("57914.xlsx");
+ final XSSFReader r;
+
+ // for now expect this to fail, when we fix 57699, this one should fail so we know we should adjust
+ // this test as well
+ try {
+ r = new XSSFReader(pkg);
+ fail("This will fail until bug 57699 is fixed");
+ } catch (POIXMLException e) {
+ assertTrue("Had " + e, e.getMessage().contains("57699"));
+ return;
+ }
+
+ XSSFReader.SheetIterator it = (XSSFReader.SheetIterator) r.getSheetsData();
+
+ String text = getShapesString(it);
+ assertTrue(text.contains("Line 1"));
+ assertTrue(text.contains("Line 2"));
+ assertTrue(text.contains("Line 3"));
+ }
+
/**
- * NPE from XSSFReader$SheetIterator.<init> on XLSX files generated by
+ * NPE from XSSFReader$SheetIterator.<init> on XLSX files generated by
* the openpyxl library
*/
public void test58747() throws Exception {
@@ -209,11 +236,11 @@ public final class TestXSSFReader extends TestCase {
XSSFReader reader = new XSSFReader(pkg);
StylesTable styles = reader.getStylesTable();
assertNotNull(styles);
-
+
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) reader.getSheetsData();
assertEquals(true, iter.hasNext());
iter.next();
-
+
assertEquals(false, iter.hasNext());
assertEquals("Orders", iter.getSheetName());
}