From f1ebcc58b2213cb49d2123543201c05c169b19bb Mon Sep 17 00:00:00 2001 From: Marius Volkhart Date: Mon, 30 Nov 2020 21:07:35 +0100 Subject: [PATCH] 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. --- .../java/org/apache/poi/openxml4j/opc/OPCPackage.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java index 292e016d1a9..c15ab456a7e 100644 --- a/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java +++ b/src/ooxml/java/org/apache/poi/openxml4j/opc/OPCPackage.java @@ -659,9 +659,15 @@ public ArrayList getPartsByRelationshipType( } ArrayList 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);