Debian Codegraph @

src/com/google/javascript/jscomp/jsonml/JsonMLUtil.java

raw symbols SCIP
1 /*
2 * Copyright 2010 The Closure Compiler Authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
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 * JsonMLUtil contains utilities for the JsonML object.
26 *
27 * @author [email protected] (Daniel Hans)
28 */
29 public class JsonMLUtil {
30
31 /**
32 * Checks if the specified JsonML element represents an expression.
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 * Parses JSON string which contains serialized JsonML content.
65 * @param jsonml string representation of JsonML
66 * @return root element of a JsonML tree
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 // set attributes for the JsonML element
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 // an unsupported attribute
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 // we do not want to deal with JSONObject.NULL
105 if (value != null && value.equals(null)) {
106 value = null;
107 }
108
109 // we want all numbers to be stored as double values
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 // recursively set children for the JsonML element
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 * Compares two specified JsonML trees.
130 *
131 * Two JsonML nodes are considered to be equal when the following conditions
132 * are met:
133 *
134 * - have the same type
135 * - have the same attributes from the list of attributes to compare
136 * - have the same number of children
137 * - nodes in each pair of corresponding children are equal
138 *
139 * Two JsonML trees are equal, if their roots are equal.
140 *
141 * When two nodes are compared, only the following attributes are taken
142 * into account:
143 * TagAttr.BODY, TagAttr.FLAGS, TagAttr.IS_PREFIX, TagAttr.LABEL,
144 * TagAttr.NAME, TagAttr.OP, TagAttr.TYPE, TagAttr.VALUE
145 * Generally, the comparator does not care about debugging attributes.
146 *
147 * @return
148 * Returns string describing the inequality in the following format:
149 *
150 * The trees are not equal:
151 *
152 * Tree1:
153 * -- string representation of Tree1
154 *
155 * Tree2:
156 * -- string representation of Tree2
157 *
158 * Subtree1:
159 * -- string representation of the subtree of the Tree1 which is not
160 * -- equal to the corresponding subtree of the Tree2
161 *
162 * Subtree2:
163 * -- see Subtree1
164 *
165 * If the trees are equal, null is returned.
166 */
167 public static String compare(JsonML tree1, JsonML tree2) {
168 return (new JsonMLComparator(tree1, tree2)).compare();
169 }
170
171 /**
172 * Returns true if the trees are equal, false otherwise.
173 */
174 static boolean compareSilent(JsonML tree1, JsonML tree2) {
175 return (new JsonMLComparator(tree1, tree2)).compareSilent();
176 }
177
178 /**
179 * Helper class which actually compares two given JsonML trees.
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 * Check if two elements are equal (including comparing their children).
209 */
210 private boolean compareElements(JsonML a, JsonML b) {
211 // the elements are considered to be equal if they are both null
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 // the elements themselves have to be equivalent
221 if (!areEquivalent(a, b)) {
222 return setMismatch(a, b);
223 }
224
225 // they both have to have the same number of children
226 if (a.childrenSize() != b.childrenSize()) {
227 return setMismatch(a, b);
228 }
229
230 // all the children has to be the same
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 * Checks if two elements are semantically the same.
244 */
245 private boolean areEquivalent(JsonML a, JsonML b) {
246 // both elements must have the same type
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 // none of the elements have the attribute
264 if (valueA == null && valueB == null) {
265 return true;
266 }
267
268 // only one of the elements has the attribute
269 if (valueA == null || valueB == null) {
270 return false;
271 }
272
273 // check if corresponding values are equal
274 if (!(valueA.equals(valueB))) {
275 // there is still a chance that both attributes are numbers, but are
276 // represented by different classes
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 }