]> source.dussan.org Git - poi.git/commitdiff
Bug 51444 - Prevent corrupted output when saving files created by LibreOffice 3.3
authorYegor Kozlov <yegor@apache.org>
Thu, 30 Jun 2011 15:39:47 +0000 (15:39 +0000)
committerYegor Kozlov <yegor@apache.org>
Thu, 30 Jun 2011 15:39:47 +0000 (15:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1141576 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/openxml4j/exceptions/OpenXML4JRuntimeException.java
src/ooxml/java/org/apache/poi/openxml4j/opc/PackageRelationshipTypes.java
src/ooxml/java/org/apache/poi/openxml4j/opc/ZipPackage.java
src/ooxml/testcases/org/apache/poi/openxml4j/opc/TestPackageCoreProperties.java
test-data/openxml4j/51444.xlsx [new file with mode: 0644]

index 5be12f315fe7d9f30e8164f8c3d60c56d47487ba..69572844b4bf2d3727ad6a1932b3198bde0e7211 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta4" date="2011-??-??">
+           <action dev="poi-developers" type="add">51444 - Prevent corrupted output when saving files created by LibreOffice 3.3 </action>
            <action dev="poi-developers" type="add">51422 - Support using RecalcIdRecord to trigger a full formula recalculation on load  </action>
            <action dev="poi-developers" type="add">50474 - Example demonstrating how to update Excel workbook embedded in a WordprocessingML document </action>
            <action dev="poi-developers" type="fix">51431 - Avoid IndexOutOfBoundException when removing freeze panes in XSSF </action>
index 7e95153fa67083aacc6ded3883039ca3e97a72a0..40086aa9fc29d6b297ebc3d47ad97d6b77c8ab4f 100644 (file)
@@ -31,4 +31,8 @@ public class OpenXML4JRuntimeException extends RuntimeException {
        public OpenXML4JRuntimeException(String msg) {
                super(msg);
        }
+
+    public OpenXML4JRuntimeException(String msg, Throwable reason) {
+        super(msg, reason);
+    }
 }
index 01ed54c965d016551e10aeb6c10a109be5a55d7b..9344a64abfd9f248738e69cbc085e76eb1c1fc1b 100644 (file)
@@ -27,9 +27,24 @@ public interface PackageRelationshipTypes {
 
        /**
         * Core properties relationship type.
+     *
+     *  <p>
+     *  The standard specifies a source relations ship for the Core File Properties part as follows:
+     *  <code>http://schemas.openxmlformats.org/officedocument/2006/relationships/metadata/core-properties.</code>
+     *  </p>
+     *  <p>
+     *   Office uses the following source relationship for the Core File Properties part:
+     *   <code>http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties.</code>
+     * </p>
+     * 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.
         */
index 52a871411e5b427890e23dfb90c6f570b8144ca4..bc65412bc77e5717d61c4a30ed9dc7a3b792f9cd 100644 (file)
@@ -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);
                }
        }
 
index f2b2f7e1c32f4c77e28f6e0b156b7b8b4e355861..d5ca8936dad84e46d105e433f564698ea4ab5e78 100644 (file)
@@ -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 (file)
index 0000000..5047dbf
Binary files /dev/null and b/test-data/openxml4j/51444.xlsx differ