poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From n...@apache.org
Subject svn commit: r1735060 - in /poi/trunk/src: java/org/apache/poi/poifs/filesystem/ ooxml/java/org/apache/poi/openxml4j/exceptions/ ooxml/java/org/apache/poi/openxml4j/opc/ ooxml/java/org/apache/poi/openxml4j/util/ ooxml/testcases/org/apache/poi/openxml4j/...
Date Tue, 15 Mar 2016 11:50:57 GMT
Author: nick
Date: Tue Mar 15 11:50:57 2016
New Revision: 1735060

URL: http://svn.apache.org/viewvc?rev=1735060&view=rev
Log:
Start to give more helpful Exceptions from OPCPackage when non-OOXML files are passed to it

Added:
    poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/NotOfficeXmlFileException.java
    poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/ODFNotOfficeXmlFileException.java
    poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OLE2NotOfficeXmlFileException.java
Modified:
    poi/trunk/src/java/org/apache/poi/poifs/filesystem/OfficeXmlFileException.java
    poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OpenXML4JException.java
    poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java
    poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java

Modified: poi/trunk/src/java/org/apache/poi/poifs/filesystem/OfficeXmlFileException.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/poifs/filesystem/OfficeXmlFileException.java?rev=1735060&r1=1735059&r2=1735060&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/poifs/filesystem/OfficeXmlFileException.java (original)
+++ poi/trunk/src/java/org/apache/poi/poifs/filesystem/OfficeXmlFileException.java Tue Mar
15 11:50:57 2016
@@ -17,12 +17,14 @@
         
 package org.apache.poi.poifs.filesystem;
 
+import org.apache.poi.UnsupportedFileFormatException;
+
 /**
  * This exception is thrown when we try to open a file that's actually
  *  an Office 2007+ XML file, rather than an OLE2 file (which is what
  *  POIFS works with)
  */
