pjfanning commented on code in PR #1543:
URL: https://github.com/apache/poi/pull/1543#discussion_r4501666734
##########
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:
This can leave `XSSFWorkbook.pictures` holding a picture whose part has been
deleted. `XSSFWorkbook.addPicture` creates the image with `noRelation=true` and
adds it to `pictures`, so the workbook has no relationship to the image. The
image's only tracked relation is from the drawing, through the separate
`XSSFPictureData` that `XSSFDrawing.addPictureReference` creates. When the
sheet is removed, the count drops to 0, `isRelationshipTarget()` returns false,
and the part is removed. The second half of
`testRemoveSheetRemovesDrawingParts` checks exactly this for `own`.
But `wb.getAllPictures()` still returns that picture afterwards, so its
index is stale. This sequence:
```java
int idx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
wb.getSheet("Template").createDrawingPatriarch().createPicture(anchor, idx);
wb.removeSheet("Template")...
wb.getSheet("Other").createDrawingPatriarch().createPicture(anchor, idx);
```
now adds a relationship to a part that is no longer in the package, so the
written file should be missing the image. On trunk it works, because the image
part was never removed.
Two ways to fix it:
- after removal, drop entries from the workbook's `pictures` list whose part
is no longer in the package (indexes shift, but they then match what a reload
would show)
- treat pictures held in that list as still in use, so they aren't removed
A test that reuses the index after `removeSheetAt` would cover it.
##########
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:
Minor: `PackageRelationship.getTargetURI()` returns the raw target, without
resolving it, for internal `HYPERLINK_PART` relationships. If that target is
relative, `createPartName` throws, the exception is swallowed, and the
relationship isn't counted. So internal hyperlinks only keep a part alive when
their target is absolute. Resolving first with
`PackagingURIHelper.resolvePartUri(rel.getSourceURI(), uri)` would handle both.
It rarely matters in practice, because xlsx/docx internal links don't use
relationships and pptx slide jumps use the `slide` type, but the PR description
currently promises more than the code does.
##########
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:
XSLF has a similar problem. `getPictureData()` fills `_pictures` with its
own `XSLFPictureData` objects, built from the package parts, and neither
picture-data class overrides `equals`. So for a loaded deck, this `remove` only
works when `picture` is the same object, which it usually isn't. That part is
already the case on trunk for pictures placed directly on a slide. What this PR
adds is that images reached indirectly are now deleted from the package too,
e.g. SmartArt media under `/ppt/diagrams`. Those images stay in `_pictures`.
If `getPictureData()` (or `addPicture`) was called before `removeSlide`,
then `addPicture(sameBytes)` finds the stale entry through `findPictureData`,
and the new picture shape links to a part that is no longer in the package.
Pruning `_pictures` of parts the package no longer contains, after the slide is
removed, would cover both the direct and the indirect cases.
##########
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:
This scans every part's relationships once for each child that would be
removed. That's fine as a first step, since it only runs on removal and the
numbers in the description are acceptable. The incoming-relationship index you
suggested would be a reasonable follow-up if anyone hits the 10k-picture case.
--
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]