pjfanning commented on code in PR #1584:
URL: https://github.com/apache/poi/pull/1584#discussion_r4652137137
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java:
##########
@@ -161,15 +161,26 @@ public boolean needsRelationToo() {
* Generates the relation if required
*/
protected void generateRelationIfNeeded(PackagePart sheetPart) {
- if (_externalRel == null && needsRelationToo()) {
+ if (!needsRelationToo()) {
+ return;
+ }
+ if (_externalRel == null) {
// Generate the relation
_externalRel =
sheetPart.addExternalRelationship(_location,
XSSFRelation.SHEET_HYPERLINKS.getRelation());
- _relShared = false;
-
- // Update the r:id
- _ctHyperlink.setId(_externalRel.getId());
+ } else if (sheetPart.equals(_externalRel.getSource()) &&
sheetPart.getRelationship(_externalRel.getId()) == null) {
+ // the relationship was removed together with this hyperlink,
which was added to the sheet
+ // again since, e.g. to move it to another cell. The old target is
used, as the location
+ // is still written as a separate attribute.
+ _externalRel = sheetPart.addExternalRelationship(
+ _externalRel.getTargetURI().toString(),
XSSFRelation.SHEET_HYPERLINKS.getRelation());
Review Comment:
Fixed in 52db527899 rather than leaving it for a follow-up.
`generateRelationIfNeeded` now also creates a relationship when the hyperlink's
relationship belongs to another sheet and this sheet is the one the hyperlink
was last added to, reusing the old target. Checking the owning sheet means a
hyperlink object that is still listed by both sheets
(`sheet2...setHyperlink(sheet1.getHyperlink(0, 0))` without removing it)
doesn't flip its relationship back and forth on every write. That shared-object
case was already broken before this PR, since both sheets share its cell
reference too. `testMoveLoadedHyperlinkToAnotherSheet` covers the move.
##########
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java:
##########
@@ -678,6 +683,98 @@ void testRemoveLoadedHyperlinkSharingItsRelationship()
throws IOException, Inval
}
}
+ @Test
+ void testAddRemovedLoadedHyperlinkAgain() throws IOException,
InvalidFormatException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFSheet sheet = wb.createSheet();
+ XSSFHyperlink link =
wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
+ link.setAddress("https://example.com/page");
+ link.setLocation("section");
+ sheet.createRow(0).createCell(0).setHyperlink(link);
+
+ try (XSSFWorkbook wb2 =
XSSFTestDataSamples.writeOutAndReadBack(wb)) {
+ // move the hyperlink from A1 to A2 by removing and adding the
same object
+ XSSFSheet sheet2 = wb2.getSheetAt(0);
Review Comment:
`cell.removeHyperlink()` does go through
`XSSFSheet.removeHyperlink(XSSFHyperlink)`: `XSSFCell.removeHyperlink()` calls
`removeHyperlink(int, int)`, and for a single-cell link that calls
`removeHyperlink(hyperlink)`, which clears the sheet. So this test exercises
`setSheet(null)` as well. `testSetAddressOfRemovedHyperlink` calls
`sheet.removeHyperlink(hyperlink)` directly, so both entry points are covered.
##########
poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFHyperlink.java:
##########
@@ -678,6 +683,98 @@ void testRemoveLoadedHyperlinkSharingItsRelationship()
throws IOException, Inval
}
}
+ @Test
+ void testAddRemovedLoadedHyperlinkAgain() throws IOException,
InvalidFormatException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFSheet sheet = wb.createSheet();
+ XSSFHyperlink link =
wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
+ link.setAddress("https://example.com/page");
+ link.setLocation("section");
+ sheet.createRow(0).createCell(0).setHyperlink(link);
+
+ try (XSSFWorkbook wb2 =
XSSFTestDataSamples.writeOutAndReadBack(wb)) {
+ // move the hyperlink from A1 to A2 by removing and adding the
same object
+ XSSFSheet sheet2 = wb2.getSheetAt(0);
+ XSSFHyperlink loaded = sheet2.getHyperlink(0, 0);
+ sheet2.getRow(0).getCell(0).removeHyperlink();
+ sheet2.createRow(1).createCell(0).setHyperlink(loaded);
+
+ try (XSSFWorkbook wb3 =
XSSFTestDataSamples.writeOutAndReadBack(wb2)) {
+ XSSFSheet sheet3 = wb3.getSheetAt(0);
+ assertNull(sheet3.getHyperlink(0, 0));
+ assertEquals("https://example.com/page#section",
sheet3.getHyperlink(1, 0).getAddress());
+ assertEquals(1, hyperlinkTargets(sheet3).size());
+ }
+ }
+ }
+ }
+
+ @Test
+ void testSetAddressOfRemovedHyperlink() throws IOException,
InvalidFormatException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFSheet sheet = wb.createSheet();
+ XSSFHyperlink link =
wb.getCreationHelper().createHyperlink(HyperlinkType.URL);
+ link.setAddress("https://example.com/old");
+ sheet.createRow(0).createCell(0).setHyperlink(link);
+
+ try (XSSFWorkbook wb2 =
XSSFTestDataSamples.writeOutAndReadBack(wb)) {
+ XSSFSheet sheet2 = wb2.getSheetAt(0);
+ XSSFHyperlink loaded = sheet2.getHyperlink(0, 0);
+ sheet2.removeHyperlink(loaded);
+ loaded.setAddress("https://example.org/new");
+
+ // the removed hyperlink doesn't add a relationship to the
sheet it was removed from
+ assertTrue(hyperlinkTargets(sheet2).isEmpty());
+
+ // and gets one for its new address once it is added again
+ sheet2.getRow(0).getCell(0).setHyperlink(loaded);
+ try (XSSFWorkbook wb3 =
XSSFTestDataSamples.writeOutAndReadBack(wb2)) {
+ XSSFSheet sheet3 = wb3.getSheetAt(0);
+ assertEquals("https://example.org/new",
sheet3.getHyperlink(0, 0).getAddress());
+ assertEquals(List.of("https://example.org/new"),
hyperlinkTargets(sheet3));
+ }
+ }
+ }
+ }
+
+ @Test
+ void testCloneSheetWithLoadedHyperlinkKeepsWorkbookRelationships() throws
IOException, InvalidFormatException {
+ try (XSSFWorkbook wb = new XSSFWorkbook()) {
+ XSSFSheet sheet = wb.createSheet("src");
Review Comment:
That assert can't pass vacuously: if the ids stopped overlapping,
`getRelationship(linkRelId)` would return null and the `assertNotNull` would
fail, flagging that the test's premise no longer holds. I've added a comment
saying so in 52db527899. Separately, CI showed the comparison after cloning was
too strict, because cloning rightly adds a relationship for the new sheet. The
test now checks that every relationship that existed before keeps its type, and
that exactly one was added.
--
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]