Clover icon

Coverage Report

  1. Project Clover database Thu Aug 13 2020 12:04:21 BST
  2. Package org.json

File XML.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
56% of files have more coverage

Code metrics

146
234
17
1
683
384
105
0.45
13.76
17
6.18

Classes

Class Line # Actions
XML 39 234 105
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package org.json;
2   
3    /*
4    Copyright (c) 2015 JSON.org
5   
6    Permission is hereby granted, free of charge, to any person obtaining a copy
7    of this software and associated documentation files (the "Software"), to deal
8    in the Software without restriction, including without limitation the rights
9    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10    copies of the Software, and to permit persons to whom the Software is
11    furnished to do so, subject to the following conditions:
12   
13    The above copyright notice and this permission notice shall be included in all
14    copies or substantial portions of the Software.
15   
16    The Software shall be used for Good, not Evil.
17   
18    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24    SOFTWARE.
25    */
26   
27    import java.io.Reader;
28    import java.io.StringReader;
29    import java.util.Iterator;
30   
31    /**
32    * This provides static methods to convert an XML text into a JSONObject, and to
33    * covert a JSONObject into an XML text.
34    *
35    * @author JSON.org
36    * @version 2016-08-10
37    */
38    @SuppressWarnings("boxing")
 
39    public class XML {
40    /** The Character '&'. */
41    public static final Character AMP = '&';
42   
43    /** The Character '''. */
44    public static final Character APOS = '\'';
45   
46    /** The Character '!'. */
47    public static final Character BANG = '!';
48   
49    /** The Character '='. */
50    public static final Character EQ = '=';
51   
52    /** The Character '>'. */
53    public static final Character GT = '>';
54   
55    /** The Character '<'. */
56    public static final Character LT = '<';
57   
58    /** The Character '?'. */
59    public static final Character QUEST = '?';
60   
61    /** The Character '"'. */
62    public static final Character QUOT = '"';
63   
64    /** The Character '/'. */
65    public static final Character SLASH = '/';
66   
67    /**
68    * Creates an iterator for navigating Code Points in a string instead of
69    * characters. Once Java7 support is dropped, this can be replaced with
70    * <code>
71    * string.codePoints()
72    * </code>
73    * which is available in Java8 and above.
74    *
75    * @see <a href=
76    * "http://stackoverflow.com/a/21791059/6030888">http://stackoverflow.com/a/21791059/6030888</a>
77    */
 
78  0 toggle private static Iterable<Integer> codePointIterator(final String string) {
79  0 return new Iterable<Integer>() {
 
80  0 toggle @Override
81    public Iterator<Integer> iterator() {
82  0 return new Iterator<Integer>() {
83    private int nextIndex = 0;
84    private int length = string.length();
85   
 
86  0 toggle @Override
87    public boolean hasNext() {
88  0 return this.nextIndex < this.length;
89    }
90   
 
91  0 toggle @Override
92    public Integer next() {
93  0 int result = string.codePointAt(this.nextIndex);
94  0 this.nextIndex += Character.charCount(result);
95  0 return result;
96    }
97   
 
98  0 toggle @Override
99    public void remove() {
100  0 throw new UnsupportedOperationException();
101    }
102    };
103    }
104    };
105    }
106   
107    /**
108    * Replace special characters with XML escapes:
109    *
110    * <pre>
111    * &amp; <small>(ampersand)</small> is replaced by &amp;amp;
112    * &lt; <small>(less than)</small> is replaced by &amp;lt;
113    * &gt; <small>(greater than)</small> is replaced by &amp;gt;
114    * &quot; <small>(double quote)</small> is replaced by &amp;quot;
115    * &apos; <small>(single quote / apostrophe)</small> is replaced by &amp;apos;
116    * </pre>
117    *
118    * @param string
119    * The string to be escaped.
120    * @return The escaped string.
121    */
 
122  0 toggle public static String escape(String string) {
123  0 StringBuilder sb = new StringBuilder(string.length());
124  0 for (final int cp : codePointIterator(string)) {
125  0 switch (cp) {
126  0 case '&':
127  0 sb.append("&amp;");
128  0 break;
129  0 case '<':
130  0 sb.append("&lt;");
131  0 break;
132  0 case '>':
133  0 sb.append("&gt;");
134  0 break;
135  0 case '"':
136  0 sb.append("&quot;");
137  0 break;
138  0 case '\'':
139  0 sb.append("&apos;");
140  0 break;
141  0 default:
142  0 if (mustEscape(cp)) {
143  0 sb.append("&#x");
144  0 sb.append(Integer.toHexString(cp));
145  0 sb.append(';');
146    } else {
147  0 sb.appendCodePoint(cp);
148    }
149    }
150    }
151  0 return sb.toString();
152    }
153   
154    /**
155    * @param cp code point to test
156    * @return true if the code point is not valid for an XML
157    */
 
158  0 toggle private static boolean mustEscape(int cp) {
159    /* Valid range from https://www.w3.org/TR/REC-xml/#charsets
160    *
161    * #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
162    *
163    * any Unicode character, excluding the surrogate blocks, FFFE, and FFFF.
164    */
165    // isISOControl is true when (cp >= 0 && cp <= 0x1F) || (cp >= 0x7F && cp <= 0x9F)
166    // all ISO control characters are out of range except tabs and new lines
167  0 return (Character.isISOControl(cp)
168    && cp != 0x9
169    && cp != 0xA
170    && cp != 0xD
171    ) || !(
172    // valid the range of acceptable characters that aren't control
173    (cp >= 0x20 && cp <= 0xD7FF)
174    || (cp >= 0xE000 && cp <= 0xFFFD)
175    || (cp >= 0x10000 && cp <= 0x10FFFF)
176    )
177    ;
178    }
179   
180    /**
181    * Removes XML escapes from the string.
182    *
183    * @param string
184    * string to remove escapes from
185    * @return string with converted entities
186    */
 
187  0 toggle public static String unescape(String string) {
188  0 StringBuilder sb = new StringBuilder(string.length());
189  0 for (int i = 0, length = string.length(); i < length; i++) {
190  0 char c = string.charAt(i);
191  0 if (c == '&') {
192  0 final int semic = string.indexOf(';', i);
193  0 if (semic > i) {
194  0 final String entity = string.substring(i + 1, semic);
195  0 sb.append(XMLTokener.unescapeEntity(entity));
196    // skip past the entity we just parsed.
197  0 i += entity.length() + 1;
198    } else {
199    // this shouldn't happen in most cases since the parser
200    // errors on unclosed entries.
201  0 sb.append(c);
202    }
203    } else {
204    // not part of an entity
205  0 sb.append(c);
206    }
207    }
208  0 return sb.toString();
209    }
210   
211    /**
212    * Throw an exception if the string contains whitespace. Whitespace is not
213    * allowed in tagNames and attributes.
214    *
215    * @param string
216    * A string.
217    * @throws JSONException Thrown if the string contains whitespace or is empty.
218    */
 
219  0 toggle public static void noSpace(String string) throws JSONException {
220  0 int i, length = string.length();
221  0 if (length == 0) {
222  0 throw new JSONException("Empty string.");
223    }
224  0 for (i = 0; i < length; i += 1) {
225  0 if (Character.isWhitespace(string.charAt(i))) {
226  0 throw new JSONException("'" + string
227    + "' contains a space character.");
228    }
229    }
230    }
231   
232    /**
233    * Scan the content following the named tag, attaching it to the context.
234    *
235    * @param x
236    * The XMLTokener containing the source string.
237    * @param context
238    * The JSONObject that will include the new material.
239    * @param name
240    * The tag name.
241    * @return true if the close tag is processed.
242    * @throws JSONException
243    */
 
244  0 toggle private static boolean parse(XMLTokener x, JSONObject context, String name, boolean keepStrings)
245    throws JSONException {
246  0 char c;
247  0 int i;
248  0 JSONObject jsonobject = null;
249  0 String string;
250  0 String tagName;
251  0 Object token;
252   
253    // Test for and skip past these forms:
254    // <!-- ... -->
255    // <! ... >
256    // <![ ... ]]>
257    // <? ... ?>
258    // Report errors for these forms:
259    // <>
260    // <=
261    // <<
262   
263  0 token = x.nextToken();
264   
265    // <!
266   
267  0 if (token == BANG) {
268  0 c = x.next();
269  0 if (c == '-') {
270  0 if (x.next() == '-') {
271  0 x.skipPast("-->");
272  0 return false;
273    }
274  0 x.back();
275  0 } else if (c == '[') {
276  0 token = x.nextToken();
277  0 if ("CDATA".equals(token)) {
278  0 if (x.next() == '[') {
279  0 string = x.nextCDATA();
280  0 if (string.length() > 0) {
281  0 context.accumulate("content", string);
282    }
283  0 return false;
284    }
285    }
286  0 throw x.syntaxError("Expected 'CDATA['");
287    }
288  0 i = 1;
289  0 do {
290  0 token = x.nextMeta();
291  0 if (token == null) {
292  0 throw x.syntaxError("Missing '>' after '<!'.");
293  0 } else if (token == LT) {
294  0 i += 1;
295  0 } else if (token == GT) {
296  0 i -= 1;
297    }
298  0 } while (i > 0);
299  0 return false;
300  0 } else if (token == QUEST) {
301   
302    // <?
303  0 x.skipPast("?>");
304  0 return false;
305  0 } else if (token == SLASH) {
306   
307    // Close tag </
308   
309  0 token = x.nextToken();
310  0 if (name == null) {
311  0 throw x.syntaxError("Mismatched close tag " + token);
312    }
313  0 if (!token.equals(name)) {
314  0 throw x.syntaxError("Mismatched " + name + " and " + token);
315    }
316  0 if (x.nextToken() != GT) {
317  0 throw x.syntaxError("Misshaped close tag");
318    }
319  0 return true;
320   
321  0 } else if (token instanceof Character) {
322  0 throw x.syntaxError("Misshaped tag");
323   
324    // Open tag <
325   
326    } else {
327  0 tagName = (String) token;
328  0 token = null;
329  0 jsonobject = new JSONObject();
330  0 for (;;) {
331  0 if (token == null) {
332  0 token = x.nextToken();
333    }
334    // attribute = value
335  0 if (token instanceof String) {
336  0 string = (String) token;
337  0 token = x.nextToken();
338  0 if (token == EQ) {
339  0 token = x.nextToken();
340  0 if (!(token instanceof String)) {
341  0 throw x.syntaxError("Missing value");
342    }
343  0 jsonobject.accumulate(string,
344  0 keepStrings ? ((String)token) : stringToValue((String) token));
345  0 token = null;
346    } else {
347  0 jsonobject.accumulate(string, "");
348    }
349   
350   
351  0 } else if (token == SLASH) {
352    // Empty tag <.../>
353  0 if (x.nextToken() != GT) {
354  0 throw x.syntaxError("Misshaped tag");
355    }
356  0 if (jsonobject.length() > 0) {
357  0 context.accumulate(tagName, jsonobject);
358    } else {
359  0 context.accumulate(tagName, "");
360    }
361  0 return false;
362   
363  0 } else if (token == GT) {
364    // Content, between <...> and </...>
365  0 for (;;) {
366  0 token = x.nextContent();
367  0 if (token == null) {
368  0 if (tagName != null) {
369  0 throw x.syntaxError("Unclosed tag " + tagName);
370    }
371  0 return false;
372  0 } else if (token instanceof String) {
373  0 string = (String) token;
374  0 if (string.length() > 0) {
375  0 jsonobject.accumulate("content",
376  0 keepStrings ? string : stringToValue(string));
377    }
378   
379  0 } else if (token == LT) {
380    // Nested element
381  0 if (parse(x, jsonobject, tagName,keepStrings)) {
382  0 if (jsonobject.length() == 0) {
383  0 context.accumulate(tagName, "");
384  0 } else if (jsonobject.length() == 1
385    && jsonobject.opt("content") != null) {
386  0 context.accumulate(tagName,
387    jsonobject.opt("content"));
388    } else {
389  0 context.accumulate(tagName, jsonobject);
390    }
391  0 return false;
392    }
393    }
394    }
395    } else {
396  0 throw x.syntaxError("Misshaped tag");
397    }
398    }
399    }
400    }
401   
402    /**
403    * This method is the same as {@link JSONObject#stringToValue(String)}.
404    *
405    * @param string String to convert
406    * @return JSON value of this string or the string
407    */
408    // To maintain compatibility with the Android API, this method is a direct copy of
409    // the one in JSONObject. Changes made here should be reflected there.
 
410  0 toggle public static Object stringToValue(String string) {
411  0 if (string.equals("")) {
412  0 return string;
413    }
414  0 if (string.equalsIgnoreCase("true")) {
415  0 return Boolean.TRUE;
416    }
417  0 if (string.equalsIgnoreCase("false")) {
418  0 return Boolean.FALSE;
419    }
420  0 if (string.equalsIgnoreCase("null")) {
421  0 return JSONObject.NULL;
422    }
423   
424    /*
425    * If it might be a number, try converting it. If a number cannot be
426    * produced, then the value will just be a string.
427    */
428   
429  0 char initial = string.charAt(0);
430  0 if ((initial >= '0' && initial <= '9') || initial == '-') {
431  0 try {
432    // if we want full Big Number support this block can be replaced with:
433    // return stringToNumber(string);
434  0 if (string.indexOf('.') > -1 || string.indexOf('e') > -1
435    || string.indexOf('E') > -1 || "-0".equals(string)) {
436  0 Double d = Double.valueOf(string);
437  0 if (!d.isInfinite() && !d.isNaN()) {
438  0 return d;
439    }
440    } else {
441  0 Long myLong = Long.valueOf(string);
442  0 if (string.equals(myLong.toString())) {
443  0 if (myLong.longValue() == myLong.intValue()) {
444  0 return Integer.valueOf(myLong.intValue());
445    }
446  0 return myLong;
447    }
448    }
449    } catch (Exception ignore) {
450    }
451    }
452  0 return string;
453    }
454   
455    /**
456    * Convert a well-formed (but not necessarily valid) XML string into a
457    * JSONObject. Some information may be lost in this transformation because
458    * JSON is a data format and XML is a document format. XML uses elements,
459    * attributes, and content text, while JSON uses unordered collections of
460    * name/value pairs and arrays of values. JSON does not does not like to
461    * distinguish between elements and attributes. Sequences of similar
462    * elements are represented as JSONArrays. Content text may be placed in a
463    * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
464    * are ignored.
465    *
466    * @param string
467    * The source string.
468    * @return A JSONObject containing the structured data from the XML string.
469    * @throws JSONException Thrown if there is an errors while parsing the string
470    */
 
471  0 toggle public static JSONObject toJSONObject(String string) throws JSONException {
472  0 return toJSONObject(string, false);
473    }
474   
475    /**
476    * Convert a well-formed (but not necessarily valid) XML into a
477    * JSONObject. Some information may be lost in this transformation because
478    * JSON is a data format and XML is a document format. XML uses elements,
479    * attributes, and content text, while JSON uses unordered collections of
480    * name/value pairs and arrays of values. JSON does not does not like to
481    * distinguish between elements and attributes. Sequences of similar
482    * elements are represented as JSONArrays. Content text may be placed in a
483    * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
484    * are ignored.
485    *
486    * @param reader The XML source reader.
487    * @return A JSONObject containing the structured data from the XML string.
488    * @throws JSONException Thrown if there is an errors while parsing the string
489    */
 
490  0 toggle public static JSONObject toJSONObject(Reader reader) throws JSONException {
491  0 return toJSONObject(reader, false);
492    }
493   
494    /**
495    * Convert a well-formed (but not necessarily valid) XML into a
496    * JSONObject. Some information may be lost in this transformation because
497    * JSON is a data format and XML is a document format. XML uses elements,
498    * attributes, and content text, while JSON uses unordered collections of
499    * name/value pairs and arrays of values. JSON does not does not like to
500    * distinguish between elements and attributes. Sequences of similar
501    * elements are represented as JSONArrays. Content text may be placed in a
502    * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
503    * are ignored.
504    *
505    * All values are converted as strings, for 1, 01, 29.0 will not be coerced to
506    * numbers but will instead be the exact value as seen in the XML document.
507    *
508    * @param reader The XML source reader.
509    * @param keepStrings If true, then values will not be coerced into boolean
510    * or numeric values and will instead be left as strings
511    * @return A JSONObject containing the structured data from the XML string.
512    * @throws JSONException Thrown if there is an errors while parsing the string
513    */
 
514  0 toggle public static JSONObject toJSONObject(Reader reader, boolean keepStrings) throws JSONException {
515  0 JSONObject jo = new JSONObject();
516  0 XMLTokener x = new XMLTokener(reader);
517  0 while (x.more()) {
518  0 x.skipPast("<");
519  0 if(x.more()) {
520  0 parse(x, jo, null, keepStrings);
521    }
522    }
523  0 return jo;
524    }
525   
526    /**
527    * Convert a well-formed (but not necessarily valid) XML string into a
528    * JSONObject. Some information may be lost in this transformation because
529    * JSON is a data format and XML is a document format. XML uses elements,
530    * attributes, and content text, while JSON uses unordered collections of
531    * name/value pairs and arrays of values. JSON does not does not like to
532    * distinguish between elements and attributes. Sequences of similar
533    * elements are represented as JSONArrays. Content text may be placed in a
534    * "content" member. Comments, prologs, DTDs, and <code>&lt;[ [ ]]></code>
535    * are ignored.
536    *
537    * All values are converted as strings, for 1, 01, 29.0 will not be coerced to
538    * numbers but will instead be the exact value as seen in the XML document.
539    *
540    * @param string
541    * The source string.
542    * @param keepStrings If true, then values will not be coerced into boolean
543    * or numeric values and will instead be left as strings
544    * @return A JSONObject containing the structured data from the XML string.
545    * @throws JSONException Thrown if there is an errors while parsing the string
546    */
 
547  0 toggle public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
548  0 return toJSONObject(new StringReader(string), keepStrings);
549    }
550   
551    /**
552    * Convert a JSONObject into a well-formed, element-normal XML string.
553    *
554    * @param object
555    * A JSONObject.
556    * @return A string.
557    * @throws JSONException Thrown if there is an error parsing the string
558    */
 
