1 |
|
package org.json; |
2 |
|
|
3 |
|
import java.io.BufferedReader; |
4 |
|
import java.io.IOException; |
5 |
|
import java.io.InputStream; |
6 |
|
import java.io.InputStreamReader; |
7 |
|
import java.io.Reader; |
8 |
|
import java.io.StringReader; |
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
@author |
40 |
|
@version |
41 |
|
|
|
|
| 0% |
Uncovered Elements: 271 (271) |
Complexity: 87 |
Complexity Density: 0.46 |
|
42 |
|
public class JSONTokener |
43 |
|
{ |
44 |
|
|
45 |
|
private long character; |
46 |
|
|
47 |
|
|
48 |
|
private boolean eof; |
49 |
|
|
50 |
|
|
51 |
|
private long index; |
52 |
|
|
53 |
|
|
54 |
|
private long line; |
55 |
|
|
56 |
|
|
57 |
|
private char previous; |
58 |
|
|
59 |
|
|
60 |
|
private final Reader reader; |
61 |
|
|
62 |
|
|
63 |
|
private boolean usePrevious; |
64 |
|
|
65 |
|
|
66 |
|
private long characterPreviousLine; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
@param |
72 |
|
|
73 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 2 |
Complexity Density: 0.25 |
|
74 |
0 |
public JSONTokener(Reader reader)... |
75 |
|
{ |
76 |
0 |
this.reader = reader.markSupported() ? reader |
77 |
|
: new BufferedReader(reader); |
78 |
0 |
this.eof = false; |
79 |
0 |
this.usePrevious = false; |
80 |
0 |
this.previous = 0; |
81 |
0 |
this.index = 0; |
82 |
0 |
this.character = 1; |
83 |
0 |
this.characterPreviousLine = 0; |
84 |
0 |
this.line = 1; |
85 |
|
} |
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
@param |
92 |
|
|
93 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
94 |
0 |
public JSONTokener(InputStream inputStream)... |
95 |
|
{ |
96 |
0 |
this(new InputStreamReader(inputStream)); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
@param |
103 |
|
|
104 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
0 |
public JSONTokener(String s)... |
106 |
|
{ |
107 |
0 |
this(new StringReader(s)); |
108 |
|
} |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
@throws |
116 |
|
|
117 |
|
|
118 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 3 |
Complexity Density: 0.6 |
|
119 |
0 |
public void back() throws JSONException... |
120 |
|
{ |
121 |
0 |
if (this.usePrevious || this.index <= 0) |
122 |
|
{ |
123 |
0 |
throw new JSONException("Stepping back two steps is not supported"); |
124 |
|
} |
125 |
0 |
this.decrementIndexes(); |
126 |
0 |
this.usePrevious = true; |
127 |
0 |
this.eof = false; |
128 |
|
} |
129 |
|
|
130 |
|
|
131 |
|
@link |
132 |
|
|
133 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 4 |
Complexity Density: 0.67 |
|
134 |
0 |
private void decrementIndexes()... |
135 |
|
{ |
136 |
0 |
this.index--; |
137 |
0 |
if (this.previous == '\r' || this.previous == '\n') |
138 |
|
{ |
139 |
0 |
this.line--; |
140 |
0 |
this.character = this.characterPreviousLine; |
141 |
|
} |
142 |
0 |
else if (this.character > 0) |
143 |
|
{ |
144 |
0 |
this.character--; |
145 |
|
} |
146 |
|
} |
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
@param |
152 |
|
|
153 |
|
|
154 |
|
@return |
155 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 7 |
Complexity Density: 1 |
|
156 |
0 |
public static int dehexchar(char c)... |
157 |
|
{ |
158 |
0 |
if (c >= '0' && c <= '9') |
159 |
|
{ |
160 |
0 |
return c - '0'; |
161 |
|
} |
162 |
0 |
if (c >= 'A' && c <= 'F') |
163 |
|
{ |
164 |
0 |
return c - ('A' - 10); |
165 |
|
} |
166 |
0 |
if (c >= 'a' && c <= 'f') |
167 |
|
{ |
168 |
0 |
return c - ('a' - 10); |
169 |
|
} |
170 |
0 |
return -1; |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
|
175 |
|
|
176 |
|
@return |
177 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
178 |
0 |
public boolean end()... |
179 |
|
{ |
180 |
0 |
return this.eof && !this.usePrevious; |
181 |
|
} |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
@return |
188 |
|
@throws |
189 |
|
|
190 |
|
|
191 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0.42 |
|
192 |
0 |
public boolean more() throws JSONException... |
193 |
|
{ |
194 |
0 |
if (this.usePrevious) |
195 |
|
{ |
196 |
0 |
return true; |
197 |
|
} |
198 |
0 |
try |
199 |
|
{ |
200 |
0 |
this.reader.mark(1); |
201 |
|
} catch (IOException e) |
202 |
|
{ |
203 |
0 |
throw new JSONException("Unable to preserve stream position", e); |
204 |
|
} |
205 |
0 |
try |
206 |
|
{ |
207 |
|
|
208 |
0 |
if (this.reader.read() <= 0) |
209 |
|
{ |
210 |
0 |
this.eof = true; |
211 |
0 |
return false; |
212 |
|
} |
213 |
0 |
this.reader.reset(); |
214 |
|
} catch (IOException e) |
215 |
|
{ |
216 |
0 |
throw new JSONException( |
217 |
|
"Unable to read the next character from the stream", e); |
218 |
|
} |
219 |
0 |
return true; |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
@return |
226 |
|
@throws |
227 |
|
|
228 |
|
|
|
|
| 0% |
Uncovered Elements: 17 (17) |
Complexity: 4 |
Complexity Density: 0.31 |
|
229 |
0 |
public char next() throws JSONException... |
230 |
|
{ |
231 |
0 |
int c; |
232 |
0 |
if (this.usePrevious) |
233 |
|
{ |
234 |
0 |
this.usePrevious = false; |
235 |
0 |
c = this.previous; |
236 |
|
} |
237 |
|
else |
238 |
|
{ |
239 |
0 |
try |
240 |
|
{ |
241 |
0 |
c = this.reader.read(); |
242 |
|
} catch (IOException exception) |
243 |
|
{ |
244 |
0 |
throw new JSONException(exception); |
245 |
|
} |
246 |
|
} |
247 |
0 |
if (c <= 0) |
248 |
|
{ |
249 |
0 |
this.eof = true; |
250 |
0 |
return 0; |
251 |
|
} |
252 |
0 |
this.incrementIndexes(c); |
253 |
0 |
this.previous = (char) c; |
254 |
0 |
return this.previous; |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
@param |
262 |
|
|
263 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 5 |
Complexity Density: 0.42 |
|
264 |
0 |
private void incrementIndexes(int c)... |
265 |
|
{ |
266 |
0 |
if (c > 0) |
267 |
|
{ |
268 |
0 |
this.index++; |
269 |
0 |
if (c == '\r') |
270 |
|
{ |
271 |
0 |
this.line++; |
272 |
0 |
this.characterPreviousLine = this.character; |
273 |
0 |
this.character = 0; |
274 |
|
} |
275 |
0 |
else if (c == '\n') |
276 |
|
{ |
277 |
0 |
if (this.previous != '\r') |
278 |
|
{ |
279 |
0 |
this.line++; |
280 |
0 |
this.characterPreviousLine = this.character; |
281 |
|
} |
282 |
0 |
this.character = 0; |
283 |
|
} |
284 |
|
else |
285 |
|
{ |
286 |
0 |
this.character++; |
287 |
|
} |
288 |
|
} |
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
|
295 |
|
@param |
296 |
|
|
297 |
|
@return |
298 |
|
@throws |
299 |
|
|
300 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
301 |
0 |
public char next(char c) throws JSONException... |
302 |
|
{ |
303 |
0 |
char n = this.next(); |
304 |
0 |
if (n != c) |
305 |
|
{ |
306 |
0 |
if (n > 0) |
307 |
|
{ |
308 |
0 |
throw this.syntaxError( |
309 |
|
"Expected '" + c + "' and instead saw '" + n + "'"); |
310 |
|
} |
311 |
0 |
throw this.syntaxError("Expected '" + c + "' and instead saw ''"); |
312 |
|
} |
313 |
0 |
return n; |
314 |
|
} |
315 |
|
|
316 |
|
|
317 |
|
|
318 |
|
|
319 |
|
@param |
320 |
|
|
321 |
|
@return |
322 |
|
@throws |
323 |
|
|
324 |
|
|
325 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 4 |
Complexity Density: 0.4 |
|
326 |
0 |
public String next(int n) throws JSONException... |
327 |
|
{ |
328 |
0 |
if (n == 0) |
329 |
|
{ |
330 |
0 |
return ""; |
331 |
|
} |
332 |
|
|
333 |
0 |
char[] chars = new char[n]; |
334 |
0 |
int pos = 0; |
335 |
|
|
336 |
0 |
while (pos < n) |
337 |
|
{ |
338 |
0 |
chars[pos] = this.next(); |
339 |
0 |
if (this.end()) |
340 |
|
{ |
341 |
0 |
throw this.syntaxError("Substring bounds error"); |
342 |
|
} |
343 |
0 |
pos += 1; |
344 |
|
} |
345 |
0 |
return new String(chars); |
346 |
|
} |
347 |
|
|
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
@throws |
352 |
|
|
353 |
|
@return |
354 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 3 |
Complexity Density: 0.75 |
|
355 |
0 |
public char nextClean() throws JSONException... |
356 |
|
{ |
357 |
0 |
for (;;) |
358 |
|
{ |
359 |
0 |
char c = this.next(); |
360 |
0 |
if (c == 0 || c > ' ') |
361 |
|
{ |
362 |
0 |
return c; |
363 |
|
} |
364 |
|
} |
365 |
|
} |
366 |
|
|
367 |
|
|
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
@param |
373 |
|
|
374 |
|
|
375 |
|
|
376 |
|
@return |
377 |
|
@throws |
378 |
|
|
379 |
|
|
|
|
| 0% |
Uncovered Elements: 47 (47) |
Complexity: 17 |
Complexity Density: 0.38 |
|
380 |
0 |
public String nextString(char quote) throws JSONException... |
381 |
|
{ |
382 |
0 |
char c; |
383 |
0 |
StringBuilder sb = new StringBuilder(); |
384 |
0 |
for (;;) |
385 |
|
{ |
386 |
0 |
c = this.next(); |
387 |
0 |
switch (c) |
388 |
|
{ |
389 |
0 |
case 0: |
390 |
0 |
case '\n': |
391 |
0 |
case '\r': |
392 |
0 |
throw this.syntaxError("Unterminated string"); |
393 |
0 |
case '\\': |
394 |
0 |
c = this.next(); |
395 |
0 |
switch (c) |
396 |
|
{ |
397 |
0 |
case 'b': |
398 |
0 |
sb.append('\b'); |
399 |
0 |
break; |
400 |
0 |
case 't': |
401 |
0 |
sb.append('\t'); |
402 |
0 |
break; |
403 |
0 |
case 'n': |
404 |
0 |
sb.append('\n'); |
405 |
0 |
break; |
406 |
0 |
case 'f': |
407 |
0 |
sb.append('\f'); |
408 |
0 |
break; |
409 |
0 |
case 'r': |
410 |
0 |
sb.append('\r'); |
411 |
0 |
break; |
412 |
0 |
case 'u': |
413 |
0 |
try |
414 |
|
{ |
415 |
0 |
sb.append((char) Integer.parseInt(this.next(4), 16)); |
416 |
|
} catch (NumberFormatException e) |
417 |
|
{ |
418 |
0 |
throw this.syntaxError("Illegal escape.", e); |
419 |
|
} |
420 |
0 |
break; |
421 |
0 |
case '"': |
422 |
0 |
case '\'': |
423 |
0 |
case '\\': |
424 |
0 |
case '/': |
425 |
0 |
sb.append(c); |
426 |
0 |
break; |
427 |
0 |
default: |
428 |
0 |
throw this.syntaxError("Illegal escape."); |
429 |
|
} |
430 |
0 |
break; |
431 |
0 |
default: |
432 |
0 |
if (c == quote) |
433 |
|
{ |
434 |
0 |
return sb.toString(); |
435 |
|
} |
436 |
0 |
sb.append(c); |
437 |
|
} |
438 |
|
} |
439 |
|
} |
440 |
|
|
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
@param |
446 |
|
|
447 |
|
@return |
448 |
|
@throws |
449 |
|
|
450 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 6 |
Complexity Density: 0.75 |
|
451 |
0 |
public String nextTo(char delimiter) throws JSONException... |
452 |
|
{ |
453 |
0 |
StringBuilder sb = new StringBuilder(); |
454 |
0 |
for (;;) |
455 |
|
{ |
456 |
0 |
char c = this.next(); |
457 |
0 |
if (c == delimiter || c == 0 || c == '\n' || c == '\r') |
458 |
|
{ |
459 |
0 |
if (c != 0) |
460 |
|
{ |
461 |
0 |
this.back(); |
462 |
|
} |
463 |
0 |
return sb.toString().trim(); |
464 |
|
} |
465 |
0 |
sb.append(c); |
466 |
|
} |
467 |
|
} |
468 |
|
|
469 |
|
|
470 |
|
|
471 |
|
|
472 |
|
|
473 |
|
@param |
474 |
|
|
475 |
|
@return |
476 |
|
@throws |
477 |
|
|
478 |
|
|
|
|
| 0% |
Uncovered Elements: 13 (13) |
Complexity: 6 |
Complexity Density: 0.67 |
|
479 |
0 |
public String nextTo(String delimiters) throws JSONException... |
480 |
|
{ |
481 |
0 |
char c; |
482 |
0 |
StringBuilder sb = new StringBuilder(); |
483 |
0 |
for (;;) |
484 |
|
{ |
485 |
0 |
c = this.next(); |
486 |
0 |
if (delimiters.indexOf(c) >= 0 || c == 0 || c == '\n' || c == '\r') |
487 |
|
{ |
488 |
0 |
if (c != 0) |
489 |
|
{ |
490 |
0 |
this.back(); |
491 |
|
} |
492 |
0 |
return sb.toString().trim(); |
493 |
|
} |
494 |
0 |
sb.append(c); |
495 |
|
} |
496 |
|
} |
497 |
|
|
498 |
|
|
499 |
|
|
500 |
|
|
501 |
|
|
502 |
|
@throws |
503 |
|
|
504 |
|
|
505 |
|
@return |
506 |
|
|
|
|
| 0% |
Uncovered Elements: 25 (25) |
Complexity: 8 |
Complexity Density: 0.38 |
|
507 |
0 |
public Object nextValue() throws JSONException... |
508 |
|
{ |
509 |
0 |
char c = this.nextClean(); |
510 |
0 |
String string; |
511 |
|
|
512 |
0 |
switch (c) |
513 |
|
{ |
514 |
0 |
case '"': |
515 |
0 |
case '\'': |
516 |
0 |
return this.nextString(c); |
517 |
0 |
case '{': |
518 |
0 |
this.back(); |
519 |
0 |
return new JSONObject(this); |
520 |
0 |
case '[': |
521 |
0 |
this.back(); |
522 |
0 |
return new JSONArray(this); |
523 |
|
} |
524 |
|
|
525 |
|
|
526 |
|
|
527 |
|
|
528 |
|
|
529 |
|
|
530 |
|
|
531 |
|
|
532 |
|
|
533 |
|
|
534 |
0 |
StringBuilder sb = new StringBuilder(); |
535 |
0 |
while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) |
536 |
|
{ |
537 |
0 |
sb.append(c); |
538 |
0 |
c = this.next(); |
539 |
|
} |
540 |
0 |
this.back(); |
541 |
|
|
542 |
0 |
string = sb.toString().trim(); |
543 |
0 |
if ("".equals(string)) |
544 |
|
{ |
545 |
0 |
throw this.syntaxError("Missing value"); |
546 |
|
} |
547 |
0 |
return JSONObject.stringToValue(string); |
548 |
|
} |
549 |
|
|
550 |
|
|
551 |
|
|
552 |
|
|
553 |
|
|
554 |
|
@param |
555 |
|
|
556 |
|
@return |
557 |
|
|
558 |
|
@throws |
559 |
|
|
560 |
|
|
|
|
| 0% |
Uncovered Elements: 22 (22) |
Complexity: 4 |
Complexity Density: 0.22 |
|
561 |
0 |
public char skipTo(char to) throws JSONException... |
562 |
|
{ |
563 |
0 |
char c; |
564 |
0 |
try |
565 |
|
{ |
566 |
0 |
long startIndex = this.index; |
567 |
0 |
long startCharacter = this.character; |
568 |
0 |
long startLine = this.line; |
569 |
0 |
this.reader.mark(1000000); |
570 |
0 |
do |
571 |
|
{ |
572 |
0 |
c = this.next(); |
573 |
0 |
if (c == 0) |
574 |
|
{ |
575 |
|
|
576 |
|
|
577 |
|
|
578 |
0 |
this.reader.reset(); |
579 |
0 |
this.index = startIndex; |
580 |
0 |
this.character = startCharacter; |
581 |
0 |
this.line = startLine; |
582 |
0 |
return 0; |
583 |
|
} |
584 |
0 |
} while (c != to); |
585 |
0 |
this.reader.mark(1); |
586 |
|
} catch (IOException exception) |
587 |
|
{ |
588 |
0 |
throw new JSONException(exception); |
589 |
|
} |
590 |
0 |
this.back(); |
591 |
0 |
return c; |
592 |
|
} |
593 |
|
|
594 |
|
|
595 |
|
|
596 |
|
|
597 |
|
@param |
598 |
|
|
599 |
|
@return |
600 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
601 |
0 |
public JSONException syntaxError(String message)... |
602 |
|
{ |
603 |
0 |
return new JSONException(message + this.toString()); |
604 |
|
} |
605 |
|
|
606 |
|
|
607 |
|
|
608 |
|
|
609 |
|
@param |
610 |
|
|
611 |
|
@param |
612 |
|
|
613 |
|
@return |
614 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
615 |
0 |
public JSONException syntaxError(String message, Throwable causedBy)... |
616 |
|
{ |
617 |
0 |
return new JSONException(message + this.toString(), causedBy); |
618 |
|
} |
619 |
|
|
620 |
|
|
621 |
|
|
622 |
|
|
623 |
|
@return |
624 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
625 |
0 |
@Override... |
626 |
|
public String toString() |
627 |
|
{ |
628 |
0 |
return " at " + this.index + " [character " + this.character + " line " |
629 |
|
+ this.line + "]"; |
630 |
|
} |
631 |
|
} |