pjfanning commented on code in PR #1601:
URL: https://github.com/apache/poi/pull/1601#discussion_r4726480047
##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSimpleShape.java:
##########
@@ -708,6 +708,11 @@ void copy(XSLFShape sh){
XSLFSimpleShape s = (XSLFSimpleShape)sh;
+ XSLFHyperlink hyperSrc = s.getHyperlink();
+ if (hyperSrc != null) {
+ getHyperlink().copy(hyperSrc);
Review Comment:
`XSLFHyperlink.getType()` returns `URL` for anything that is not a slide
jump or a `mailto:`, so I think these cases change for the worse:
1. Actions with no relationship, such as `<a:hlinkClick r:id=""
action="ppaction://media"/>` on a video/audio picture, or
`ppaction://macro?name=...`, `ppaction://noaction`,
`ppaction://customshow?...`. `linkToExternal(src.getAddress())` would add an
external relationship and unset the action, so the media/macro action is lost.
2. `ppaction://hlinkfile` and `ppaction://hlinkpres?...` links, which have a
relationship id and an action: the relationship is recreated as a plain
hyperlink and the action is unset.
3. If `getAddress()` returns null (empty id and no action, or an id with no
relationship in the source), or a string that is not a valid URI (e.g. a macro
name with a space), `addExternalRelationship` throws `IllegalArgumentException`
and the whole `importContent` fails, where it worked before.
Could the link only be rewritten when the source is a real URL/email
relationship or a slide jump, leaving the copied XML alone otherwise (and
preserving the action where there is one)? Tests for `ppaction://media`, a
macro action and an empty-id link would be good.
##########
poi-ooxml/src/test/java/org/apache/poi/xslf/usermodel/TestXSLFHyperlink.java:
##########
@@ -177,4 +179,42 @@ void bug47291() throws IOException {
ppt2.close();
}
-}
\ No newline at end of file
+
+ @Test
+ void testImportContentKeepsShapeHyperlinks() throws IOException {
+ try (XMLSlideShow src = new XMLSlideShow(); XMLSlideShow dst = new
XMLSlideShow()) {
+ XSLFSlide slide1 = src.createSlide();
+ XSLFSlide slide2 = src.createSlide();
+
+ XSLFAutoShape urlShape = slide1.createAutoShape();
+ urlShape.createHyperlink().linkToUrl("https://poi.apache.org/");
+ XSLFAutoShape slideShape = slide1.createAutoShape();
+ slideShape.createHyperlink().linkToSlide(slide2);
+ XSLFAutoShape nextShape = slide1.createAutoShape();
+ nextShape.createHyperlink().linkToNextSlide();
+ XSLFPictureShape picture = slide1.createPicture(src.addPicture(new
byte[]{1, 2, 3}, PictureType.PNG));
+ picture.createHyperlink().linkToEmail("[email protected]");
+
+ XSLFSlide copy = dst.createSlide().importContent(slide1);
+ List<XSLFShape> shapes = copy.getShapes();
+
+ XSLFHyperlink url = ((XSLFAutoShape) shapes.get(0)).getHyperlink();
+ assertEquals(HyperlinkType.URL, url.getType());
+ assertEquals("https://poi.apache.org/", url.getAddress());
+
+ XSLFHyperlink toSlide = ((XSLFAutoShape)
shapes.get(1)).getHyperlink();
+ assertEquals(HyperlinkType.DOCUMENT, toSlide.getType());
+ assertEquals(slide2.getPackagePart().getPartName().getName(),
toSlide.getAddress());
+
Review Comment:
`dst` has only one slide, so this asserts that the copy links to
`/ppt/slides/slide2.xml`, which does not exist in `dst` (the relationship is
added with the part of the source slideshow - the "kind of a hack" in
`XSLFHyperlink.copy`). That was already the behaviour for text runs, but the
test never writes `dst` out and reads it back, so it would not notice if the
saved file is invalid. Could the test round-trip `dst` and check the links
after the read?
##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFHyperlink.java:
##########
@@ -150,6 +150,9 @@ public void linkToLastSlide() {
}
void copy(XSLFHyperlink src) {
+ // the link was copied together with the xml of another sheet, so its
id
+ // is not a relation of this sheet and must not be removed by the
linkTo methods
+ _link.setId("");
Review Comment:
This also changes the existing text run path. In the `DOCUMENT` branch
below, when `src._sheet.getRelationById(idSrc)` returns null nothing is set, so
the copy is now left with `r:id=""` and `action="ppaction://hlinksldjump"`,
i.e. a slide jump with no target. Should the action (or the hyperlink element)
be removed in that case?
##########
poi-ooxml/src/main/java/org/apache/poi/xslf/usermodel/XSLFSimpleShape.java:
##########
@@ -708,6 +708,11 @@ void copy(XSLFShape sh){
XSLFSimpleShape s = (XSLFSimpleShape)sh;
+ XSLFHyperlink hyperSrc = s.getHyperlink();
Review Comment:
`getHyperlink()` dereferences `getCNvPr()` without a null check, and other
code in this class (e.g. `getShapeName`) treats a null `cNvPr` as possible.
Calling it here for every copied shape (source and target) means such a shape
would now fail the import with a `NullPointerException`. A null check in
`getHyperlink()` would cover it.
--
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]