Skip to content

Commit 124982c

Browse files
committed
PDFBOX-6097: avoid misunderstandings, simplify
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1929753 13f79535-47bb-0310-9956-ffa450edef68
1 parent e52911b commit 124982c

File tree

2 files changed

+25
-30
lines changed

2 files changed

+25
-30
lines changed

pdfbox/src/main/java/org/apache/pdfbox/cos/COSArray.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -794,25 +794,21 @@ public void getIndirectObjectKeys(Collection<COSObjectKey> indirectObjects)
794794
{
795795
return;
796796
}
797-
else
798-
{
799-
indirectObjects.add(key);
800-
}
797+
indirectObjects.add(key);
801798
}
802-
803799
for (COSBase cosBase : objects)
804800
{
805801
if (cosBase == null)
806802
{
807803
continue;
808804
}
809-
COSObjectKey cosBaseKey = cosBase.getKey();
810-
if (cosBaseKey != null && indirectObjects.contains(cosBaseKey))
811-
{
812-
continue;
813-
}
814-
if (cosBase instanceof COSObject)
805+
COSObjectKey indirectObjectKey = cosBase instanceof COSObject ? cosBase.getKey() : null;
806+
if (indirectObjectKey != null)
815807
{
808+
if (indirectObjects.contains(indirectObjectKey))
809+
{
810+
continue;
811+
}
816812
// dereference object
817813
cosBase = ((COSObject) cosBase).getObject();
818814
}
@@ -826,10 +822,10 @@ else if (cosBase instanceof COSArray)
826822
// descend to included array to collect all included indirect objects
827823
((COSArray) cosBase).getIndirectObjectKeys(indirectObjects);
828824
}
829-
else if (cosBaseKey != null)
825+
else if (indirectObjectKey != null)
830826
{
831827
// add key for all indirect objects other than COSDictionary/COSArray
832-
indirectObjects.add(cosBaseKey);
828+
indirectObjects.add(indirectObjectKey);
833829
}
834830
}
835831
}

pdfbox/src/main/java/org/apache/pdfbox/cos/COSDictionary.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ public class COSDictionary extends COSBase implements COSUpdateInfo
5151

5252
private static final String PATH_SEPARATOR = "/";
5353

54-
private static final List<COSName> PARENT_KEYS = Arrays.asList(COSName.PARENT, COSName.P);
55-
5654
/**
5755
* The name-value pairs of this dictionary. The pairs are kept in the order they were added to the dictionary.
5856
*/
@@ -1449,40 +1447,41 @@ public void getIndirectObjectKeys(Collection<COSObjectKey> indirectObjects)
14491447
{
14501448
return;
14511449
}
1452-
else
1453-
{
1454-
indirectObjects.add(key);
1455-
}
1450+
indirectObjects.add(key);
14561451
}
14571452
for (Entry<COSName, COSBase> entry : items.entrySet())
14581453
{
14591454
COSBase cosBase = entry.getValue();
1460-
COSObjectKey cosBaseKey = cosBase != null ? cosBase.getKey() : null;
1461-
// avoid endless recursions
1462-
if (PARENT_KEYS.contains(entry.getKey())
1463-
|| (cosBaseKey != null && indirectObjects.contains(cosBaseKey)))
1464-
{
1465-
continue;
1466-
}
1467-
if (cosBase instanceof COSObject)
1455+
COSObjectKey indirectObjectKey = cosBase instanceof COSObject ? cosBase.getKey() : null;
1456+
if (indirectObjectKey != null)
14681457
{
1458+
// avoid endless recursions
1459+
if (indirectObjects.contains(indirectObjectKey))
1460+
{
1461+
continue;
1462+
}
14691463
// dereference object
14701464
cosBase = ((COSObject) cosBase).getObject();
14711465
}
14721466
if (cosBase instanceof COSDictionary)
14731467
{
1468+
COSName entryKey = entry.getKey();
14741469
// descend to included dictionary to collect all included indirect objects
1475-
((COSDictionary) cosBase).getIndirectObjectKeys(indirectObjects);
1470+
// skip PARENT and P references to avoid recursions
1471+
if (!COSName.PARENT.equals(entryKey) && !COSName.P.equals(entryKey))
1472+
{
1473+
((COSDictionary) cosBase).getIndirectObjectKeys(indirectObjects);
1474+
}
14761475
}
14771476
else if (cosBase instanceof COSArray)
14781477
{
14791478
// descend to included array to collect all included indirect objects
14801479
((COSArray) cosBase).getIndirectObjectKeys(indirectObjects);
14811480
}
1482-
else if (cosBaseKey != null)
1481+
else if (indirectObjectKey != null)
14831482
{
14841483
// add key for all indirect objects other than COSDictionary/COSArray
1485-
indirectObjects.add(cosBaseKey);
1484+
indirectObjects.add(indirectObjectKey);
14861485
}
14871486
}
14881487
}

0 commit comments

Comments
 (0)