Skip to content

Commit

Permalink
Improve performance of OPCPackage#getPartsByRelationshipType
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
MariusVolkhart committed Nov 30, 2020
1 parent 17592b0 commit d7a8f16
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,15 @@ public ArrayList<PackagePart> getPartsByRelationshipType(
}
ArrayList<PackagePart> retArr = new ArrayList<>();
for (PackageRelationship rel : getRelationshipsByType(relationshipType)) {
PackagePart part = getPart(rel);
PackagePart part = null;
try {
part = getPart(PackagingURIHelper.createPartName(rel.getTargetURI()));
} catch (InvalidFormatException ignored) {
// The relationship already exists, so the part should have a valid URI.
}

if (part != null) {
retArr.add(part);
retArr.add(part);
}
}
Collections.sort(retArr);
Expand Down

0 comments on commit d7a8f16

Please sign in to comment.