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.Writer;
\r
27 import java.util.Stack;
\r
29 import org.onap.aaf.inno.env.util.IndentPrintWriter;
\r
31 public class OutJson extends Out {
\r
34 public<IN,S> void extract(IN in, Writer writer, Parse<IN, S> prs, boolean ... options) throws IOException, ParseException {
\r
35 Parsed<S> p = prs.newParsed();
\r
36 IndentPrintWriter ipw;
\r
37 if(options.length>0 && options[0]) { // is Pretty
\r
38 ipw = writer instanceof IndentPrintWriter?(IndentPrintWriter)writer:new IndentPrintWriter(writer);
\r
44 // If it's a fragment, print first Object Name. If root Object, skip first name
\r
45 Stack<LevelStack> jsonLevel = new Stack<LevelStack>();
\r
46 jsonLevel.push(new LevelStack(options.length>1 && options[1]));
\r
47 boolean print = true, hadData=false;
\r
48 char afterName=0, beforeName=0, maybe = 0, prev=0;
\r
51 while((p = prs.parse(in,p.reuse())).valid()) {
\r
57 if(count==2) { // it's empty, write open/close on it's own
\r
65 if(jsonLevel.peek().printObjectName) {
\r
67 } else { // don't print names on first
\r
70 maybe=jsonLevel.peek().listItem();
\r
71 jsonLevel.push(new LevelStack(true));
\r
74 if(p.hasData()) { // if we have data, we print that, so may need to prepend a comma.
\r
75 maybe = jsonLevel.peek().listItem();
\r
76 } else { // No data means just print,
\r
77 p.name = ""; // XML tags come through with names, but no data
\r
81 afterName = p.event;
\r
84 afterName = p.event;
\r
85 if((prev==',' && !hadData) || prev==']')maybe=',';
\r
86 else maybe = jsonLevel.peek().listItem();
\r
88 jsonLevel.push(new LevelStack(false));
\r
92 afterName = p.event;
\r
94 if(prev==',' && !hadData)maybe=',';
\r
95 else maybe = jsonLevel.peek().listItem();
\r
97 p.name = ""; // XML tags come through with names, but no data
\r
109 maybe=jsonLevel.peek().listItem();
\r
118 if(ipw==null)writer.append(maybe);
\r
119 else ipw.println(maybe);
\r
123 if(beforeName!=0) {
\r
124 if(ipw==null)writer.append(beforeName);
\r
125 else ipw.println(beforeName);
\r
130 writer.append('"');
\r
131 if(p.event==3)writer.append("__");
\r
132 writer.append(p.name);
\r
133 writer.append("\":");
\r
137 writer.append('"');
\r
138 escapedWrite(writer, p.sb);
\r
139 writer.append('"');
\r
140 } else if(p.sb.length()>0) {
\r
141 writer.append(p.sb);
\r
146 if(ipw==null)writer.append(afterName);
\r
148 switch(afterName) {
\r
150 ipw.println(afterName);
\r
156 ipw.print(afterName);
\r
159 if(prev=='}' || prev==',')ipw.println();
\r
161 ipw.print(afterName);
\r
165 ipw.println(afterName);
\r
168 ipw.print(afterName);
\r
183 hadData = p.hasData();
\r
189 private void escapedWrite(Writer writer, StringBuilder sb) throws IOException {
\r
191 for(int i=0;i<sb.length();++i) {
\r
192 switch(c=sb.charAt(i)) {
\r
195 if(i<sb.length()) {
\r
201 writer.append('\\');
\r
202 // Passthrough on purpose
\r
212 public String logName() {
\r
213 return "Rosetta JSON";
\r
216 private static class LevelStack {
\r
217 public boolean printObjectName=false;
\r
218 private boolean first_n_List=true;
\r
220 public LevelStack(boolean printObjectName) {
\r
221 this.printObjectName = printObjectName;
\r
224 public char listItem() {
\r
226 first_n_List=false;
\r