You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by fa...@apache.org on 2018/07/29 23:05:01 UTC

svn commit: r1837007 - in /poi/trunk: src/ooxml/java/org/apache/poi/ooxml/POIXMLProperties.java src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java test-data/slideshow/Divino_Revelado.pptx

Author: fanningpj
Date: Sun Jul 29 23:05:01 2018
New Revision: 1837007

URL: http://svn.apache.org/viewvc?rev=1837007&view=rev
Log:
Handle NPE issues in POIXMLProperties

Added:
    poi/trunk/test-data/slideshow/Divino_Revelado.pptx   (with props)
Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/ooxml/POIXMLProperties.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/ooxml/POIXMLProperties.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/ooxml/POIXMLProperties.java?rev=1837007&r1=1837006&r2=1837007&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/ooxml/POIXMLProperties.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/ooxml/POIXMLProperties.java Sun Jul 29 23:05:01 2018
@@ -68,17 +68,20 @@ public class POIXMLProperties {
         this.pkg = docPackage;
 
         // Core properties
-        core = new CoreProperties((PackagePropertiesPart)pkg.getPackageProperties() );
+        core = new CoreProperties((PackagePropertiesPart)pkg.getPackageProperties());
 
         // Extended properties
         PackageRelationshipCollection extRel =
                 pkg.getRelationshipsByType(PackageRelationshipTypes.EXTENDED_PROPERTIES);
         if(extRel.size() == 1) {
-            extPart = pkg.getPart( extRel.getRelationship(0));
-            org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument props = org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument.Factory.parse(
-                    extPart.getInputStream(), DEFAULT_XML_OPTIONS
-            );
-            ext = new ExtendedProperties(props);
+            extPart = pkg.getPart(extRel.getRelationship(0));
+            if (extPart == null) {
+                ext = new ExtendedProperties((org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument)NEW_EXT_INSTANCE.copy());
+            } else {
+                org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument props = org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument.Factory.parse(
+                        extPart.getInputStream(), DEFAULT_XML_OPTIONS);
+                ext = new ExtendedProperties(props);
+            }
         } else {
             extPart = null;
             ext = new ExtendedProperties((org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument)NEW_EXT_INSTANCE.copy());
@@ -88,11 +91,14 @@ public class POIXMLProperties {
         PackageRelationshipCollection custRel =
                 pkg.getRelationshipsByType(PackageRelationshipTypes.CUSTOM_PROPERTIES);
         if(custRel.size() == 1) {
-            custPart = pkg.getPart( custRel.getRelationship(0));
-            org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument props = org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument.Factory.parse(
-                    custPart.getInputStream(), DEFAULT_XML_OPTIONS
-            );
-            cust = new CustomProperties(props);
+            custPart = pkg.getPart(custRel.getRelationship(0));
+            if (custPart == null) {
+                cust = new CustomProperties((org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument)NEW_CUST_INSTANCE.copy());
+            } else {
+                org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument props = org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument.Factory.parse(
+                        custPart.getInputStream(), DEFAULT_XML_OPTIONS);
+                cust = new CustomProperties(props);
+            }
         } else {
             custPart = null;
             cust = new CustomProperties((org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument)NEW_CUST_INSTANCE.copy());
@@ -197,9 +203,9 @@ public class POIXMLProperties {
      * @throws IOException if the properties can't be saved
      * @throws POIXMLException if the properties are erroneous
      */
-    public void commit() throws IOException{
+    public void commit() throws IOException {
 
-        if(extPart == null && !NEW_EXT_INSTANCE.toString().equals(ext.props.toString())){
+        if(extPart == null && ext != null && ext.props != null && !NEW_EXT_INSTANCE.toString().equals(ext.props.toString())){
             try {
                 PackagePartName prtname = PackagingURIHelper.createPartName("/docProps/app.xml");
                 pkg.addRelationship(prtname, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties");
@@ -208,7 +214,7 @@ public class POIXMLProperties {
                 throw new POIXMLException(e);
             }
         }
-        if(custPart == null && !NEW_CUST_INSTANCE.toString().equals(cust.props.toString())){
+        if(custPart == null && cust != null && cust.props != null && !NEW_CUST_INSTANCE.toString().equals(cust.props.toString())){
             try {
                 PackagePartName prtname = PackagingURIHelper.createPartName("/docProps/custom.xml");
                 pkg.addRelationship(prtname, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties");
@@ -218,17 +224,17 @@ public class POIXMLProperties {
             }
         }
         if(extPart != null){
-            OutputStream out = extPart.getOutputStream();
-            if (extPart.getSize() > 0) {
-                extPart.clear();
+            try (OutputStream out = extPart.getOutputStream()) {
+                if (extPart.getSize() > 0) {
+                    extPart.clear();
+                }
+                ext.props.save(out, DEFAULT_XML_OPTIONS);
             }
-            ext.props.save(out, DEFAULT_XML_OPTIONS);
-            out.close();
         }
         if(custPart != null){
-            OutputStream out = custPart.getOutputStream();
-            cust.props.save(out, DEFAULT_XML_OPTIONS);
-            out.close();
+            try (OutputStream out = custPart.getOutputStream()) {
+                cust.props.save(out, DEFAULT_XML_OPTIONS);
+            }
         }
     }
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java?rev=1837007&r1=1837006&r2=1837007&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xslf/TestXSLFBugs.java Sun Jul 29 23:05:01 2018
@@ -724,6 +724,18 @@ public class TestXSLFBugs {
         ppt.close();
     }
 
+    @Ignore
+    @Test
+    public void testDivinoRevelado() throws IOException {
+        XMLSlideShow ppt = XSLFTestDataSamples.openSampleDocument("Divino_Revelado.pptx");
+        try {
+            XMLSlideShow saved = XSLFTestDataSamples.writeOutAndReadBack(ppt);
+        } catch (IOException e) {
+            fail("Could not read back saved presentation.");
+        }
+        ppt.close();
+    }
+
     @Test
     public void bug62051() throws IOException {
         final Function<List<XSLFShape>, int[]> ids = (shapes) ->

Added: poi/trunk/test-data/slideshow/Divino_Revelado.pptx
URL: http://svn.apache.org/viewvc/poi/trunk/test-data/slideshow/Divino_Revelado.pptx?rev=1837007&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/slideshow/Divino_Revelado.pptx
------------------------------------------------------------------------------
--- svn:mime-type (added)
+++ svn:mime-type Sun Jul 29 23:05:01 2018
@@ -0,0 +1 @@
+application/vnd.openxmlformats-officedocument.presentationml.presentation



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org