@@ -1120,6 +1120,10 @@ private boolean compareDictionariesExtended(PdfDictionary outDict, PdfDictionary
1120
1120
Set <PdfName > mergedKeys = new TreeSet <>(cmpDict .keySet ());
1121
1121
mergedKeys .addAll (outDict .keySet ());
1122
1122
for (PdfName key : mergedKeys ) {
1123
+ if (!dictsAreSame && (currentPath == null || compareResult == null || compareResult .isMessageLimitReached ())) {
1124
+ return false ;
1125
+ }
1126
+
1123
1127
if (excludedKeys != null && excludedKeys .contains (key )) {
1124
1128
continue ;
1125
1129
}
@@ -1148,6 +1152,9 @@ private boolean compareDictionariesExtended(PdfDictionary outDict, PdfDictionary
1148
1152
// A number tree can be stored in multiple, semantically equivalent ways.
1149
1153
// Flatten to a single array, in order to get a canonical representation.
1150
1154
if (key .equals (PdfName .ParentTree ) || key .equals (PdfName .PageLabels )) {
1155
+ if (currentPath != null ) {
1156
+ currentPath .pushDictItemToPath (key );
1157
+ }
1151
1158
PdfDictionary outNumTree = outDict .getAsDictionary (key );
1152
1159
PdfDictionary cmpNumTree = cmpDict .getAsDictionary (key );
1153
1160
LinkedList <PdfObject > outItems = new LinkedList <PdfObject >();
@@ -1156,35 +1163,50 @@ private boolean compareDictionariesExtended(PdfDictionary outDict, PdfDictionary
1156
1163
PdfNumber cmpLeftover = flattenNumTree (cmpNumTree , null , cmpItems );
1157
1164
if (outLeftover != null ) {
1158
1165
LoggerFactory .getLogger (CompareTool .class ).warn (LogMessageConstant .NUM_TREE_SHALL_NOT_END_WITH_KEY );
1159
- if (compareResult != null && cmpLeftover == null ) {
1160
- compareResult .addError (currentPath , "Number tree unexpectedly ends with a key" );
1166
+ if (cmpLeftover == null ) {
1167
+ if (compareResult != null && currentPath != null ) {
1168
+ compareResult .addError (currentPath , "Number tree unexpectedly ends with a key" );
1169
+ }
1170
+ dictsAreSame = false ;
1161
1171
}
1162
1172
}
1163
1173
if (cmpLeftover != null ) {
1164
1174
LoggerFactory .getLogger (CompareTool .class ).warn (LogMessageConstant .NUM_TREE_SHALL_NOT_END_WITH_KEY );
1165
- if (compareResult != null && outLeftover == null ) {
1166
- compareResult .addError (currentPath , "Number tree was expected to end with a key (although it is invalid according to the specification), but ended with a value" );
1175
+ if (outLeftover == null ) {
1176
+ if (compareResult != null && currentPath != null ) {
1177
+ compareResult .addError (currentPath , "Number tree was expected to end with a key (although it is invalid according to the specification), but ended with a value" );
1178
+ }
1179
+ dictsAreSame = false ;
1167
1180
}
1168
1181
}
1169
1182
if (outLeftover != null && cmpLeftover != null && !compareNumbers (outLeftover , cmpLeftover )) {
1170
- if (compareResult != null ) {
1183
+ if (compareResult != null && currentPath != null ) {
1171
1184
compareResult .addError (currentPath , "Number tree was expected to end with a different key (although it is invalid according to the specification)" );
1172
1185
}
1173
- return false ;
1186
+ dictsAreSame = false ;
1174
1187
}
1175
1188
PdfArray outArray = new PdfArray (outItems , outItems .size ());
1176
1189
PdfArray cmpArray = new PdfArray (cmpItems , cmpItems .size ());
1177
- if (!compareArraysExtended (outArray , cmpArray , currentPath , compareResult ))
1178
- return false ;
1190
+ if (!compareArraysExtended (outArray , cmpArray , currentPath , compareResult )) {
1191
+ if (compareResult != null && currentPath != null ) {
1192
+ compareResult .addError (currentPath , "Number trees were flattened, compared and found to be different." );
1193
+ }
1194
+ dictsAreSame = false ;
1195
+ }
1196
+
1197
+ if (currentPath != null ) {
1198
+ currentPath .pop ();
1199
+ }
1179
1200
continue ;
1180
1201
}
1181
- if (currentPath != null )
1202
+
1203
+ if (currentPath != null ) {
1182
1204
currentPath .pushDictItemToPath (key );
1205
+ }
1183
1206
dictsAreSame = compareObjects (outDict .get (key , false ), cmpDict .get (key , false ), currentPath , compareResult ) && dictsAreSame ;
1184
- if (currentPath != null )
1207
+ if (currentPath != null ) {
1185
1208
currentPath .pop ();
1186
- if (!dictsAreSame && (currentPath == null || compareResult == null || compareResult .isMessageLimitReached ()))
1187
- return false ;
1209
+ }
1188
1210
}
1189
1211
return dictsAreSame ;
1190
1212
}
0 commit comments