/** 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;
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
, TestDocumentNode.class
, TestDocumentOutputStream.class
, TestEmptyDocument.class
- , TestOffice2007XMLException.class
+ , TestOfficeXMLException.class
, TestPOIFSDocumentPath.class
, TestPOIFSFileSystem.class
, TestNPOIFSFileSystem.class
+++ /dev/null
-/* ====================================================================
- 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());
- }
-}
--- /dev/null
+/* ====================================================================
+ 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());
+ }
+}
--- /dev/null
+<?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