Debian Codegraph @

src/com/google/javascript/jscomp/jsonml/JsonML.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 java.io.IOException;
20
21 import java.util.ArrayList;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.EnumMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Map.Entry;
29
30 /**
31 * Class which represents JsonML element according to the specification at
32 * "http://code.google.com/p/es-lab/wiki/JsonMLASTFormat"
33 *
34 * @author [email protected] (Daniel Hans)
35 */
36 public class JsonML {
37 private final TagType type;
38 private Map<TagAttr, Object> attributes =
39 new EnumMap<TagAttr, Object>(TagAttr.class);
40 private List<JsonML> children = new ArrayList<JsonML>();
41
42 /**
43 * Creates a new element with a given type.
44 * @param type
45 */
46 public JsonML(TagType type) {
47 this.type = type;
48 }
49
50 /**
51 * Creates a new element.
52 * @param type type of the element
53 * @param children children to append to the element
54 */
55 public JsonML(TagType type, JsonML... children) {
56 this(type, Arrays.asList(children));
57 }
58
59 public JsonML(TagType type, List<? extends JsonML> children) {
60 this(type, Collections.<TagAttr, Object>emptyMap(), children);
61 }
62
63 public JsonML(TagType type, Map<? extends TagAttr, ?> attributes) {
64 this(type, attributes, Collections.<JsonML>emptyList());
65 }
66
67 public JsonML(TagType type, Map<? extends TagAttr, ?> attributes,
68 List<? extends JsonML> children) {
69 this.type = type;
70 this.attributes.putAll(attributes);
71 appendChildren(children);
72 }
73
74 /**
75 * Inserts the given JsonML element at the given position in the
76 * list of children.
77 * @param index index at which the given element is to be inserted
78 * @param element JsonML element to be inserted
79 */
80 public void addChild(int index, JsonML element) {
81 children.add(index, element);
82 }
83
84 /**
85 * Appends a given child element to the list of children.
86 * @param element JsonML element to append
87 */
88 public void appendChild(JsonML element) {
89 children.add(element);
90 }
91
92 /**
93 * Appends a collection of children to the back of the list of children.
94 * @param elements collection of JsonML elements to append
95 */
96 public void appendChildren(Collection<? extends JsonML> elements) {
97 children.addAll(elements);
98 }
99
100 /**
101 * Returns number of the children.
102 */
103 public int childrenSize() {
104 return children.size();
105 }
106
107 /**
108 * Removes all elements from the list of children.
109 */
110 public void clearChildren() {
111 setChildren();
112 }
113
114 /**
115 * Returns value associated with a given attribute.
116 * @param name name of the attribute
117 * @return associated value or null if the attribute is not present
118 */
119 public Object getAttribute(TagAttr name) {
120 return attributes.get(name);
121 }
122
123 /**
124 * Returns a map with attributes and respective values.
125 */
126 public Map<TagAttr, Object> getAttributes() {
127 return attributes;
128 }
129
130 /**
131 * Returns child at a given position.
132 */
133 public JsonML getChild(int index) {
134 return children.get(index);
135 }
136
137 /**
138 * Returns a list of all children.
139 */
140 public List<JsonML> getChildren() {
141 return children;
142 }
143
144 /**
145 * Returns the portion of children list between the specified
146 * fromIndex, inclusive, and toIndex, exclusive.
147 * @param fromIndex low endpoint (inclusive)
148 * @param toIndex high endpoint (exclusive)
149 */
150 public List<JsonML> getChildren(int fromIndex, int toIndex) {
151 return children.subList(fromIndex, toIndex);
152 }
153
154 /**
155 * Returns type of the JsonML element.
156 */
157 public TagType getType() {
158 return type;
159 }
160
161 /**
162 * Returns true if the JsonML element has at least one child.
163 */
164 public boolean hasChildren() {
165 return !children.isEmpty();
166 }
167
168 /**
169 * Sets value for a given attribute.
170 * @param name name of the attribute
171 * @param value value to associate with the attribute
172 */
173 public void setAttribute(TagAttr name, Object value) {
174 attributes.put(name, value);
175 }
176
177 /**
178 * Sets attributes of the JsonML element.
179 * @param attributes map with attributes and their values
180 */
181 public void setAttributes(Map<TagAttr, Object> attributes) {
182 this.attributes = attributes;
183 }
184
185 /**
186 * Replaces the element at the given position in the list of children wit
187 * the given JsonML element.
188 * @param index index of element to replace
189 * @param element JsonML element to append
190 */
191 public void setChild(int index, JsonML element) {
192 children.set(index, element);
193 }
194
195 /**
196 * Replaces all elements in the list of children with the given
197 * JsonML elements.
198 * @param children a comma separated list of JsonML elements
199 */
200 public void setChildren(JsonML... children) {
201 this.children.clear();
202 this.children.addAll(Arrays.asList(children));
203 }
204
205 /**
206 * Replaces all elements in the list of children with the given
207 * list of JsonML elements..
208 * @param children a list of JsonML elements.
209 */
210 public void setChildren(List<JsonML> children) {
211 this.children = children;
212 }
213
214 @Override
215 public String toString() {
216 StringBuilder sb = new StringBuilder();
217 toString(sb, true, true);
218 return sb.toString();
219 }
220
221 private void toString(StringBuilder sb, boolean printAttributes,
222 boolean printChildren) {
223 sb.append("[\"");
224 escapeStringOnto(type.name(), sb);
225 sb.append('"');
226
227 if (printAttributes) {
228 sb.append(", {");
229 boolean first = true;
230 for (Entry<TagAttr, Object> entry : attributes.entrySet()) {
231 if (first) {
232 first = false;
233 } else {
234 sb.append(", ");
235 }
236 sb.append('"');
237 escapeStringOnto(entry.getKey().toString(), sb);
238 sb.append("\": ");
239 Object value = entry.getValue();
240 if (value == null) {
241 sb.append("null");
242 } else if (value instanceof String) {
243 sb.append('"');
244 escapeStringOnto((String) value, sb);
245 sb.append('"');
246 } else {
247 sb.append(value);
248 }
249 }
250 sb.append("}");
251 }
252
253 if (printChildren) {
254 for (JsonML child : children) {
255 sb.append(", ");
256 sb.append(child.toString());
257 }
258 }
259 sb.append(']');
260 }
261
262
263 /**
264 * Encodes the specified string and appends it to the given StringBuilder.
265 */
266 private static void escapeStringOnto(String s, StringBuilder sb) {
267 int pos = 0, n = s.length();
268 for (int i = 0; i < n; ++i) {
269 char ch = s.charAt(i);
270 switch (ch) {
271 case '\r': case '\n': case '"': case '\\':
272 // these two characters are the exceptions to the general rule
273 // that JSON is a syntactic subset of JavaScript
274 // From JSON's perspective they are considered to be whitespaces,
275 // while ES5 specifies them as line terminators.
276 case '\u2028': case '\u2029':
277 String hex = Integer.toString(ch, 16);
278 sb.append(s, pos, i)
279 .append("\\u").append("0000", hex.length(), 4).append(hex);
280 pos = i + 1;
281 break;
282 }
283 }
284 sb.append(s, pos, n);
285 }
286
287 /**
288 * Prints a JsonML tree in a human readable format.
289 */
290 public String toStringTree() {
291 try {
292 StringBuilder s = new StringBuilder();
293 toStringTreeHelper(this, 0, s);
294 return s.toString();
295 } catch (IOException e) {
296 throw new RuntimeException("Should not happen\n" + e);
297 }
298 }
299
300 private static void toStringTreeHelper(JsonML element, int level,
301 StringBuilder sb) throws IOException {
302 for (int i = 0; i < level; ++i) {
303 sb.append(" ");
304 }
305 element.toString(sb, true, false);
306 sb.append("\n");
307 for (JsonML child : element.getChildren()) {
308 toStringTreeHelper(child, level + 1, sb);
309 }
310 }
311 }
312