1 |
|
package org.json; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
import java.io.Reader; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
@author |
34 |
|
@version |
35 |
|
|
|
|
| 0% |
Uncovered Elements: 264 (264) |
Complexity: 84 |
Complexity Density: 0.44 |
|
36 |
|
public class XMLTokener extends JSONTokener |
37 |
|
{ |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
public static final java.util.HashMap<String, Character> entity; |
44 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
45 |
0 |
static... |
46 |
|
{ |
47 |
0 |
entity = new java.util.HashMap<String, Character>(8); |
48 |
0 |
entity.put("amp", XML.AMP); |
49 |
0 |
entity.put("apos", XML.APOS); |
50 |
0 |
entity.put("gt", XML.GT); |
51 |
0 |
entity.put("lt", XML.LT); |
52 |
0 |
entity.put("quot", XML.QUOT); |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@param |
59 |
|
|
60 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
61 |
0 |
public XMLTokener(Reader r)... |
62 |
|
{ |
63 |
0 |
super(r); |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@param |
70 |
|
|
71 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
72 |
0 |
public XMLTokener(String s)... |
73 |
|
{ |
74 |
0 |
super(s); |
75 |
|
} |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@return |
81 |
|
@throws |
82 |
|
|
83 |
|
|
|
|
| 0% |
Uncovered Elements: 15 (15) |
Complexity: 6 |
Complexity Density: 0.55 |
|
84 |
0 |
public String nextCDATA() throws JSONException... |
85 |
|
{ |
86 |
0 |
char c; |
87 |
0 |
int i; |
88 |
0 |
StringBuilder sb = new StringBuilder(); |
89 |
0 |
while (more()) |
90 |
|
{ |
91 |
0 |
c = next(); |
92 |
0 |
sb.append(c); |
93 |
0 |
i = sb.length() - 3; |
94 |
0 |
if (i >= 0 && sb.charAt(i) == ']' && sb.charAt(i + 1) == ']' |
95 |
|
&& sb.charAt(i + 2) == '>') |
96 |
|
{ |
97 |
0 |
sb.setLength(i); |
98 |
0 |
return sb.toString(); |
99 |
|
} |
100 |
|
} |
101 |
0 |
throw syntaxError("Unclosed CDATA"); |
102 |
|
} |
103 |
|
|
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
@return |
110 |
|
|
111 |
|
@throws |
112 |
|
|
|
|
| 0% |
Uncovered Elements: 31 (31) |
Complexity: 7 |
Complexity Density: 0.37 |
|
113 |
0 |
public Object nextContent() throws JSONException... |
114 |
|
{ |
115 |
0 |
char c; |
116 |
0 |
StringBuilder sb; |
117 |
0 |
do |
118 |
|
{ |
119 |
0 |
c = next(); |
120 |
0 |
} while (Character.isWhitespace(c)); |
121 |
0 |
if (c == 0) |
122 |
|
{ |
123 |
0 |
return null; |
124 |
|
} |
125 |
0 |
if (c == '<') |
126 |
|
{ |
127 |
0 |
return XML.LT; |
128 |
|
} |
129 |
0 |
sb = new StringBuilder(); |
130 |
0 |
for (;;) |
131 |
|
{ |
132 |
0 |
if (c == 0) |
133 |
|
{ |
134 |
0 |
return sb.toString().trim(); |
135 |
|
} |
136 |
0 |
if (c == '<') |
137 |
|
{ |
138 |
0 |
back(); |
139 |
0 |
return sb.toString().trim(); |
140 |
|
} |
141 |
0 |
if (c == '&') |
142 |
|
{ |
143 |
0 |
sb.append(nextEntity(c)); |
144 |
|
} |
145 |
|
else |
146 |
|
{ |
147 |
0 |
sb.append(c); |
148 |
|
} |
149 |
0 |
c = next(); |
150 |
|
} |
151 |
|
} |
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
@param |
158 |
|
|
159 |
|
@return |
160 |
|
@throws |
161 |
|
|
162 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
163 |
0 |
public Object nextEntity(char ampersand) throws JSONException... |
164 |
|
{ |
165 |
0 |
StringBuilder sb = new StringBuilder(); |
166 |
0 |
for (;;) |
167 |
|
{ |
168 |
0 |
char c = next(); |
169 |
0 |
if (Character.isLetterOrDigit(c) || c == '#') |
170 |
|
{ |
171 |
0 |
sb.append(Character.toLowerCase(c)); |
172 |
|
} |
173 |
0 |
else if (c == ';') |
174 |
|
{ |
175 |
0 |
break; |
176 |
|
} |
177 |
|
else |
178 |
|
{ |
179 |
0 |
throw syntaxError("Missing ';' in XML entity: &" + sb); |
180 |
|
} |
181 |
|
} |
182 |
0 |
String string = sb.toString(); |
183 |
0 |
return unescapeEntity(string); |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
@param |
190 |
|
|
191 |
|
|
192 |
|
@return |
193 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 6 |
Complexity Density: 0.5 |
|
194 |
0 |
static String unescapeEntity(String e)... |
195 |
|
{ |
196 |
|
|
197 |
0 |
if (e == null || e.isEmpty()) |
198 |
|
{ |
199 |
0 |
return ""; |
200 |
|
} |
201 |
|
|
202 |
0 |
if (e.charAt(0) == '#') |
203 |
|
{ |
204 |
0 |
int cp; |
205 |
0 |
if (e.charAt(1) == 'x') |
206 |
|
{ |
207 |
|
|
208 |
0 |
cp = Integer.parseInt(e.substring(2), 16); |
209 |
|
} |
210 |
|
else |
211 |
|
{ |
212 |
|
|
213 |
0 |
cp = Integer.parseInt(e.substring(1)); |
214 |
|
} |
215 |
0 |
return new String(new int[] { cp }, 0, 1); |
216 |
|
} |
217 |
0 |
Character knownEntity = entity.get(e); |
218 |
0 |
if (knownEntity == null) |
219 |
|
{ |
220 |
|
|
221 |
0 |
return '&' + e + ';'; |
222 |
|
} |
223 |
0 |
return knownEntity.toString(); |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
@return |
231 |
|
|
232 |
|
|
233 |
|
@throws |
234 |
|
|
235 |
|
|
236 |
|
|
|
|
| 0% |
Uncovered Elements: 53 (53) |
Complexity: 23 |
Complexity Density: 0.51 |
|
237 |
0 |
public Object nextMeta() throws JSONException... |
238 |
|
{ |
239 |
0 |
char c; |
240 |
0 |
char q; |
241 |
0 |
do |
242 |
|
{ |
243 |
0 |
c = next(); |
244 |
0 |
} while (Character.isWhitespace(c)); |
245 |
0 |
switch (c) |
246 |
|
{ |
247 |
0 |
case 0: |
248 |
0 |
throw syntaxError("Misshaped meta tag"); |
249 |
0 |
case '<': |
250 |
0 |
return XML.LT; |
251 |
0 |
case '>': |
252 |
0 |
return XML.GT; |
253 |
0 |
case '/': |
254 |
0 |
return XML.SLASH; |
255 |
0 |
case '=': |
256 |
0 |
return XML.EQ; |
257 |
0 |
case '!': |
258 |
0 |
return XML.BANG; |
259 |
0 |
case '?': |
260 |
0 |
return XML.QUEST; |
261 |
0 |
case '"': |
262 |
0 |
case '\'': |
263 |
0 |
q = c; |
264 |
0 |
for (;;) |
265 |
|
{ |
266 |
0 |
c = next(); |
267 |
0 |
if (c == 0) |
268 |
|
{ |
269 |
0 |
throw syntaxError("Unterminated string"); |
270 |
|
} |
271 |
0 |
if (c == q) |
272 |
|
{ |
273 |
0 |
return Boolean.TRUE; |
274 |
|
} |
275 |
|
} |
276 |
0 |
default: |
277 |
0 |
for (;;) |
278 |
|
{ |
279 |
0 |
c = next(); |
280 |
0 |
if (Character.isWhitespace(c)) |
281 |
|
{ |
282 |
0 |
return Boolean.TRUE; |
283 |
|
} |
284 |
0 |
switch (c) |
285 |
|
{ |
286 |
0 |
case 0: |
287 |
0 |
case '<': |
288 |
0 |
case '>': |
289 |
0 |
case '/': |
290 |
0 |
case '=': |
291 |
0 |
case '!': |
292 |
0 |
case '?': |
293 |
0 |
case '"': |
294 |
0 |
case '\'': |
295 |
0 |
back(); |
296 |
0 |
return Boolean.TRUE; |
297 |
|
} |
298 |
|
} |
299 |
|
} |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
@return |
308 |
|
@throws |
309 |
|
|
310 |
|
|
|
|
| 0% |
Uncovered Elements: 66 (66) |
Complexity: 26 |
Complexity Density: 0.46 |
|
311 |
0 |
public Object nextToken() throws JSONException... |
312 |
|
{ |
313 |
0 |
char c; |
314 |
0 |
char q; |
315 |
0 |
StringBuilder sb; |
316 |
0 |
do |
317 |
|
{ |
318 |
0 |
c = next(); |
319 |
0 |
} while (Character.isWhitespace(c)); |
320 |
0 |
switch (c) |
321 |
|
{ |
322 |
0 |
case 0: |
323 |
0 |
throw syntaxError("Misshaped element"); |
324 |
0 |
case '<': |
325 |
0 |
throw syntaxError("Misplaced '<'"); |
326 |
0 |
case '>': |
327 |
0 |
return XML.GT; |
328 |
0 |
case '/': |
329 |
0 |
return XML.SLASH; |
330 |
0 |
case '=': |
331 |
0 |
return XML.EQ; |
332 |
0 |
case '!': |
333 |
0 |
return XML.BANG; |
334 |
0 |
case '?': |
335 |
0 |
return XML.QUEST; |
336 |
|
|
337 |
|
|
338 |
|
|
339 |
0 |
case '"': |
340 |
0 |
case '\'': |
341 |
0 |
q = c; |
342 |
0 |
sb = new StringBuilder(); |
343 |
0 |
for (;;) |
344 |
|
{ |
345 |
0 |
c = next(); |
346 |
0 |
if (c == 0) |
347 |
|
{ |
348 |
0 |
throw syntaxError("Unterminated string"); |
349 |
|
} |
350 |
0 |
if (c == q) |
351 |
|
{ |
352 |
0 |
return sb.toString(); |
353 |
|
} |
354 |
0 |
if (c == '&') |
355 |
|
{ |
356 |
0 |
sb.append(nextEntity(c)); |
357 |
|
} |
358 |
|
else |
359 |
|
{ |
360 |
0 |
sb.append(c); |
361 |
|
} |
362 |
|
} |
363 |
0 |
default: |
364 |
|
|
365 |
|
|
366 |
|
|
367 |
0 |
sb = new StringBuilder(); |
368 |
0 |
for (;;) |
369 |
|
{ |
370 |
0 |
sb.append(c); |
371 |
0 |
c = next(); |
372 |
0 |
if (Character.isWhitespace(c)) |
373 |
|
{ |
374 |
0 |
return sb.toString(); |
375 |
|
} |
376 |
0 |
switch (c) |
377 |
|
{ |
378 |
0 |
case 0: |
379 |
0 |
return sb.toString(); |
380 |
0 |
case '>': |
381 |
0 |
case '/': |
382 |
0 |
case '=': |
383 |
0 |
case '!': |
384 |
0 |
case '?': |
385 |
0 |
case '[': |
386 |
0 |
case ']': |
387 |
0 |
back(); |
388 |
0 |
return sb.toString(); |
389 |
0 |
case '<': |
390 |
0 |
case '"': |
391 |
0 |
case '\'': |
392 |
0 |
throw syntaxError("Bad character in a name"); |
393 |
|
} |
394 |
|
} |
395 |
|
} |
396 |
|
} |
397 |
|
|
398 |
|
|
399 |
|
|
400 |
|
|
401 |
|
|
402 |
|
@param |
403 |
|
|
404 |
|
|
405 |
|
|
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
|
|
|
| 0% |
Uncovered Elements: 47 (47) |
Complexity: 9 |
Complexity Density: 0.29 |
|
410 |
0 |
public void skipPast(String to)... |
411 |
|
{ |
412 |
0 |
boolean b; |
413 |
0 |
char c; |
414 |
0 |
int i; |
415 |
0 |
int j; |
416 |
0 |
int offset = 0; |
417 |
0 |
int length = to.length(); |
418 |
0 |
char[] circle = new char[length]; |
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
|
425 |
0 |
for (i = 0; i < length; i += 1) |
426 |
|
{ |
427 |
0 |
c = next(); |
428 |
0 |
if (c == 0) |
429 |
|
{ |
430 |
0 |
return; |
431 |
|
} |
432 |
0 |
circle[i] = c; |
433 |
|
} |
434 |
|
|
435 |
|
|
436 |
|
|
437 |
0 |
for (;;) |
438 |
|
{ |
439 |
0 |
j = offset; |
440 |
0 |
b = true; |
441 |
|
|
442 |
|
|
443 |
|
|
444 |
0 |
for (i = 0; i < length; i += 1) |
445 |
|
{ |
446 |
0 |
if (circle[j] != to.charAt(i)) |
447 |
|
{ |
448 |
0 |
b = false; |
449 |
0 |
break; |
450 |
|
} |
451 |
0 |
j += 1; |
452 |
0 |
if (j >= length) |
453 |
|
{ |
454 |
0 |
j -= length; |
455 |
|
} |
456 |
|
} |
457 |
|
|
458 |
|
|
459 |
|
|
460 |
0 |
if (b) |
461 |
|
{ |
462 |
0 |
return; |
463 |
|
} |
464 |
|
|
465 |
|
|
466 |
|
|
467 |
0 |
c = next(); |
468 |
0 |
if (c == 0) |
469 |
|
{ |
470 |
0 |
return; |
471 |
|
} |
472 |
|
|
473 |
|
|
474 |
|
|
475 |
|
|
476 |
0 |
circle[offset] = c; |
477 |
0 |
offset += 1; |
478 |
0 |
if (offset >= length) |
479 |
|
{ |
480 |
0 |
offset -= length; |
481 |
|
} |
482 |
|
} |
483 |
|
} |
484 |
|
} |