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.env;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.InputStreamReader;
27 import java.io.OutputStream;
28 import java.io.OutputStreamWriter;
29 import java.io.Reader;
30 import java.io.StringReader;
31 import java.io.StringWriter;
32 import java.io.Writer;
34 import org.onap.aaf.misc.env.APIException;
35 import org.onap.aaf.misc.env.Data;
36 import org.onap.aaf.misc.env.Env;
37 import org.onap.aaf.misc.env.TimeTaken;
38 import org.onap.aaf.misc.rosetta.Out;
39 import org.onap.aaf.misc.rosetta.Parse;
40 import org.onap.aaf.misc.rosetta.Saved;
42 public class RosettaData<T> implements Data<T>{
44 private RosettaDF<T> df;
46 private TYPE inType, outType;
47 // Note: This is an array of boolean in order to pass into other methods
48 private boolean options[] = new boolean[] {false, false};
49 // Temp Storage of XML. Only when we must use JAXB to read in Objects
50 private String xml,json;
53 RosettaData(Env env, RosettaDF<T> rosettaDF) {
55 saved = new Saved(); // Note: Saved constructs storage as needed...
57 inType = df.getInType();
58 outType = df.getOutType(); // take defaults
62 void setSaved(Saved saved) {
67 public RosettaData<T> in(TYPE rosettaType) {
73 public RosettaData<T> out(TYPE rosettaType) {
74 outType = rosettaType;
79 public RosettaData<T> load(Reader rdr) throws APIException {
80 Parse<Reader,?> in = df.getIn(inType);
81 TimeTaken tt = in.start(trans);
83 saved.extract(rdr, (Writer)null, in);
85 } catch (Exception e) {
86 throw new APIException(e);
94 public RosettaData<T> load(InputStream is) throws APIException {
95 Parse<Reader,?> in = df.getIn(inType);
96 TimeTaken tt = in.start(trans);
98 saved.extract(new InputStreamReader(is), (Writer)null, in);
100 } catch (Exception e) {
101 throw new APIException(e);
109 public RosettaData<T> load(String str) throws APIException {
110 Parse<Reader,?> in = df.getIn(inType);
111 TimeTaken tt = in.start(trans);
113 saved.extract(new StringReader(str), (Writer)null, in);
124 } catch (Exception e) {
125 throw new APIException(e);
133 public RosettaData<T> load(T t) throws APIException {
134 Parse<?,?> in = df.getIn(inType);
135 TimeTaken tt = in.start(trans);
137 if (df.marshal==null) { // Unknown marshaller... do working XML marshal/extraction
138 StringWriter sw = new StringWriter();
139 df.jaxMar.marshal(trans.debug(), t, sw, options);
140 saved.extract(new StringReader(xml = sw.toString()), (Writer)null, df.inXML);
142 saved.extract(t, (Writer)null, df.marshal);
144 } catch (Exception e) {
145 throw new APIException(e);
152 public Saved getEvents() {
157 public T asObject() throws APIException {
158 Out out = df.getOut(TYPE.XML);
159 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
161 //TODO Replace JAXB with Direct Object method!!!
162 StringWriter sw = new StringWriter();
163 out.extract(null, sw, saved);
164 return df.jaxUmar.unmarshal(trans.debug(), sw.toString());
165 } catch (Exception e) {
166 throw new APIException(e);
173 public String asString() throws APIException {
174 Out out = df.getOut(outType);
175 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
177 if (outType==TYPE.XML) {
179 StringWriter sw = new StringWriter();
180 out.extract(null, sw, saved, options);
186 StringWriter sw = new StringWriter();
187 out.extract(null, sw, saved, options);
188 json = sw.toString();
192 } catch (Exception e) {
193 throw new APIException(e);
201 public RosettaData<T> to(OutputStream os) throws APIException, IOException {
202 Out out = df.getOut(outType);
203 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
205 if (outType==TYPE.XML && xml!=null) {
206 os.write(xml.getBytes());
207 } else if (outType==TYPE.JSON && json!=null) {
208 os.write(json.getBytes());
210 out.extract(null, os, saved, options);
212 } catch (Exception e) {
213 throw new APIException(e);
221 public RosettaData<T> to(Writer writer) throws APIException, IOException {
222 Out out = df.getOut(outType);
223 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
225 if (outType==TYPE.XML && xml!=null) {
227 } else if (outType==TYPE.JSON && json!=null) {
230 out.extract(null, writer, saved, options);
232 } catch (Exception e) {
233 throw new APIException(e);
241 public Class<T> getTypeClass() {
242 return df.getTypeClass();
245 private static final boolean[] emptyOption = new boolean[0];
247 public void direct(InputStream is, OutputStream os) throws APIException, IOException {
248 direct(is,os,emptyOption);
251 public void direct(Reader reader, Writer writer, boolean ... options) throws APIException, IOException {
252 Parse<Reader,?> in = df.getIn(inType);
253 Out out = df.getOut(outType);
254 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
256 out.extract(reader, writer, in,options);
257 } catch (Exception e) {
258 throw new APIException(e);
264 public void direct(T t, Writer writer, boolean ... options) throws APIException, IOException {
265 Out out = df.getOut(outType);
266 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
268 if (df.marshal==null) { // Unknown marshaller... do working XML marshal/extraction
269 StringWriter sw = new StringWriter();
270 df.jaxMar.marshal(trans.debug(), t, sw, options);
271 out.extract(new StringReader(xml = sw.toString()), writer, df.inXML,options);
273 out.extract(t, writer, df.marshal,options);
275 } catch (Exception e) {
276 throw new APIException(e);
282 public void direct(T t, OutputStream os, boolean ... options) throws APIException, IOException {
283 Out out = df.getOut(outType);
284 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
286 if (df.marshal==null) { // Unknown marshaller... do working XML marshal/extraction
287 if (outType.equals(TYPE.XML)) {
288 df.jaxMar.marshal(trans.debug(), t, os, options);
290 StringWriter sw = new StringWriter();
291 df.jaxMar.marshal(trans.debug(), t, sw, options);
292 out.extract(new StringReader(xml = sw.toString()), new OutputStreamWriter(os), df.inXML,options);
295 out.extract(t, new OutputStreamWriter(os), df.marshal,options);
298 } catch (Exception e) {
299 throw new APIException(e);
306 public void direct(InputStream is, OutputStream os, boolean ... options) throws APIException, IOException {
307 direct(new InputStreamReader(is),new OutputStreamWriter(os), options);
311 public RosettaData<T> option(int option) {
312 options[0] = (option&Data.PRETTY)==Data.PRETTY;
313 options[1] = (option&Data.FRAGMENT)==Data.FRAGMENT;