Skip to content

Commit

Permalink
Fix array element replacements, simplify number comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
tresf committed May 1, 2024
1 parent 9ed33ff commit 98d72a0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/qz/utils/ByteUtilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,14 @@ public static int[] unwind(int bitwiseCode) {

public static boolean numberEquals(Object val1, Object val2) {
try {
if(val1 == null) {
return val2 == null;
} else if(val1.getClass().getName() == val2.getClass().getName()) {
if(val1 == null || val2 == null) {
return val1 == val2;
} else if(val1.getClass() == val2.getClass()) {
return val1.equals(val2);
} else if(val1 instanceof Long) {
return val1.equals(Long.parseLong(val2.toString()));
} else if(val2 instanceof Long) {
return val2.equals(Long.parseLong(val1.toString()));
} else {
return Double.parseDouble(val1.toString()) == Double.parseDouble(val2.toString());
}
Expand Down
16 changes: 7 additions & 9 deletions src/qz/ws/substitutions/Substitutions.java
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ private static boolean find(Object base, Object match, boolean caseSensitive, bo
return false; // mismatched types
}
} else if (base instanceof JSONArray) {
boolean found = false;
if(match instanceof JSONArray) {
JSONArray matchArray = (JSONArray)match;
JSONArray baseArray = (JSONArray)base;
Expand All @@ -267,23 +268,20 @@ private static boolean find(Object base, Object match, boolean caseSensitive, bo
for(int j = 0; j < baseArray.length(); j++) {
Object newBase = baseArray.get(j);
if(find(newBase, newMatch, caseSensitive, replace)) {
continue match;
found = true;
if(!replace) {
continue match;
}
}
}
return false;
}
return true; // assume found
} else {
return false;
}
return found;
} else {
// Treat as primitives
if(match instanceof Number) {
if(match instanceof Number || base instanceof Number) {
return ByteUtilities.numberEquals(match, base);
}
if(base instanceof Number) {
return ByteUtilities.numberEquals(base, match);
}
// Fallback: Cast both to String
if(caseSensitive) {
return match.toString().equals(base.toString());
Expand Down
14 changes: 12 additions & 2 deletions test/qz/ws/substitutions/resources/printRequest.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,17 @@
"ignoreTransparency": false,
"altFontRendering": false
}
}
},
{
"type": "pixel",
"format": "image",
"flavor": "file",
"data": "https://demo.qz.io/assets/img/image_sample.png",
},
"^XA\n",
"^FO50,50^ADN,36,20^FDPRINTED WITH QZ 2.2.4-SNAPSHOT\n",
"^FS\n",
"^XZ\n"
]
},
"signature": "",
Expand All @@ -54,4 +64,4 @@
"y": 462.5
},
"signAlgorithm": "SHA512"
}"
}
20 changes: 20 additions & 0 deletions test/qz/ws/substitutions/resources/substitutions.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,5 +85,25 @@
"data": "https://demo.qz.io/assets/pdf_sample.pdf"
}
}
},
{
"use": {
"printer": "ZDesigner"
},
"for": {
"data": [ "^XA\n" ]
}
},
{
"use": {
"data": {
"type": "PIXEL"
}
},
"for": {
"data": {
"type": "pixel"
}
}
}
]

0 comments on commit 98d72a0

Please sign in to comment.