pjfanning commented on code in PR #1579:
URL: https://github.com/apache/poi/pull/1579#discussion_r4648027235
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java:
##########
@@ -237,6 +237,13 @@ public void setLocation(String location) {
public void setAddress(String address) {
validate(address);
+ if (_externalRel != null && !address.equals(_location)) {
Review Comment:
Clearing `_externalRel` here breaks `XSSFWorkbook.cloneSheet` for a loaded
link: `cloneSheet` copies the source sheet's relationships first and then calls
`srcSheet.write(out)`, and it's that write which creates the new relationship
(`generateRelationIfNeeded`). The serialised XML then has `r:id=rIdN+1`, which
the clone doesn't have, and the clone's `initHyperlinks` throws
`IllegalStateException("... references relation rIdN+1, but that didn't
exist!")`.
Repro (worked out from the code, not run): load an .xlsx with an external
link, `sheet.getHyperlink(0,0).setAddress("https://example.org/new")`, then
`wb.cloneSheet(0)`. Before this PR the link kept its old relationship and
cloning worked.
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java:
##########
@@ -239,6 +239,32 @@ private void initHyperlinks() {
}
}
+ /**
+ * Remove the hyperlink relations no hyperlink refers to anymore, i.e.
those of hyperlinks
+ * that were removed or got a new address, as they still hold the old
target
+ */
+ private void removeUnusedHyperlinkRelations() {
+ PackagePart part = getPackagePart();
+ if (part == null) {
+ return;
+ }
+
+ Set<String> ids = new HashSet<>();
+ for (XSSFHyperlink hyperlink : hyperlinks) {
+ ids.add(hyperlink.getCTHyperlink().getId());
+ }
+
+ try {
+ for (PackageRelationship rel :
part.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation())) {
+ if (!ids.contains(rel.getId())) {
+ part.removeRelationship(rel.getId());
Review Comment:
`part.removeRelationship` removes it from the package part, but the sheet's
`POIXMLDocumentPart.referenceRelationships` map keeps the
`HyperlinkRelationship` entry, so `getReferenceRelationships()` /
`getReferenceRelationship(id)` still return the old URL. `cloneSheet` iterates
`getReferenceRelationships()` and calls `createHyperlink(uri, ..., id)`, which
puts the removed/old target back into the package. That defeats the goal of
removing the old address. Using `removeReferenceRelationship(id)` would keep
the two in sync.
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java:
##########
@@ -239,6 +239,32 @@ private void initHyperlinks() {
}
}
+ /**
+ * Remove the hyperlink relations no hyperlink refers to anymore, i.e.
those of hyperlinks
+ * that were removed or got a new address, as they still hold the old
target
+ */
+ private void removeUnusedHyperlinkRelations() {
+ PackagePart part = getPackagePart();
+ if (part == null) {
+ return;
+ }
+
+ Set<String> ids = new HashSet<>();
+ for (XSSFHyperlink hyperlink : hyperlinks) {
+ ids.add(hyperlink.getCTHyperlink().getId());
+ }
+
+ try {
+ for (PackageRelationship rel :
part.getRelationshipsByType(XSSFRelation.SHEET_HYPERLINKS.getRelation())) {
Review Comment:
This deletes every hyperlink-type relationship that the `<hyperlinks>` list
doesn't reference, including ones referenced from markup POI doesn't model
(e.g. an `r:id` in `extLst`) or added by user code via
`POIXMLDocumentPart.createHyperlink(...)`. After any load/save round trip those
references would dangle. Could the cleanup be limited to relationships POI
knows it made obsolete (the old `_externalRel` of a changed link, or the rel of
a removed hyperlink), rather than a sweep by type?
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java:
##########
@@ -3961,6 +3987,7 @@ protected void write(OutputStream out) throws IOException
{
// nothing to do
}*/
}
+ removeUnusedHyperlinkRelations();
Review Comment:
This mutates the package inside `write()`. `cloneSheet` calls
`srcSheet.write()` only to serialise the source, so cloning now also deletes
relationships from the source sheet, after they've already been copied into the
clone (which keeps the stale rel until it's written itself). A serialisation
step shouldn't have side effects on the sheet; doing the removal in
`setAddress` / `removeHyperlink` instead would avoid that.
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java:
##########
@@ -237,6 +237,13 @@ public void setLocation(String location) {
public void setAddress(String address) {
validate(address);
+ if (_externalRel != null && !address.equals(_location)) {
+ // the relationship holds the old target, a new one is generated
when the sheet is written
+ _externalRel = null;
+ if (_ctHyperlink.isSetLocation()) {
Review Comment:
This silently unsets the location even if the caller just set it, so the
result depends on call order and on where the link came from: on a loaded link,
`setLocation("Top"); setAddress("https://example.org/page");` saves `/page`,
while the same calls on a link from `createHyperlink` keep `#Top`. Could the
location only be cleared when it came from the old relationship, not when it
was set explicitly?
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java:
##########
@@ -239,6 +239,32 @@ private void initHyperlinks() {
}
}
+ /**
+ * Remove the hyperlink relations no hyperlink refers to anymore, i.e.
those of hyperlinks
+ * that were removed or got a new address, as they still hold the old
target
+ */
+ private void removeUnusedHyperlinkRelations() {
+ PackagePart part = getPackagePart();
+ if (part == null) {
+ return;
+ }
+
+ Set<String> ids = new HashSet<>();
+ for (XSSFHyperlink hyperlink : hyperlinks) {
+ ids.add(hyperlink.getCTHyperlink().getId());
Review Comment:
The ids come from `getCTHyperlink().getId()`, which can be the id of a
relationship in another sheet:
`sheet2...setHyperlink(sheet1.getHyperlink(0,0))` shares an `XSSFHyperlink`
whose `_externalRel` belongs to sheet1, so `generateRelationIfNeeded` skips it
and sheet2's XML references sheet1's `rId1`. If sheet2 has an unrelated
hyperlink rel `rId1` (e.g. of a link removed in sheet2), it's kept and the
copied cell points to that old URL.
##########
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java:
##########
@@ -600,4 +601,36 @@ private void testRemoveSharedHyperlinkFromOneCell(String
area, CellAddress cellA
}
}
+ @Test
+ void testChangeOrRemoveLoadedHyperlink() throws IOException,
InvalidFormatException {
Review Comment:
Could the test also cover:
- `wb2.cloneSheet(0)` after `setAddress` (currently throws, see the comment
on `setAddress`);
- `sheet2.getReferenceRelationships()` no longer containing the old/removed
URL;
- writing the same workbook twice (relationship ids shouldn't churn);
- `setLocation` before `setAddress` on a loaded link.
--
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]