]> source.dussan.org Git - poi.git/commitdiff
Give a more helpful exception if a Visio VSDX ooxml file is passed to ExtractorFactory
authorNick Burch <nick@apache.org>
Wed, 11 Mar 2015 16:17:41 +0000 (16:17 +0000)
committerNick Burch <nick@apache.org>
Wed, 11 Mar 2015 16:17:41 +0000 (16:17 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1665929 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/extractor/ExtractorFactory.java
src/ooxml/testcases/org/apache/poi/extractor/TestExtractorFactory.java
test-data/diagram/test.vsdx [new file with mode: 0644]

index 46cd2cd386d0075b2c433c84b01e91ecf048c383..0bc7eb31db174c472b1954c10ad34160ef87cab4 100644 (file)
@@ -68,6 +68,8 @@ import org.apache.xmlbeans.XmlException;
 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";
 
 
        /** Should this thread prefer event based over usermodel based extractors? */
@@ -158,12 +160,25 @@ public class ExtractorFactory {
        }
 
        public static POIXMLTextExtractor createExtractor(OPCPackage pkg) throws IOException, OpenXML4JException, XmlException {
+          // Check for the normal Office core document
        PackageRelationshipCollection core =
             pkg.getRelationshipsByType(CORE_DOCUMENT_REL);
-       if(core.size() != 1) {
-          throw new IllegalArgumentException("Invalid OOXML Package received - expected 1 core document, found " + core.size());
+       
+       // If nothing was found, try some of the other OOXML-based core types
+       if (core.size() == 0) {
+           // Could it be a visio one?
+           PackageRelationshipCollection visio =
+                   pkg.getRelationshipsByType(VISIO_DOCUMENT_REL);
+           if (visio.size() == 1) {
+               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());
        }
 
+       // Grab the core document part, and try to identify from that
        PackagePart corePart = pkg.getPart(core.getRelationship(0));
 
        // Is it XSSF?
index 7f79afaad3c2529f235326e647545ac6d1d7b3dc..35198a80ea15c20055c8e17bbdadbf2a6cfbe67f 100644 (file)
@@ -71,6 +71,7 @@ public class TestExtractorFactory extends TestCase {
    private File msgEmbMsg;
    
    private File vsd;
+   private File vsdx;
    
    private File pub;
 
@@ -109,6 +110,7 @@ public class TestExtractorFactory extends TestCase {
 
       POIDataSamples dgTests = POIDataSamples.getDiagramInstance();
       vsd = getFileAndCheck(dgTests, "Test_Visio-Some_Random_Text.vsd");
+      vsdx = getFileAndCheck(dgTests, "test.vsdx");
       
       POIDataSamples pubTests = POIDataSamples.getPublisherInstance();
       pub = getFileAndCheck(pubTests, "Simple.pub");
@@ -230,7 +232,7 @@ public class TestExtractorFactory extends TestCase {
       );
       extractor.close();
 
-      // Visio
+      // Visio - binary
       assertTrue(
             ExtractorFactory.createExtractor(vsd)
             instanceof VisioTextExtractor
@@ -238,6 +240,13 @@ public class TestExtractorFactory extends TestCase {
       assertTrue(
             ExtractorFactory.createExtractor(vsd).getText().length() > 50
       );
+      // Visio - vsdx
+      try {
+          ExtractorFactory.createExtractor(vsdx);
+          fail();
+      } catch(IllegalArgumentException e) {
+          // Good
+      }
       
       // Publisher
       assertTrue(
@@ -342,6 +351,13 @@ public class TestExtractorFactory extends TestCase {
                assertTrue(
                                ExtractorFactory.createExtractor(new FileInputStream(vsd)).getText().length() > 50
                );
+             // Visio - vsdx
+             try {
+                 ExtractorFactory.createExtractor(new FileInputStream(vsdx));
+                 fail();
+             } catch(IllegalArgumentException e) {
+                 // Good
+             }
                
       // Publisher
       assertTrue(
diff --git a/test-data/diagram/test.vsdx b/test-data/diagram/test.vsdx
new file mode 100644 (file)
index 0000000..1fa6903
Binary files /dev/null and b/test-data/diagram/test.vsdx differ