Author: nick
Date: Fri Aug  1 14:33:17 2014
New Revision: 1615118

URL: http://svn.apache.org/r1615118
Log:
Fix bug #56800 - Provide a helpful exception, XLSBUnsupportedException, if 
XSSFWorkbook is passed a .xlsb file

Added:
    poi/trunk/src/java/org/apache/poi/UnsupportedFileFormatException.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java
    poi/trunk/test-data/spreadsheet/Simple.xlsb   (with props)
Modified:
    poi/trunk/src/java/org/apache/poi/OldFileFormatException.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
    
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

Modified: poi/trunk/src/java/org/apache/poi/OldFileFormatException.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/OldFileFormatException.java?rev=1615118&r1=1615117&r2=1615118&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/OldFileFormatException.java (original)
+++ poi/trunk/src/java/org/apache/poi/OldFileFormatException.java Fri Aug  1 
14:33:17 2014
@@ -20,8 +20,10 @@ package org.apache.poi;
  * Base class of all the exceptions that POI throws in the event
  * that it's given a file that's older than currently supported.
  */
-public abstract class OldFileFormatException extends IllegalArgumentException {
-       public OldFileFormatException(String s) {
+public abstract class OldFileFormatException extends 
UnsupportedFileFormatException {
+    private static final long serialVersionUID = 7849681804154571175L;
+
+    public OldFileFormatException(String s) {
                super(s);
        }
 }
\ No newline at end of file

Added: poi/trunk/src/java/org/apache/poi/UnsupportedFileFormatException.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/UnsupportedFileFormatException.java?rev=1615118&view=auto
==============================================================================
--- poi/trunk/src/java/org/apache/poi/UnsupportedFileFormatException.java 
(added)
+++ poi/trunk/src/java/org/apache/poi/UnsupportedFileFormatException.java Fri 
Aug  1 14:33:17 2014
@@ -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;
+
+/**
+ * Base class of all the exceptions that POI throws in the event
+ * that it's given a file that isn't supported
+ */
+public abstract class UnsupportedFileFormatException extends 
IllegalArgumentException {
+    private static final long serialVersionUID = -8281969197282030046L;
+
+    public UnsupportedFileFormatException(String s) {
+               super(s);
+       }
+}
\ No newline at end of file

Added: 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java?rev=1615118&view=auto
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java 
(added)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java 
Fri Aug  1 14:33:17 2014
@@ -0,0 +1,31 @@
+/* ====================================================================
+   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.xssf;
+
+import org.apache.poi.UnsupportedFileFormatException;
+
+/**
+ * We don't support .xlsb files, sorry
+ */
+public class XLSBUnsupportedException extends UnsupportedFileFormatException {
+    private static final long serialVersionUID = 7849681804154571175L;
+    public static final String MESSAGE = ".XLSB Binary Workbooks are not 
supported"; 
+
+    public XLSBUnsupportedException() {
+               super(MESSAGE);
+       }
+}
\ No newline at end of file

Modified: 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java?rev=1615118&r1=1615117&r2=1615118&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java 
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java 
Fri Aug  1 14:33:17 2014
@@ -81,7 +81,6 @@ public final class XSSFRelation extends 
         "/xl/workbook.xml",
         null
     );
-
     public static final XSSFRelation MACRO_ADDIN_WORKBOOK = new XSSFRelation(
         "application/vnd.ms-excel.addin.macroEnabled.main+xml",
         PackageRelationshipTypes.CORE_DOCUMENT,
@@ -89,6 +88,13 @@ public final class XSSFRelation extends 
         null
     );
 
+    public static final XSSFRelation XLSB_BINARY_WORKBOOK = new XSSFRelation(
+            "application/vnd.ms-excel.sheet.binary.macroEnabled.main",
+            PackageRelationshipTypes.CORE_DOCUMENT,
+            "/xl/workbook.bin",
+            null
+        );
+
     public static final XSSFRelation WORKSHEET = new XSSFRelation(
         
"application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
         
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet";,

Modified: 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java?rev=1615118&r1=1615117&r2=1615118&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java 
(original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java 
Fri Aug  1 14:33:17 2014
@@ -63,6 +63,7 @@ import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
 import org.apache.poi.util.PackageHelper;
+import org.apache.poi.xssf.XLSBUnsupportedException;
 import org.apache.poi.xssf.model.CalculationChain;
 import org.apache.poi.xssf.model.ExternalLinksTable;
 import org.apache.poi.xssf.model.MapInfo;
@@ -228,8 +229,10 @@ public class XSSFWorkbook extends POIXML
      */
     public XSSFWorkbook(OPCPackage pkg) throws IOException {
         super(pkg);
-
-        //build a tree of POIXMLDocumentParts, this workbook being the root
+        
+        beforeDocumentRead();
+        
+        // Build a tree of POIXMLDocumentParts, this workbook being the root
         load(XSSFFactory.getInstance());
     }
 
@@ -250,7 +253,9 @@ public class XSSFWorkbook extends POIXML
     public XSSFWorkbook(InputStream is) throws IOException {
         super(PackageHelper.open(is));
 
-        //build a tree of POIXMLDocumentParts, this workbook being the root
+        beforeDocumentRead();
+        
+        // Build a tree of POIXMLDocumentParts, this workbook being the root
         load(XSSFFactory.getInstance());
     }
 
@@ -286,6 +291,17 @@ public class XSSFWorkbook extends POIXML
     public XSSFWorkbook(String path) throws IOException {
         this(openPackage(path));
     }
+    
+    protected void beforeDocumentRead() {
+        // Ensure it isn't a XLSB file, which we don't support
+        if 
(getCorePart().getContentType().equals(XSSFRelation.XLSB_BINARY_WORKBOOK.getContentType()))
 {
+            throw new XLSBUnsupportedException();
+        }
+
+        // Create arrays for parts attached to the workbook itself
+        pivotTables = new ArrayList<XSSFPivotTable>();
+        pivotCaches = new ArrayList<CTPivotCache>();
+    }
 
     @Override
     @SuppressWarnings("deprecation") //  getXYZArray() array accessors are 
deprecated
@@ -1869,12 +1885,6 @@ public class XSSFWorkbook extends POIXML
 
     @Beta
     public List<XSSFPivotTable> getPivotTables() {
-        // Lazy create the list. It gets populated with existing ones on sheet 
setup
-        if (pivotTables == null) {
-            pivotTables = new ArrayList<XSSFPivotTable>();
-            pivotCaches = new ArrayList<CTPivotCache>();
-        }
-
         return pivotTables;
     }
 

Modified: 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1615118&r1=1615117&r2=1615118&view=diff
==============================================================================
--- 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java 
(original)
+++ 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java 
Fri Aug  1 14:33:17 2014
@@ -74,6 +74,7 @@ import org.apache.poi.ss.util.AreaRefere
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.util.TempFile;
+import org.apache.poi.xssf.XLSBUnsupportedException;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.apache.poi.xssf.model.CalculationChain;
@@ -1844,6 +1845,41 @@ public final class TestXSSFBugs extends 
         cRef = sheet.getRow(4).getCell(1);
         assertEquals("A4", cRef.getCellFormula());
     }
+    
+    /**
+     * .xlsb files are not supported, but we should generate a helpful
+     *  error message if given one
+     */
+    @Test
+    public void bug56800_xlsb() throws Exception {
+        // Can be opened at the OPC level
+        OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("Simple.xlsb");
+        
+        // XSSF Workbook gives helpful error
+        try {
+            new XSSFWorkbook(pkg);
+            fail(".xlsb files not supported");
+        } catch (XLSBUnsupportedException e) {
+            // Good, detected and warned
+        }
+        
+        // Workbook Factory gives helpful error on package
+        try {
+            WorkbookFactory.create(pkg);
+            fail(".xlsb files not supported");
+        } catch (XLSBUnsupportedException e) {
+            // Good, detected and warned
+        }
+        
+        // Workbook Factory gives helpful error on file
+        File xlsbFile = HSSFTestDataSamples.getSampleFile("Simple.xlsb");
+        try {
+            WorkbookFactory.create(xlsbFile);
+            fail(".xlsb files not supported");
+        } catch (XLSBUnsupportedException e) {
+            // Good, detected and warned
+        }
+    }
 
     private void checkValue(XSSFWorkbook excel, String expect) {
         XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(excel);

Added: poi/trunk/test-data/spreadsheet/Simple.xlsb
URL: 
http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/Simple.xlsb?rev=1615118&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/Simple.xlsb
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to