#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
This commit is contained in:
Andreas Beeker 2019-04-16 21:27:30 +00:00
parent 08d44a1d84
commit e2cd384259
2 changed files with 33 additions and 2 deletions

View File

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

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