diff options
author | Andreas Beeker <kiwiwings@apache.org> | 2017-09-17 22:45:03 +0000 |
---|---|---|
committer | Andreas Beeker <kiwiwings@apache.org> | 2017-09-17 22:45:03 +0000 |
commit | 910c91900ac71c7b376ff632e79f36b7debde2cc (patch) | |
tree | d03f8d38ff194c02d30a73593ad831be5d99b79c /src/ooxml/java | |
parent | e504e87873d0837079da7567cf04d8fd219217b4 (diff) | |
download | poi-910c91900ac71c7b376ff632e79f36b7debde2cc.tar.gz poi-910c91900ac71c7b376ff632e79f36b7debde2cc.zip |
#60499 - Deleting a picture that is used twice on a slide corrupt the slide
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1808661 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java')
3 files changed, 76 insertions, 12 deletions
diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java b/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java index c5afb15446..5b6add51ab 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java +++ b/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java @@ -231,15 +231,35 @@ public class POIXMLDocumentPart { * @return the target part of the relation, or null, if none exists */ public final POIXMLDocumentPart getRelationById(String id) { - RelationPart rp = relations.get(id); + RelationPart rp = getRelationPartById(id); return (rp == null) ? null : rp.getDocumentPart(); } /** - * Returns the {@link PackageRelationship#getId()} of the + * Returns the target {@link RelationPart}, where a + * {@link PackageRelationship} is set from the {@link PackagePart} of this + * {@link POIXMLDocumentPart} to the {@link PackagePart} of the target + * {@link POIXMLDocumentPart} with a {@link PackageRelationship#getId()} + * matching the given parameter value. + * + * @param id + * The relation id to look for + * @return the target relation part, or null, if none exists + * + * @since 4.0.0 + */ + public final RelationPart getRelationPartById(String id) { + return relations.get(id); + } + + /** + * Returns the first {@link PackageRelationship#getId()} of the * {@link PackageRelationship}, that sources from the {@link PackagePart} of * this {@link POIXMLDocumentPart} to the {@link PackagePart} of the given - * parameter value. + * parameter value.<p> + * + * There can be multiple references to the given {@link POIXMLDocumentPart} + * and only the first in the order of creation is returned. * * @param part * The {@link POIXMLDocumentPart} for which the according @@ -292,7 +312,11 @@ public class POIXMLDocumentPart { /** * Remove the relation to the specified part in this package and remove the - * part, if it is no longer needed. + * part, if it is no longer needed.<p> + * + * If there are multiple relationships to the same part, this will only + * remove the first relationship in the order of creation. The removal + * via the part id ({@link #removeRelation(String)} is preferred. * * @param part the part which relation is to be removed from this document */ @@ -302,7 +326,11 @@ public class POIXMLDocumentPart { /** * Remove the relation to the specified part in this package and remove the - * part, if it is no longer needed and flag is set to true. + * part, if it is no longer needed and flag is set to true.<p> + * + * If there are multiple relationships to the same part, this will only + * remove the first relationship in the order of creation. The removal + * via the part id ({@link #removeRelation(String,boolean)} is preferred. * * @param part * The related part, to which the relation shall be removed. @@ -311,18 +339,53 @@ public class POIXMLDocumentPart { * needed any longer. * @return true, if the relation was removed */ - protected final boolean removeRelation(POIXMLDocumentPart part, boolean removeUnusedParts){ + protected final boolean removeRelation(POIXMLDocumentPart part, boolean removeUnusedParts) { String id = getRelationId(part); - if (id == null) { + return removeRelation(id, removeUnusedParts); + } + + /** + * Remove the relation to the specified part in this package and remove the + * part, if it is no longer needed.<p> + * + * If there are multiple relationships to the same part, this will only + * remove the first relationship in the order of creation. The removal + * via the part id ({@link #removeRelation(String)} is preferred. + * + * @param partId the part id which relation is to be removed from this document + * + * @since 4.0.0 + */ + protected final void removeRelation(String partId) { + removeRelation(partId, true); + } + + /** + * Remove the relation to the specified part in this package and remove the + * part, if it is no longer needed and flag is set to true.<p> + * + * @param partId + * The related part id, to which the relation shall be removed. + * @param removeUnusedParts + * true, if the part shall be removed from the package if not + * needed any longer. + * @return true, if the relation was removed + * + * @since 4.0.0 + */ + private final boolean removeRelation(String partId, boolean removeUnusedParts) { + RelationPart rp = relations.get(partId); + if (rp == null) { // part is not related with this POIXMLDocumentPart return false; } + POIXMLDocumentPart part = rp.getDocumentPart(); /* decrement usage counter */ part.decrementRelationCounter(); /* remove packagepart relationship */ - getPackagePart().removeRelationship(id); + getPackagePart().removeRelationship(partId); /* remove POIXMLDocument from relations */ - relations.remove(id); + relations.remove(partId); if (removeUnusedParts) { /* if last relation to target part was removed, delete according target part */ @@ -338,6 +401,8 @@ public class POIXMLDocumentPart { return true; } + + /** * Returns the parent POIXMLDocumentPart. All parts except root have not-null parent. * diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java index aed2ff7c49..148f5cd6a7 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSheet.java @@ -612,7 +612,6 @@ implements XSLFShapeContainer, Sheet<XSLFShape,XSLFTextParagraph> { * @param pictureShape the picture shapes whose relation is to be removed */ void removePictureRelation(XSLFPictureShape pictureShape) { - POIXMLDocumentPart pd = getRelationById(pictureShape.getBlipId()); - removeRelation(pd); + removeRelation(pictureShape.getBlipId()); } }
\ No newline at end of file diff --git a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlide.java b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlide.java index e443fa3106..cfa5873cc5 100644 --- a/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlide.java +++ b/src/ooxml/java/org/apache/poi/xslf/usermodel/XSLFSlide.java @@ -243,7 +243,7 @@ implements Slide<XSLFShape,XSLFTextParagraph> { if (bgThis != null) { if (bgThis.isSetBgPr() && bgThis.getBgPr().isSetBlipFill()) { String oldId = bgThis.getBgPr().getBlipFill().getBlip().getEmbed(); - removeRelation(getRelationById(oldId)); + removeRelation(oldId); } _slide.getCSld().unsetBg(); } |