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 public RosettaData<T> in(TYPE rosettaType) {
68 public RosettaData<T> out(TYPE rosettaType) {
69 outType = rosettaType;
74 public RosettaData<T> load(Reader rdr) throws APIException {
75 Parse<Reader,?> in = df.getIn(inType);
76 TimeTaken tt = in.start(trans);
78 saved.extract(rdr, (Writer)null, in);
80 } catch (Exception e) {
81 throw new APIException(e);
89 public RosettaData<T> load(InputStream is) throws APIException {
90 Parse<Reader,?> in = df.getIn(inType);
91 TimeTaken tt = in.start(trans);
93 saved.extract(new InputStreamReader(is), (Writer)null, in);
95 } catch (Exception e) {
96 throw new APIException(e);
104 public RosettaData<T> load(String str) throws APIException {
105 Parse<Reader,?> in = df.getIn(inType);
106 TimeTaken tt = in.start(trans);
108 saved.extract(new StringReader(str), (Writer)null, in);
119 } catch (Exception e) {
120 throw new APIException(e);
128 public RosettaData<T> load(T t) throws APIException {
129 Parse<?,?> in = df.getIn(inType);
130 TimeTaken tt = in.start(trans);
132 if (df.marshal==null) { // Unknown marshaller... do working XML marshal/extraction
133 StringWriter sw = new StringWriter();
134 df.jaxMar.marshal(trans.debug(), t, sw, options);
135 saved.extract(new StringReader(xml = sw.toString()), (Writer)null, df.inXML);
137 saved.extract(t, (Writer)null, df.marshal);
139 } catch (Exception e) {
140 throw new APIException(e);
147 public Saved getEvents() {
152 public T asObject() throws APIException {
153 Out out = df.getOut(TYPE.XML);
154 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
156 //TODO Replace JAXB with Direct Object method!!!
157 StringWriter sw = new StringWriter();
158 out.extract(null, sw, saved);
159 return df.jaxUmar.unmarshal(trans.debug(), sw.toString());
160 } catch (Exception e) {
161 throw new APIException(e);
168 public String asString() throws APIException {
169 Out out = df.getOut(outType);
170 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
172 if (outType==TYPE.XML) {
174 StringWriter sw = new StringWriter();
175 out.extract(null, sw, saved, options);
181 StringWriter sw = new StringWriter();
182 out.extract(null, sw, saved, options);
183 json = sw.toString();
187 } catch (Exception e) {
188 throw new APIException(e);
196 public RosettaData<T> to(OutputStream os) throws APIException, IOException {
197 Out out = df.getOut(outType);
198 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
200 if (outType==TYPE.XML && xml!=null) {
201 os.write(xml.getBytes());
202 } else if (outType==TYPE.JSON && json!=null) {
203 os.write(json.getBytes());
205 out.extract(null, os, saved, options);
207 } catch (Exception e) {
208 throw new APIException(e);
216 public RosettaData<T> to(Writer writer) throws APIException, IOException {
217 Out out = df.getOut(outType);
218 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
220 if (outType==TYPE.XML && xml!=null) {
222 } else if (outType==TYPE.JSON && json!=null) {
225 out.extract(null, writer, saved, options);
227 } catch (Exception e) {
228 throw new APIException(e);
236 public Class<T> getTypeClass() {
237 return df.getTypeClass();
240 private static final boolean[] emptyOption = new boolean[0];
242 public void direct(InputStream is, OutputStream os) throws APIException, IOException {
243 direct(is,os,emptyOption);
246 public void direct(Reader reader, Writer writer, boolean ... options) throws APIException, IOException {
247 Parse<Reader,?> in = df.getIn(inType);
248 Out out = df.getOut(outType);
249 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
251 out.extract(reader, writer, in,options);
252 } catch (Exception e) {
253 throw new APIException(e);
259 public void direct(T t, Writer writer, boolean ... options) throws APIException, IOException {
260 Out out = df.getOut(outType);
261 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
263 if (df.marshal==null) { // Unknown marshaller... do working XML marshal/extraction
264 StringWriter sw = new StringWriter();
265 df.jaxMar.marshal(trans.debug(), t, sw, options);
266 out.extract(new StringReader(xml = sw.toString()), writer, df.inXML,options);
268 out.extract(t, writer, df.marshal,options);
270 } catch (Exception e) {
271 throw new APIException(e);
277 public void direct(T t, OutputStream os, boolean ... options) throws APIException, IOException {
278 Out out = df.getOut(outType);
279 TimeTaken tt = trans.start(out.logName(),df.logType(outType)); // determine from Out.. without dependency on Env?
281 if (df.marshal==null) { // Unknown marshaller... do working XML marshal/extraction
282 if (outType.equals(TYPE.XML)) {
283 df.jaxMar.marshal(trans.debug(), t, os, options);
285 StringWriter sw = new StringWriter();
286 df.jaxMar.marshal(trans.debug(), t, sw, options);
287 out.extract(new StringReader(xml = sw.toString()), new OutputStreamWriter(os), df.inXML,options);
290 out.extract(t, new OutputStreamWriter(os), df.marshal,options);
293 } catch (Exception e) {
294 throw new APIException(e);
301 public void direct(InputStream is, OutputStream os, boolean ... options) throws APIException, IOException {
302 direct(new InputStreamReader(is),new OutputStreamWriter(os), options);
306 public RosettaData<T> option(int option) {
307 options[0] = (option&Data.PRETTY)==Data.PRETTY;
308 options[1] = (option&Data.FRAGMENT)==Data.FRAGMENT;