From ceb9427afb11bfbb9f805d032c98b173df0c0b37 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Fri, 1 Aug 2014 14:33:17 +0000 Subject: [PATCH] Fix bug #56800 - Provide a helpful exception, XLSBUnsupportedException, if XSSFWorkbook is passed a .xlsb file git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1615118 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/OldFileFormatException.java | 6 ++- .../poi/UnsupportedFileFormatException.java | 29 ++++++++++++++ .../poi/xssf/XLSBUnsupportedException.java | 31 +++++++++++++++ .../poi/xssf/usermodel/XSSFRelation.java | 8 +++- .../poi/xssf/usermodel/XSSFWorkbook.java | 28 +++++++++----- .../poi/xssf/usermodel/TestXSSFBugs.java | 36 ++++++++++++++++++ test-data/spreadsheet/Simple.xlsb | Bin 0 -> 9161 bytes 7 files changed, 126 insertions(+), 12 deletions(-) create mode 100644 src/java/org/apache/poi/UnsupportedFileFormatException.java create mode 100644 src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java create mode 100644 test-data/spreadsheet/Simple.xlsb diff --git a/src/java/org/apache/poi/OldFileFormatException.java b/src/java/org/apache/poi/OldFileFormatException.java index abcdc97103..3c01f5459e 100644 --- a/src/java/org/apache/poi/OldFileFormatException.java +++ b/src/java/org/apache/poi/OldFileFormatException.java @@ -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 diff --git a/src/java/org/apache/poi/UnsupportedFileFormatException.java b/src/java/org/apache/poi/UnsupportedFileFormatException.java new file mode 100644 index 0000000000..a8caebb4bf --- /dev/null +++ b/src/java/org/apache/poi/UnsupportedFileFormatException.java @@ -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 diff --git a/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java b/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java new file mode 100644 index 0000000000..63260276f8 --- /dev/null +++ b/src/ooxml/java/org/apache/poi/xssf/XLSBUnsupportedException.java @@ -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 diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java index 213f739fea..27caf13e5a 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRelation.java @@ -81,7 +81,6 @@ public final class XSSFRelation extends POIXMLRelation { "/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 POIXMLRelation { 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", diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index a502028e94..60e1ea415e 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -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 POIXMLDocument implements Workbook, Iterable(); + pivotCaches = new ArrayList(); + } @Override @SuppressWarnings("deprecation") // getXYZArray() array accessors are deprecated @@ -1869,12 +1885,6 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Iterable getPivotTables() { - // Lazy create the list. It gets populated with existing ones on sheet setup - if (pivotTables == null) { - pivotTables = new ArrayList(); - pivotCaches = new ArrayList(); - } - return pivotTables; } diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java index f33b345916..dd5e0505e9 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java @@ -74,6 +74,7 @@ import org.apache.poi.ss.util.AreaReference; 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 BaseTestBugzillaIssues { 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); diff --git a/test-data/spreadsheet/Simple.xlsb b/test-data/spreadsheet/Simple.xlsb new file mode 100644 index 0000000000000000000000000000000000000000..78fdf269f32a81029cd8b9bde816cf155b23858b GIT binary patch literal 9161 zcmeHMcT`hbvk$#@kRny2BZMA02q;CRNE1N_5PIk;y@M3#g7gjoBE1L%q<2JmQ@RxC z9YN$p?)@$rx$oUS-&)^2Ywet5oio48-ZQgj=C@UpP*6z$=m1Or0KfoP#n3y5Mgjo# z(EtDv04DN785?Uyh_$1>y0b0BL66G`YDJ%cip-h@K!(5n@AY520tM=Km7Amp>k^OX z66z8*keS;kurXR&LZSkP?xq1NH?Ossf(hA7&}n9e#m~!KNPK10LtF%?Ezc3F;P>^k5DtL26z7eHIQ33Ze;vrX9kMzJ$5TA@b)F)aR2=*UACxt149u{SNB#;q3EHtaB<@E!coxT(J zN;1A6<95Kb54`(FAQLX^c{kgO2y1yr6z26*kQ)?B~l>Nn!F+4rGrd?4F)J>C9_}d{kblJC;81D8L5xYPHawepnlq?sKT%z{x^)iYGB>&zTgRCV z_X|?Edl10ya5wtdfh$9abQ)32l;+a*?M?6Y$ku%8enwyRjk4 zo52UG&?`N|H!?jn5{#dTeg`)z5iKx;LX1+40+({kYt0}4H$a`96KN;%g-?$(tH90s z7JdaUDCP2a64ZOP%=et^7`ndBCDU8er{K)S3o+TKQz8;gd~iDX~M(HTVOnE zVrp;?u#*CSrs9#BgN=W<^Y4M-SE~@?41cl$9~AKM=ZqZ8AP`3f?(-Y(kJzvn~K-IQpXl!O=?9|n> zONR-faBQ$(=Hq2{O) zD<@(<1ifv5z}{V*a>|@ggy7}H<;a&5k0hiJg*+5iTj4J$?=28kgYt04b+`{SIc=QW z?szdd zm;^B2LWe7Si8VW4QhB+;pX9&?DSW`4&%q3A4>7*)Xm4(9>hNP8oX>f%o1{o(r=J{D zH>60lv^^rUT7xx2$r1-LFZ(=d5uqJ^$>e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