Changeset 13231 in josm for trunk/src/org/glassfish/json/JsonParserImpl.java
- Timestamp:
- 2017-12-23T02:40:43+01:00 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/glassfish/json/JsonParserImpl.java
r6756 r13231 2 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3 3 * 4 * Copyright (c) 2012-201 3Oracle and/or its affiliates. All rights reserved.4 * Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved. 5 5 * 6 6 * The contents of this file are subject to the terms of either the GNU … … 9 9 * may not use this file except in compliance with the License. You can 10 10 * obtain a copy of the License at 11 * https:// glassfish.dev.java.net/public/CDDL+GPL_1_1.html12 * or packager/legal/LICENSE.txt. See the License for the specific11 * https://oss.oracle.com/licenses/CDDL+GPL-1.1 12 * or LICENSE.txt. See the License for the specific 13 13 * language governing permissions and limitations under the License. 14 14 * 15 15 * When distributing the software, include this License Header Notice in each 16 * file and include the License file at packager/legal/LICENSE.txt.16 * file and include the License file at LICENSE.txt. 17 17 * 18 18 * GPL Classpath Exception: … … 41 41 package org.glassfish.json; 42 42 43 import javax.json.*; 43 import java.io.IOException; 44 import java.io.InputStream; 45 import java.io.InputStreamReader; 46 import java.io.Reader; 47 import java.math.BigDecimal; 48 import java.nio.charset.Charset; 49 import java.util.AbstractMap; 50 import java.util.Map; 51 import java.util.NoSuchElementException; 52 import java.util.Spliterator; 53 import java.util.Spliterators; 54 import java.util.function.Consumer; 55 import java.util.stream.Stream; 56 import java.util.stream.StreamSupport; 57 58 import javax.json.JsonArray; 59 import javax.json.JsonArrayBuilder; 60 import javax.json.JsonException; 61 import javax.json.JsonObject; 62 import javax.json.JsonObjectBuilder; 63 import javax.json.JsonValue; 44 64 import javax.json.stream.JsonLocation; 45 65 import javax.json.stream.JsonParser; 66 import javax.json.stream.JsonParser.Event; 46 67 import javax.json.stream.JsonParsingException; 47 import java.io.*;48 import java.math.BigDecimal;49 import java.nio.charset.Charset;50 import java.util.*;51 68 52 69 import org.glassfish.json.JsonTokenizer.JsonToken; … … 58 75 * 59 76 * @author Jitendra Kotamraju 77 * @author Kin-man Chung 60 78 */ 61 79 public class JsonParserImpl implements JsonParser { 62 80 81 private final BufferPool bufferPool; 63 82 private Context currentContext = new NoneContext(); 64 83 private Event currentEvent; 65 84 66 85 private final Stack stack = new Stack(); 67 private final StateIterator stateIterator;68 86 private final JsonTokenizer tokenizer; 69 87 70 88 public JsonParserImpl(Reader reader, BufferPool bufferPool) { 89 this.bufferPool = bufferPool; 71 90 tokenizer = new JsonTokenizer(reader, bufferPool); 72 stateIterator = new StateIterator();73 91 } 74 92 75 93 public JsonParserImpl(InputStream in, BufferPool bufferPool) { 94 this.bufferPool = bufferPool; 76 95 UnicodeDetectingInputStream uin = new UnicodeDetectingInputStream(in); 77 96 tokenizer = new JsonTokenizer(new InputStreamReader(uin, uin.getCharset()), bufferPool); 78 stateIterator = new StateIterator();79 97 } 80 98 81 99 public JsonParserImpl(InputStream in, Charset encoding, BufferPool bufferPool) { 100 this.bufferPool = bufferPool; 82 101 tokenizer = new JsonTokenizer(new InputStreamReader(in, encoding), bufferPool); 83 stateIterator = new StateIterator();84 } 85 102 } 103 104 @Override 86 105 public String getString() { 87 106 if (currentEvent == Event.KEY_NAME || currentEvent == Event.VALUE_STRING … … 115 134 } 116 135 136 boolean isDefinitelyLong() { 137 return tokenizer.isDefinitelyLong(); 138 } 139 117 140 @Override 118 141 public long getLong() { … … 121 144 JsonMessages.PARSER_GETLONG_ERR(currentEvent)); 122 145 } 123 return tokenizer.get BigDecimal().longValue();146 return tokenizer.getLong(); 124 147 } 125 148 … … 134 157 135 158 @Override 159 public JsonArray getArray() { 160 if (currentEvent != Event.START_ARRAY) { 161 throw new IllegalStateException( 162 JsonMessages.PARSER_GETARRAY_ERR(currentEvent)); 163 } 164 return getArray(new JsonArrayBuilderImpl(bufferPool)); 165 } 166 167 @Override 168 public JsonObject getObject() { 169 if (currentEvent != Event.START_OBJECT) { 170 throw new IllegalStateException( 171 JsonMessages.PARSER_GETOBJECT_ERR(currentEvent)); 172 } 173 return getObject(new JsonObjectBuilderImpl(bufferPool)); 174 } 175 176 @Override 177 public JsonValue getValue() { 178 switch (currentEvent) { 179 case START_ARRAY: 180 return getArray(new JsonArrayBuilderImpl(bufferPool)); 181 case START_OBJECT: 182 return getObject(new JsonObjectBuilderImpl(bufferPool)); 183 case KEY_NAME: 184 case VALUE_STRING: 185 return new JsonStringImpl(getString()); 186 case VALUE_NUMBER: 187 if (isDefinitelyInt()) { 188 return JsonNumberImpl.getJsonNumber(getInt()); 189 } else if (isDefinitelyLong()) { 190 return JsonNumberImpl.getJsonNumber(getLong()); 191 } 192 return JsonNumberImpl.getJsonNumber(getBigDecimal()); 193 case VALUE_TRUE: 194 return JsonValue.TRUE; 195 case VALUE_FALSE: 196 return JsonValue.FALSE; 197 case VALUE_NULL: 198 return JsonValue.NULL; 199 case END_ARRAY: 200 case END_OBJECT: 201 default: 202 throw new IllegalStateException(JsonMessages.PARSER_GETVALUE_ERR(currentEvent)); 203 } 204 } 205 206 @Override 207 public Stream<JsonValue> getArrayStream() { 208 if (currentEvent != Event.START_ARRAY) { 209 throw new IllegalStateException( 210 JsonMessages.PARSER_GETARRAY_ERR(currentEvent)); 211 } 212 Spliterator<JsonValue> spliterator = 213 new Spliterators.AbstractSpliterator<JsonValue>(Long.MAX_VALUE, Spliterator.ORDERED) { 214 @Override 215 public Spliterator<JsonValue> trySplit() { 216 return null; 217 } 218 @Override 219 public boolean tryAdvance(Consumer<? super JsonValue> action) { 220 if (action == null) { 221 throw new NullPointerException(); 222 } 223 if (! hasNext()) { 224 return false; 225 } 226 if (next() == JsonParser.Event.END_ARRAY) { 227 return false; 228 } 229 action.accept(getValue()); 230 return true; 231 } 232 }; 233 return StreamSupport.stream(spliterator, false); 234 } 235 236 @Override 237 public Stream<Map.Entry<String, JsonValue>> getObjectStream() { 238 if (currentEvent != Event.START_OBJECT) { 239 throw new IllegalStateException( 240 JsonMessages.PARSER_GETOBJECT_ERR(currentEvent)); 241 } 242 Spliterator<Map.Entry<String, JsonValue>> spliterator = 243 new Spliterators.AbstractSpliterator<Map.Entry<String, JsonValue>>(Long.MAX_VALUE, Spliterator.ORDERED) { 244 @Override 245 public Spliterator<Map.Entry<String,JsonValue>> trySplit() { 246 return null; 247 } 248 @Override 249 public boolean tryAdvance(Consumer<? super Map.Entry<String, JsonValue>> action) { 250 if (action == null) { 251 throw new NullPointerException(); 252 } 253 if (! hasNext()) { 254 return false; 255 } 256 JsonParser.Event e = next(); 257 if (e == JsonParser.Event.END_OBJECT) { 258 return false; 259 } 260 if (e != JsonParser.Event.KEY_NAME) { 261 throw new JsonException(JsonMessages.INTERNAL_ERROR()); 262 } 263 String key = getString(); 264 if (! hasNext()) { 265 throw new JsonException(JsonMessages.INTERNAL_ERROR()); 266 } 267 next(); 268 JsonValue value = getValue(); 269 action.accept(new AbstractMap.SimpleImmutableEntry<>(key, value)); 270 return true; 271 } 272 }; 273 return StreamSupport.stream(spliterator, false); 274 } 275 276 @Override 277 public Stream<JsonValue> getValueStream() { 278 if (! (currentContext instanceof NoneContext)) { 279 throw new IllegalStateException( 280 JsonMessages.PARSER_GETVALUESTREAM_ERR()); 281 } 282 Spliterator<JsonValue> spliterator = 283 new Spliterators.AbstractSpliterator<JsonValue>(Long.MAX_VALUE, Spliterator.ORDERED) { 284 @Override 285 public Spliterator<JsonValue> trySplit() { 286 return null; 287 } 288 @Override 289 public boolean tryAdvance(Consumer<? super JsonValue> action) { 290 if (action == null) { 291 throw new NullPointerException(); 292 } 293 if (! hasNext()) { 294 return false; 295 } 296 next(); 297 action.accept(getValue()); 298 return true; 299 } 300 }; 301 return StreamSupport.stream(spliterator, false); 302 } 303 304 @Override 305 public void skipArray() { 306 if (currentEvent == Event.START_ARRAY) { 307 currentContext.skip(); 308 currentContext = stack.pop(); 309 } 310 } 311 312 @Override 313 public void skipObject() { 314 if (currentEvent == Event.START_OBJECT) { 315 currentContext.skip(); 316 currentContext = stack.pop(); 317 } 318 } 319 320 private JsonArray getArray(JsonArrayBuilder builder) { 321 while(hasNext()) { 322 JsonParser.Event e = next(); 323 if (e == JsonParser.Event.END_ARRAY) { 324 return builder.build(); 325 } 326 builder.add(getValue()); 327 } 328 throw parsingException(JsonToken.EOF, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL, SQUARECLOSE]"); 329 } 330 331 private JsonObject getObject(JsonObjectBuilder builder) { 332 while(hasNext()) { 333 JsonParser.Event e = next(); 334 if (e == JsonParser.Event.END_OBJECT) { 335 return builder.build(); 336 } 337 String key = getString(); 338 next(); 339 builder.add(key, getValue()); 340 } 341 throw parsingException(JsonToken.EOF, "[STRING, CURLYCLOSE]"); 342 } 343 344 @Override 136 345 public JsonLocation getLocation() { 137 346 return tokenizer.getLocation(); … … 142 351 } 143 352 353 @Override 144 354 public boolean hasNext() { 145 return stateIterator.hasNext(); 146 } 147 355 return tokenizer.hasNextToken(); 356 } 357 358 @Override 148 359 public Event next() { 149 return stateIterator.next(); 150 } 151 152 private class StateIterator implements Iterator<JsonParser.Event> { 153 154 @Override 155 public boolean hasNext() { 156 if (stack.isEmpty() && (currentEvent == Event.END_ARRAY || currentEvent == Event.END_OBJECT)) { 157 JsonToken token = tokenizer.nextToken(); 158 if (token != JsonToken.EOF) { 159 throw new JsonParsingException(JsonMessages.PARSER_EXPECTED_EOF(token), 160 getLastCharLocation()); 161 } 162 return false; 163 } 164 return true; 165 } 166 167 @Override 168 public JsonParser.Event next() { 169 if (!hasNext()) { 170 throw new NoSuchElementException(); 171 } 172 return currentEvent = currentContext.getNextEvent(); 173 } 174 175 @Override 176 public void remove() { 177 throw new UnsupportedOperationException(); 178 } 179 } 180 360 if (!hasNext()) { 361 throw new NoSuchElementException(); 362 } 363 return currentEvent = currentContext.getNextEvent(); 364 } 365 366 @Override 181 367 public void close() { 182 368 try { … … 206 392 } 207 393 394 private Context peek() { 395 return head; 396 } 397 208 398 private boolean isEmpty() { 209 399 return head == null; … … 214 404 Context next; 215 405 abstract Event getNextEvent(); 406 abstract void skip(); 216 407 } 217 408 … … 219 410 @Override 220 411 public Event getNextEvent() { 221 // Handle 1. { 2. [412 // Handle 1. { 2. [ 3. value 222 413 JsonToken token = tokenizer.nextToken(); 223 414 if (token == JsonToken.CURLYOPEN) { … … 229 420 currentContext = new ArrayContext(); 230 421 return Event.START_ARRAY; 231 } 232 throw parsingException(token, "[CURLYOPEN, SQUAREOPEN]"); 422 } else if (token.isValue()) { 423 return token.getEvent(); 424 } 425 throw parsingException(token, "[CURLYOPEN, SQUAREOPEN, STRING, NUMBER, TRUE, FALSE, NULL]"); 426 } 427 428 @Override 429 void skip() { 430 // no-op 233 431 } 234 432 } … … 293 491 } 294 492 493 @Override 494 void skip() { 495 JsonToken token; 496 int depth = 1; 497 do { 498 token = tokenizer.nextToken(); 499 switch (token) { 500 case CURLYCLOSE: 501 depth--; 502 break; 503 case CURLYOPEN: 504 depth++; 505 break; 506 } 507 } while (!(token == JsonToken.CURLYCLOSE && depth == 0)); 508 } 509 295 510 } 296 511 … … 328 543 } 329 544 545 @Override 546 void skip() { 547 JsonToken token; 548 int depth = 1; 549 do { 550 token = tokenizer.nextToken(); 551 switch (token) { 552 case SQUARECLOSE: 553 depth--; 554 break; 555 case SQUAREOPEN: 556 depth++; 557 break; 558 } 559 } while (!(token == JsonToken.SQUARECLOSE && depth == 0)); 560 } 330 561 } 331 562
Note:
See TracChangeset
for help on using the changeset viewer.