Skip to content

Commit f1ebcc5

Browse files
Improve performance of OPCPackage#getPartsByRelationshipType
The previous version of this function would first retrieve a filtered set of all the relationships, and then for each of those, iterate through all of the relationships again before finding the part. Since the relationships returned by getRelationshipsByType() are already trusted, we can retrieve the part directly by formulating the part name from the relationship.
1 parent dc2fa9b commit f1ebcc5

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -659,9 +659,15 @@ public ArrayList<PackagePart> getPartsByRelationshipType(
659659
}
660660
ArrayList<PackagePart> retArr = new ArrayList<>();
661661
for (PackageRelationship rel : getRelationshipsByType(relationshipType)) {
662-
PackagePart part = getPart(rel);
662+
PackagePart part = null;
663+
try {
664+
part = getPart(PackagingURIHelper.createPartName(rel.getTargetURI()));
665+
} catch (InvalidFormatException ignored) {
666+
// The relationship already exists, so the part should have a valid URI.
667+
}
668+
663669
if (part != null) {
664-
retArr.add(part);
670+
retArr.add(part);
665671
}
666672
}
667673
Collections.sort(retArr);

0 commit comments

Comments
 (0)