-public class OfficeXmlFileException extends IllegalArgumentException {
+public class OfficeXmlFileException extends UnsupportedFileFormatException {
 	public OfficeXmlFileException(String s) {
 		super(s);
 	}

Added: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/NotOfficeXmlFileException.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/NotOfficeXmlFileException.java?rev=1735060&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/NotOfficeXmlFileException.java
(added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/NotOfficeXmlFileException.java
Tue Mar 15 11:50:57 2016
@@ -0,0 +1,29 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.openxml4j.exceptions;
+
+import org.apache.poi.UnsupportedFileFormatException;
+
+/**
+ * This exception is thrown when we try to open a file that doesn't
+ *  seem to actually be an OOXML (Office Open XML) file after all
+ */
+public class NotOfficeXmlFileException extends UnsupportedFileFormatException {
+    public NotOfficeXmlFileException(String message) {
+        super(message);
+    }
+}

Added: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/ODFNotOfficeXmlFileException.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/ODFNotOfficeXmlFileException.java?rev=1735060&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/ODFNotOfficeXmlFileException.java
(added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/ODFNotOfficeXmlFileException.java
Tue Mar 15 11:50:57 2016
@@ -0,0 +1,27 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.openxml4j.exceptions;
+
+/**
+ * This exception is thrown when we are given an ODF-based file
+ *  (eg OpenOffice .ods) instead of an actually OOXML (Office Open XML) file
+ */
+public class ODFNotOfficeXmlFileException extends NotOfficeXmlFileException {
+    public ODFNotOfficeXmlFileException(String message) {
+        super(message);
+    }
+}

Added: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OLE2NotOfficeXmlFileException.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OLE2NotOfficeXmlFileException.java?rev=1735060&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OLE2NotOfficeXmlFileException.java
(added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OLE2NotOfficeXmlFileException.java
Tue Mar 15 11:50:57 2016
@@ -0,0 +1,27 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.poi.openxml4j.exceptions;
+
+/**
+ * This exception is thrown when we are given an OLE2-based file
+ *  (eg Excel .xls) instead of an actually OOXML (Office Open XML) file
+ */
+public class OLE2NotOfficeXmlFileException extends NotOfficeXmlFileException {
+    public OLE2NotOfficeXmlFileException(String message) {
+        super(message);
+    }
+}

Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OpenXML4JException.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OpenXML4JException.java?rev=1735060&r1=1735059&r2=1735060&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OpenXML4JException.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OpenXML4JException.java Tue
Mar 15 11:50:57 2016
@@ -21,9 +21,6 @@ package org.apache.poi.openxml4j.excepti
  * Global exception throws when a critical error occurs. (this exception is not
  * set as Runtime in order to force user to manage the exception in a
  * try/catch).
- *
- * @author CDubettier, Julien Chable
- * @version 1.0
  */
 @SuppressWarnings("serial")
 public class OpenXML4JException extends Exception {

Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java?rev=1735060&r1=1735059&r2=1735060&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java Tue Mar 15 11:50:57
2016
@@ -29,6 +29,7 @@ import java.util.zip.ZipOutputStream;
 
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
+import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
 import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
 import org.apache.poi.openxml4j.opc.internal.ContentTypeManager;
@@ -176,6 +177,7 @@ public final class ZipPackage extends Pa
 			return this.partList.values().toArray(
 					new PackagePart[this.partList.values().size()]);
 		}
+		
 		// First we need to parse the content type part
 		Enumeration<? extends ZipEntry> entries = this.zipArchive.getEntries();
 		while (entries.hasMoreElements()) {
@@ -194,6 +196,26 @@ public final class ZipPackage extends Pa
 
 		// At this point, we should have loaded the content type part
 		if (this.contentTypeManager == null) {
+		    // Is it a different Zip-based format?
+		    boolean hasMimetype = false;
+		    boolean hasSettingsXML = false;
+		    entries = this.zipArchive.getEntries();
+	        while (entries.hasMoreElements()) {
+	            ZipEntry entry = entries.nextElement();
+	            if (entry.getName().equals("mimetype")) {
+	                hasMimetype = true;
+	            }
+                if (entry.getName().equals("settings.xml")) {
+                    hasSettingsXML = true;
+                }
+	        }
+	        if (hasMimetype && hasSettingsXML) {
+	            throw new ODFNotOfficeXmlFileException(
+	               "The supplied data appears to be in ODF (Open Document) Format. " +
+	               "Formats like these (eg ODS, ODP) are not supported, try Apache ODFToolkit");
+	        }
+		    
+		    // Fallback exception
 			throw new InvalidFormatException(
 					"Package should contain a content type part [M1.13]");
 		}

Modified: poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java?rev=1735060&r1=1735059&r2=1735060&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/openxml4j/util/ZipInputStreamZipEntrySource.java
Tue Mar 15 11:50:57 2016
@@ -24,7 +24,6 @@ import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.Iterator;
 import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
 
 import org.apache.poi.openxml4j.util.ZipSecureFile.ThresholdInputStream;
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java?rev=1735060&r1=1735059&r2=1735060&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackage.java Tue Mar 15
11:50:57 2016
@@ -45,11 +45,14 @@ import java.util.zip.ZipFile;
 import java.util.zip.ZipOutputStream;
 
 import org.apache.poi.EncryptedDocumentException;
+import org.apache.poi.POIDataSamples;
 import org.apache.poi.POITestCase;
 import org.apache.poi.POIXMLException;
 import org.apache.poi.openxml4j.OpenXML4JTestDataSamples;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
+import org.apache.poi.openxml4j.exceptions.ODFNotOfficeXmlFileException;
+import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
 import org.apache.poi.openxml4j.opc.internal.ContentTypeManager;
 import org.apache.poi.openxml4j.opc.internal.FileHelper;
 import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
@@ -665,6 +668,49 @@ public final class TestPackage {
         p.revert();
         is.close();
     }
+    
+    /**
+     * Verify we give helpful exceptions (or as best we can) when
+     *  supplied with non-OOXML file types (eg OLE2, ODF)
+     */
+    @Test
+    public void NonOOXMLFileTypes() throws Exception {
+        // Spreadsheet has a good mix of alternate file types
+        POIDataSamples files = POIDataSamples.getSpreadSheetInstance();
+        
+        // OLE2 - Stream
+//        try {
+//            OPCPackage.open(files.openResourceAsStream("SampleSS.xls"));
+//            fail("Shouldn't be able to open OLE2");
+//        } catch (OLE2NotOfficeXmlFileException e) {
+//            // TODO Check details
+//        }
+        
+        // OLE2 - File
+        
+        // ODF / ODS - Stream
+        try {
+            OPCPackage.open(files.openResourceAsStream("SampleSS.ods"));
+            fail("Shouldn't be able to open ODS");
+        } catch (ODFNotOfficeXmlFileException e) {
+            assertTrue(e.toString().contains("The supplied data appears to be in ODF"));
+            assertTrue(e.toString().contains("Formats like these (eg ODS"));
+        }
+        // ODF / ODS - File
+        try {
+            OPCPackage.open(files.getFile("SampleSS.ods"));
+            fail("Shouldn't be able to open ODS");
+        } catch (ODFNotOfficeXmlFileException e) {
+            assertTrue(e.toString().contains("The supplied data appears to be in ODF"));
+            assertTrue(e.toString().contains("Formats like these (eg ODS"));
+        }
+        
+        // Plain Text - Stream
+        // Plain Text - File
+        
+        // Raw XML - Stream
+        // Raw XML - File
+    }
 
     @Test(expected=IOException.class)
     public void zipBombCreateAndHandle()



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org


Mime
View raw message