From: Yegor Kozlov Date: Thu, 30 Jun 2011 15:39:47 +0000 (+0000) Subject: Bug 51444 - Prevent corrupted output when saving files created by LibreOffice 3.3 X-Git-Tag: REL_3_8_BETA4~354 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=586645ade856d55179a9c49c7e38a26d594f8e9f;p=poi.git Bug 51444 - Prevent corrupted output when saving files created by LibreOffice 3.3 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1141576 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index 5be12f315f..69572844b4 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 51444 - Prevent corrupted output when saving files created by LibreOffice 3.3 51422 - Support using RecalcIdRecord to trigger a full formula recalculation on load 50474 - Example demonstrating how to update Excel workbook embedded in a WordprocessingML document 51431 - Avoid IndexOutOfBoundException when removing freeze panes in XSSF diff --git a/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OpenXML4JRuntimeException.java b/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OpenXML4JRuntimeException.java index 7e95153fa6..40086aa9fc 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OpenXML4JRuntimeException.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/exceptions/OpenXML4JRuntimeException.java @@ -31,4 +31,8 @@ public class OpenXML4JRuntimeException extends RuntimeException { public OpenXML4JRuntimeException(String msg) { super(msg); } + + public OpenXML4JRuntimeException(String msg, Throwable reason) { + super(msg, reason); + } } diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipTypes.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipTypes.java index 01ed54c965..9344a64abf 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipTypes.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipTypes.java @@ -27,9 +27,24 @@ public interface PackageRelationshipTypes { /** * Core properties relationship type. + * + *

+ * The standard specifies a source relations ship for the Core File Properties part as follows: + * http://schemas.openxmlformats.org/officedocument/2006/relationships/metadata/core-properties. + *

+ *

+ * Office uses the following source relationship for the Core File Properties part: + * http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties. + *

+ * See 2.1.33 Part 1 Section 15.2.11.1, Core File Properties Part in [MS-OE376].pdf */ String CORE_PROPERTIES = "http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties"; + /** + * Core properties relationship type as defiend in ECMA 376. + */ + String CORE_PROPERTIES_ECMA376 = "http://schemas.openxmlformats.org/officedocument/2006/relationships/metadata/core-properties"; + /** * Digital signature relationship type. */ diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java index 52a871411e..bc65412bc7 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java @@ -30,6 +30,7 @@ import java.util.zip.ZipOutputStream; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidOperationException; import org.apache.poi.openxml4j.exceptions.OpenXML4JException; +import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException; import org.apache.poi.openxml4j.opc.internal.ContentTypeManager; import org.apache.poi.openxml4j.opc.internal.FileHelper; import org.apache.poi.openxml4j.opc.internal.MemoryPackagePart; @@ -384,13 +385,13 @@ public final class ZipPackage extends Package { // If the core properties part does not exist in the part list, // we save it as well - if (this.getPartsByRelationshipType( - PackageRelationshipTypes.CORE_PROPERTIES).size() == 0) { + if (this.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES).size() == 0 && + this.getPartsByRelationshipType(PackageRelationshipTypes.CORE_PROPERTIES_ECMA376).size() == 0 ) { logger.log(POILogger.DEBUG,"Save core properties part"); // We have to save the core properties part ... new ZipPackagePropertiesMarshaller().marshall( - this.packageProperties, zos); + this.packageProperties, zos); // ... and to add its relationship ... this.relationships.addRelationship(this.packageProperties .getPartName().getURI(), TargetMode.INTERNAL, @@ -445,9 +446,9 @@ public final class ZipPackage extends Package { } zos.close(); } catch (Exception e) { - logger - .log(POILogger.ERROR,"Fail to save: an error occurs while saving the package : " - + e.getMessage()); + throw new OpenXML4JRuntimeException( + "Fail to save: an error occurs while saving the package : " + + e.getMessage(), e); } } diff --git a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java index f2b2f7e1c3..d5ca8936da 100644 --- a/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java +++ b/src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java @@ -17,6 +17,8 @@ package org.apache.poi.openxml4j.opc; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.text.ParsePosition; @@ -180,4 +182,19 @@ public final class TestPackageCoreProperties extends TestCase { assertEquals(date, props.getModifiedProperty().getValue()); } + public void testGetPropertiesLO() throws Exception { + // Open the package + OPCPackage pkg1 = OPCPackage.open(OpenXML4JTestDataSamples.openSampleStream("51444.xlsx")); + PackageProperties props1 = pkg1.getPackageProperties(); + assertEquals(null, props1.getTitleProperty().getValue()); + props1.setTitleProperty("Bug 51444 fixed"); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + pkg1.save(out); + out.close(); + + OPCPackage pkg2 = OPCPackage.open(new ByteArrayInputStream(out.toByteArray())); + PackageProperties props2 = pkg2.getPackageProperties(); + props2.setTitleProperty("Bug 51444 fixed"); + } + } diff --git a/test-data/openxml4j/51444.xlsx b/test-data/openxml4j/51444.xlsx new file mode 100644 index 0000000000..5047dbf440 Binary files /dev/null and b/test-data/openxml4j/51444.xlsx differ