| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| JSONTokener |
|
| 0.0;0 |
| 1 | // Copyright 2007 The Apache Software Foundation | |
| 2 | // | |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 | // you may not use this file except in compliance with the License. | |
| 5 | // You may obtain a copy of the License at | |
| 6 | // | |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 | // | |
| 9 | // Unless required by applicable law or agreed to in writing, software | |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 | // See the License for the specific language governing permissions and | |
| 13 | // limitations under the License. | |
| 14 | ||
| 15 | package org.apache.tapestry5.json; | |
| 16 | ||
| 17 | /* | |
| 18 | Copyright (c) 2002 JSON.org | |
| 19 | ||
| 20 | Permission is hereby granted, free of charge, to any person obtaining a copy | |
| 21 | of this software and associated documentation files (the "Software"), to deal | |
| 22 | in the Software without restriction, including without limitation the rights | |
| 23 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| 24 | copies of the Software, and to permit persons to whom the Software is | |
| 25 | furnished to do so, subject to the following conditions: | |
| 26 | ||
| 27 | The above copyright notice and this permission notice shall be included in all | |
| 28 | copies or substantial portions of the Software. | |
| 29 | ||
| 30 | The Software shall be used for Good, not Evil. | |
| 31 | ||
| 32 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| 33 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| 34 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| 35 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| 36 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 37 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| 38 | SOFTWARE. | |
| 39 | */ | |
| 40 | ||
| 41 | /** | |
| 42 | * A JSONTokener takes a source string and extracts characters and tokens from it. It is used by the JSONObject and | |
| 43 | * JSONArray constructors to parse JSON source strings. | |
| 44 | * | |
| 45 | * @author JSON.org | |
| 46 | * @version 2 | |
| 47 | */ | |
| 48 | 2 | class JSONTokener |
| 49 | { | |
| 50 | ||
| 51 | /** | |
| 52 | * The index of the next character. | |
| 53 | */ | |
| 54 | private int index; | |
| 55 | ||
| 56 | /** | |
| 57 | * The source string being tokenized. | |
| 58 | */ | |
| 59 | private final String source; | |
| 60 | ||
| 61 | /** | |
| 62 | * Construct a JSONTokener from a string. | |
| 63 | * | |
| 64 | * @param source A source string, in JSON format. | |
| 65 | */ | |
| 66 | public JSONTokener(String source) | |
| 67 | 70 | { |
| 68 | 70 | assert source != null; |
| 69 | ||
| 70 | 70 | index = 0; |
| 71 | 70 | this.source = source; |
| 72 | 70 | } |
| 73 | ||
| 74 | /** | |
| 75 | * Back up one character. This provides a sort of lookahead capability, so that you can test for a digit or letter | |
| 76 | * before attempting to parse the next number or identifier. | |
| 77 | */ | |
| 78 | public void back() | |
| 79 | { | |
| 80 | 338 | if (index > 0) |
| 81 | { | |
| 82 | 338 | index -= 1; |
| 83 | } | |
| 84 | 338 | } |
| 85 | ||
| 86 | /** | |
| 87 | * Determine if the source string still contains characters that next() can consume. | |
| 88 | * | |
| 89 | * @return true if not yet at the end of the source. | |
| 90 | */ | |
| 91 | public boolean more() | |
| 92 | { | |
| 93 | 2212 | return index < source.length(); |
| 94 | } | |
| 95 | ||
| 96 | /** | |
| 97 | * Get the next character in the source string. | |
| 98 | * | |
| 99 | * @return The next character, or 0 if past the end of the source string. | |
| 100 | */ | |
| 101 | public char next() | |
| 102 | { | |
| 103 | 2212 | if (more()) |
| 104 | { | |
| 105 | 2208 | return source.charAt(index++); |
| 106 | } | |
| 107 | ||
| 108 | 4 | return 0; |
| 109 | } | |
| 110 | ||
| 111 | /** | |
| 112 | * Get the next n characters. | |
| 113 | * | |
| 114 | * @param n The number of characters to take. | |
| 115 | * @return A string of n characters. | |
| 116 | * @throws RuntimeException Substring bounds error if there are not n characters remaining in the source string. | |
| 117 | */ | |
| 118 | public String next(int n) | |
| 119 | { | |
| 120 | 4 | int i = index; |
| 121 | 4 | int j = i + n; |
| 122 | 4 | if (j >= source.length()) |
| 123 | { | |
| 124 | 0 | throw syntaxError("Substring bounds error"); |
| 125 | } | |
| 126 | 4 | index += n; |
| 127 | 4 | return source.substring(i, j); |
| 128 | } | |
| 129 | ||
| 130 | /** | |
| 131 | * Get the next char in the string, skipping whitespace and comments (slashslash, slashstar, and hash). | |
| 132 | * | |
| 133 | * @return A character, or 0 if there are no more characters. | |
| 134 | * @throws RuntimeException | |
| 135 | */ | |
| 136 | public char nextClean() | |
| 137 | { | |
| 138 | for (; ;) | |
| 139 | { | |
| 140 | 1026 | char c = next(); |
| 141 | 1026 | if (c == '/') |
| 142 | { | |
| 143 | 0 | switch (next()) |
| 144 | { | |
| 145 | case '/': | |
| 146 | do | |
| 147 | { | |
| 148 | 0 | c = next(); |
| 149 | 0 | } while (c != '\n' && c != '\r' && c != 0); |
| 150 | ||
| 151 | 0 | break; |
| 152 | case '*': | |
| 153 | ||
| 154 | while (true) | |
| 155 | { | |
| 156 | 0 | c = next(); |
| 157 | 0 | if (c == 0) |
| 158 | { | |
| 159 | 0 | throw syntaxError("Unclosed comment"); |
| 160 | } | |
| 161 | 0 | if (c == '*') |
| 162 | { | |
| 163 | 0 | if (next() == '/') |
| 164 | { | |
| 165 | 0 | break; |
| 166 | } | |
| 167 | 0 | back(); |
| 168 | } | |
| 169 | } | |
| 170 | break; | |
| 171 | ||
| 172 | default: | |
| 173 | 0 | back(); |
| 174 | 0 | return '/'; |
| 175 | } | |
| 176 | } | |
| 177 | 1026 | else if (c == '#') |
| 178 | { | |
| 179 | do | |
| 180 | { | |
| 181 | 0 | c = next(); |
| 182 | 0 | } while (c != '\n' && c != '\r' && c != 0); |
| 183 | } | |
| 184 | 1026 | else if (c == 0 || c > ' ') |
| 185 | { | |
| 186 | 800 | return c; |
| 187 | } | |
| 188 | 226 | } |
| 189 | } | |
| 190 | ||
| 191 | /** | |
| 192 | * Return the characters up to the next close quote character. Backslash processing is done. The formal JSON format | |
| 193 | * does not allow strings in single quotes, but an implementation is allowed to accept them. | |
| 194 | * | |
| 195 | * @param quote The quoting character, either <code>"</code> <small>(double quote)</small> or | |
| 196 | * <code>'</code> <small>(single quote)</small>. | |
| 197 | * @return A String. | |
| 198 | * @throws RuntimeException Unterminated string. | |
| 199 | */ | |
| 200 | public String nextString(char quote) | |
| 201 | { | |
| 202 | 160 | StringBuilder builder = new StringBuilder(); |
| 203 | ||
| 204 | while (true) | |
| 205 | { | |
| 206 | 976 | char c = next(); |
| 207 | 976 | switch (c) |
| 208 | { | |
| 209 | case 0: | |
| 210 | case '\n': | |
| 211 | case '\r': | |
| 212 | 0 | throw syntaxError("Unterminated string"); |
| 213 | case '\\': | |
| 214 | 18 | c = next(); |
| 215 | 18 | switch (c) |
| 216 | { | |
| 217 | case 'b': | |
| 218 | 2 | builder.append('\b'); |
| 219 | 2 | break; |
| 220 | case 't': | |
| 221 | 2 | builder.append('\t'); |
| 222 | 2 | break; |
| 223 | case 'n': | |
| 224 | 2 | builder.append('\n'); |
| 225 | 2 | break; |
| 226 | case 'f': | |
| 227 | 2 | builder.append('\f'); |
| 228 | 2 | break; |
| 229 | case 'r': | |
| 230 | 2 | builder.append('\r'); |
| 231 | 2 | break; |
| 232 | case 'u': | |
| 233 | 2 | builder.append((char) Integer.parseInt(next(4), 16)); |
| 234 | 2 | break; |
| 235 | case 'x': | |
| 236 | 2 | builder.append((char) Integer.parseInt(next(2), 16)); |
| 237 | 2 | break; |
| 238 | default: | |
| 239 | 4 | builder.append(c); |
| 240 | } | |
| 241 | 4 | break; |
| 242 | default: | |
| 243 | 958 | if (c == quote) |
| 244 | { | |
| 245 | 160 | return builder.toString(); |
| 246 | } | |
| 247 | 798 | builder.append(c); |
| 248 | } | |
| 249 | 816 | } |
| 250 | } | |
| 251 | ||
| 252 | ||
| 253 | /** | |
| 254 | * Get the next value. The value can be a Boolean, Double, Integer, JSONArray, JSONObject, Long, or String, or the | |
| 255 | * JSONObject.NULL object. | |
| 256 | * | |
| 257 | * @return An object. | |
| 258 | * @throws RuntimeException If syntax error. | |
| 259 | */ | |
| 260 | public Object nextValue() | |
| 261 | { | |
| 262 | 220 | char c = nextClean(); |
| 263 | String s; | |
| 264 | ||
| 265 | 220 | switch (c) |
| 266 | { | |
| 267 | case '"': | |
| 268 | case '\'': | |
| 269 | 160 | return nextString(c); |
| 270 | case '{': | |
| 271 | 2 | back(); |
| 272 | 2 | return new JSONObject(this); |
| 273 | case '[': | |
| 274 | 4 | back(); |
| 275 | 4 | return new JSONArray(this); |
| 276 | } | |
| 277 | ||
| 278 | /* | |
| 279 | * Handle unquoted text. This could be the values true, false, or null, or it can be a | |
| 280 | * number. An implementation (such as this one) is allowed to also accept non-standard | |
| 281 | * forms. Accumulate characters until we reach the end of the text or a formatting | |
| 282 | * character. | |
| 283 | */ | |
| 284 | ||
| 285 | 54 | StringBuffer sb = new StringBuffer(); |
| 286 | 54 | char b = c; |
| 287 | 242 | while (c >= ' ' && ",:]}/\\\"[{;=#".indexOf(c) < 0) |
| 288 | { | |
| 289 | 188 | sb.append(c); |
| 290 | 188 | c = next(); |
| 291 | } | |
| 292 | 54 | back(); |
| 293 | ||
| 294 | /* | |
| 295 | * If it is true, false, or null, return the proper value. | |
| 296 | */ | |
| 297 | ||
| 298 | 54 | s = sb.toString().trim(); |
| 299 | 54 | if (s.equals("")) |
| 300 | { | |
| 301 | 0 | throw syntaxError("Missing value"); |
| 302 | } | |
| 303 | 54 | if (s.equalsIgnoreCase("true")) |
| 304 | { | |
| 305 | 4 | return Boolean.TRUE; |
| 306 | } | |
| 307 | 50 | if (s.equalsIgnoreCase("false")) |
| 308 | { | |
| 309 | 2 | return Boolean.FALSE; |
| 310 | } | |
| 311 | 48 | if (s.equalsIgnoreCase("null")) |
| 312 | { | |
| 313 | 2 | return JSONObject.NULL; |
| 314 | } | |
| 315 | ||
| 316 | /* | |
| 317 | * If it might be a number, try converting it. We support the 0- and 0x- conventions. If a | |
| 318 | * number cannot be produced, then the value will just be a string. Note that the 0-, 0x-, | |
| 319 | * plus, and implied string conventions are non-standard. A JSON parser is free to accept | |
| 320 | * non-JSON forms as long as it accepts all correct JSON forms. | |
| 321 | */ | |
| 322 | ||
| 323 | 46 | if ((b >= '0' && b <= '9') || b == '.' || b == '-' || b == '+') |
| 324 | { | |
| 325 | 38 | if (b == '0') |
| 326 | { | |
| 327 | 4 | if (s.length() > 2 && (s.charAt(1) == 'x' || s.charAt(1) == 'X')) |
| 328 | { | |
| 329 | try | |
| 330 | { | |
| 331 | 2 | return Integer.parseInt(s.substring(2), 16); |
| 332 | } | |
| 333 | 0 | catch (Exception e) |
| 334 | { | |
| 335 | /* Ignore the error */ | |
| 336 | 0 | } |
| 337 | } | |
| 338 | else | |
| 339 | { | |
| 340 | try | |
| 341 | { | |
| 342 | 2 | return Integer.parseInt(s, 8); |
| 343 | } | |
| 344 | 0 | catch (Exception e) |
| 345 | { | |
| 346 | /* Ignore the error */ | |
| 347 | } | |
| 348 | } | |
| 349 | } | |
| 350 | try | |
| 351 | { | |
| 352 | 34 | return new Integer(s); |
| 353 | } | |
| 354 | 6 | catch (Exception e) |
| 355 | { | |
| 356 | try | |
| 357 | { | |
| 358 | 6 | return new Long(s); |
| 359 | } | |
| 360 | 4 | catch (Exception f) |
| 361 | { | |
| 362 | try | |
| 363 | { | |
| 364 | 4 | return new Double(s); |
| 365 | } | |
| 366 | 0 | catch (Exception g) |
| 367 | { | |
| 368 | 0 | return s; |
| 369 | } | |
| 370 | } | |
| 371 | } | |
| 372 | } | |
| 373 | 8 | return s; |
| 374 | } | |
| 375 | ||
| 376 | /** | |
| 377 | * Make a JSONException to signal a syntax error. | |
| 378 | * | |
| 379 | * @param message The error message. | |
| 380 | * @return A JSONException object, suitable for throwing | |
| 381 | */ | |
| 382 | RuntimeException syntaxError(String message) | |
| 383 | { | |
| 384 | 12 | return new RuntimeException(message + toString()); |
| 385 | } | |
| 386 | ||
| 387 | /** | |
| 388 | * Make a printable string of this JSONTokener. | |
| 389 | * | |
| 390 | * @return " at character [myIndex] of [mySource]" | |
| 391 | */ | |
| 392 | @Override | |
| 393 | public String toString() | |
| 394 | { | |
| 395 | 12 | return " at character " + index + " of " + source; |
| 396 | } | |
| 397 | } |