Bug 62736 - [PATCH] Relations on XSLFPictureShape are removed unconditionally
Summary: [PATCH] Relations on XSLFPictureShape are removed unconditionally
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSLF (show other bugs)
Version: 4.0.x-dev
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-09-18 20:14 UTC by Mate Borcsok
Modified: 2018-09-26 21:43 UTC (History)
0 users



Attachments
change in removePictureRelation function and testcase (5.06 KB, patch)
2018-09-18 20:19 UTC, Mate Borcsok
Details | Diff
example pptx (49.73 KB, application/vnd.openxmlformats-officedocument.presentationml.presentation)
2018-09-18 20:20 UTC, Mate Borcsok
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Mate Borcsok 2018-09-18 20:14:59 UTC
It is only safe to remove a relation of a PictureShape if there are no more relations to that media on its slide.

I created a small example pptx where I copy pasted an image on a slide and removed only one of them using poi. The current implementation removes the relation and the document gets corrupted because of the invalid link.

The proposed fix is to iterate on the pictures of the slide and only remove the relation if that is the only one.
Comment 1 Mate Borcsok 2018-09-18 20:19:51 UTC
Created attachment 36155 [details]
change in removePictureRelation function and testcase
Comment 2 Mate Borcsok 2018-09-18 20:20:21 UTC
Created attachment 36156 [details]
example pptx
Comment 3 PJ Fanning 2018-09-18 23:36:40 UTC
Comment on attachment 36155 [details]
change in removePictureRelation function and testcase

Would it be better if the new code threw an exception if it isn't safe to remove the picture relation?

+        if (numberOfRelations <= 1) {
+            removeRelation(pictureShape.getBlipId());
+        } else {
+            throw new IllegalStateException("Cannot remove picture relation because there are other references to the picture");
+        }
Comment 4 Mate Borcsok 2018-09-19 06:35:45 UTC
(In reply to PJ Fanning from comment #3)
> Comment on attachment 36155 [details]
> change in removePictureRelation function and testcase
> 
> Would it be better if the new code threw an exception if it isn't safe to
> remove the picture relation?
> 
> +        if (numberOfRelations <= 1) {
> +            removeRelation(pictureShape.getBlipId());
> +        } else {
> +            throw new IllegalStateException("Cannot remove picture relation
> because there are other references to the picture");
> +        }

As far as I understood, it wouldn't.
It is a valid case to remove just one PictureShape, but leave the relation between the Slide and the media.
The new testcase would also fail at TestXSLFBugs.bug62736(TestXSLFBugs.java:124)

The reason why I used <= 1 that in POIXMLDocumentPart.java we just return false if there are no relations, and I wanted to keep the existing functionality in that case.
        RelationPart rp = relations.get(partId);
        if (rp == null) {
            // part is not related with this POIXMLDocumentPart
            return false;
        }