More helpful exception message if POIFS is given a raw XML file (eg an Office 2003 XML file), plus tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1734215 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2016-03-09 10:57:52 +00:00
parent 08bfa54296
commit a20e6a38d3
5 changed files with 175 additions and 7 deletions

View 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;

View 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

View 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

View File

@ -28,16 +28,15 @@ 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
* Class to test that POIFS complains when given an Office 2003 XML
* of Office Open XML (OOXML, 2007+) document
*/
public class TestOffice2007XMLException extends TestCase {
public class TestOfficeXMLException extends TestCase {
private static final InputStream openSampleStream(String sampleFileName) {
return HSSFTestDataSamples.openSampleFileStream(sampleFileName);
}
public void testXMLException() throws IOException
public void testOOXMLException() throws IOException
{
InputStream in = openSampleStream("sample.xlsx");
@ -50,12 +49,27 @@ public class TestOffice2007XMLException extends TestCase {
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);

View File

@ -0,0 +1,142 @@
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Title>Sample Spreadsheet</Title>
<Subject>Spreadsheet for testing</Subject>
<Author>Nick Burch</Author>
<Keywords>Testing Sample Formulas</Keywords>
<Description>This is a sample spreadsheet, for use when testing things</Description>
<LastAuthor>Nick Burch</LastAuthor>
<Created>2008-01-04T11:51:36Z</Created>
<LastSaved>2008-01-04T11:56:04Z</LastSaved>
<Version>14.00</Version>
</DocumentProperties>
<OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office">
<AllowPNG/>
</OfficeDocumentSettings>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>5580</WindowHeight>
<WindowWidth>11295</WindowWidth>
<WindowTopX>360</WindowTopX>
<WindowTopY>60</WindowTopY>
<ActiveSheet>1</ActiveSheet>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Vertical="Bottom"/>
<Borders/>
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#000000"/>
<Interior/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s62">
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#FF0000"/>
</Style>
<Style ss:ID="s63">
<Font ss:FontName="Calibri" x:Family="Swiss" ss:Size="11" ss:Color="#1F497D"
ss:Bold="1"/>
<Interior ss:Color="#FFFF00" ss:Pattern="Solid"/>
</Style>
</Styles>
<Worksheet ss:Name="First Sheet">
<Table ss:ExpandedColumnCount="2" ss:ExpandedRowCount="4" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Row>
<Cell><Data ss:Type="String">Test spreadsheet</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">2nd row</Data></Cell>
<Cell><Data ss:Type="String">2nd row 2nd column</Data></Cell>
</Row>
<Row ss:Index="4">
<Cell ss:StyleID="s62"><Data ss:Type="String">This one is red</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Print>
<ValidPrinterInfo/>
<PaperSizeIndex>0</PaperSizeIndex>
<VerticalResolution>0</VerticalResolution>
<NumberofCopies>0</NumberofCopies>
</Print>
<Panes>
<Pane>
<Number>3</Number>
<RangeSelection>R1C1:R4C2</RangeSelection>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet Number 2">
<Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="7" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
<Row>
<Cell><Data ss:Type="String">Start of 2nd sheet</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="String">Sheet 2 row 2</Data></Cell>
</Row>
<Row ss:Index="4">
<Cell ss:StyleID="s63"><Data ss:Type="String">I'm in bold blue, on a yellow background</Data></Cell>
</Row>
<Row ss:Index="6">
<Cell><Data ss:Type="String">cb=1</Data></Cell>
<Cell><Data ss:Type="String">cb=10</Data></Cell>
<Cell><Data ss:Type="String">cb=2</Data></Cell>
<Cell><Data ss:Type="String">cb=sum</Data></Cell>
</Row>
<Row>
<Cell><Data ss:Type="Number">1</Data></Cell>
<Cell><Data ss:Type="Number">10</Data></Cell>
<Cell><Data ss:Type="Number">2</Data></Cell>
<Cell ss:Formula="=SUM(RC[-3]:RC[-1])"><Data ss:Type="Number">13</Data></Cell>
</Row>
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<Selected/>
<Panes>
<Pane>
<Number>3</Number>
<ActiveRow>6</ActiveRow>
<ActiveCol>3</ActiveCol>
</Pane>
</Panes>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
<Worksheet ss:Name="Sheet3">
<Table ss:ExpandedColumnCount="1" ss:ExpandedRowCount="1" x:FullColumns="1"
x:FullRows="1" ss:DefaultRowHeight="15">
</Table>
<WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
<PageSetup>
<Header x:Margin="0.3"/>
<Footer x:Margin="0.3"/>
<PageMargins x:Bottom="0.75" x:Left="0.7" x:Right="0.7" x:Top="0.75"/>
</PageSetup>
<ProtectObjects>False</ProtectObjects>
<ProtectScenarios>False</ProtectScenarios>
</WorksheetOptions>
</Worksheet>
</Workbook>