2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6 * ===========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END====================================================
22 package org.onap.aaf.misc.rosetta;
24 import java.io.IOException;
25 import java.io.Reader;
27 import org.onap.aaf.misc.env.Env;
28 import org.onap.aaf.misc.env.TimeTaken;
29 import org.onap.aaf.misc.rosetta.InJson.State;
31 public class InJson implements Parse<Reader, State> {
32 public Parsed<State> parse(Reader r, Parsed<State> parsed) throws ParseException {
33 // First things first, if there's a "leftover" event, process that immediately
34 State state = (State)parsed.state;
35 if (state.unsent > 0) {
36 parsed.event = state.unsent;
43 StringBuilder sb = parsed.sb;
44 boolean inQuotes = false, escaped = false;
47 // Gather data from Reader, looking for special characters when not in Quotes
48 while (go && (ch=r.read())>=0) {
49 if (state.braces>=0 || ch==Parse.START_OBJ) { // ignore garbage/whitespace before content
51 // Character is a quote.
54 if (escaped) { // if escaped Quote, add to data.
64 } else { // Not a Quote
80 parsed.isString = false;
83 if (state.braces++ == 0) {
84 parsed.event = START_DOC;
92 if (--state.braces == 0) {
94 state.unsent = END_DOC;
100 // These three end the data gathering, and send it along with the event that is ending the data gathering
102 if (parsed.name.startsWith("__")) {
103 parsed.event = Parse.ATTRIB;
104 parsed.name = parsed.name.substring(2);
110 case Parse.START_ARRAY:
111 case Parse.END_ARRAY:
116 // The Escape Sequence, for Quote marks within Quotes
118 // Ignore these, unless within quotes, at which point data-gather
126 // Normal data... gather it
135 } catch (IOException e) {
136 throw new ParseException(e);
140 public static class State {
141 public int braces = 0;
142 public char unsent = 0;
146 public Parsed<State> newParsed() {
147 return new Parsed<State>(new State()); // no State needed
151 public TimeTaken start(Env env) {
152 return env.start("Rosetta JSON In", Env.JSON);