naruto-lgtm commented on code in PR #1577:
URL: https://github.com/apache/poi/pull/1577#discussion_r4651317502
##########
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java:
##########
@@ -100,6 +100,7 @@ public class XWPFDocument extends POIXMLDocument implements
Document, IBody {
protected List<XWPFFooter> footers = new ArrayList<>();
protected List<XWPFHeader> headers = new ArrayList<>();
protected List<XWPFHyperlink> hyperlinks = new ArrayList<>();
Review Comment:
Dropped the private map in 934d603, so there's no second source of truth
anymore. `getHyperlinkByID` looks up the id on the package part first, and for
ids that aren't relationships of the part it falls back to scanning
`hyperlinks`, so entries a subclass adds to the list still resolve.
`testHyperlinkAddedToList` covers that case.
##########
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java:
##########
@@ -443,22 +445,29 @@ public String getTblStyle(XWPFTable table) {
}
public XWPFHyperlink getHyperlinkByID(String id) {
- for (XWPFHyperlink link : hyperlinks) {
- if (link.getId().equals(id)) {
- return link;
- }
+ if (id == null) {
+ return null;
+ }
+ XWPFHyperlink link = hyperlinksById.get(id);
Review Comment:
Fixed, there's nothing left to go stale. A hyperlink relationship on the
part always wins and is read fresh, so a removed and re-added `rId5` resolves
to the new URL, and if it's re-added with a different type it returns null.
`getHyperlinks()` re-reads the relationships, so a removed link drops out of
the list too. Both cases are in `testHyperlinksFollowRelationships`.
##########
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java:
##########
@@ -443,22 +445,29 @@ public String getTblStyle(XWPFTable table) {
}
public XWPFHyperlink getHyperlinkByID(String id) {
- for (XWPFHyperlink link : hyperlinks) {
- if (link.getId().equals(id)) {
- return link;
- }
+ if (id == null) {
+ return null;
+ }
+ XWPFHyperlink link = hyperlinksById.get(id);
+ if (link != null) {
+ return link;
}
- // If the link was not found, rebuild the list (maybe a new link was
added into the document) and check again.
- initHyperlinks();
- for (XWPFHyperlink link : hyperlinks) {
- if (link.getId().equals(id)) {
- return link;
- }
+ // The link may have been added after the document was read, e.g. by
createHyperlinkRun.
+ // Only look up this relationship: rebuilding the list for every
unknown id is quadratic
+ // in the number of hyperlinks, and the ids are taken from the
document content.
+ PackageRelationship rel = getPackagePart().getRelationship(id);
Review Comment:
`getHyperlinks()` now re-reads the relationships on every call, as you
suggested, so after two `createHyperlinkRun` calls it returns both links in
relationship order whatever has been resolved in between. Agreed on
headers/footers/footnotes/comments using the main part's relationships. That's
a separate problem and I'll keep it out of this PR.
##########
poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java:
##########
@@ -443,22 +445,29 @@ public String getTblStyle(XWPFTable table) {
}
public XWPFHyperlink getHyperlinkByID(String id) {
- for (XWPFHyperlink link : hyperlinks) {
- if (link.getId().equals(id)) {
- return link;
- }
+ if (id == null) {
+ return null;
+ }
+ XWPFHyperlink link = hyperlinksById.get(id);
+ if (link != null) {
+ return link;
}
- // If the link was not found, rebuild the list (maybe a new link was
added into the document) and check again.
- initHyperlinks();
- for (XWPFHyperlink link : hyperlinks) {
- if (link.getId().equals(id)) {
- return link;
- }
+ // The link may have been added after the document was read, e.g. by
createHyperlinkRun.
+ // Only look up this relationship: rebuilding the list for every
unknown id is quadratic
+ // in the number of hyperlinks, and the ids are taken from the
document content.
+ PackageRelationship rel = getPackagePart().getRelationship(id);
+ if (rel == null ||
!XWPFRelation.HYPERLINK.getRelation().equals(rel.getRelationshipType())) {
+ return null;
}
+ return addHyperlink(rel);
+ }
- // Link still not there? Giving up.
- return null;
+ private XWPFHyperlink addHyperlink(PackageRelationship rel) {
+ XWPFHyperlink link = new XWPFHyperlink(rel.getId(),
rel.getTargetURI().toString());
+ hyperlinks.add(link);
Review Comment:
The getter doesn't write anything now. `getHyperlinkByID` only reads, and
`initHyperlinks` builds a new list and assigns it rather than changing the old
one in place, so a thread iterating an earlier list isn't affected. Ordering
follows the relationships again because `getHyperlinks()` rebuilds.
##########
poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java:
##########
@@ -223,6 +228,44 @@ void testAddHyperlink() throws IOException {
}
}
+ /**
+ * Looking up an id that isn't a hyperlink relationship must not rebuild
the hyperlink list,
+ * otherwise a document with n such links and n hyperlink relationships
takes O(n^2) to extract
+ */
+ @Test
+ void testUnknownHyperlinkIds() throws Exception {
+ final int count = 10_000;
+ try (XWPFDocument doc = new XWPFDocument()) {
+ for (int i = 0; i < count; i++) {
+ doc.getPackagePart().addRelationship(new
URI("https://poi.apache.org/" + i),
+ TargetMode.EXTERNAL, XWPFRelation.HYPERLINK.getRelation(),
"rIdH" + i);
+ }
+ doc.getPackagePart().addRelationship(new
URI("https://poi.apache.org/"),
+ TargetMode.EXTERNAL, "http://example.com/other", "rIdOther");
+ XWPFParagraph p = doc.createParagraph();
+ for (int i = 0; i < count; i++) {
+ CTHyperlink link = p.getCTP().addNewHyperlink();
+ link.setId("x" + i);
+ link.addNewR().addNewT().setStringValue("a");
+ }
+ CTHyperlink known = p.getCTP().addNewHyperlink();
+ known.setId("rIdH42");
+ known.addNewR().addNewT().setStringValue("b");
+
+ try (XWPFDocument doc2 =
XWPFTestDataSamples.writeOutAndReadBack(doc)) {
+ assertTimeout(Duration.ofSeconds(20), () -> {
+ try (XWPFWordExtractor ex = new XWPFWordExtractor(doc2)) {
+ ex.setFetchHyperlinks(true);
+ assertTrue(ex.getText().contains("
<https://poi.apache.org/42>"));
Review Comment:
Done, it uses `assertThat(..., containsString(...))` now.
##########
poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java:
##########
@@ -223,6 +228,44 @@ void testAddHyperlink() throws IOException {
}
}
+ /**
+ * Looking up an id that isn't a hyperlink relationship must not rebuild
the hyperlink list,
+ * otherwise a document with n such links and n hyperlink relationships
takes O(n^2) to extract
+ */
+ @Test
+ void testUnknownHyperlinkIds() throws Exception {
+ final int count = 10_000;
+ try (XWPFDocument doc = new XWPFDocument()) {
+ for (int i = 0; i < count; i++) {
+ doc.getPackagePart().addRelationship(new
URI("https://poi.apache.org/" + i),
+ TargetMode.EXTERNAL, XWPFRelation.HYPERLINK.getRelation(),
"rIdH" + i);
+ }
+ doc.getPackagePart().addRelationship(new
URI("https://poi.apache.org/"),
+ TargetMode.EXTERNAL, "http://example.com/other", "rIdOther");
+ XWPFParagraph p = doc.createParagraph();
+ for (int i = 0; i < count; i++) {
+ CTHyperlink link = p.getCTP().addNewHyperlink();
+ link.setId("x" + i);
+ link.addNewR().addNewT().setStringValue("a");
+ }
+ CTHyperlink known = p.getCTP().addNewHyperlink();
+ known.setId("rIdH42");
+ known.addNewR().addNewT().setStringValue("b");
+
+ try (XWPFDocument doc2 =
XWPFTestDataSamples.writeOutAndReadBack(doc)) {
+ assertTimeout(Duration.ofSeconds(20), () -> {
Review Comment:
Removed the timeout. The test now checks the behaviour directly: after the
extraction and the unknown-id lookups, `doc.hyperlinks` must still be the same
list instance that was loaded. On trunk the first miss rebuilds and replaces
that list, so the assertion fails straight away, without depending on timing.
--
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]