naruto-lgtm commented on code in PR #1579:
URL: https://github.com/apache/poi/pull/1579#discussion_r4651347884
##########
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:
You're right, and dropping the relationship was the wrong half of the fix.
`setAddress` now asks the sheet the link belongs to (`XSSFHyperlink` remembers
it from `initHyperlinks` / `addHyperlink`) to add a relationship for the new
address and remove the old one straight away, and it updates the `r:id` itself.
So by the time `cloneSheet` copies the relationships, the package already
matches what `write()` emits and the clone resolves its `r:id`.
`testCloneSheetAfterSetAddress` covers it, it also fails on trunk because the
clone used to get the old URL.
##########
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:
Agreed. The location that was folded into the address at load time is kept
separately now, and it's only unset if it's still the one the relationship
contributed, so a location the caller set stays. `setLocation("Top");
setAddress("https://example.org/page")` saves `/page#Top` on a loaded link and
on one from `createHyperlink`, same for the other order.
`testSetLocationBeforeSetAddress` asserts both.
##########
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:
Good catch. Removal goes through `removeReferenceRelationship` first and
only falls back to `part.removeRelationship` when the id isn't in that map,
which is the case for relationships created at write time.
`testChangeOrRemoveLoadedHyperlink` now asserts the changed and the removed URL
are gone from `getReferenceRelationships()`.
##########
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:
The sweep is gone. Only the relationship a hyperlink actually held is
removed, so an `r:id` in `extLst` or one added with
`POIXMLDocumentPart.createHyperlink` isn't touched anymore.
##########
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:
Right, matching by id was wrong. It now works off the `PackageRelationship`
the hyperlink holds, and removes it only if its source is this sheet's part and
no hyperlink in the workbook still refers to it. A link shared from another
sheet keeps that sheet's relationship, and the copies `removeHyperlink(int,
int)` makes keep the shared one.
##########
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:
Moved out of `write()`. The relationship work happens in `setAddress` and
`removeHyperlink` now, `write()` only generates a relationship for a link that
doesn't have one yet, so cloning no longer changes the source sheet.
##########
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:
Added all four: `testCloneSheetAfterSetAddress`, the
`getReferenceRelationships()` assertions in
`testChangeOrRemoveLoadedHyperlink`, `testWriteTwiceKeepsOneHyperlinkRelation`
(same rel id and one relationship after two writes), and
`testSetLocationBeforeSetAddress`.
--
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]