naruto-lgtm commented on code in PR #1570:
URL: https://github.com/apache/poi/pull/1570#discussion_r4662159727
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java:
##########
@@ -319,11 +320,14 @@ public static List<PackagePart>
getSheetDrawingParts(PackagePart sheetPart, bool
throws InvalidFormatException {
PackageRelationshipCollection drawingsList =
getRelationshipsByType(sheetPart, XSSFRelation.DRAWINGS,
allowStrictOoxmlFiles);
- int drawingsSize = drawingsList.size();
- List<PackagePart> drawingParts = new ArrayList<>(drawingsSize);
- for (int i = 0; i < drawingsSize; i++) {
- PackageRelationship drawings = drawingsList.getRelationship(i);
+ List<PackagePart> drawingParts = new ArrayList<>();
+ Set<PackagePartName> seen = new HashSet<>();
+ for (PackageRelationship drawings : drawingsList) {
PackagePartName drawingsName =
PackagingURIHelper.createPartName(drawings.getTargetURI());
+ if (!seen.add(drawingsName)) {
Review Comment:
Went with the cache: SheetIterator now keeps the parsed shapes by drawing
part name, so duplicate <sheet> entries resolving to one worksheet parse its
drawing once, and so do repeated getShapes calls on a sheet. I left sheetRefs
alone since getSheetRefs is public and the usermodel also keeps one XSSFSheet
per <sheet> entry. The sheet XML itself is still read per entry, but that's
linear in the sheet, not multiplied by the drawing.
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java:
##########
@@ -319,11 +320,14 @@ public static List<PackagePart>
getSheetDrawingParts(PackagePart sheetPart, bool
throws InvalidFormatException {
PackageRelationshipCollection drawingsList =
getRelationshipsByType(sheetPart, XSSFRelation.DRAWINGS,
allowStrictOoxmlFiles);
- int drawingsSize = drawingsList.size();
- List<PackagePart> drawingParts = new ArrayList<>(drawingsSize);
- for (int i = 0; i < drawingsSize; i++) {
- PackageRelationship drawings = drawingsList.getRelationship(i);
+ List<PackagePart> drawingParts = new ArrayList<>();
+ Set<PackagePartName> seen = new HashSet<>();
+ for (PackageRelationship drawings : drawingsList) {
PackagePartName drawingsName =
PackagingURIHelper.createPartName(drawings.getTargetURI());
Review Comment:
Done. External targets are skipped up front and createPartName is caught per
relationship, both logged at warn like the missing-part case, so the valid
drawings still come back.
##########
poi-ooxml/src/main/java/org/apache/poi/xssf/eventusermodel/XSSFReader.java:
##########
@@ -319,11 +320,14 @@ public static List<PackagePart>
getSheetDrawingParts(PackagePart sheetPart, bool
throws InvalidFormatException {
PackageRelationshipCollection drawingsList =
getRelationshipsByType(sheetPart, XSSFRelation.DRAWINGS,
allowStrictOoxmlFiles);
- int drawingsSize = drawingsList.size();
- List<PackagePart> drawingParts = new ArrayList<>(drawingsSize);
- for (int i = 0; i < drawingsSize; i++) {
- PackageRelationship drawings = drawingsList.getRelationship(i);
+ List<PackagePart> drawingParts = new ArrayList<>();
+ Set<PackagePartName> seen = new HashSet<>();
+ for (PackageRelationship drawings : drawingsList) {
Review Comment:
Agreed that would bound it properly, but I'd keep it out of this PR: the
helper only has the sheet PackagePart, and the sheet XML is streamed by the
caller's handler, so reading the <drawing> r:id means a second parse of the
sheet or an API change. What's left per relationship is one URI resolve plus
the part-name check, linear in the .rels and the same order as loading the
.rels at all. Can do the <drawing> lookup as a follow-up if you want it.
##########
poi-ooxml/src/test/java/org/apache/poi/xssf/eventusermodel/TestXSSFReader.java:
##########
@@ -461,6 +461,28 @@ void testGetSheetDrawingParts() throws Exception {
}
}
+ @Test
+ void testDrawingTargetedByDuplicateRelationships() throws Exception {
+ try (OPCPackage pkg =
XSSFTestDataSamples.openSamplePackage("WithTextBox.xlsx")) {
Review Comment:
Added testSheetsSharingOneDrawing (two extra <sheet> entries on rId1,
asserts the same shape instances come back for all three sheets and for a
repeated call) and testDrawingRelationshipsWithUnusableTargets (an external
target plus two targets that aren't part names next to the real drawing). Both
fail on the previous commit.
--
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]