Handle NPE issues in POIXMLProperties

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1837007 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2018-07-29 23:05:01 +00:00
parent 83b60f4777
commit 3be215e070
3 changed files with 40 additions and 22 deletions

View File

@ -68,17 +68,20 @@ public class POIXMLProperties {
this.pkg = docPackage; this.pkg = docPackage;
// Core properties // Core properties
core = new CoreProperties((PackagePropertiesPart)pkg.getPackageProperties() ); core = new CoreProperties((PackagePropertiesPart)pkg.getPackageProperties());
// Extended properties // Extended properties
PackageRelationshipCollection extRel = PackageRelationshipCollection extRel =
pkg.getRelationshipsByType(PackageRelationshipTypes.EXTENDED_PROPERTIES); pkg.getRelationshipsByType(PackageRelationshipTypes.EXTENDED_PROPERTIES);
if(extRel.size() == 1) { if(extRel.size() == 1) {
extPart = pkg.getPart( extRel.getRelationship(0)); extPart = pkg.getPart(extRel.getRelationship(0));
org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument props = org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument.Factory.parse( if (extPart == null) {
extPart.getInputStream(), DEFAULT_XML_OPTIONS ext = new ExtendedProperties((org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument)NEW_EXT_INSTANCE.copy());
); } else {
ext = new ExtendedProperties(props); 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 { } else {
extPart = null; extPart = null;
ext = new ExtendedProperties((org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument)NEW_EXT_INSTANCE.copy()); ext = new ExtendedProperties((org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument)NEW_EXT_INSTANCE.copy());
@ -88,11 +91,14 @@ public class POIXMLProperties {
PackageRelationshipCollection custRel = PackageRelationshipCollection custRel =
pkg.getRelationshipsByType(PackageRelationshipTypes.CUSTOM_PROPERTIES); pkg.getRelationshipsByType(PackageRelationshipTypes.CUSTOM_PROPERTIES);
if(custRel.size() == 1) { if(custRel.size() == 1) {
custPart = pkg.getPart( custRel.getRelationship(0)); custPart = pkg.getPart(custRel.getRelationship(0));
org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument props = org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument.Factory.parse( if (custPart == null) {
custPart.getInputStream(), DEFAULT_XML_OPTIONS cust = new CustomProperties((org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument)NEW_CUST_INSTANCE.copy());
); } else {
cust = new CustomProperties(props); 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 { } else {
custPart = null; custPart = null;
cust = new CustomProperties((org.openxmlformats.schemas.officeDocument.x2006.customProperties.PropertiesDocument)NEW_CUST_INSTANCE.copy()); 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 IOException if the properties can't be saved
* @throws POIXMLException if the properties are erroneous * @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 { try {
PackagePartName prtname = PackagingURIHelper.createPartName("/docProps/app.xml"); PackagePartName prtname = PackagingURIHelper.createPartName("/docProps/app.xml");
pkg.addRelationship(prtname, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties"); 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); 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 { try {
PackagePartName prtname = PackagingURIHelper.createPartName("/docProps/custom.xml"); PackagePartName prtname = PackagingURIHelper.createPartName("/docProps/custom.xml");
pkg.addRelationship(prtname, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties"); pkg.addRelationship(prtname, TargetMode.INTERNAL, "http://schemas.openxmlformats.org/officeDocument/2006/relationships/custom-properties");
@ -218,17 +224,17 @@ public class POIXMLProperties {
} }
} }
if(extPart != null){ if(extPart != null){
OutputStream out = extPart.getOutputStream(); try (OutputStream out = extPart.getOutputStream()) {
if (extPart.getSize() > 0) { if (extPart.getSize() > 0) {
extPart.clear(); extPart.clear();
}
ext.props.save(out, DEFAULT_XML_OPTIONS);
} }
ext.props.save(out, DEFAULT_XML_OPTIONS);
out.close();
} }
if(custPart != null){ if(custPart != null){
OutputStream out = custPart.getOutputStream(); try (OutputStream out = custPart.getOutputStream()) {
cust.props.save(out, DEFAULT_XML_OPTIONS); cust.props.save(out, DEFAULT_XML_OPTIONS);
out.close(); }
} }
} }

View File

@ -724,6 +724,18 @@ public class TestXSLFBugs {
ppt.close(); 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 @Test
public void bug62051() throws IOException { public void bug62051() throws IOException {
final Function<List<XSLFShape>, int[]> ids = (shapes) -> final Function<List<XSLFShape>, int[]> ids = (shapes) ->

Binary file not shown.