Skip to content

Commit 12fe56e

Browse files
Make reports of num trees comparison in CompareTool more user-friendly
ITXT-CR-260
1 parent 4d4a4ba commit 12fe56e

File tree

1 file changed

+34
-12
lines changed

1 file changed

+34
-12
lines changed

kernel/src/main/java/com/itextpdf/kernel/utils/CompareTool.java

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,10 @@ private boolean compareDictionariesExtended(PdfDictionary outDict, PdfDictionary
11201120
Set<PdfName> mergedKeys = new TreeSet<>(cmpDict.keySet());
11211121
mergedKeys.addAll(outDict.keySet());
11221122
for (PdfName key : mergedKeys) {
1123+
if (!dictsAreSame && (currentPath == null || compareResult == null || compareResult.isMessageLimitReached())) {
1124+
return false;
1125+
}
1126+
11231127
if (excludedKeys != null && excludedKeys.contains(key)) {
11241128
continue;
11251129
}
@@ -1148,6 +1152,9 @@ private boolean compareDictionariesExtended(PdfDictionary outDict, PdfDictionary
11481152
// A number tree can be stored in multiple, semantically equivalent ways.
11491153
// Flatten to a single array, in order to get a canonical representation.
11501154
if (key.equals(PdfName.ParentTree) || key.equals(PdfName.PageLabels)) {
1155+
if (currentPath != null) {
1156+
currentPath.pushDictItemToPath(key);
1157+
}
11511158
PdfDictionary outNumTree = outDict.getAsDictionary(key);
11521159
PdfDictionary cmpNumTree = cmpDict.getAsDictionary(key);
11531160
LinkedList<PdfObject> outItems = new LinkedList<PdfObject>();
@@ -1156,35 +1163,50 @@ private boolean compareDictionariesExtended(PdfDictionary outDict, PdfDictionary
11561163
PdfNumber cmpLeftover = flattenNumTree(cmpNumTree, null, cmpItems);
11571164
if (outLeftover != null) {
11581165
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;
11611171
}
11621172
}
11631173
if (cmpLeftover != null) {
11641174
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;
11671180
}
11681181
}
11691182
if (outLeftover != null && cmpLeftover != null && !compareNumbers(outLeftover, cmpLeftover)) {
1170-
if (compareResult != null) {
1183+
if (compareResult != null && currentPath != null) {
11711184
compareResult.addError(currentPath, "Number tree was expected to end with a different key (although it is invalid according to the specification)");
11721185
}
1173-
return false;
1186+
dictsAreSame = false;
11741187
}
11751188
PdfArray outArray = new PdfArray(outItems, outItems.size());
11761189
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+
}
11791200
continue;
11801201
}
1181-
if (currentPath != null)
1202+
1203+
if (currentPath != null) {
11821204
currentPath.pushDictItemToPath(key);
1205+
}
11831206
dictsAreSame = compareObjects(outDict.get(key, false), cmpDict.get(key, false), currentPath, compareResult) && dictsAreSame;
1184-
if (currentPath != null)
1207+
if (currentPath != null) {
11851208
currentPath.pop();
1186-
if (!dictsAreSame && (currentPath == null || compareResult == null || compareResult.isMessageLimitReached()))
1187-
return false;
1209+
}
11881210
}
11891211
return dictsAreSame;
11901212
}

0 commit comments

Comments
 (0)