diff --git a/src/java/org/apache/poi/poifs/common/POIFSConstants.java b/src/java/org/apache/poi/poifs/common/POIFSConstants.java index b732db45d..1cc5d038d 100644 --- a/src/java/org/apache/poi/poifs/common/POIFSConstants.java +++ b/src/java/org/apache/poi/poifs/common/POIFSConstants.java @@ -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; diff --git a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java index 49e491c5a..20613896b 100644 --- a/src/java/org/apache/poi/poifs/storage/HeaderBlock.java +++ b/src/java/org/apache/poi/poifs/storage/HeaderBlock.java @@ -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 diff --git a/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java b/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java index 364aa1a6e..999f2de1b 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/AllPOIFSFileSystemTests.java @@ -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/TestOfficeXMLException.java similarity index 81% rename from src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java rename to src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java index d5d92bd6b..087fbb2bd 100644 --- a/src/testcases/org/apache/poi/poifs/filesystem/TestOffice2007XMLException.java +++ b/src/testcases/org/apache/poi/poifs/filesystem/TestOfficeXMLException.java @@ -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); diff --git a/test-data/spreadsheet/SampleSS.xml b/test-data/spreadsheet/SampleSS.xml new file mode 100644 index 000000000..45cd58cec --- /dev/null +++ b/test-data/spreadsheet/SampleSS.xml @@ -0,0 +1,142 @@ + + + + + Sample Spreadsheet + Spreadsheet for testing + Nick Burch + Testing Sample Formulas + This is a sample spreadsheet, for use when testing things + Nick Burch + 2008-01-04T11:51:36Z + 2008-01-04T11:56:04Z + 14.00 + + + + + + 5580 + 11295 + 360 + 60 + 1 + False + False + + + + + + + + + + Test spreadsheet + + + 2nd row + 2nd row 2nd column + + + This one is red + +
+ + +
+