1 |
|
package org.json; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
@author |
33 |
|
@version |
34 |
|
|
|
|
| 0% |
Uncovered Elements: 331 (331) |
Complexity: 76 |
Complexity Density: 0.38 |
|
35 |
|
public class JSONML |
36 |
|
{ |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
@param |
41 |
|
|
42 |
|
@param |
43 |
|
|
44 |
|
@param |
45 |
|
|
46 |
|
|
47 |
|
@param |
48 |
|
|
49 |
|
@return |
50 |
|
@throws |
51 |
|
|
|
|
| 0% |
Uncovered Elements: 185 (185) |
Complexity: 48 |
Complexity Density: 0.48 |
|
52 |
0 |
private static Object parse(XMLTokener x, boolean arrayForm, JSONArray ja,... |
53 |
|
boolean keepStrings) throws JSONException |
54 |
|
{ |
55 |
0 |
String attribute; |
56 |
0 |
char c; |
57 |
0 |
String closeTag = null; |
58 |
0 |
int i; |
59 |
0 |
JSONArray newja = null; |
60 |
0 |
JSONObject newjo = null; |
61 |
0 |
Object token; |
62 |
0 |
String tagName = null; |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
0 |
while (true) |
71 |
|
{ |
72 |
0 |
if (!x.more()) |
73 |
|
{ |
74 |
0 |
throw x.syntaxError("Bad XML"); |
75 |
|
} |
76 |
0 |
token = x.nextContent(); |
77 |
0 |
if (token == XML.LT) |
78 |
|
{ |
79 |
0 |
token = x.nextToken(); |
80 |
0 |
if (token instanceof Character) |
81 |
|
{ |
82 |
0 |
if (token == XML.SLASH) |
83 |
|
{ |
84 |
|
|
85 |
|
|
86 |
|
|
87 |
0 |
token = x.nextToken(); |
88 |
0 |
if (!(token instanceof String)) |
89 |
|
{ |
90 |
0 |
throw new JSONException("Expected a closing name instead of '" |
91 |
|
+ token + "'."); |
92 |
|
} |
93 |
0 |
if (x.nextToken() != XML.GT) |
94 |
|
{ |
95 |
0 |
throw x.syntaxError("Misshaped close tag"); |
96 |
|
} |
97 |
0 |
return token; |
98 |
|
} |
99 |
0 |
else if (token == XML.BANG) |
100 |
|
{ |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
0 |
c = x.next(); |
105 |
0 |
if (c == '-') |
106 |
|
{ |
107 |
0 |
if (x.next() == '-') |
108 |
|
{ |
109 |
0 |
x.skipPast("-->"); |
110 |
|
} |
111 |
|
else |
112 |
|
{ |
113 |
0 |
x.back(); |
114 |
|
} |
115 |
|
} |
116 |
0 |
else if (c == '[') |
117 |
|
{ |
118 |
0 |
token = x.nextToken(); |
119 |
0 |
if (token.equals("CDATA") && x.next() == '[') |
120 |
|
{ |
121 |
0 |
if (ja != null) |
122 |
|
{ |
123 |
0 |
ja.put(x.nextCDATA()); |
124 |
|
} |
125 |
|
} |
126 |
|
else |
127 |
|
{ |
128 |
0 |
throw x.syntaxError("Expected 'CDATA['"); |
129 |
|
} |
130 |
|
} |
131 |
|
else |
132 |
|
{ |
133 |
0 |
i = 1; |
134 |
0 |
do |
135 |
|
{ |
136 |
0 |
token = x.nextMeta(); |
137 |
0 |
if (token == null) |
138 |
|
{ |
139 |
0 |
throw x.syntaxError("Missing '>' after '<!'."); |
140 |
|
} |
141 |
0 |
else if (token == XML.LT) |
142 |
|
{ |
143 |
0 |
i += 1; |
144 |
|
} |
145 |
0 |
else if (token == XML.GT) |
146 |
|
{ |
147 |
0 |
i -= 1; |
148 |
|
} |
149 |
0 |
} while (i > 0); |
150 |
|
} |
151 |
|
} |
152 |
0 |
else if (token == XML.QUEST) |
153 |
|
{ |
154 |
|
|
155 |
|
|
156 |
|
|
157 |
0 |
x.skipPast("?>"); |
158 |
|
} |
159 |
|
else |
160 |
|
{ |
161 |
0 |
throw x.syntaxError("Misshaped tag"); |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
} |
167 |
|
else |
168 |
|
{ |
169 |
0 |
if (!(token instanceof String)) |
170 |
|
{ |
171 |
0 |
throw x.syntaxError("Bad tagName '" + token + "'."); |
172 |
|
} |
173 |
0 |
tagName = (String) token; |
174 |
0 |
newja = new JSONArray(); |
175 |
0 |
newjo = new JSONObject(); |
176 |
0 |
if (arrayForm) |
177 |
|
{ |
178 |
0 |
newja.put(tagName); |
179 |
0 |
if (ja != null) |
180 |
|
{ |
181 |
0 |
ja.put(newja); |
182 |
|
} |
183 |
|
} |
184 |
|
else |
185 |
|
{ |
186 |
0 |
newjo.put("tagName", tagName); |
187 |
0 |
if (ja != null) |
188 |
|
{ |
189 |
0 |
ja.put(newjo); |
190 |
|
} |
191 |
|
} |
192 |
0 |
token = null; |
193 |
0 |
for (;;) |
194 |
|
{ |
195 |
0 |
if (token == null) |
196 |
|
{ |
197 |
0 |
token = x.nextToken(); |
198 |
|
} |
199 |
0 |
if (token == null) |
200 |
|
{ |
201 |
0 |
throw x.syntaxError("Misshaped tag"); |
202 |
|
} |
203 |
0 |
if (!(token instanceof String)) |
204 |
|
{ |
205 |
0 |
break; |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
|
|
210 |
0 |
attribute = (String) token; |
211 |
0 |
if (!arrayForm && ("tagName".equals(attribute) |
212 |
|
|| "childNode".equals(attribute))) |
213 |
|
{ |
214 |
0 |
throw x.syntaxError("Reserved attribute."); |
215 |
|
} |
216 |
0 |
token = x.nextToken(); |
217 |
0 |
if (token == XML.EQ) |
218 |
|
{ |
219 |
0 |
token = x.nextToken(); |
220 |
0 |
if (!(token instanceof String)) |
221 |
|
{ |
222 |
0 |
throw x.syntaxError("Missing value"); |
223 |
|
} |
224 |
0 |
newjo.accumulate(attribute, keepStrings ? ((String) token) |
225 |
|
: XML.stringToValue((String) token)); |
226 |
0 |
token = null; |
227 |
|
} |
228 |
|
else |
229 |
|
{ |
230 |
0 |
newjo.accumulate(attribute, ""); |
231 |
|
} |
232 |
|
} |
233 |
0 |
if (arrayForm && newjo.length() > 0) |
234 |
|
{ |
235 |
0 |
newja.put(newjo); |
236 |
|
} |
237 |
|
|
238 |
|
|
239 |
|
|
240 |
0 |
if (token == XML.SLASH) |
241 |
|
{ |
242 |
0 |
if (x.nextToken() != XML.GT) |
243 |
|
{ |
244 |
0 |
throw x.syntaxError("Misshaped tag"); |
245 |
|
} |
246 |
0 |
if (ja == null) |
247 |
|
{ |
248 |
0 |
if (arrayForm) |
249 |
|
{ |
250 |
0 |
return newja; |
251 |
|
} |
252 |
0 |
return newjo; |
253 |
|
} |
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
} |
258 |
|
else |
259 |
|
{ |
260 |
0 |
if (token != XML.GT) |
261 |
|
{ |
262 |
0 |
throw x.syntaxError("Misshaped tag"); |
263 |
|
} |
264 |
0 |
closeTag = (String) parse(x, arrayForm, newja, keepStrings); |
265 |
0 |
if (closeTag != null) |
266 |
|
{ |
267 |
0 |
if (!closeTag.equals(tagName)) |
268 |
|
{ |
269 |
0 |
throw x.syntaxError("Mismatched '" + tagName + "' and '" |
270 |
|
+ closeTag + "'"); |
271 |
|
} |
272 |
0 |
tagName = null; |
273 |
0 |
if (!arrayForm && newja.length() > 0) |
274 |
|
{ |
275 |
0 |
newjo.put("childNodes", newja); |
276 |
|
} |
277 |
0 |
if (ja == null) |
278 |
|
{ |
279 |
0 |
if (arrayForm) |
280 |
|
{ |
281 |
0 |
return newja; |
282 |
|
} |
283 |
0 |
return newjo; |
284 |
|
} |
285 |
|
} |
286 |
|
} |
287 |
|
} |
288 |
|
} |
289 |
|
else |
290 |
|
{ |
291 |
0 |
if (ja != null) |
292 |
|
{ |
293 |
0 |
ja.put(token instanceof String |
294 |
0 |
? keepStrings ? XML.unescape((String) token) |
295 |
|
: XML.stringToValue((String) token) |
296 |
|
: token); |
297 |
|
} |
298 |
|
} |
299 |
|
} |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
@param |
312 |
|
|
313 |
|
@return |
314 |
|
@throws |
315 |
|
|
316 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
317 |
0 |
public static JSONArray toJSONArray(String string) throws JSONException... |
318 |
|
{ |
319 |
0 |
return (JSONArray) parse(new XMLTokener(string), true, null, false); |
320 |
|
} |
321 |
|
|
322 |
|
|
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
|
333 |
|
@param |
334 |
|
|
335 |
|
@param |
336 |
|
|
337 |
|
|
338 |
|
@return |
339 |
|
@throws |
340 |
|
|
341 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
342 |
0 |
public static JSONArray toJSONArray(String string, boolean keepStrings)... |
343 |
|
throws JSONException |
344 |
|
{ |
345 |
0 |
return (JSONArray) parse(new XMLTokener(string), true, null, |
346 |
|
keepStrings); |
347 |
|
} |
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
@param |
361 |
|
|
362 |
|
@param |
363 |
|
|
364 |
|
|
365 |
|
@return |
366 |
|
@throws |
367 |
|
|
368 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
369 |
0 |
public static JSONArray toJSONArray(XMLTokener x, boolean keepStrings)... |
370 |
|
throws JSONException |
371 |
|
{ |
372 |
0 |
return (JSONArray) parse(x, true, null, keepStrings); |
373 |
|
} |
374 |
|
|
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
|
384 |
|
@param |
385 |
|
|
386 |
|
@return |
387 |
|
@throws |
388 |
|
|
389 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
390 |
0 |
public static JSONArray toJSONArray(XMLTokener x) throws JSONException... |
391 |
|
{ |
392 |
0 |
return (JSONArray) parse(x, true, null, false); |
393 |
|
} |
394 |
|
|
395 |
|
|
396 |
|
|
397 |
|
|
398 |
|
|
399 |
|
|
400 |
|
|
401 |
|
|
402 |
|
|
403 |
|
|
404 |
|
|
405 |
|
@param |
406 |
|
|
407 |
|
@return |
408 |
|
@throws |
409 |
|
|
410 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
411 |
0 |
public static JSONObject toJSONObject(String string) throws JSONException... |
412 |
|
{ |
413 |
0 |
return (JSONObject) parse(new XMLTokener(string), false, null, false); |
414 |
|
} |
415 |
|
|
416 |
|
|
417 |
|
|
418 |
|
|
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
@param |
427 |
|
|
428 |
|
@param |
429 |
|
|
430 |
|
|
431 |
|
@return |
432 |
|
@throws |
433 |
|
|
434 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
435 |
0 |
public static JSONObject toJSONObject(String string, boolean keepStrings)... |
436 |
|
throws JSONException |
437 |
|
{ |
438 |
0 |
return (JSONObject) parse(new XMLTokener(string), false, null, |
439 |
|
keepStrings); |
440 |
|
} |
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
|
446 |
|
|
447 |
|
|
448 |
|
|
449 |
|
|
450 |
|
|
451 |
|
|
452 |
|
@param |
453 |
|
|
454 |
|
@return |
455 |
|
@throws |
456 |
|
|
457 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
458 |
0 |
public static JSONObject toJSONObject(XMLTokener x) throws JSONException... |
459 |
|
{ |
460 |
0 |
return (JSONObject) parse(x, false, null, false); |
461 |
|
} |
462 |
|
|
463 |
|
|
464 |
|
|
465 |
|
|
466 |
|
|
467 |
|
|
468 |
|
|
469 |
|
|
470 |
|
|
471 |
|
|
472 |
|
|
473 |
|
@param |
474 |
|
|
475 |
|
@param |
476 |
|
|
477 |
|
|
478 |
|
@return |
479 |
|
@throws |
480 |
|
|
481 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
482 |
0 |
public static JSONObject toJSONObject(XMLTokener x, boolean keepStrings)... |
483 |
|
throws JSONException |
484 |
|
{ |
485 |
0 |
return (JSONObject) parse(x, false, null, keepStrings); |
486 |
|
} |
487 |
|
|
488 |
|
|
489 |
|
|
490 |
|
|
491 |
|
@param |
492 |
|
|
493 |
|
@return |
494 |
|
@throws |
495 |
|
|
496 |
|
|
|
|
| 0% |
Uncovered Elements: 63 (63) |
Complexity: 9 |
Complexity Density: 0.19 |
|
497 |
0 |
public static String toString(JSONArray ja) throws JSONException... |
498 |
|
{ |
499 |
0 |
int i; |
500 |
0 |
JSONObject jo; |
501 |
0 |
int length; |
502 |
0 |
Object object; |
503 |
0 |
StringBuilder sb = new StringBuilder(); |
504 |
0 |
String tagName; |
505 |
|
|
506 |
|
|
507 |
|
|
508 |
0 |
tagName = ja.getString(0); |
509 |
0 |
XML.noSpace(tagName); |
510 |
0 |
tagName = XML.escape(tagName); |
511 |
0 |
sb.append('<'); |
512 |
0 |
sb.append(tagName); |
513 |
|
|
514 |
0 |
object = ja.opt(1); |
515 |
0 |
if (object instanceof JSONObject) |
516 |
|
{ |
517 |
0 |
i = 2; |
518 |
0 |
jo = (JSONObject) object; |
519 |
|
|
520 |
|
|
521 |
|
|
522 |
|
|
523 |
0 |
for (final String key : jo.keySet()) |
524 |
|
{ |
525 |
0 |
final Object value = jo.opt(key); |
526 |
0 |
XML.noSpace(key); |
527 |
0 |
if (value != null) |
528 |
|
{ |
529 |
0 |
sb.append(' '); |
530 |
0 |
sb.append(XML.escape(key)); |
531 |
0 |
sb.append('='); |
532 |
0 |
sb.append('"'); |
533 |
0 |
sb.append(XML.escape(value.toString())); |
534 |
0 |
sb.append('"'); |
535 |
|
} |
536 |
|
} |
537 |
|
} |
538 |
|
else |
539 |
|
{ |
540 |
0 |
i = 1; |
541 |
|
} |
542 |
|
|
543 |
|
|
544 |
|
|
545 |
0 |
length = ja.length(); |
546 |
0 |
if (i >= length) |
547 |
|
{ |
548 |
0 |
sb.append('/'); |
549 |
0 |
sb.append('>'); |
550 |
|
} |
551 |
|
else |
552 |
|
{ |
553 |
0 |
sb.append('>'); |
554 |
0 |
do |
555 |
|
{ |
556 |
0 |
object = ja.get(i); |
557 |
0 |
i += 1; |
558 |
0 |
if (object != null) |
559 |
|
{ |
560 |
0 |
if (object instanceof String) |
561 |
|
{ |
562 |
0 |
sb.append(XML.escape(object.toString())); |
563 |
|
} |
564 |
0 |
else if (object instanceof JSONObject) |
565 |
|
{ |
566 |
0 |
sb.append(toString((JSONObject) object)); |
567 |
|
} |
568 |
0 |
else if (object instanceof JSONArray) |
569 |
|
{ |
570 |
0 |
sb.append(toString((JSONArray) object)); |
571 |
|
} |
572 |
|
else |
573 |
|
{ |
574 |
0 |
sb.append(object.toString()); |
575 |
|
} |
576 |
|
} |
577 |
0 |
} while (i < length); |
578 |
0 |
sb.append('<'); |
579 |
0 |
sb.append('/'); |
580 |
0 |
sb.append(tagName); |
581 |
0 |
sb.append('>'); |
582 |
|
} |
583 |
0 |
return sb.toString(); |
584 |
|
} |
585 |
|
|
586 |
|
|
587 |
|
|
588 |
|
|
589 |
|
|
590 |
|
|
591 |
|
|
592 |
|
@param |
593 |
|
|
594 |
|
@return |
595 |
|
@throws |
596 |
|
|
597 |
|
|
|
|
| 0% |
Uncovered Elements: 64 (64) |
Complexity: 11 |
Complexity Density: 0.24 |
|
598 |
0 |
public static String toString(JSONObject jo) throws JSONException... |
599 |
|
{ |
600 |
0 |
StringBuilder sb = new StringBuilder(); |
601 |
0 |
int i; |
602 |
0 |
JSONArray ja; |
603 |
0 |
int length; |
604 |
0 |
Object object; |
605 |
0 |
String tagName; |
606 |
0 |
Object value; |
607 |
|
|
608 |
|
|
609 |
|
|
610 |
0 |
tagName = jo.optString("tagName"); |
611 |
0 |
if (tagName == null) |
612 |
|
{ |
613 |
0 |
return XML.escape(jo.toString()); |
614 |
|
} |
615 |
0 |
XML.noSpace(tagName); |
616 |
0 |
tagName = XML.escape(tagName); |
617 |
0 |
sb.append('<'); |
618 |
0 |
sb.append(tagName); |
619 |
|
|
620 |
|
|
621 |
|
|
622 |
|
|
623 |
0 |
for (final String key : jo.keySet()) |
624 |
|
{ |
625 |
0 |
if (!"tagName".equals(key) && !"childNodes".equals(key)) |
626 |
|
{ |
627 |
0 |
XML.noSpace(key); |
628 |
0 |
value = jo.opt(key); |
629 |
0 |
if (value != null) |
630 |
|
{ |
631 |
0 |
sb.append(' '); |
632 |
0 |
sb.append(XML.escape(key)); |
633 |
0 |
sb.append('='); |
634 |
0 |
sb.append('"'); |
635 |
0 |
sb.append(XML.escape(value.toString())); |
636 |
0 |
sb.append('"'); |
637 |
|
} |
638 |
|
} |
639 |
|
} |
640 |
|
|
641 |
|
|
642 |
|
|
643 |
0 |
ja = jo.optJSONArray("childNodes"); |
644 |
0 |
if (ja == null) |
645 |
|
{ |
646 |
0 |
sb.append('/'); |
647 |
0 |
sb.append('>'); |
648 |
|
} |
649 |
|
else |
650 |
|
{ |
651 |
0 |
sb.append('>'); |
652 |
0 |
length = ja.length(); |
653 |
0 |
for (i = 0; i < length; i += 1) |
654 |
|
{ |
655 |
0 |
object = ja.get(i); |
656 |
0 |
if (object != null) |
657 |
|
{ |
658 |
0 |
if (object instanceof String) |
659 |
|
{ |
660 |
0 |
sb.append(XML.escape(object.toString())); |
661 |
|
} |
662 |
0 |
else if (object instanceof JSONObject) |
663 |
|
{ |
664 |
0 |
sb.append(toString((JSONObject) object)); |
665 |
|
} |
666 |
0 |
else if (object instanceof JSONArray) |
667 |
|
{ |
668 |
0 |
sb.append(toString((JSONArray) object)); |
669 |
|
} |
670 |
|
else |
671 |
|
{ |
672 |
0 |
sb.append(object.toString()); |
673 |
|
} |
674 |
|
} |
675 |
|
} |
676 |
0 |
sb.append('<'); |
677 |
0 |
sb.append('/'); |
678 |
0 |
sb.append(tagName); |
679 |
0 |
sb.append('>'); |
680 |
|
} |
681 |
0 |
return sb.toString(); |
682 |
|
} |
683 |
|
} |