4 Copyright (c) 2002 JSON.org
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:
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
16 The Software shall be used for Good, not Evil.
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
27 import java.io.Reader;
30 * The XMLTokener extends the JSONTokener to provide additional methods
31 * for the parsing of XML texts.
35 public class XMLTokener extends JSONTokener {
38 /** The table of entity values. It initially contains Character values for
39 * amp, apos, gt, lt, quot.
41 public static final java.util.HashMap<String, Character> entity;
44 entity = new java.util.HashMap<String, Character>(8);
45 entity.put("amp", XML.AMP);
46 entity.put("apos", XML.APOS);
47 entity.put("gt", XML.GT);
48 entity.put("lt", XML.LT);
49 entity.put("quot", XML.QUOT);
53 * Construct an XMLTokener from a Reader.
54 * @param r A source reader.
56 public XMLTokener(Reader r) {
61 * Construct an XMLTokener from a string.
62 * @param s A source string.
64 public XMLTokener(String s) {
69 * Get the text in the CDATA block.
70 * @return The string up to the <code>]]></code>.
71 * @throws JSONException If the <code>]]></code> is not found.
73 public String nextCDATA() throws JSONException {
76 StringBuilder sb = new StringBuilder();
81 if (i >= 0 && sb.charAt(i) == ']' &&
82 sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') {
87 throw syntaxError("Unclosed CDATA");
92 * Get the next XML outer token, trimming whitespace. There are two kinds
93 * of tokens: the '<' character which begins a markup tag, and the content
94 * text between markup tags.
96 * @return A string, or a '<' Character, or null if there is no more
98 * @throws JSONException
100 public Object nextContent() throws JSONException {
105 } while (Character.isWhitespace(c));
112 sb = new StringBuilder();
115 return sb.toString().trim();
119 return sb.toString().trim();
122 sb.append(nextEntity(c));
132 * Return the next entity. These entities are translated to Characters:
133 * <code>& ' > < "</code>.
134 * @param ampersand An ampersand character.
135 * @return A Character or an entity String if the entity is not recognized.
136 * @throws JSONException If missing ';' in XML entity.
138 public Object nextEntity(char ampersand) throws JSONException {
139 StringBuilder sb = new StringBuilder();
142 if (Character.isLetterOrDigit(c) || c == '#') {
143 sb.append(Character.toLowerCase(c));
144 } else if (c == ';') {
147 throw syntaxError("Missing ';' in XML entity: &" + sb);
150 String string = sb.toString();
151 return unescapeEntity(string);
155 * Unescapes an XML entity encoding;
156 * @param e entity (only the actual entity value, not the preceding & or ending ;
159 static String unescapeEntity(String e) {
161 if (e == null || e.isEmpty()) {
164 // if our entity is an encoded unicode point, parse it.
165 if (e.charAt(0) == '#') {
167 if (e.charAt(1) == 'x') {
168 // hex encoded unicode
169 cp = Integer.parseInt(e.substring(2), 16);
171 // decimal encoded unicode
172 cp = Integer.parseInt(e.substring(1));
174 return new String(new int[] {cp},0,1);
176 Character knownEntity = entity.get(e);
177 if(knownEntity==null) {
178 // we don't know the entity so keep it encoded
179 return '&' + e + ';';
181 return knownEntity.toString();
186 * Returns the next XML meta token. This is used for skipping over <!...>
187 * and <?...?> structures.
188 * @return Syntax characters (<code>< > / = ! ?</code>) are returned as
189 * Character, and strings and names are returned as Boolean. We don't care
190 * what the values actually are.
191 * @throws JSONException If a string is not properly closed or if the XML
192 * is badly structured.
194 public Object nextMeta() throws JSONException {
199 } while (Character.isWhitespace(c));
202 throw syntaxError("Misshaped meta tag");
221 throw syntaxError("Unterminated string");
230 if (Character.isWhitespace(c)) {
252 * Get the next XML Token. These tokens are found inside of angle
253 * brackets. It may be one of these characters: <code>/ > = ! ?</code> or it
254 * may be a string wrapped in single quotes or double quotes, or it may be a
256 * @return a String or a Character.
257 * @throws JSONException If the XML is not well formed.
259 public Object nextToken() throws JSONException {
265 } while (Character.isWhitespace(c));
268 throw syntaxError("Misshaped element");
270 throw syntaxError("Misplaced '<'");
287 sb = new StringBuilder();
291 throw syntaxError("Unterminated string");
294 return sb.toString();
297 sb.append(nextEntity(c));
306 sb = new StringBuilder();
310 if (Character.isWhitespace(c)) {
311 return sb.toString();
315 return sb.toString();
324 return sb.toString();
328 throw syntaxError("Bad character in a name");
336 * Skip characters until past the requested string.
337 * If it is not found, we are left at the end of the source with a result of false.
338 * @param to A string to skip past.
340 // The Android implementation of JSONTokener has a public method of public void skipPast(String to)
341 // even though ours does not have that method, to have API compatibility, our method in the subclass
343 public void skipPast(String to) {
349 int length = to.length();
350 char[] circle = new char[length];
353 * First fill the circle buffer with as many characters as are in the
354 * to string. If we reach an early end, bail.
357 for (i = 0; i < length; i += 1) {
365 /* We will loop, possibly for all of the remaining characters. */
371 /* Compare the circle buffer with the to string. */
373 for (i = 0; i < length; i += 1) {
374 if (circle[j] != to.charAt(i)) {
384 /* If we exit the loop with b intact, then victory is ours. */
390 /* Get the next character. If there isn't one, then defeat is ours. */
397 * Shove the character in the circle buffer and advance the
398 * circle offset. The offset is mod n.
402 if (offset >= length) {