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