PackageRelationship coreDocRelationship = this.pkg.getRelationshipsByType(
PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0);
+ // strict OOXML likely not fully supported, see #57699
+ // this code is similar to POIXMLDocumentPart.getPartFromOPCPackage(), but I could not combine it
+ // easily due to different return values
+ if(coreDocRelationship == null) {
+ if (this.pkg.getRelationshipsByType(
+ PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0) != null) {
+ throw new POIXMLException("Strict OOXML isn't currently supported, please see bug #57699");
+ }
+
+ throw new POIXMLException("OOXML file structure broken/invalid - no core document found!");
+ }
+
// Get the part that holds the workbook
workbookPart = this.pkg.getPart(coreDocRelationship);
}
import java.util.Iterator;
import java.util.List;
-import junit.framework.TestCase;
-
+import org.apache.poi.POIDataSamples;
+import org.apache.poi.POIXMLException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.XSSFRichTextString;
import org.apache.poi.xssf.usermodel.XSSFShape;
import org.apache.poi.xssf.usermodel.XSSFSimpleShape;
-import org.apache.poi.POIDataSamples;
+
+import junit.framework.TestCase;
/**
* Tests for {@link XSSFReader}
stream.close();
}
}
- /**
- * Test text extraction from text box using getShapes()
- * @throws Exception
- */
- public void testShapes() throws Exception{
- OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("WithTextBox.xlsx");
- XSSFReader r = new XSSFReader(pkg);
- XSSFReader.SheetIterator it = (XSSFReader.SheetIterator)r.getSheetsData();
-
- StringBuilder sb = new StringBuilder();
- while(it.hasNext())
- {
- it.next();
- List<XSSFShape> shapes = it.getShapes();
- if (shapes != null){
- for (XSSFShape shape : shapes){
- if (shape instanceof XSSFSimpleShape){
- String t = ((XSSFSimpleShape)shape).getText();
- sb.append(t).append('\n');
- }
- }
- }
- }
- String text = sb.toString();
- assertTrue(text.indexOf("Line 1") > -1);
- assertTrue(text.indexOf("Line 2") > -1);
- assertTrue(text.indexOf("Line 3") > -1);
- }
-
+ /**
+ * Test text extraction from text box using getShapes()
+ *
+ * @throws Exception
+ */
+ public void testShapes() throws Exception {
+ OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("WithTextBox.xlsx");
+ XSSFReader r = new XSSFReader(pkg);
+ XSSFReader.SheetIterator it = (XSSFReader.SheetIterator) r.getSheetsData();
+
+ String text = getShapesString(it);
+ assertTrue(text.contains("Line 1"));
+ assertTrue(text.contains("Line 2"));
+ assertTrue(text.contains("Line 3"));
+ }
+
+ private String getShapesString(XSSFReader.SheetIterator it) {
+ StringBuilder sb = new StringBuilder();
+ while (it.hasNext()) {
+ it.next();
+ List<XSSFShape> shapes = it.getShapes();
+ if (shapes != null) {
+ for (XSSFShape shape : shapes) {
+ if (shape instanceof XSSFSimpleShape) {
+ String t = ((XSSFSimpleShape) shape).getText();
+ sb.append(t).append('\n');
+ }
+ }
+ }
+ }
+ return sb.toString();
+ }
+
+ public void testBug57914() throws Exception {
+ OPCPackage pkg = XSSFTestDataSamples.openSamplePackage("57914.xlsx");
+ final XSSFReader r;
+
+ // for now expect this to fail, when we fix 57699, this one should fail so we know we should adjust
+ // this test as well
+ try {
+ r = new XSSFReader(pkg);
+ fail("This will fail until bug 57699 is fixed");
+ } catch (POIXMLException e) {
+ assertTrue("Had " + e, e.getMessage().contains("57699"));
+ return;
+ }
+
+ XSSFReader.SheetIterator it = (XSSFReader.SheetIterator) r.getSheetsData();
+
+ String text = getShapesString(it);
+ assertTrue(text.contains("Line 1"));
+ assertTrue(text.contains("Line 2"));
+ assertTrue(text.contains("Line 3"));
+ }
+
/**
- * NPE from XSSFReader$SheetIterator.<init> on XLSX files generated by
+ * NPE from XSSFReader$SheetIterator.<init> on XLSX files generated by
* the openpyxl library
*/
public void test58747() throws Exception {
XSSFReader reader = new XSSFReader(pkg);
StylesTable styles = reader.getStylesTable();
assertNotNull(styles);
-
+
XSSFReader.SheetIterator iter = (XSSFReader.SheetIterator) reader.getSheetsData();
assertEquals(true, iter.hasNext());
iter.next();
-
+
assertEquals(false, iter.hasNext());
assertEquals("Orders", iter.getSheetName());
}