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 for the
31 * parsing of XML texts.
36 public class XMLTokener extends JSONTokener
40 * The table of entity values. It initially contains Character values for amp,
43 public static final java.util.HashMap<String, Character> entity;
47 entity = new java.util.HashMap<String, Character>(8);
48 entity.put("amp", XML.AMP);
49 entity.put("apos", XML.APOS);
50 entity.put("gt", XML.GT);
51 entity.put("lt", XML.LT);
52 entity.put("quot", XML.QUOT);
56 * Construct an XMLTokener from a Reader.
61 public XMLTokener(Reader r)
67 * Construct an XMLTokener from a string.
72 public XMLTokener(String s)
78 * Get the text in the CDATA block.
80 * @return The string up to the <code>]]></code>.
81 * @throws JSONException
82 * If the <code>]]></code> is not found.
84 public String nextCDATA() throws JSONException
88 StringBuilder sb = new StringBuilder();
94 if (i >= 0 && sb.charAt(i) == ']' && sb.charAt(i + 1) == ']'
95 && sb.charAt(i + 2) == '>')
101 throw syntaxError("Unclosed CDATA");
105 * Get the next XML outer token, trimming whitespace. There are two kinds of
106 * tokens: the '<' character which begins a markup tag, and the content text
107 * between markup tags.
109 * @return A string, or a '<' Character, or null if there is no more source
111 * @throws JSONException
113 public Object nextContent() throws JSONException
120 } while (Character.isWhitespace(c));
129 sb = new StringBuilder();
134 return sb.toString().trim();
139 return sb.toString().trim();
143 sb.append(nextEntity(c));
154 * Return the next entity. These entities are translated to Characters:
155 * <code>& ' > < "</code>.
158 * An ampersand character.
159 * @return A Character or an entity String if the entity is not recognized.
160 * @throws JSONException
161 * If missing ';' in XML entity.
163 public Object nextEntity(char ampersand) throws JSONException
165 StringBuilder sb = new StringBuilder();
169 if (Character.isLetterOrDigit(c) || c == '#')
171 sb.append(Character.toLowerCase(c));
179 throw syntaxError("Missing ';' in XML entity: &" + sb);
182 String string = sb.toString();
183 return unescapeEntity(string);
187 * Unescapes an XML entity encoding;
190 * entity (only the actual entity value, not the preceding & or
194 static String unescapeEntity(String e)
197 if (e == null || e.isEmpty())
201 // if our entity is an encoded unicode point, parse it.
202 if (e.charAt(0) == '#')
205 if (e.charAt(1) == 'x')
207 // hex encoded unicode
208 cp = Integer.parseInt(e.substring(2), 16);
212 // decimal encoded unicode
213 cp = Integer.parseInt(e.substring(1));
215 return new String(new int[] { cp }, 0, 1);
217 Character knownEntity = entity.get(e);
218 if (knownEntity == null)
220 // we don't know the entity so keep it encoded
221 return '&' + e + ';';
223 return knownEntity.toString();
227 * Returns the next XML meta token. This is used for skipping over <!...> and
228 * <?...?> structures.
230 * @return Syntax characters (<code>< > / = ! ?</code>) are returned as
231 * Character, and strings and names are returned as Boolean. We don't
232 * care what the values actually are.
233 * @throws JSONException
234 * If a string is not properly closed or if the XML is badly
237 public Object nextMeta() throws JSONException
244 } while (Character.isWhitespace(c));
248 throw syntaxError("Misshaped meta tag");
269 throw syntaxError("Unterminated string");
280 if (Character.isWhitespace(c))
303 * Get the next XML Token. These tokens are found inside of angle brackets. It
304 * may be one of these characters: <code>/ > = ! ?</code> or it may be a
305 * string wrapped in single quotes or double quotes, or it may be a name.
307 * @return a String or a Character.
308 * @throws JSONException
309 * If the XML is not well formed.
311 public Object nextToken() throws JSONException
319 } while (Character.isWhitespace(c));
323 throw syntaxError("Misshaped element");
325 throw syntaxError("Misplaced '<'");
342 sb = new StringBuilder();
348 throw syntaxError("Unterminated string");
352 return sb.toString();
356 sb.append(nextEntity(c));
367 sb = new StringBuilder();
372 if (Character.isWhitespace(c))
374 return sb.toString();
379 return sb.toString();
388 return sb.toString();
392 throw syntaxError("Bad character in a name");
399 * Skip characters until past the requested string. If it is not found, we are
400 * left at the end of the source with a result of false.
403 * A string to skip past.
405 // The Android implementation of JSONTokener has a public method of public
406 // void skipPast(String to)
407 // even though ours does not have that method, to have API compatibility, our
408 // method in the subclass
410 public void skipPast(String to)
417 int length = to.length();
418 char[] circle = new char[length];
421 * First fill the circle buffer with as many characters as are in the
422 * to string. If we reach an early end, bail.
425 for (i = 0; i < length; i += 1)
435 /* We will loop, possibly for all of the remaining characters. */
442 /* Compare the circle buffer with the to string. */
444 for (i = 0; i < length; i += 1)
446 if (circle[j] != to.charAt(i))
458 /* If we exit the loop with b intact, then victory is ours. */
465 /* Get the next character. If there isn't one, then defeat is ours. */
473 * Shove the character in the circle buffer and advance the
474 * circle offset. The offset is mod n.
478 if (offset >= length)