]> source.dussan.org Git - poi.git/commitdiff
allo XSSFReader to be sublclassed with implementation that allows OOXMl Strict files
authorPJ Fanning <fanningpj@apache.org>
Sat, 13 Feb 2021 19:43:07 +0000 (19:43 +0000)
committerPJ Fanning <fanningpj@apache.org>
Sat, 13 Feb 2021 19:43:07 +0000 (19:43 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1886493 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java
src/ooxml/testcases/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java

index 31e20e58a7a46114142d55d5036e2f6ecce11f93..3a13bfb07e7a9ada33db5e3754a14cc49827216b 100644 (file)
@@ -82,6 +82,16 @@ public class XSSFReader {
      * Creates a new XSSFReader, for the given package
      */
     public XSSFReader(OPCPackage pkg) throws IOException, OpenXML4JException {
+        this(pkg, false);
+    }
+
+    /**
+     * Creates a new XSSFReader, for the given package
+     *
+     * @param pkg an <code>OPCPackage</code> representing a spreasheet file
+     * @param allowStrictOoxmlFiles whether to try to handle Strict OOXML format files
+     */
+    public XSSFReader(OPCPackage pkg, boolean allowStrictOoxmlFiles) throws IOException, OpenXML4JException {
         this.pkg = pkg;
 
         PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType(
@@ -91,19 +101,23 @@ public class XSSFReader {
         // 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(
+            if (allowStrictOoxmlFiles) {
+                coreDocRelationship = this.pkg.getRelationshipsByType(
+                        PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0);
+            } else 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!");
+            if (coreDocRelationship == null) {
+                throw new POIXMLException("OOXML file structure broken/invalid - no core document found!");
+            }
         }
 
         // Get the part that holds the workbook
         workbookPart = this.pkg.getPart(coreDocRelationship);
     }
 
-
     /**
      * Opens up the Shared Strings Table, parses it, and
      * returns a handy object for working with
index 70c78b31847d56db7c3b4396fb87333917372544..697af659c7388c98a5642c83f17899b217ec2a1e 100644 (file)
@@ -334,6 +334,28 @@ public final class TestXSSFReader {
         }
     }
 
+    @Test
+    void testStrictOoxmlNotAllowed() throws Exception {
+        assertThrows(POIXMLException.class, () -> {
+            try (OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("sample.strict.xlsx"))) {
+                XSSFReader reader = new XSSFReader(pkg);
+            }
+        });
+        assertThrows(POIXMLException.class, () -> {
+            try (OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("sample.strict.xlsx"))) {
+                XSSFReader reader = new XSSFReader(pkg, false);
+            }
+        });
+    }
+
+    @Test
+    void testStrictOoxmlAllowed() throws Exception {
+        try (OPCPackage pkg = OPCPackage.open(_ssTests.openResourceAsStream("sample.strict.xlsx"))) {
+            XSSFReader reader = new XSSFReader(pkg, true);
+            assertNotNull(reader.pkg);
+        }
+    }
+
     private static String hash(XSSFReader reader) throws IOException {
         Iterable<InputStream> iter = () -> {
             try {