naruto-lgtm commented on code in PR #1543:
URL: https://github.com/apache/poi/pull/1543#discussion_r4506847873


##########
poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java:
##########
@@ -406,17 +407,76 @@ private boolean removeRelation(String partId, boolean 
removeUnusedParts) {
         if (removeUnusedParts) {
             /* if last relation to target part was removed, delete according 
target part */
             if (part.getRelationCounter() == 0) {
-                try {
-                    part.onDocumentRemove();
-                } catch (IOException e) {
-                    throw new POIXMLException(e);
-                }
-                
getPackagePart().getPackage().removePart(part.getPackagePart());
+                part.removeFromPackage();
             }
         }
         return true;
     }
 
+    /**
+     * Remove this part from the package, together with the parts it relates to
+     * which are not used by any other part
+     */
+    private void removeFromPackage() {
+        try {
+            onDocumentRemove();
+        } catch (IOException e) {
+            throw new POIXMLException(e);
+        }
+        for (RelationPart rp : getRelationParts()) {
+            POIXMLDocumentPart child = rp.getDocumentPart();
+            removeRelation(rp.getRelationship().getId(), false);
+            // the relation counter alone is not enough, as a package part can 
be
+            // represented by more than one document part, e.g. a picture on 
two sheets
+            if (child.getRelationCounter() == 0 && 
!child.isRelationshipTarget()) {
+                child.removeFromPackage();

Review Comment:
   Went with the first option: `removeSheetAt` now drops entries from 
`pictures` whose part is gone, so `getAllPictures()` matches what a reload 
would show. Treating listed pictures as in use would have kept every image once 
`getAllPictures()` had been called, which `createPicture` does internally.
   
   That needed one more change. `addPicture` used `size() + 1` as the part 
number, which collides once the list has a gap (drop image1, keep image2, the 
next add tries `image2.png` and throws `PartAlreadyExistsException`). It now 
asks `getNextPartNumber` starting from that number and returns the list index.
   
   The trade-off you mentioned stands: an index held from before the removal 
refers to the shifted list (or is past its end) rather than to a deleted part. 
`testRemoveSheetUpdatesPictureList` reuses the remaining indexes and adds a 
picture after the removal, then checks the written file.
   



##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XMLSlideShow.java:
##########
@@ -519,6 +518,9 @@ public XSLFSlide removeSlide(int index) {
                 _pictures.remove(picture);

Review Comment:
   Done. After the slide's relation is removed, `removeSlide` drops the 
`_pictures` entries whose part is no longer in the package, which covers 
pictures placed directly on a slide of a loaded deck as well as the SmartArt 
media. `addPicture` already picks a free name through `getUnusedPartIndex`, so 
nothing else was needed there.
   
   `testRemoveSlideUpdatesPictureData` covers both: on smartart-simple.pptx, 
adding the same bytes again after the removal creates a new part, and the 
read-back deck then removes its directly placed picture from the list too.
   



##########
poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java:
##########
@@ -406,17 +407,76 @@ private boolean removeRelation(String partId, boolean 
removeUnusedParts) {
         if (removeUnusedParts) {
             /* if last relation to target part was removed, delete according 
target part */
             if (part.getRelationCounter() == 0) {
-                try {
-                    part.onDocumentRemove();
-                } catch (IOException e) {
-                    throw new POIXMLException(e);
-                }
-                
getPackagePart().getPackage().removePart(part.getPackagePart());
+                part.removeFromPackage();
             }
         }
         return true;
     }
 
+    /**
+     * Remove this part from the package, together with the parts it relates to
+     * which are not used by any other part
+     */
+    private void removeFromPackage() {
+        try {
+            onDocumentRemove();
+        } catch (IOException e) {
+            throw new POIXMLException(e);
+        }
+        for (RelationPart rp : getRelationParts()) {
+            POIXMLDocumentPart child = rp.getDocumentPart();
+            removeRelation(rp.getRelationship().getId(), false);
+            // the relation counter alone is not enough, as a package part can 
be
+            // represented by more than one document part, e.g. a picture on 
two sheets
+            if (child.getRelationCounter() == 0 && 
!child.isRelationshipTarget()) {
+                child.removeFromPackage();
+            }
+        }
+        getPackagePart().getPackage().removePart(getPackagePart());
+    }
+
+    /**
+     * @return true, if a relationship of the package or one of its parts 
targets this part
+     */
+    private boolean isRelationshipTarget() {
+        OPCPackage pkg = getPackagePart().getPackage();
+        PackagePartName name = getPackagePart().getPartName();
+        try {
+            if (hasTarget(pkg.getRelationships(), name)) {
+                return true;
+            }
+            for (PackagePart pp : pkg.getParts()) {
+                if (!pp.isRelationshipPart() && 
hasTarget(pp.getRelationships(), name)) {
+                    return true;
+                }
+            }
+            return false;
+        } catch (InvalidFormatException | InvalidOperationException e) {
+            // keep the part, if the package relationships can't be checked
+            return true;
+        }
+    }
+
+    private static boolean hasTarget(PackageRelationshipCollection rels, 
PackagePartName name) {
+        for (PackageRelationship rel : rels) {
+            if (rel.getTargetMode() != TargetMode.INTERNAL) {
+                continue;
+            }
+            URI uri = rel.getTargetURI();

Review Comment:
   Fixed, a relative target is now resolved with 
`PackagingURIHelper.resolvePartUri(rel.getSourceURI(), uri)` before comparing. 
Fragment-only targets (`#foo`) are still skipped as before, since resolving 
them would make a part count as targeted by its own links. 
`testRemoveSheetKeepsPartTargetedByRelativeHyperlink` covers it, so the 
description's line about internal hyperlinks now holds.
   



##########
poi-ooxml/src/main/java/org/apache/poi/ooxml/POIXMLDocumentPart.java:
##########
@@ -406,17 +407,76 @@ private boolean removeRelation(String partId, boolean 
removeUnusedParts) {
         if (removeUnusedParts) {
             /* if last relation to target part was removed, delete according 
target part */
             if (part.getRelationCounter() == 0) {
-                try {
-                    part.onDocumentRemove();
-                } catch (IOException e) {
-                    throw new POIXMLException(e);
-                }
-                
getPackagePart().getPackage().removePart(part.getPackagePart());
+                part.removeFromPackage();
             }
         }
         return true;
     }
 
+    /**
+     * Remove this part from the package, together with the parts it relates to
+     * which are not used by any other part
+     */
+    private void removeFromPackage() {
+        try {
+            onDocumentRemove();
+        } catch (IOException e) {
+            throw new POIXMLException(e);
+        }
+        for (RelationPart rp : getRelationParts()) {
+            POIXMLDocumentPart child = rp.getDocumentPart();
+            removeRelation(rp.getRelationship().getId(), false);
+            // the relation counter alone is not enough, as a package part can 
be
+            // represented by more than one document part, e.g. a picture on 
two sheets
+            if (child.getRelationCounter() == 0 && 
!child.isRelationshipTarget()) {
+                child.removeFromPackage();
+            }
+        }
+        getPackagePart().getPackage().removePart(getPackagePart());
+    }
+
+    /**
+     * @return true, if a relationship of the package or one of its parts 
targets this part
+     */
+    private boolean isRelationshipTarget() {
+        OPCPackage pkg = getPackagePart().getPackage();
+        PackagePartName name = getPackagePart().getPartName();
+        try {
+            if (hasTarget(pkg.getRelationships(), name)) {
+                return true;
+            }
+            for (PackagePart pp : pkg.getParts()) {

Review Comment:
   Agreed, leaving the incoming-relationship index for a follow-up.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to