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