55362b2749d07e120ada98a17db1e9000741aad4
[jalview.git] / src / org / json / XML.java
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     private static Iterable<Integer> codePointIterator(final String string) {
79         return new Iterable<Integer>() {
80             @Override
81             public Iterator<Integer> iterator() {
82                 return new Iterator<Integer>() {
83                     private int nextIndex = 0;
84                     private int length = string.length();
85
86                     @Override
87                     public boolean hasNext() {
88                         return this.nextIndex < this.length;
89                     }
90
91                     @Override
92                     public Integer next() {
93                         int result = string.codePointAt(this.nextIndex);
94                         this.nextIndex += Character.charCount(result);
95                         return result;
96                     }
97
98                     @Override
99                     public void remove() {
100                         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     public static String escape(String string) {
123         StringBuilder sb = new StringBuilder(string.length());
124         for (final int cp : codePointIterator(string)) {
125             switch (cp) {
126             case '&':
127                 sb.append("&amp;");
128                 break;
129             case '<':
130                 sb.append("&lt;");
131                 break;
132             case '>':
133                 sb.append("&gt;");
134                 break;
135             case '"':
136                 sb.append("&quot;");
137                 break;
138             case '\'':
139                 sb.append("&apos;");
140                 break;
141             default:
142                 if (mustEscape(cp)) {
143                     sb.append("&#x");
144                     sb.append(Integer.toHexString(cp));
145                     sb.append(';');
146                 } else {
147                     sb.appendCodePoint(cp);
148                 }
149             }
150         }
151         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     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         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     public static String unescape(String string) {
188         StringBuilder sb = new StringBuilder(string.length());
189         for (int i = 0, length = string.length(); i < length; i++) {
190             char c = string.charAt(i);
191             if (c == '&') {
192                 final int semic = string.indexOf(';', i);
193                 if (semic > i) {
194                     final String entity = string.substring(i + 1, semic);
195                     sb.append(XMLTokener.unescapeEntity(entity));
196                     // skip past the entity we just parsed.
197                     i += entity.length() + 1;
198                 } else {
199                     // this shouldn't happen in most cases since the parser
200                     // errors on unclosed entries.
201                     sb.append(c);
202                 }
203             } else {
204                 // not part of an entity
205                 sb.append(c);
206             }
207         }
208         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     public static void noSpace(String string) throws JSONException {
220         int i, length = string.length();
221         if (length == 0) {
222             throw new JSONException("Empty string.");
223         }
224         for (i = 0; i < length; i += 1) {
225             if (Character.isWhitespace(string.charAt(i))) {
226                 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     private static boolean parse(XMLTokener x, JSONObject context, String name, boolean keepStrings)
245             throws JSONException {
246         char c;
247         int i;
248         JSONObject jsonobject = null;
249         String string;
250         String tagName;
251         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         token = x.nextToken();
264
265         // <!
266
267         if (token == BANG) {
268             c = x.next();
269             if (c == '-') {
270                 if (x.next() == '-') {
271                     x.skipPast("-->");
272                     return false;
273                 }
274                 x.back();
275             } else if (c == '[') {
276                 token = x.nextToken();
277                 if ("CDATA".equals(token)) {
278                     if (x.next() == '[') {
279                         string = x.nextCDATA();
280                         if (string.length() > 0) {
281                             context.accumulate("content", string);
282                         }
283                         return false;
284                     }
285                 }
286                 throw x.syntaxError("Expected 'CDATA['");
287             }
288             i = 1;
289             do {
290                 token = x.nextMeta();
291                 if (token == null) {
292                     throw x.syntaxError("Missing '>' after '<!'.");
293                 } else if (token == LT) {
294                     i += 1;
295                 } else if (token == GT) {
296                     i -= 1;
297                 }
298             } while (i > 0);
299             return false;
300         } else if (token == QUEST) {
301
302             // <?
303             x.skipPast("?>");
304             return false;
305         } else if (token == SLASH) {
306
307             // Close tag </
308
309             token = x.nextToken();
310             if (name == null) {
311                 throw x.syntaxError("Mismatched close tag " + token);
312             }
313             if (!token.equals(name)) {
314                 throw x.syntaxError("Mismatched " + name + " and " + token);
315             }
316             if (x.nextToken() != GT) {
317                 throw x.syntaxError("Misshaped close tag");
318             }
319             return true;
320
321         } else if (token instanceof Character) {
322             throw x.syntaxError("Misshaped tag");
323
324             // Open tag <
325
326         } else {
327             tagName = (String) token;
328             token = null;
329             jsonobject = new JSONObject();
330             for (;;) {
331                 if (token == null) {
332                     token = x.nextToken();
333                 }
334                 // attribute = value
335                 if (token instanceof String) {
336                     string = (String) token;
337                     token = x.nextToken();
338                     if (token == EQ) {
339                         token = x.nextToken();
340                         if (!(token instanceof String)) {
341                             throw x.syntaxError("Missing value");
342                         }
343                         jsonobject.accumulate(string,
344                                 keepStrings ? ((String)token) : stringToValue((String) token));
345                         token = null;
346                     } else {
347                         jsonobject.accumulate(string, "");
348                     }
349
350
351                 } else if (token == SLASH) {
352                     // Empty tag <.../>
353                     if (x.nextToken() != GT) {
354                         throw x.syntaxError("Misshaped tag");
355                     }
356                     if (jsonobject.length() > 0) {
357                         context.accumulate(tagName, jsonobject);
358                     } else {
359                         context.accumulate(tagName, "");
360                     }
361                     return false;
362
363                 } else if (token == GT) {
364                     // Content, between <...> and </...>
365                     for (;;) {
366                         token = x.nextContent();
367                         if (token == null) {
368                             if (tagName != null) {
369                                 throw x.syntaxError("Unclosed tag " + tagName);
370                             }
371                             return false;
372                         } else if (token instanceof String) {
373                             string = (String) token;
374                             if (string.length() > 0) {
375                                 jsonobject.accumulate("content",
376                                         keepStrings ? string : stringToValue(string));
377                             }
378
379                         } else if (token == LT) {
380                             // Nested element
381                             if (parse(x, jsonobject, tagName,keepStrings)) {
382                                 if (jsonobject.length() == 0) {
383                                     context.accumulate(tagName, "");
384                                 } else if (jsonobject.length() == 1
385                                         && jsonobject.opt("content") != null) {
386                                     context.accumulate(tagName,
387                                             jsonobject.opt("content"));
388                                 } else {
389                                     context.accumulate(tagName, jsonobject);
390                                 }
391                                 return false;
392                             }
393                         }
394                     }
395                 } else {
396                     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     public static Object stringToValue(String string) {
411         if (string.equals("")) {
412             return string;
413         }
414         if (string.equalsIgnoreCase("true")) {
415             return Boolean.TRUE;
416         }
417         if (string.equalsIgnoreCase("false")) {
418             return Boolean.FALSE;
419         }
420         if (string.equalsIgnoreCase("null")) {
421             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         char initial = string.charAt(0);
430         if ((initial >= '0' && initial <= '9') || initial == '-') {
431             try {
432                 // if we want full Big Number support this block can be replaced with:
433                 // return stringToNumber(string);
434                 if (string.indexOf('.') > -1 || string.indexOf('e') > -1
435                         || string.indexOf('E') > -1 || "-0".equals(string)) {
436                     Double d = Double.valueOf(string);
437                     if (!d.isInfinite() && !d.isNaN()) {
438                         return d;
439                     }
440                 } else {
441                     Long myLong = Long.valueOf(string);
442                     if (string.equals(myLong.toString())) {
443                         if (myLong.longValue() == myLong.intValue()) {
444                             return Integer.valueOf(myLong.intValue());
445                         }
446                         return myLong;
447                     }
448                 }
449             } catch (Exception ignore) {
450             }
451         }
452         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     public static JSONObject toJSONObject(String string) throws JSONException {
472         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     public static JSONObject toJSONObject(Reader reader) throws JSONException {
491         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     public static JSONObject toJSONObject(Reader reader, boolean keepStrings) throws JSONException {
515         JSONObject jo = new JSONObject();
516         XMLTokener x = new XMLTokener(reader);
517         while (x.more()) {
518             x.skipPast("<");
519             if(x.more()) {
520                 parse(x, jo, null, keepStrings);
521             }
522         }
523         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     public static JSONObject toJSONObject(String string, boolean keepStrings) throws JSONException {
548         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     public static String toString(Object object) throws JSONException {
560         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     public static String toString(final Object object, final String tagName)
574             throws JSONException {
575         StringBuilder sb = new StringBuilder();
576         JSONArray ja;
577         JSONObject jo;
578         String string;
579
580         if (object instanceof JSONObject) {
581
582             // Emit <tagName>
583             if (tagName != null) {
584                 sb.append('<');
585                 sb.append(tagName);
586                 sb.append('>');
587             }
588
589             // Loop thru the keys.
590             // don't use the new entrySet accessor to maintain Android Support
591             jo = (JSONObject) object;
592             for (final String key : jo.keySet()) {
593                 Object value = jo.opt(key);
594                 if (value == null) {
595                     value = "";
596                 } else if (value.getClass().isArray()) {
597                     value = new JSONArray(value);
598                 }
599
600                 // Emit content in body
601                 if ("content".equals(key)) {
602                     if (value instanceof JSONArray) {
603                         ja = (JSONArray) value;
604                         int jaLength = ja.length();
605                         // don't use the new iterator API to maintain support for Android
606                                                 for (int i = 0; i < jaLength; i++) {
607                             if (i > 0) {
608                                 sb.append('\n');
609                             }
610                             Object val = ja.opt(i);
611                             sb.append(escape(val.toString()));
612                         }
613                     } else {
614                         sb.append(escape(value.toString()));
615                     }
616
617                     // Emit an array of similar keys
618
619                 } else if (value instanceof JSONArray) {
620                     ja = (JSONArray) value;
621                     int jaLength = ja.length();
622                     // don't use the new iterator API to maintain support for Android
623                                         for (int i = 0; i < jaLength; i++) {
624                         Object val = ja.opt(i);
625                         if (val instanceof JSONArray) {
626                             sb.append('<');
627                             sb.append(key);
628                             sb.append('>');
629                             sb.append(toString(val));
630                             sb.append("</");
631                             sb.append(key);
632                             sb.append('>');
633                         } else {
634                             sb.append(toString(val, key));
635                         }
636                     }
637                 } else if ("".equals(value)) {
638                     sb.append('<');
639                     sb.append(key);
640                     sb.append("/>");
641
642                     // Emit a new tag <k>
643
644                 } else {
645                     sb.append(toString(value, key));
646                 }
647             }
648             if (tagName != null) {
649
650                 // Emit the </tagname> close tag
651                 sb.append("</");
652                 sb.append(tagName);
653                 sb.append('>');
654             }
655             return sb.toString();
656
657         }
658
659         if (object != null && (object instanceof JSONArray ||  object.getClass().isArray())) {
660             if(object.getClass().isArray()) {
661                 ja = new JSONArray(object);
662             } else {
663                 ja = (JSONArray) object;
664             }
665             int jaLength = ja.length();
666             // don't use the new iterator API to maintain support for Android
667                         for (int i = 0; i < jaLength; i++) {
668                 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                 sb.append(toString(val, tagName == null ? "array" : tagName));
673             }
674             return sb.toString();
675         }
676
677         string = (object == null) ? "null" : escape(object.toString());
678         return (tagName == null) ? "\"" + string + "\""
679                 : (string.length() == 0) ? "<" + tagName + "/>" : "<" + tagName
680                         + ">" + string + "</" + tagName + ">";
681
682     }
683 }