1 package org
.gaixie
.jibu
.json
;
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
28 * The XMLTokener extends the JSONTokener to provide additional methods
29 * for the parsing of XML texts.
33 public class XMLTokener
extends JSONTokener
{
36 /** The table of entity values. It initially contains Character values for
37 * amp, apos, gt, lt, quot.
39 public static final java
.util
.HashMap entity
;
42 entity
= new java
.util
.HashMap(8);
43 entity
.put("amp", XML
.AMP
);
44 entity
.put("apos", XML
.APOS
);
45 entity
.put("gt", XML
.GT
);
46 entity
.put("lt", XML
.LT
);
47 entity
.put("quot", XML
.QUOT
);
51 * Construct an XMLTokener from a string.
52 * @param s A source string.
54 public XMLTokener(String s
) {
59 * Get the text in the CDATA block.
60 * @return The string up to the <code>]]></code>.
61 * @throws JSONException If the <code>]]></code> is not found.
63 public String
nextCDATA() throws JSONException
{
66 StringBuffer sb
= new StringBuffer();
70 throw syntaxError("Unclosed CDATA");
74 if (i
>= 0 && sb
.charAt(i
) == ']' &&
75 sb
.charAt(i
+ 1) == ']' && sb
.charAt(i
+ 2) == '>') {
84 * Get the next XML outer token, trimming whitespace. There are two kinds
85 * of tokens: the '<' character which begins a markup tag, and the content
86 * text between markup tags.
88 * @return A string, or a '<' Character, or null if there is no more
90 * @throws JSONException
92 public Object
nextContent() throws JSONException
{
97 } while (Character
.isWhitespace(c
));
104 sb
= new StringBuffer();
106 if (c
== '<' || c
== 0) {
108 return sb
.toString().trim();
111 sb
.append(nextEntity(c
));
121 * Return the next entity. These entities are translated to Characters:
122 * <code>& ' > < "</code>.
123 * @param a An ampersand character.
124 * @return A Character or an entity String if the entity is not recognized.
125 * @throws JSONException If missing ';' in XML entity.
127 public Object
nextEntity(char a
) throws JSONException
{
128 StringBuffer sb
= new StringBuffer();
131 if (Character
.isLetterOrDigit(c
) || c
== '#') {
132 sb
.append(Character
.toLowerCase(c
));
133 } else if (c
== ';') {
136 throw syntaxError("Missing ';' in XML entity: &" + sb
);
139 String s
= sb
.toString();
140 Object e
= entity
.get(s
);
141 return e
!= null ? e
: a
+ s
+ ";";
146 * Returns the next XML meta token. This is used for skipping over <!...>
147 * and <?...?> structures.
148 * @return Syntax characters (<code>< > / = ! ?</code>) are returned as
149 * Character, and strings and names are returned as Boolean. We don't care
150 * what the values actually are.
151 * @throws JSONException If a string is not properly closed or if the XML
152 * is badly structured.
154 public Object
nextMeta() throws JSONException
{
159 } while (Character
.isWhitespace(c
));
162 throw syntaxError("Misshaped meta tag");
181 throw syntaxError("Unterminated string");
190 if (Character
.isWhitespace(c
)) {
212 * Get the next XML Token. These tokens are found inside of angle
213 * brackets. It may be one of these characters: <code>/ > = ! ?</code> or it
214 * may be a string wrapped in single quotes or double quotes, or it may be a
216 * @return a String or a Character.
217 * @throws JSONException If the XML is not well formed.
219 public Object
nextToken() throws JSONException
{
225 } while (Character
.isWhitespace(c
));
228 throw syntaxError("Misshaped element");
230 throw syntaxError("Misplaced '<'");
247 sb
= new StringBuffer();
251 throw syntaxError("Unterminated string");
254 return sb
.toString();
257 sb
.append(nextEntity(c
));
266 sb
= new StringBuffer();
270 if (Character
.isWhitespace(c
)) {
271 return sb
.toString();
275 return sb
.toString();
284 return sb
.toString();
288 throw syntaxError("Bad character in a name");
296 * Skip characters until past the requested string.
297 * If it is not found, we are left at the end of the source with a result of false.
298 * @param to A string to skip past.
299 * @throws JSONException
301 public boolean skipPast(String to
) throws JSONException
{
308 char[] circle
= new char[n
];
311 * First fill the circle buffer with as many characters as are in the
312 * to string. If we reach an early end, bail.
315 for (i
= 0; i
< n
; i
+= 1) {
323 * We will loop, possibly for all of the remaining characters.
329 * Compare the circle buffer with the to string.
331 for (i
= 0; i
< n
; i
+= 1) {
332 if (circle
[j
] != to
.charAt(i
)) {
342 * If we exit the loop with b intact, then victory is ours.
348 * Get the next character. If there isn't one, then defeat is ours.
355 * Shove the character in the circle buffer and advance the
356 * circle offset. The offset is mod n.