Skip to content

Commit d7a8f16

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 17592b0 commit d7a8f16

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
@@ -658,9 +658,15 @@ public ArrayList<PackagePart> getPartsByRelationshipType(
658658
}
659659
ArrayList<PackagePart> retArr = new ArrayList<>();
660660
for (PackageRelationship rel : getRelationshipsByType(relationshipType)) {
661-
PackagePart part = getPart(rel);
661+
PackagePart part = null;
662+
try {
663+
part = getPart(PackagingURIHelper.createPartName(rel.getTargetURI()));
664+
} catch (InvalidFormatException ignored) {
665+
// The relationship already exists, so the part should have a valid URI.
666+
}
667+
662668
if (part != null) {
663-
retArr.add(part);
669+
retArr.add(part);
664670
}
665671
}
666672
Collections.sort(retArr);

0 commit comments

Comments
 (0)