| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp.jsonml; |
| 18 |
|
| 19 |
import org.json.JSONArray; |
| 20 |
import org.json.JSONObject; |
| 21 |
|
| 22 |
import java.util.Iterator; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public class JsonMLUtil { |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public static boolean isExpression(JsonML element) { |
| 35 |
switch (element.getType()) { |
| 36 |
case ArrayExpr: |
| 37 |
case AssignExpr: |
| 38 |
case BinaryExpr: |
| 39 |
case CallExpr: |
| 40 |
case ConditionalExpr: |
| 41 |
case CountExpr: |
| 42 |
case DeleteExpr: |
| 43 |
case EvalExpr: |
| 44 |
case FunctionExpr: |
| 45 |
case IdExpr: |
| 46 |
case InvokeExpr: |
| 47 |
case LiteralExpr: |
| 48 |
case LogicalAndExpr: |
| 49 |
case LogicalOrExpr: |
| 50 |
case MemberExpr: |
| 51 |
case NewExpr: |
| 52 |
case ObjectExpr: |
| 53 |
case RegExpExpr: |
| 54 |
case ThisExpr: |
| 55 |
case TypeofExpr: |
| 56 |
case UnaryExpr: |
| 57 |
return true; |
| 58 |
default: |
| 59 |
return false; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public static JsonML parseString(String jsonml) throws Exception { |
| 69 |
return parseElement(new JSONArray(jsonml)); |
| 70 |
} |
| 71 |
|
| 72 |
private static JsonML parseElement(JSONArray element) |
| 73 |
throws Exception { |
| 74 |
JsonML jsonMLElement = new JsonML(TagType.valueOf(element.getString(0))); |
| 75 |
|
| 76 |
|
| 77 |
JSONObject attrs = element.getJSONObject(1); |
| 78 |
Iterator<?> it = attrs.keys(); |
| 79 |
while (it.hasNext()) { |
| 80 |
String key = (String) it.next(); |
| 81 |
Object value = attrs.get(key); |
| 82 |
TagAttr tag = TagAttr.get(key); |
| 83 |
|
| 84 |
|
| 85 |
if (tag == null) { |
| 86 |
continue; |
| 87 |
} |
| 88 |
|
| 89 |
if (value instanceof Number) { |
| 90 |
value = ((Number) value).doubleValue(); |
| 91 |
} |
| 92 |
|
| 93 |
switch (tag) { |
| 94 |
case NAME: |
| 95 |
case BODY: |
| 96 |
case FLAGS: |
| 97 |
case OP: |
| 98 |
case TYPE: |
| 99 |
case IS_PREFIX: |
| 100 |
case LABEL: |
| 101 |
jsonMLElement.setAttribute(tag, value); |
| 102 |
break; |
| 103 |
case VALUE: |
| 104 |
|
| 105 |
if (value != null && value.equals(null)) { |
| 106 |
value = null; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
if (value instanceof Number) { |
| 111 |
jsonMLElement.setAttribute(tag, ((Number) value).doubleValue()); |
| 112 |
} else { |
| 113 |
jsonMLElement.setAttribute(tag, value); |
| 114 |
} |
| 115 |
break; |
| 116 |
default: |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
for (int i = 2; i < element.length(); ++i) { |
| 122 |
jsonMLElement.appendChild(parseElement(element.getJSONArray(i))); |
| 123 |
} |
| 124 |
|
| 125 |
return jsonMLElement; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public static String compare(JsonML tree1, JsonML tree2) { |
| 168 |
return (new JsonMLComparator(tree1, tree2)).compare(); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static boolean compareSilent(JsonML tree1, JsonML tree2) { |
| 175 |
return (new JsonMLComparator(tree1, tree2)).compareSilent(); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
private static class JsonMLComparator { |
| 183 |
private static final TagAttr[] ATTRS_TO_COMPARE = { |
| 184 |
TagAttr.BODY, TagAttr.FLAGS, TagAttr.IS_PREFIX, TagAttr.LABEL, |
| 185 |
TagAttr.NAME, TagAttr.OP, TagAttr.TYPE, TagAttr.VALUE |
| 186 |
}; |
| 187 |
private JsonML treeA; |
| 188 |
private JsonML treeB; |
| 189 |
private JsonML mismatchA; |
| 190 |
private JsonML mismatchB; |
| 191 |
|
| 192 |
JsonMLComparator(JsonML treeA, JsonML treeB) { |
| 193 |
this.treeA = treeA; |
| 194 |
this.treeB = treeB; |
| 195 |
if (compareElements(treeA, treeB)) { |
| 196 |
mismatchA = null; |
| 197 |
mismatchB = null; |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
private boolean setMismatch(JsonML a, JsonML b) { |
| 202 |
mismatchA = a; |
| 203 |
mismatchB = b; |
| 204 |
return false; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private boolean compareElements(JsonML a, JsonML b) { |
| 211 |
|
| 212 |
if (a == null || b == null) { |
| 213 |
if (a == null && b == null) { |
| 214 |
return true; |
| 215 |
} else { |
| 216 |
return setMismatch(a, b); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
if (!areEquivalent(a, b)) { |
| 222 |
return setMismatch(a, b); |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
if (a.childrenSize() != b.childrenSize()) { |
| 227 |
return setMismatch(a, b); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
Iterator<JsonML> itA = a.getChildren().listIterator(); |
| 232 |
Iterator<JsonML> itB = b.getChildren().listIterator(); |
| 233 |
while (itA.hasNext()) { |
| 234 |
if (!compareElements(itA.next(), itB.next())) { |
| 235 |
return false; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
return true; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
private boolean areEquivalent(JsonML a, JsonML b) { |
| 246 |
|
| 247 |
if (a.getType() != b.getType()) { |
| 248 |
return false; |
| 249 |
} |
| 250 |
|
| 251 |
for (TagAttr attr : ATTRS_TO_COMPARE) { |
| 252 |
if (!compareAttribute(attr, a, b)) { |
| 253 |
return false; |
| 254 |
} |
| 255 |
} |
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
private boolean compareAttribute(TagAttr attr, JsonML a, JsonML b) { |
| 260 |
Object valueA = a.getAttributes().get(attr); |
| 261 |
Object valueB = b.getAttributes().get(attr); |
| 262 |
|
| 263 |
|
| 264 |
if (valueA == null && valueB == null) { |
| 265 |
return true; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
if (valueA == null || valueB == null) { |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
if (!(valueA.equals(valueB))) { |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
Double doubleA = null, doubleB = null; |
| 279 |
|
| 280 |
if (valueA instanceof Number) { |
| 281 |
doubleA = ((Number) valueA).doubleValue(); |
| 282 |
} else if (valueA instanceof String) { |
| 283 |
doubleA = Double.valueOf((String) valueA); |
| 284 |
} else { |
| 285 |
return false; |
| 286 |
} |
| 287 |
|
| 288 |
if (valueB instanceof Number) { |
| 289 |
doubleB = ((Number) valueB).doubleValue(); |
| 290 |
} else if (valueB instanceof String) { |
| 291 |
doubleB = Double.valueOf((String) valueB); |
| 292 |
} else { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
if (!doubleA.equals(doubleB)) { |
| 297 |
return false; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
return true; |
| 302 |
} |
| 303 |
|
| 304 |
private boolean compareSilent() { |
| 305 |
return mismatchA == null && mismatchB == null; |
| 306 |
} |
| 307 |
|
| 308 |
private String compare() { |
| 309 |
if (compareSilent()) { |
| 310 |
return null; |
| 311 |
} |
| 312 |
return "The trees are not equal: " + |
| 313 |
"\n\nTree1:\n " + treeA.toStringTree() + |
| 314 |
"\n\nTree2:\n " + treeB.toStringTree() + |
| 315 |
"\n\nSubtree1:\n " + mismatchA.toStringTree() + |
| 316 |
"\n\nSubtree2:\n " + mismatchB.toStringTree(); |
| 317 |
|
| 318 |
} |
| 319 |
} |
| 320 |
} |