]> source.dussan.org Git - poi.git/commitdiff
Detect OOXML-strict, and give more helpful exceptions for them
authorNick Burch <nick@apache.org>
Fri, 13 Mar 2015 17:39:47 +0000 (17:39 +0000)
committerNick Burch <nick@apache.org>
Fri, 13 Mar 2015 17:39:47 +0000 (17:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1666525 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java
src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java
src/ooxml/testcases/org/apache/poi/extractor/TestExtractorFactory.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

index 4759ddbd3b78ff9b36764add926d0a20aa0da88f..f051eb32af5997e5d04d103deeb71507f4018bae 100644 (file)
@@ -94,6 +94,15 @@ public class POIXMLDocumentPart {
      */
     public POIXMLDocumentPart(OPCPackage pkg) {
         PackageRelationship coreRel = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
+        if (coreRel == null) {
+            coreRel = pkg.getRelationshipsByType(PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0);
+            if (coreRel != null) {
+                throw new POIXMLException("Strict OOXML isn't currently supported, please see bug #57699");
+            }
+        }
+        if (coreRel == null) {
+            throw new POIXMLException("OOXML file structure broken/invalid - no core document found!");
+        }
 
         this.packagePart = pkg.getPart(coreRel);
         this.packageRel = coreRel;
index 0bc7eb31db174c472b1954c10ad34160ef87cab4..b484e2ed06aa4bb8ea669f56929f8b26a1513eca 100644 (file)
@@ -47,6 +47,7 @@ import org.apache.poi.openxml4j.opc.OPCPackage;
 import org.apache.poi.openxml4j.opc.PackageAccess;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
+import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
 import org.apache.poi.poifs.filesystem.DirectoryEntry;
 import org.apache.poi.poifs.filesystem.DirectoryNode;
 import org.apache.poi.poifs.filesystem.Entry;
@@ -66,10 +67,9 @@ import org.apache.xmlbeans.XmlException;
  *  document, and returns it.
  */
 public class ExtractorFactory {
-       public static final String CORE_DOCUMENT_REL =
-               "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
-       public static final String VISIO_DOCUMENT_REL =
-           "http://schemas.microsoft.com/visio/2010/relationships/document";
+       public static final String CORE_DOCUMENT_REL = PackageRelationshipTypes.CORE_DOCUMENT;
+       protected static final String VISIO_DOCUMENT_REL = PackageRelationshipTypes.VISIO_CORE_DOCUMENT;
+       protected static final String STRICT_DOCUMENT_REL = PackageRelationshipTypes.STRICT_CORE_DOCUMENT;
 
 
        /** Should this thread prefer event based over usermodel based extractors? */
@@ -165,6 +165,10 @@ public class ExtractorFactory {
             pkg.getRelationshipsByType(CORE_DOCUMENT_REL);
        
        // If nothing was found, try some of the other OOXML-based core types
+       if (core.size() == 0) {
+           // Could it be an OOXML-Strict one?
+           core = pkg.getRelationshipsByType(STRICT_DOCUMENT_REL);
+       }
        if (core.size() == 0) {
            // Could it be a visio one?
            PackageRelationshipCollection visio =
@@ -173,6 +177,7 @@ public class ExtractorFactory {
                throw new IllegalArgumentException("Text extraction not supported for Visio OOXML files");
            }
        }
+       
        // Should just be a single core document, complain if not
        if (core.size() != 1) {
            throw new IllegalArgumentException("Invalid OOXML Package received - expected 1 core document, found " + core.size());
index 003c0eccf57295f0459f360a719d190ee3d30c82..a8644f420295c518cd9fdc3e234f391df0236bd9 100644 (file)
@@ -25,6 +25,7 @@ import junit.framework.TestCase;
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.POIOLE2TextExtractor;
 import org.apache.poi.POITextExtractor;
+import org.apache.poi.POIXMLException;
 import org.apache.poi.POIXMLTextExtractor;
 import org.apache.poi.hdgf.extractor.VisioTextExtractor;
 import org.apache.poi.hpbf.extractor.PublisherTextExtractor;
@@ -162,6 +163,12 @@ public class TestExtractorFactory extends TestCase {
         extractor.close();
 
         // TODO Support OOXML-Strict, see bug #57699
+        try {
+            extractor = ExtractorFactory.createExtractor(xlsxStrict);
+            fail("OOXML-Strict isn't yet supported");
+        } catch (POIXMLException e) {
+            // Expected, for now
+        }
 //        extractor = ExtractorFactory.createExtractor(xlsxStrict);
 //        assertTrue(
 //                extractor
@@ -307,6 +314,14 @@ public class TestExtractorFactory extends TestCase {
         assertTrue(
                 ExtractorFactory.createExtractor(new FileInputStream(xlsx)).getText().length() > 200
         );
+        // TODO Support OOXML-Strict, see bug #57699
+//        assertTrue(
+//                ExtractorFactory.createExtractor(new FileInputStream(xlsxStrict))
+//                instanceof XSSFExcelExtractor
+//        );
+//        assertTrue(
+//                ExtractorFactory.createExtractor(new FileInputStream(xlsxStrict)).getText().length() > 200
+//        );
 
         // Word
         assertTrue(
index 6334326c5e5b7d66894acccabd46d2477de695ea..bb9efa4073040d5272b258a73c7fed13ef165db7 100644 (file)
@@ -2293,13 +2293,19 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
     
     /**
      * OOXML-Strict files
+     * Not currently working - namespace mis-match from XMLBeans
      */
     @Test
-    @Ignore
+    @Ignore("XMLBeans namespace mis-match on ooxml-strict files")
     public void test57699() throws Exception {
-        Workbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.strict.xlsx");
+        XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("sample.strict.xlsx");
         assertEquals(3, wb.getNumberOfSheets());
+        // TODO Check sheet contents
+        // TODO Check formula evaluation
         
-        // TODO Check the rest
+        XSSFWorkbook wbBack = XSSFTestDataSamples.writeOutAndReadBack(wb);
+        assertEquals(3, wbBack.getNumberOfSheets());
+        // TODO Re-check sheet contents
+        // TODO Re-check formula evaluation
     }
 }