From e2cd3842599d5a8db606ba27ac2c5c595c74bf8f Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Tue, 16 Apr 2019 21:27:30 +0000 Subject: #54803 - Error opening XLSX after saving with a Drawing using POI git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1857663 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/openxml4j/opc/OPCPackage.java | 21 +++++++++++++++++++-- .../apache/poi/xssf/usermodel/TestXSSFDrawing.java | 14 ++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java index f055f931dc..c79b682e8d 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -17,6 +17,11 @@ package org.apache.poi.openxml4j.opc; +import static org.apache.poi.openxml4j.opc.ContentTypes.EXTENSION_XML; +import static org.apache.poi.openxml4j.opc.ContentTypes.PLAIN_OLD_XML; +import static org.apache.poi.openxml4j.opc.ContentTypes.RELATIONSHIPS_PART; +import static org.apache.poi.openxml4j.opc.PackagingURIHelper.RELATIONSHIP_PART_EXTENSION_NAME; + import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; @@ -31,6 +36,7 @@ import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.apache.poi.ooxml.util.PackageHelper; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidOperationException; import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException; @@ -382,10 +388,10 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { pkg.contentTypeManager.addContentType( PackagingURIHelper.createPartName( PackagingURIHelper.PACKAGE_RELATIONSHIPS_ROOT_URI), - ContentTypes.RELATIONSHIPS_PART); + RELATIONSHIPS_PART); pkg.contentTypeManager.addContentType( PackagingURIHelper.createPartName("/default.xml"), - ContentTypes.PLAIN_OLD_XML); + PLAIN_OLD_XML); // Initialise some PackageBase properties pkg.packageProperties = new PackagePropertiesPart(pkg, @@ -846,6 +852,17 @@ public abstract class OPCPackage implements RelationshipSource, Closeable { PackagePart part = this.createPartImpl(partName, contentType, loadRelationships); + + /* check/create default entries - for bug54803 */ + try { + PackagePartName ppn = PackagingURIHelper.createPartName("/."+EXTENSION_XML); + contentTypeManager.addContentType(ppn, PLAIN_OLD_XML); + ppn = PackagingURIHelper.createPartName("/"+RELATIONSHIP_PART_EXTENSION_NAME); + contentTypeManager.addContentType(ppn, RELATIONSHIPS_PART); + } catch (InvalidFormatException e) { + throw new InvalidOperationException("unable to create default content-type entries.", e); + } + this.contentTypeManager.addContentType(partName, contentType); this.partList.put(partName, part); this.isDirty = true; diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java index e7d7edb6a7..e22f707b5e 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java @@ -24,6 +24,7 @@ import org.apache.poi.ss.usermodel.ClientAnchor; import org.apache.poi.ss.usermodel.FontUnderline; import org.apache.poi.ss.usermodel.ShapeTypes; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.ss.usermodel.WorkbookFactory; import org.apache.poi.util.Units; import org.apache.poi.xssf.XSSFTestDataSamples; import org.junit.Test; @@ -34,12 +35,25 @@ import org.openxmlformats.schemas.drawingml.x2006.main.STTextUnderlineType; import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing; import java.awt.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import static org.junit.Assert.*; public class TestXSSFDrawing { + @Test + public void bug54803() throws Exception { + try (XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("bug54803.xlsx")) { + XSSFSheet sheet = wb.getSheetAt(0); + XSSFDrawing drawing = sheet.createDrawingPatriarch(); + XSSFTestDataSamples.writeOutAndReadBack(wb).close(); + } + } + @Test public void testRead() throws IOException { XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx"); -- cgit v1.2.3