]> source.dussan.org Git - poi.git/commitdiff
More helpful exception message if POIFS is given a raw XML file (eg an Office 2003...
authorNick Burch <nick@apache.org>
Wed, 9 Mar 2016 10:57:52 +0000 (10:57 +0000)
committerNick Burch <nick@apache.org>
Wed, 9 Mar 2016 10:57:52 +0000 (10:57 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734215 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/poifs/common/POIFSConstants.java
src/java/org/apache/poi/poifs/storage/HeaderBlock.java
src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java
src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java [deleted file]
src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java [new file with mode: 0644]
test-data/spreadsheet/SampleSS.xml [new file with mode: 0644]

index b732db45d9626a6e8b5b4c79ce7c7b5bf5a95001..1cc5d038d65aae936b62ec207584b6ecbcc9eb51 100644 (file)
@@ -61,4 +61,7 @@ public interface POIFSConstants
     /** The first 4 bytes of an OOXML file, used in detection */
     public static final byte[] OOXML_FILE_HEADER = 
        new byte[] { 0x50, 0x4b, 0x03, 0x04 };
+    /** The first 5 bytes of a raw XML file, used in detection */
+    public static final byte[] RAW_XML_FILE_HEADER =
+        new byte[] { 0x3c, 0x3f, 0x78, 0x6d, 0x6c };
 }   // end public interface POIFSConstants;
index 49e491c5ad93c00e01302a2abd8912151ee393d2..20613896bf20666e1c921b859bd9e065725623b0 100644 (file)
@@ -128,6 +128,15 @@ public final class HeaderBlock implements HeaderBlockConstants {
                                throw new OfficeXmlFileException("The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)");
                        }
                        
+            byte[] RAW_XML_FILE_HEADER = POIFSConstants.RAW_XML_FILE_HEADER;
+            if (_data[0] == RAW_XML_FILE_HEADER[0] &&
+                _data[1] == RAW_XML_FILE_HEADER[1] &&
+                _data[2] == RAW_XML_FILE_HEADER[2] &&
+                _data[3] == RAW_XML_FILE_HEADER[3] &&
+                _data[4] == RAW_XML_FILE_HEADER[4]) {
+                throw new NotOLE2FileException("The supplied data appears to be a raw XML file. Formats such as Office 2003 XML are not supported");
+            }
+            
             if (_data[0] == 0x09 && _data[1] == 0x00 && // sid=0x0009
                 _data[2] == 0x04 && _data[3] == 0x00 && // size=0x0004
                 _data[4] == 0x00 && _data[5] == 0x00 && // unused
index 364aa1a6e20fa5b43aafc55238d7eb08b4ca3afb..999f2de1be31ab5e0f3be828eeb2433963eae7bc 100644 (file)
@@ -32,7 +32,7 @@ import org.junit.runners.Suite;
     , TestDocumentNode.class
     , TestDocumentOutputStream.class
     , TestEmptyDocument.class
-    , TestOffice2007XMLException.class
+    , TestOfficeXMLException.class
     , TestPOIFSDocumentPath.class
     , TestPOIFSFileSystem.class
     , TestNPOIFSFileSystem.class
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java b/src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java
deleted file mode 100644 (file)
index d5d92bd..0000000
+++ /dev/null
@@ -1,114 +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.poifs.filesystem;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PushbackInputStream;
-import java.util.Arrays;
-
-import org.apache.poi.hssf.HSSFTestDataSamples;
-
-import junit.framework.TestCase;
-
-/**
- * Class to test that POIFS complains when given an Office 2007 XML document
- *
- * @author Marc Johnson
- */
-public class TestOffice2007XMLException extends TestCase {
-
-       private static final InputStream openSampleStream(String sampleFileName) {
-               return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
-       }
-       public void testXMLException() throws IOException
-       {
-               InputStream in = openSampleStream("sample.xlsx");
-
-               try {
-                       new POIFSFileSystem(in).close();
-                       fail("expected exception was not thrown");
-               } catch(OfficeXmlFileException e) {
-                       // expected during successful test
-                       assertTrue(e.getMessage().indexOf("The supplied data appears to be in the Office 2007+ XML") > -1);
-                       assertTrue(e.getMessage().indexOf("You are calling the part of POI that deals with OLE2 Office Documents") > -1);
-               }
-       }
-       
-       public void testDetectAsPOIFS() throws IOException {
-               
-               // ooxml file isn't
-               confirmIsPOIFS("SampleSS.xlsx", false);
-               
-               // xls file is
-               confirmIsPOIFS("SampleSS.xls", true);
-               
-               // text file isn't
-               confirmIsPOIFS("SampleSS.txt", false);
-       }
-       private void confirmIsPOIFS(String sampleFileName, boolean expectedResult) throws IOException {
-               InputStream in  = new PushbackInputStream(openSampleStream(sampleFileName), 10);
-               try {
-               boolean actualResult;
-               try {
-                       actualResult = POIFSFileSystem.hasPOIFSHeader(in);
-               } catch (IOException e) {
-                       throw new RuntimeException(e);
-               }
-               assertEquals(expectedResult, actualResult);
-               } finally {
-                   in.close();
-               }
-       }
-    
-    public void testFileCorruption() throws Exception {
-        
-        // create test InputStream
-        byte[] testData = { (byte)1, (byte)2, (byte)3 };
-        InputStream testInput = new ByteArrayInputStream(testData);
-        
-        // detect header
-        InputStream in = new PushbackInputStream(testInput, 10);
-        assertFalse(POIFSFileSystem.hasPOIFSHeader(in));
-        
-        // check if InputStream is still intact
-        byte[] test = new byte[3];
-        in.read(test);
-        assertTrue(Arrays.equals(testData, test));
-        assertEquals(-1, in.read());
-    }
-
-
-    public void testFileCorruptionOPOIFS() throws Exception {
-        
-        // create test InputStream
-        byte[] testData = { (byte)1, (byte)2, (byte)3 };
-        InputStream testInput = new ByteArrayInputStream(testData);
-        
-        // detect header
-        InputStream in = new PushbackInputStream(testInput, 10);
-        assertFalse(OPOIFSFileSystem.hasPOIFSHeader(in));
-
-        // check if InputStream is still intact
-        byte[] test = new byte[3];
-        in.read(test);
-        assertTrue(Arrays.equals(testData, test));
-        assertEquals(-1, in.read());
-    }
-}
diff --git a/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java b/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java
new file mode 100644 (file)
index 0000000..087fbb2
--- /dev/null
@@ -0,0 +1,128 @@
+/* ====================================================================
+   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.poifs.filesystem;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PushbackInputStream;
+import java.util.Arrays;
+
+import org.apache.poi.hssf.HSSFTestDataSamples;
+
+import junit.framework.TestCase;
+
+/**
+ * Class to test that POIFS complains when given an Office 2003 XML
+ *  of Office Open XML (OOXML, 2007+) document
+ */
+public class TestOfficeXMLException extends TestCase {
+
+       private static final InputStream openSampleStream(String sampleFileName) {
+               return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
+       }
+       public void testOOXMLException() throws IOException
+       {
+               InputStream in = openSampleStream("sample.xlsx");
+
+               try {
+                       new POIFSFileSystem(in).close();
+                       fail("expected exception was not thrown");
+               } catch(OfficeXmlFileException e) {
+                       // expected during successful test
+                       assertTrue(e.getMessage().indexOf("The supplied data appears to be in the Office 2007+ XML") > -1);
+                       assertTrue(e.getMessage().indexOf("You are calling the part of POI that deals with OLE2 Office Documents") > -1);
+               }
+       }
+    public void test2003XMLException() throws IOException
+    {
+        InputStream in = openSampleStream("SampleSS.xml");
+
+        try {
+            new POIFSFileSystem(in).close();
+            fail("expected exception was not thrown");
+        } catch(NotOLE2FileException e) {
+            // expected during successful test
+            assertTrue(e.getMessage().indexOf("The supplied data appears to be a raw XML file") > -1);
+            assertTrue(e.getMessage().indexOf("Formats such as Office 2003 XML") > -1);
+        }
+    }
+       
+       public void testDetectAsPOIFS() throws IOException {
+               // ooxml file isn't
+               confirmIsPOIFS("SampleSS.xlsx", false);
+               
+        // 2003 xml file isn't
+        confirmIsPOIFS("SampleSS.xml", false);
+        
+               // xls file is
+               confirmIsPOIFS("SampleSS.xls", true);
+               
+               // text file isn't
+               confirmIsPOIFS("SampleSS.txt", false);
+       }
+       private void confirmIsPOIFS(String sampleFileName, boolean expectedResult) throws IOException {
+               InputStream in  = new PushbackInputStream(openSampleStream(sampleFileName), 10);
+               try {
+               boolean actualResult;
+               try {
+                       actualResult = POIFSFileSystem.hasPOIFSHeader(in);
+               } catch (IOException e) {
+                       throw new RuntimeException(e);
+               }
+               assertEquals(expectedResult, actualResult);
+               } finally {
+                   in.close();
+               }
+       }
+    
+    public void testFileCorruption() throws Exception {
+        
+        // create test InputStream
+        byte[] testData = { (byte)1, (byte)2, (byte)3 };
+        InputStream testInput = new ByteArrayInputStream(testData);
+        
+        // detect header
+        InputStream in = new PushbackInputStream(testInput, 10);
+        assertFalse(POIFSFileSystem.hasPOIFSHeader(in));
+        
+        // check if InputStream is still intact
+        byte[] test = new byte[3];
+        in.read(test);
+        assertTrue(Arrays.equals(testData, test));
+        assertEquals(-1, in.read());
+    }
+
+
+    public void testFileCorruptionOPOIFS() throws Exception {
+        
+        // create test InputStream
+        byte[] testData = { (byte)1, (byte)2, (byte)3 };
+        InputStream testInput = new ByteArrayInputStream(testData);
+        
+        // detect header
+        InputStream in = new PushbackInputStream(testInput, 10);
+        assertFalse(OPOIFSFileSystem.hasPOIFSHeader(in));
+
+        // check if InputStream is still intact
+        byte[] test = new byte[3];
+        in.read(test);
+        assertTrue(Arrays.equals(testData, test));
+        assertEquals(-1, in.read());
+    }
+}
diff --git a/test-data/spreadsheet/SampleSS.xml b/test-data/spreadsheet/SampleSS.xml
new file mode 100644 (file)
index 0000000..45cd58c
--- /dev/null
@@ -0,0 +1,142 @@
+<?xml version="1.0"?>\r
+<?mso-application progid="Excel.Sheet"?>\r
+<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"\r
+ xmlns:o="urn:schemas-microsoft-com:office:office"\r
+ xmlns:x="urn:schemas-microsoft-com:office:excel"\r
+ xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"\r
+ xmlns:html="http://www.w3.org/TR/REC-html40">\r
+ <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">\r
+  <Title>Sample Spreadsheet</Title>\r
+  <Subject>Spreadsheet for testing</Subject>\r
+  <Author>Nick Burch</Author>\r
+  <Keywords>Testing Sample Formulas</Keywords>\r
+  <Description>This is a sample spreadsheet, for use when testing things</Description>\r
+  <LastAuthor>Nick Burch</LastAuthor>\r
+  <Created>2008-01-04T11:51:36Z</Created>\r
+  <LastSaved>2008-01-04T11:56:04Z</LastSaved>\r
+  <Version>14.00</Version>\r
+ </DocumentProperties>\r
+ <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">\r
+  <AllowPNG/>\r
+ </OfficeDocumentSettings>\r
+ <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">\r
+  <WindowHeight>5580</WindowHeight>\r
+  <WindowWidth>11295</WindowWidth>\r
+  <WindowTopX>360</WindowTopX>\r
+  <WindowTopY>60</WindowTopY>\r
+  <ActiveSheet>1</ActiveSheet>\r
+  <ProtectStructure>False</ProtectStructure>\r
+  <ProtectWindows>False</ProtectWindows>\r
+ </ExcelWorkbook>\r
+ <Styles>\r
+  <Style ss:ID="Default" ss:Name="Normal">\r
+   <Alignment ss:Vertical="Bottom"/>\r
+   <Borders/>\r
+   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>\r
+   <Interior/>\r
+   <NumberFormat/>\r
+   <Protection/>\r
+  </Style>\r
+  <Style ss:ID="s62">\r
+   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#FF0000"/>\r
+  </Style>\r
+  <Style ss:ID="s63">\r
+   <Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#1F497D"\r
+    ss:Bold="1"/>\r
+   <Interior ss:Color="#FFFF00" ss:Pattern="Solid"/>\r
+  </Style>\r
+ </Styles>\r
+ <Worksheet ss:Name="First Sheet">\r
+  <Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="4" x:FullColumns="1"\r
+   x:FullRows="1" ss:DefaultRowHeight="15">\r
+   <Row>\r
+    <Cell><Data ss:Type="String">Test spreadsheet</Data></Cell>\r
+   </Row>\r
+   <Row>\r
+    <Cell><Data ss:Type="String">2nd row</Data></Cell>\r
+    <Cell><Data ss:Type="String">2nd row 2nd column</Data></Cell>\r
+   </Row>\r
+   <Row ss:Index="4">\r
+    <Cell ss:StyleID="s62"><Data ss:Type="String">This one is red</Data></Cell>\r
+   </Row>\r
+  </Table>\r
+  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">\r
+   <PageSetup>\r
+    <Header x:Margin="0.3"/>\r
+    <Footer x:Margin="0.3"/>\r
+    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>\r
+   </PageSetup>\r
+   <Print>\r
+    <ValidPrinterInfo/>\r
+    <PaperSizeIndex>0</PaperSizeIndex>\r
+    <VerticalResolution>0</VerticalResolution>\r
+    <NumberofCopies>0</NumberofCopies>\r
+   </Print>\r
+   <Panes>\r
+    <Pane>\r
+     <Number>3</Number>\r
+     <RangeSelection>R1C1:R4C2</RangeSelection>\r
+    </Pane>\r
+   </Panes>\r
+   <ProtectObjects>False</ProtectObjects>\r
+   <ProtectScenarios>False</ProtectScenarios>\r
+  </WorksheetOptions>\r
+ </Worksheet>\r
+ <Worksheet ss:Name="Sheet Number 2">\r
+  <Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="7" x:FullColumns="1"\r
+   x:FullRows="1" ss:DefaultRowHeight="15">\r
+   <Row>\r
+    <Cell><Data ss:Type="String">Start of 2nd sheet</Data></Cell>\r
+   </Row>\r
+   <Row>\r
+    <Cell><Data ss:Type="String">Sheet 2 row 2</Data></Cell>\r
+   </Row>\r
+   <Row ss:Index="4">\r
+    <Cell ss:StyleID="s63"><Data ss:Type="String">I'm in bold blue, on a yellow background</Data></Cell>\r
+   </Row>\r
+   <Row ss:Index="6">\r
+    <Cell><Data ss:Type="String">cb=1</Data></Cell>\r
+    <Cell><Data ss:Type="String">cb=10</Data></Cell>\r
+    <Cell><Data ss:Type="String">cb=2</Data></Cell>\r
+    <Cell><Data ss:Type="String">cb=sum</Data></Cell>\r
+   </Row>\r
+   <Row>\r
+    <Cell><Data ss:Type="Number">1</Data></Cell>\r
+    <Cell><Data ss:Type="Number">10</Data></Cell>\r
+    <Cell><Data ss:Type="Number">2</Data></Cell>\r
+    <Cell ss:Formula="=SUM(RC[-3]:RC[-1])"><Data ss:Type="Number">13</Data></Cell>\r
+   </Row>\r
+  </Table>\r
+  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">\r
+   <PageSetup>\r
+    <Header x:Margin="0.3"/>\r
+    <Footer x:Margin="0.3"/>\r
+    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>\r
+   </PageSetup>\r
+   <Selected/>\r
+   <Panes>\r
+    <Pane>\r
+     <Number>3</Number>\r
+     <ActiveRow>6</ActiveRow>\r
+     <ActiveCol>3</ActiveCol>\r
+    </Pane>\r
+   </Panes>\r
+   <ProtectObjects>False</ProtectObjects>\r
+   <ProtectScenarios>False</ProtectScenarios>\r
+  </WorksheetOptions>\r
+ </Worksheet>\r
+ <Worksheet ss:Name="Sheet3">\r
+  <Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"\r
+   x:FullRows="1" ss:DefaultRowHeight="15">\r
+  </Table>\r
+  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">\r
+   <PageSetup>\r
+    <Header x:Margin="0.3"/>\r
+    <Footer x:Margin="0.3"/>\r
+    <PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>\r
+   </PageSetup>\r
+   <ProtectObjects>False</ProtectObjects>\r
+   <ProtectScenarios>False</ProtectScenarios>\r
+  </WorksheetOptions>\r
+ </Worksheet>\r
+</Workbook>\r