]> source.dussan.org Git - poi.git/commitdiff
#54803 - Error opening XLSX after saving with a Drawing using POI
authorAndreas Beeker <kiwiwings@apache.org>
Tue, 16 Apr 2019 21:27:30 +0000 (21:27 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Tue, 16 Apr 2019 21:27:30 +0000 (21:27 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1857663 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java

index f055f931dcefcfa4ac46d86cf8e79889cd2b1f84..c79b682e8d4308bd36d523295f425456a408d793 100644 (file)
 
 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;
index e7d7edb6a7b8b16ef8542378cd6dd4db3800239e..e22f707b5ec47a3b55fc8c1ffd44549d611e6f7e 100644 (file)
@@ -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");