]> source.dussan.org Git - poi.git/commitdiff
Fix bug #54506 - Add "BOOK" to the list of unusual-but-largely-ok Workbook directory...
authorNick Burch <nick@apache.org>
Thu, 7 Feb 2013 21:53:07 +0000 (21:53 +0000)
committerNick Burch <nick@apache.org>
Thu, 7 Feb 2013 21:53:07 +0000 (21:53 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1443745 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/testcases/org/apache/poi/hssf/usermodel/AllUserModelTests.java
src/testcases/org/apache/poi/hssf/usermodel/TestNonStandardWorkbookStreamNames.java [new file with mode: 0644]
src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java [deleted file]
test-data/spreadsheet/BOOK_in_capitals.xls [new file with mode: 0644]

index c218775886a518461282842ec372eee207b26ec8..f3076d3ccc0a3ac8521608e06fc017d031a6516e 100644 (file)
@@ -193,7 +193,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
      */
     private static final String[] WORKBOOK_DIR_ENTRY_NAMES = {
         "Workbook", // as per BIFF8 spec
-        "WORKBOOK",
+        "WORKBOOK", // Typically from third party programs
+        "BOOK",     // Typically odd Crystal Reports exports 
     };
 
 
@@ -1180,9 +1181,11 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
         if (preserveNodes) {
             // Don't write out the old Workbook, we'll be doing our new one
             excepts.add("Workbook");
-            // If the file had WORKBOOK instead of Workbook, we'll write it
-            //  out correctly shortly, so don't include the old one
-            excepts.add("WORKBOOK");
+            // If the file had an "incorrect" name for the workbook stream,
+            // don't write the old one as we'll use the correct name shortly
+            for (String wrongName : WORKBOOK_DIR_ENTRY_NAMES) {
+               excepts.add(wrongName);
+            }
 
             // Copy over all the other nodes to our new poifs
             copyNodes(this.directory, fs.getRoot(), excepts);
index bf43435613bf3dd7c349a8d07138edd7649f1f2b..e71289c149eb7b41edec62fc1cebf43a6390432f 100644 (file)
@@ -75,7 +75,7 @@ public class AllUserModelTests {
                        result.addTestSuite(TestUnfixedBugs.class);
                }
                result.addTestSuite(TestUnicodeWorkbook.class);
-               result.addTestSuite(TestUppercaseWorkbook.class);
+               result.addTestSuite(TestNonStandardWorkbookStreamNames.class);
                result.addTestSuite(TestWorkbook.class);
 
                return result;
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestNonStandardWorkbookStreamNames.java b/src/testcases/org/apache/poi/hssf/usermodel/TestNonStandardWorkbookStreamNames.java
new file mode 100644 (file)
index 0000000..1a28cb4
--- /dev/null
@@ -0,0 +1,154 @@
+/*
+* 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.hssf.usermodel;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.InputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+
+/**
+ * Tests for how HSSFWorkbook behaves with XLS files
+ *  with a WORKBOOK or BOOK directory entry (instead of 
+ *  the more usual, Workbook)
+ */
+public final class TestNonStandardWorkbookStreamNames extends TestCase {
+       private String xlsA = "WORKBOOK_in_capitals.xls";
+   private String xlsB = "BOOK_in_capitals.xls";
+
+       /**
+        * Test that we can open a file with WORKBOOK
+        */
+       public void testOpenWORKBOOK() throws Exception {
+               InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
+               
+               POIFSFileSystem fs = new POIFSFileSystem(is);
+
+               // Ensure that we have a WORKBOOK entry
+               fs.getRoot().getEntry("WORKBOOK");
+               // And a summary
+               fs.getRoot().getEntry("\005SummaryInformation");
+               assertTrue(true);
+
+               // But not a Workbook one
+               try {
+                       fs.getRoot().getEntry("Workbook");
+                       fail();
+               } catch(FileNotFoundException e) {}
+               
+               // Try to open the workbook
+               HSSFWorkbook wb = new HSSFWorkbook(fs);
+       }
+
+   /**
+    * Test that we can open a file with BOOK
+    */
+   public void testOpenBOOK() throws Exception {
+      InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsB);
+      
+      POIFSFileSystem fs = new POIFSFileSystem(is);
+
+      // Ensure that we have a BOOK entry
+      fs.getRoot().getEntry("BOOK");
+      assertTrue(true);
+
+      // But not a Workbook one
+      try {
+         fs.getRoot().getEntry("Workbook");
+         fail();
+      } catch(FileNotFoundException e) {}
+      // And not a Summary one
+      try {
+         fs.getRoot().getEntry("\005SummaryInformation");
+         fail();
+      } catch(FileNotFoundException e) {}
+      
+      // Try to open the workbook
+      HSSFWorkbook wb = new HSSFWorkbook(fs);
+   }
+
+       /**
+        * Test that when we write out, we go back to the correct case
+        */
+       public void testWrite() throws Exception {
+          for (String file : new String[] {xlsA, xlsB}) {
+               InputStream is = HSSFTestDataSamples.openSampleFileStream(file);
+               POIFSFileSystem fs = new POIFSFileSystem(is);
+   
+               // Open the workbook, not preserving nodes
+               HSSFWorkbook wb = new HSSFWorkbook(fs);
+               ByteArrayOutputStream out = new ByteArrayOutputStream();
+               wb.write(out);
+   
+               // Check now
+               ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+               POIFSFileSystem fs2 = new POIFSFileSystem(in);
+   
+               // Check that we have the new entries
+               fs2.getRoot().getEntry("Workbook");
+               try {
+                       fs2.getRoot().getEntry("BOOK");
+                       fail();
+               } catch(FileNotFoundException e) {}
+         try {
+            fs2.getRoot().getEntry("WORKBOOK");
+            fail();
+         } catch(FileNotFoundException e) {}
+   
+               // And it can be opened
+               HSSFWorkbook wb2 = new HSSFWorkbook(fs2);
+          }
+       }
+
+       /**
+        * Test that when we write out preserving nodes, we go back to the
+        *  correct case
+        */
+       public void testWritePreserve() throws Exception {
+               InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
+               POIFSFileSystem fs = new POIFSFileSystem(is);
+
+               // Open the workbook, not preserving nodes
+               HSSFWorkbook wb = new HSSFWorkbook(fs,true);
+
+               ByteArrayOutputStream out = new ByteArrayOutputStream();
+               wb.write(out);
+
+               // Check now
+               ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
+               POIFSFileSystem fs2 = new POIFSFileSystem(in);
+
+               // Check that we have the new entries
+               fs2.getRoot().getEntry("Workbook");
+               try {
+                       fs2.getRoot().getEntry("WORKBOOK");
+                       fail();
+               } catch(FileNotFoundException e) {}
+
+               // As we preserved, should also have a few other streams
+               fs2.getRoot().getEntry("\005SummaryInformation");
+
+               // And it can be opened
+               HSSFWorkbook wb2 = new HSSFWorkbook(fs2);
+       }
+}
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java
deleted file mode 100644 (file)
index ad7ae65..0000000
+++ /dev/null
@@ -1,121 +0,0 @@
-/*
-* 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.hssf.usermodel;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStream;
-
-import junit.framework.TestCase;
-
-import org.apache.poi.hssf.HSSFTestDataSamples;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-
-/**
- * Tests for how HSSFWorkbook behaves with XLS files
- *  with a WORKBOOK directory entry (instead of the more
- *  usual, Workbook)
- */
-public final class TestUppercaseWorkbook extends TestCase {
-
-       private String xlsA = "WORKBOOK_in_capitals.xls";
-
-       /**
-        * Test that we can open a file with WORKBOOK
-        */
-       public void testOpen() throws Exception {
-               InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
-               
-               POIFSFileSystem fs = new POIFSFileSystem(is);
-
-               // Ensure that we have a WORKBOOK entry
-               fs.getRoot().getEntry("WORKBOOK");
-               // And a summary
-               fs.getRoot().getEntry("\005SummaryInformation");
-               assertTrue(true);
-
-               // But not a Workbook one
-               try {
-                       fs.getRoot().getEntry("Workbook");
-                       fail();
-               } catch(FileNotFoundException e) {}
-               
-               // Try to open the workbook
-               HSSFWorkbook wb = new HSSFWorkbook(fs);
-       }
-
-       /**
-        * Test that when we write out, we go back to the correct case
-        */
-       public void testWrite() throws Exception {
-               InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
-               POIFSFileSystem fs = new POIFSFileSystem(is);
-
-               // Open the workbook, not preserving nodes
-               HSSFWorkbook wb = new HSSFWorkbook(fs);
-               ByteArrayOutputStream out = new ByteArrayOutputStream();
-               wb.write(out);
-
-               // Check now
-               ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
-               POIFSFileSystem fs2 = new POIFSFileSystem(in);
-
-               // Check that we have the new entries
-               fs2.getRoot().getEntry("Workbook");
-               try {
-                       fs2.getRoot().getEntry("WORKBOOK");
-                       fail();
-               } catch(FileNotFoundException e) {}
-
-               // And it can be opened
-               HSSFWorkbook wb2 = new HSSFWorkbook(fs2);
-       }
-
-       /**
-        * Test that when we write out preserving nodes, we go back to the
-        *  correct case
-        */
-       public void testWritePreserve() throws Exception {
-               InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA);
-               POIFSFileSystem fs = new POIFSFileSystem(is);
-
-               // Open the workbook, not preserving nodes
-               HSSFWorkbook wb = new HSSFWorkbook(fs,true);
-
-               ByteArrayOutputStream out = new ByteArrayOutputStream();
-               wb.write(out);
-
-               // Check now
-               ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
-               POIFSFileSystem fs2 = new POIFSFileSystem(in);
-
-               // Check that we have the new entries
-               fs2.getRoot().getEntry("Workbook");
-               try {
-                       fs2.getRoot().getEntry("WORKBOOK");
-                       fail();
-               } catch(FileNotFoundException e) {}
-
-               // As we preserved, should also have a few other streams
-               fs2.getRoot().getEntry("\005SummaryInformation");
-
-               // And it can be opened
-               HSSFWorkbook wb2 = new HSSFWorkbook(fs2);
-       }
-}
diff --git a/test-data/spreadsheet/BOOK_in_capitals.xls b/test-data/spreadsheet/BOOK_in_capitals.xls
new file mode 100644 (file)
index 0000000..002d0ca
Binary files /dev/null and b/test-data/spreadsheet/BOOK_in_capitals.xls differ