annotate lib/org.json_2.0/src/org/json/XMLTokener.java @ 0:db87c1b7eb6d

initial
author dwinter
date Wed, 03 Nov 2010 12:18:46 +0100
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
db87c1b7eb6d initial
dwinter
parents:
diff changeset
1 package org.json;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
2
db87c1b7eb6d initial
dwinter
parents:
diff changeset
3 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
4 Copyright (c) 2002 JSON.org
db87c1b7eb6d initial
dwinter
parents:
diff changeset
5
db87c1b7eb6d initial
dwinter
parents:
diff changeset
6 Permission is hereby granted, free of charge, to any person obtaining a copy
db87c1b7eb6d initial
dwinter
parents:
diff changeset
7 of this software and associated documentation files (the "Software"), to deal
db87c1b7eb6d initial
dwinter
parents:
diff changeset
8 in the Software without restriction, including without limitation the rights
db87c1b7eb6d initial
dwinter
parents:
diff changeset
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
db87c1b7eb6d initial
dwinter
parents:
diff changeset
10 copies of the Software, and to permit persons to whom the Software is
db87c1b7eb6d initial
dwinter
parents:
diff changeset
11 furnished to do so, subject to the following conditions:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
12
db87c1b7eb6d initial
dwinter
parents:
diff changeset
13 The above copyright notice and this permission notice shall be included in all
db87c1b7eb6d initial
dwinter
parents:
diff changeset
14 copies or substantial portions of the Software.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
15
db87c1b7eb6d initial
dwinter
parents:
diff changeset
16 The Software shall be used for Good, not Evil.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
17
db87c1b7eb6d initial
dwinter
parents:
diff changeset
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
db87c1b7eb6d initial
dwinter
parents:
diff changeset
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
db87c1b7eb6d initial
dwinter
parents:
diff changeset
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
db87c1b7eb6d initial
dwinter
parents:
diff changeset
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
db87c1b7eb6d initial
dwinter
parents:
diff changeset
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
db87c1b7eb6d initial
dwinter
parents:
diff changeset
24 SOFTWARE.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
25 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
26
db87c1b7eb6d initial
dwinter
parents:
diff changeset
27 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
28 * The XMLTokener extends the JSONTokener to provide additional methods
db87c1b7eb6d initial
dwinter
parents:
diff changeset
29 * for the parsing of XML texts.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
30 * @author JSON.org
db87c1b7eb6d initial
dwinter
parents:
diff changeset
31 * @version 2008-09-18
db87c1b7eb6d initial
dwinter
parents:
diff changeset
32 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
33 public class XMLTokener extends JSONTokener {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
34
db87c1b7eb6d initial
dwinter
parents:
diff changeset
35
db87c1b7eb6d initial
dwinter
parents:
diff changeset
36 /** The table of entity values. It initially contains Character values for
db87c1b7eb6d initial
dwinter
parents:
diff changeset
37 * amp, apos, gt, lt, quot.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
38 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
39 public static final java.util.HashMap entity;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
40
db87c1b7eb6d initial
dwinter
parents:
diff changeset
41 static {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
42 entity = new java.util.HashMap(8);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
43 entity.put("amp", XML.AMP);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
44 entity.put("apos", XML.APOS);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
45 entity.put("gt", XML.GT);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
46 entity.put("lt", XML.LT);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
47 entity.put("quot", XML.QUOT);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
48 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
49
db87c1b7eb6d initial
dwinter
parents:
diff changeset
50 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
51 * Construct an XMLTokener from a string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
52 * @param s A source string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
53 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
54 public XMLTokener(String s) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
55 super(s);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
56 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
57
db87c1b7eb6d initial
dwinter
parents:
diff changeset
58 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
59 * Get the text in the CDATA block.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
60 * @return The string up to the <code>]]&gt;</code>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
61 * @throws JSONException If the <code>]]&gt;</code> is not found.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
62 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
63 public String nextCDATA() throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
64 char c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
65 int i;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
66 StringBuffer sb = new StringBuffer();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
67 for (;;) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
68 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
69 if (c == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
70 throw syntaxError("Unclosed CDATA");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
71 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
72 sb.append(c);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
73 i = sb.length() - 3;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
74 if (i >= 0 && sb.charAt(i) == ']' &&
db87c1b7eb6d initial
dwinter
parents:
diff changeset
75 sb.charAt(i + 1) == ']' && sb.charAt(i + 2) == '>') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
76 sb.setLength(i);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
77 return sb.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
78 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
79 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
80 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
81
db87c1b7eb6d initial
dwinter
parents:
diff changeset
82
db87c1b7eb6d initial
dwinter
parents:
diff changeset
83 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
84 * Get the next XML outer token, trimming whitespace. There are two kinds
db87c1b7eb6d initial
dwinter
parents:
diff changeset
85 * of tokens: the '<' character which begins a markup tag, and the content
db87c1b7eb6d initial
dwinter
parents:
diff changeset
86 * text between markup tags.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
87 *
db87c1b7eb6d initial
dwinter
parents:
diff changeset
88 * @return A string, or a '<' Character, or null if there is no more
db87c1b7eb6d initial
dwinter
parents:
diff changeset
89 * source text.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
90 * @throws JSONException
db87c1b7eb6d initial
dwinter
parents:
diff changeset
91 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
92 public Object nextContent() throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
93 char c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
94 StringBuffer sb;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
95 do {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
96 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
97 } while (Character.isWhitespace(c));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
98 if (c == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
99 return null;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
100 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
101 if (c == '<') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
102 return XML.LT;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
103 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
104 sb = new StringBuffer();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
105 for (;;) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
106 if (c == '<' || c == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
107 back();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
108 return sb.toString().trim();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
109 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
110 if (c == '&') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
111 sb.append(nextEntity(c));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
112 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
113 sb.append(c);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
114 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
115 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
116 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
117 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
118
db87c1b7eb6d initial
dwinter
parents:
diff changeset
119
db87c1b7eb6d initial
dwinter
parents:
diff changeset
120 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
121 * Return the next entity. These entities are translated to Characters:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
122 * <code>&amp; &apos; &gt; &lt; &quot;</code>.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
123 * @param a An ampersand character.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
124 * @return A Character or an entity String if the entity is not recognized.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
125 * @throws JSONException If missing ';' in XML entity.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
126 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
127 public Object nextEntity(char a) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
128 StringBuffer sb = new StringBuffer();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
129 for (;;) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
130 char c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
131 if (Character.isLetterOrDigit(c) || c == '#') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
132 sb.append(Character.toLowerCase(c));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
133 } else if (c == ';') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
134 break;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
135 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
136 throw syntaxError("Missing ';' in XML entity: &" + sb);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
137 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
138 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
139 String s = sb.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
140 Object e = entity.get(s);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
141 return e != null ? e : a + s + ";";
db87c1b7eb6d initial
dwinter
parents:
diff changeset
142 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
143
db87c1b7eb6d initial
dwinter
parents:
diff changeset
144
db87c1b7eb6d initial
dwinter
parents:
diff changeset
145 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
146 * Returns the next XML meta token. This is used for skipping over <!...>
db87c1b7eb6d initial
dwinter
parents:
diff changeset
147 * and <?...?> structures.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
148 * @return Syntax characters (<code>< > / = ! ?</code>) are returned as
db87c1b7eb6d initial
dwinter
parents:
diff changeset
149 * Character, and strings and names are returned as Boolean. We don't care
db87c1b7eb6d initial
dwinter
parents:
diff changeset
150 * what the values actually are.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
151 * @throws JSONException If a string is not properly closed or if the XML
db87c1b7eb6d initial
dwinter
parents:
diff changeset
152 * is badly structured.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
153 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
154 public Object nextMeta() throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
155 char c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
156 char q;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
157 do {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
158 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
159 } while (Character.isWhitespace(c));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
160 switch (c) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
161 case 0:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
162 throw syntaxError("Misshaped meta tag");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
163 case '<':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
164 return XML.LT;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
165 case '>':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
166 return XML.GT;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
167 case '/':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
168 return XML.SLASH;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
169 case '=':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
170 return XML.EQ;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
171 case '!':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
172 return XML.BANG;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
173 case '?':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
174 return XML.QUEST;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
175 case '"':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
176 case '\'':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
177 q = c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
178 for (;;) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
179 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
180 if (c == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
181 throw syntaxError("Unterminated string");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
182 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
183 if (c == q) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
184 return Boolean.TRUE;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
185 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
186 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
187 default:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
188 for (;;) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
189 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
190 if (Character.isWhitespace(c)) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
191 return Boolean.TRUE;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
192 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
193 switch (c) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
194 case 0:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
195 case '<':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
196 case '>':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
197 case '/':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
198 case '=':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
199 case '!':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
200 case '?':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
201 case '"':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
202 case '\'':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
203 back();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
204 return Boolean.TRUE;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
205 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
206 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
207 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
208 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
209
db87c1b7eb6d initial
dwinter
parents:
diff changeset
210
db87c1b7eb6d initial
dwinter
parents:
diff changeset
211 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
212 * Get the next XML Token. These tokens are found inside of angle
db87c1b7eb6d initial
dwinter
parents:
diff changeset
213 * brackets. It may be one of these characters: <code>/ > = ! ?</code> or it
db87c1b7eb6d initial
dwinter
parents:
diff changeset
214 * may be a string wrapped in single quotes or double quotes, or it may be a
db87c1b7eb6d initial
dwinter
parents:
diff changeset
215 * name.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
216 * @return a String or a Character.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
217 * @throws JSONException If the XML is not well formed.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
218 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
219 public Object nextToken() throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
220 char c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
221 char q;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
222 StringBuffer sb;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
223 do {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
224 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
225 } while (Character.isWhitespace(c));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
226 switch (c) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
227 case 0:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
228 throw syntaxError("Misshaped element");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
229 case '<':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
230 throw syntaxError("Misplaced '<'");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
231 case '>':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
232 return XML.GT;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
233 case '/':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
234 return XML.SLASH;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
235 case '=':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
236 return XML.EQ;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
237 case '!':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
238 return XML.BANG;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
239 case '?':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
240 return XML.QUEST;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
241
db87c1b7eb6d initial
dwinter
parents:
diff changeset
242 // Quoted string
db87c1b7eb6d initial
dwinter
parents:
diff changeset
243
db87c1b7eb6d initial
dwinter
parents:
diff changeset
244 case '"':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
245 case '\'':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
246 q = c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
247 sb = new StringBuffer();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
248 for (;;) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
249 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
250 if (c == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
251 throw syntaxError("Unterminated string");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
252 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
253 if (c == q) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
254 return sb.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
255 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
256 if (c == '&') {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
257 sb.append(nextEntity(c));
db87c1b7eb6d initial
dwinter
parents:
diff changeset
258 } else {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
259 sb.append(c);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
260 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
261 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
262 default:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
263
db87c1b7eb6d initial
dwinter
parents:
diff changeset
264 // Name
db87c1b7eb6d initial
dwinter
parents:
diff changeset
265
db87c1b7eb6d initial
dwinter
parents:
diff changeset
266 sb = new StringBuffer();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
267 for (;;) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
268 sb.append(c);
db87c1b7eb6d initial
dwinter
parents:
diff changeset
269 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
270 if (Character.isWhitespace(c)) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
271 return sb.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
272 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
273 switch (c) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
274 case 0:
db87c1b7eb6d initial
dwinter
parents:
diff changeset
275 return sb.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
276 case '>':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
277 case '/':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
278 case '=':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
279 case '!':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
280 case '?':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
281 case '[':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
282 case ']':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
283 back();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
284 return sb.toString();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
285 case '<':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
286 case '"':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
287 case '\'':
db87c1b7eb6d initial
dwinter
parents:
diff changeset
288 throw syntaxError("Bad character in a name");
db87c1b7eb6d initial
dwinter
parents:
diff changeset
289 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
290 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
291 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
292 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
293
db87c1b7eb6d initial
dwinter
parents:
diff changeset
294
db87c1b7eb6d initial
dwinter
parents:
diff changeset
295 /**
db87c1b7eb6d initial
dwinter
parents:
diff changeset
296 * Skip characters until past the requested string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
297 * If it is not found, we are left at the end of the source with a result of false.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
298 * @param to A string to skip past.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
299 * @throws JSONException
db87c1b7eb6d initial
dwinter
parents:
diff changeset
300 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
301 public boolean skipPast(String to) throws JSONException {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
302 boolean b;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
303 char c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
304 int i;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
305 int j;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
306 int offset = 0;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
307 int n = to.length();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
308 char[] circle = new char[n];
db87c1b7eb6d initial
dwinter
parents:
diff changeset
309
db87c1b7eb6d initial
dwinter
parents:
diff changeset
310 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
311 * First fill the circle buffer with as many characters as are in the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
312 * to string. If we reach an early end, bail.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
313 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
314
db87c1b7eb6d initial
dwinter
parents:
diff changeset
315 for (i = 0; i < n; i += 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
316 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
317 if (c == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
318 return false;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
319 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
320 circle[i] = c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
321 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
322 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
323 * We will loop, possibly for all of the remaining characters.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
324 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
325 for (;;) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
326 j = offset;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
327 b = true;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
328 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
329 * Compare the circle buffer with the to string.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
330 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
331 for (i = 0; i < n; i += 1) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
332 if (circle[j] != to.charAt(i)) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
333 b = false;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
334 break;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
335 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
336 j += 1;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
337 if (j >= n) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
338 j -= n;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
339 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
340 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
341 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
342 * If we exit the loop with b intact, then victory is ours.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
343 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
344 if (b) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
345 return true;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
346 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
347 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
348 * Get the next character. If there isn't one, then defeat is ours.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
349 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
350 c = next();
db87c1b7eb6d initial
dwinter
parents:
diff changeset
351 if (c == 0) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
352 return false;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
353 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
354 /*
db87c1b7eb6d initial
dwinter
parents:
diff changeset
355 * Shove the character in the circle buffer and advance the
db87c1b7eb6d initial
dwinter
parents:
diff changeset
356 * circle offset. The offset is mod n.
db87c1b7eb6d initial
dwinter
parents:
diff changeset
357 */
db87c1b7eb6d initial
dwinter
parents:
diff changeset
358 circle[offset] = c;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
359 offset += 1;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
360 if (offset >= n) {
db87c1b7eb6d initial
dwinter
parents:
diff changeset
361 offset -= n;
db87c1b7eb6d initial
dwinter
parents:
diff changeset
362 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
363 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
364 }
db87c1b7eb6d initial
dwinter
parents:
diff changeset
365 }