559  0 toggle public static String toString(Object object) throws JSONException {
560  0 return toString(object, null);
561    }
562   
563    /**
564    * Convert a JSONObject into a well-formed, element-normal XML string.
565    *
566    * @param object
567    * A JSONObject.
568    * @param tagName
569    * The optional name of the enclosing tag.
570    * @return A string.
571    * @throws JSONException Thrown if there is an error parsing the string
572    */
 
573  0 toggle public static String toString(final Object object, final String tagName)
574    throws JSONException {
575  0 StringBuilder sb = new StringBuilder();
576  0 JSONArray ja;
577  0 JSONObject jo;
578  0 String string;
579   
580  0 if (object instanceof JSONObject) {
581   
582    // Emit <tagName>
583  0 if (tagName != null) {
584  0 sb.append('<');
585  0 sb.append(tagName);
586  0 sb.append('>');
587    }
588   
589    // Loop thru the keys.
590    // don't use the new entrySet accessor to maintain Android Support
591  0 jo = (JSONObject) object;
592  0 for (final String key : jo.keySet()) {
593  0 Object value = jo.opt(key);
594  0 if (value == null) {
595  0 value = "";
596  0 } else if (value.getClass().isArray()) {
597  0 value = new JSONArray(value);
598    }
599   
600    // Emit content in body
601  0 if ("content".equals(key)) {
602  0 if (value instanceof JSONArray) {
603  0 ja = (JSONArray) value;
604  0 int jaLength = ja.length();
605    // don't use the new iterator API to maintain support for Android
606  0 for (int i = 0; i < jaLength; i++) {
607  0 if (i > 0) {
608  0 sb.append('\n');
609    }
610  0 Object val = ja.opt(i);
611  0 sb.append(escape(val.toString()));
612    }
613    } else {
614  0 sb.append(escape(value.toString()));
615    }
616   
617    // Emit an array of similar keys
618   
619  0 } else if (value instanceof JSONArray) {
620  0 ja = (JSONArray) value;
621  0 int jaLength = ja.length();
622    // don't use the new iterator API to maintain support for Android
623  0 for (int i = 0; i < jaLength; i++) {
624  0 Object val = ja.opt(i);
625  0 if (val instanceof JSONArray) {
626  0 sb.append('<');
627  0 sb.append(key);
628  0 sb.append('>');
629  0 sb.append(toString(val));
630  0 sb.append("</");
631  0 sb.append(key);
632  0 sb.append('>');
633    } else {
634  0 sb.append(toString(val, key));
635    }
636    }
637  0 } else if ("".equals(value)) {
638  0 sb.append('<');
639  0 sb.append(key);
640  0 sb.append("/>");
641   
642    // Emit a new tag <k>
643   
644    } else {
645  0 sb.append(toString(value, key));
646    }
647    }
648  0 if (tagName != null) {
649   
650    // Emit the </tagname> close tag
651  0 sb.append("</");
652  0 sb.append(tagName);
653  0 sb.append('>');
654    }
655  0 return sb.toString();
656   
657    }
658   
659  0 if (object != null && (object instanceof JSONArray || object.getClass().isArray())) {
660  0 if(object.getClass().isArray()) {
661  0 ja = new JSONArray(object);
662    } else {
663  0 ja = (JSONArray) object;
664    }
665  0 int jaLength = ja.length();
666    // don't use the new iterator API to maintain support for Android
667  0 for (int i = 0; i < jaLength; i++) {
668  0 Object val = ja.opt(i);
669    // XML does not have good support for arrays. If an array
670    // appears in a place where XML is lacking, synthesize an
671    // <array> element.
672  0 sb.append(toString(val, tagName == null ? "array" : tagName));
673    }
674  0 return sb.toString();
675    }
676   
677  0 string = (object == null) ? "null" : escape(object.toString());
678  0 return (tagName == null) ? "\"" + string + "\""
679  0 : (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
680    + ">" + string + "</" + tagName + ">";
681   
682    }
683    }