From c5b2f7dc7f66cf07502e033aafaeaafd0210e885 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Thu, 7 Feb 2013 21:53:07 +0000 Subject: [PATCH] Fix bug #54506 - Add "BOOK" to the list of unusual-but-largely-ok Workbook directory entry names list, alongside WORKBOOK, plus tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1443745 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/usermodel/HSSFWorkbook.java | 11 ++- .../poi/hssf/usermodel/AllUserModelTests.java | 2 +- ...> TestNonStandardWorkbookStreamNames.java} | 85 ++++++++++++------ test-data/spreadsheet/BOOK_in_capitals.xls | Bin 0 -> 5632 bytes 4 files changed, 67 insertions(+), 31 deletions(-) rename src/testcases/org/apache/poi/hssf/usermodel/{TestUppercaseWorkbook.java => TestNonStandardWorkbookStreamNames.java} (59%) create mode 100644 test-data/spreadsheet/BOOK_in_capitals.xls diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index c218775886..f3076d3ccc 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -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); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/AllUserModelTests.java b/src/testcases/org/apache/poi/hssf/usermodel/AllUserModelTests.java index bf43435613..e71289c149 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/AllUserModelTests.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/AllUserModelTests.java @@ -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/TestUppercaseWorkbook.java b/src/testcases/org/apache/poi/hssf/usermodel/TestNonStandardWorkbookStreamNames.java similarity index 59% rename from src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java rename to src/testcases/org/apache/poi/hssf/usermodel/TestNonStandardWorkbookStreamNames.java index ad7ae65a08..1a28cb4219 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestUppercaseWorkbook.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestNonStandardWorkbookStreamNames.java @@ -29,17 +29,17 @@ 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) + * with a WORKBOOK or BOOK directory entry (instead of + * the more usual, Workbook) */ -public final class TestUppercaseWorkbook extends TestCase { - +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 testOpen() throws Exception { + public void testOpenWORKBOOK() throws Exception { InputStream is = HSSFTestDataSamples.openSampleFileStream(xlsA); POIFSFileSystem fs = new POIFSFileSystem(is); @@ -60,31 +60,64 @@ public final class TestUppercaseWorkbook extends TestCase { 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 { - 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); + 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); + } } /** diff --git a/test-data/spreadsheet/BOOK_in_capitals.xls b/test-data/spreadsheet/BOOK_in_capitals.xls new file mode 100644 index 0000000000000000000000000000000000000000..002d0cac2486f04e25651ddffeba6b9929dc2223 GIT binary patch literal 5632 zcmeHL&u<$=6drFjX&mY%aelZ>(sb&UwrSI(uG5r8(8k#qWYtcr*eO9ms?hXJuCp%oq~>QmO}r2H1la}{({Mx2PinQAA{oQ&LHiF`;7`nz z{72B?#BP9mehBCV`~b-g+N)`SHRj^yG0x)5!Bwy-JpS@+6cN*~%x-Sz@Ot{)+z&tg z`kk)}^2NhSX^E}kceBGAFQ)sL=4*^k*B8H`G$!ifzpVcA^#0EH&+Bi$vVSr@`Hb>7 z`J~hS?}O|$fJ_zujsX1tjsP|Y1c4!77#IOYfe>&MI0lRX3~&}W2TTFyfeXMia1odRW`RqJ(hcuvEOSP^KG=RIq_sz$^Z6p zJlVOOekY!KZKwY#^FGEId&Zs@lpX${l~Zfjf2spN-GM*ff%muNX(XS_)pSR#F)s0x zzZ*^QF&VyAa2|BLusZRPt~SRo+G%CP0h!TIBz#@AL6`~|5(%6z)n|< zu7f*0@*3RH<#lN1ki4=TCa=Rg-L((d4Hl;bu&XS3DKf*Y`z5zr5muSssq$ROt8&xH zb8|~rR?%|2+fcf_lFe@1j&B(8AUZW#f9k%tOTzgY8qcEOYS-g2=hFftwOO#AeUtz4V zLcVNyrd`mrQCSv*D3vs8FX-9=Bt9JO5lB{3F@qbibRru|@?)MDcH9}NOq=j8HMU}r}B+zXT0_qlD9MR~Nt8?>nSLb!DUsfd{ zI+u*qbbL)J>J7!nd}KZvnV*a5+K~Ko3Bl$pWtOoxo>+$R5zszVA#n1wYtbv$bS)sy zxDfSgWJqNY1Pu&N#j=^Y){y)HlHFmNPR=fw1?ZjzO}Z#&QkD>Qp}><>CwG@8$exbr z*{);4ddklsD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