Remove Tabs, per Jococo
[aaf/authz.git] / misc / rosetta / src / main / java / org / onap / aaf / misc / rosetta / env / RosettaData.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.rosetta.env;
23
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;
33
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;
41
42 public class RosettaData<T> implements Data<T>{
43     private Env trans;
44     private RosettaDF<T> df;
45     private Saved saved;
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;
51     
52     // package on purpose
53     RosettaData(Env env, RosettaDF<T> rosettaDF) {
54         df = rosettaDF;
55         saved = new Saved(); // Note: Saved constructs storage as needed...
56         trans = env;
57         inType = df.getInType();
58         outType = df.getOutType(); // take defaults
59     }
60     
61     //Added for junits
62     void setSaved(Saved saved) {
63         this.saved = saved;
64     }
65
66 //    // @Override
67     public RosettaData<T> in(TYPE rosettaType) {
68         inType = rosettaType;
69         return this;
70     }
71     
72 //    // @Override
73     public RosettaData<T> out(TYPE rosettaType) {
74         outType = rosettaType;
75         return this;
76     }
77
78 //    // @Override
79     public RosettaData<T> load(Reader rdr) throws APIException {
80         Parse<Reader,?> in = df.getIn(inType);
81         TimeTaken tt = in.start(trans);
82         try {
83             saved.extract(rdr, (Writer)null, in);
84             xml=json=null;
85         } catch (Exception e) {
86             throw new APIException(e);
87         } finally {
88             tt.done();
89         }
90         return this;
91     }
92     
93     // @Override
94     public RosettaData<T> load(InputStream is) throws APIException {
95         Parse<Reader,?> in = df.getIn(inType);
96         TimeTaken tt = in.start(trans);
97         try {
98             saved.extract(new InputStreamReader(is), (Writer)null, in);
99             xml=json=null;
100         } catch (Exception e) {
101             throw new APIException(e);
102         } finally {
103             tt.done();
104         }
105         return this;
106     }
107
108     // @Override
109     public RosettaData<T> load(String str) throws APIException {
110         Parse<Reader,?> in = df.getIn(inType);
111         TimeTaken tt = in.start(trans);
112         try {
113             saved.extract(new StringReader(str), (Writer)null, in);
114             switch(inType) {
115                 case XML:
116                     xml = str;
117                     break;
118                 case JSON:
119                     json = str;
120                     break;
121                 default:
122                     
123                 }
124         } catch (Exception e) {
125             throw new APIException(e);
126         } finally {
127             tt.done();
128         }
129         return this;
130     }
131
132     // @Override
133     public RosettaData<T> load(T t) throws APIException {
134         Parse<?,?> in = df.getIn(inType);
135         TimeTaken tt = in.start(trans);
136         try {
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);
141             } else {
142                 saved.extract(t, (Writer)null, df.marshal);
143             }
144         } catch (Exception e) {
145             throw new APIException(e);
146         } finally {
147             tt.done();
148         }
149         return this;
150     }
151
152     public Saved getEvents() {
153         return saved;
154     }
155     
156     // @Override
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?
160         try {
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);
167         } finally {
168             tt.done();
169         }
170     }
171
172     // @Override
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?
176         try {
177             if (outType==TYPE.XML) {
178                 if (xml==null) {
179                     StringWriter sw = new StringWriter();
180                     out.extract(null, sw, saved, options);
181                     xml = sw.toString();
182                 }
183                 return xml;
184             } else {  // is JSON
185                 if (json==null) {
186                     StringWriter sw = new StringWriter();
187                     out.extract(null, sw, saved, options);
188                     json = sw.toString();
189                 }
190                 return json;
191             }
192         } catch (Exception e) {
193             throw new APIException(e);
194         } finally {
195             tt.done();
196         }
197     }
198
199
200     // @Override
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?
204         try {
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());
209             } else { 
210                 out.extract(null, os, saved, options);
211             }
212         } catch (Exception e) {
213             throw new APIException(e);
214         } finally {
215             tt.done();
216         }
217         return this;
218     }
219
220     // @Override
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?
224         try {
225             if (outType==TYPE.XML && xml!=null) {
226                 writer.append(xml);
227             } else if (outType==TYPE.JSON && json!=null) {
228                 writer.append(json);
229             } else { 
230                 out.extract(null, writer, saved, options);
231             }
232         } catch (Exception e) {
233             throw new APIException(e);
234         } finally {
235             tt.done();
236         }
237         return this;
238     }
239     
240     // @Override
241     public Class<T> getTypeClass() {
242         return df.getTypeClass();
243     }
244
245     private static final boolean[] emptyOption = new boolean[0];
246     
247     public void direct(InputStream is, OutputStream os) throws APIException, IOException {
248         direct(is,os,emptyOption);
249     }
250     
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?
255         try {
256             out.extract(reader, writer, in,options);
257         } catch (Exception e) {
258             throw new APIException(e);
259         } finally {
260             tt.done();
261         }
262     }
263
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?
267         try {
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);
272             } else {
273                 out.extract(t, writer, df.marshal,options);
274             }
275         } catch (Exception e) {
276             throw new APIException(e);
277         } finally {
278             tt.done();
279         }
280     }
281
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?
285         try {
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);
289                 } else {
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);
293                 }
294             } else {
295                 out.extract(t, new OutputStreamWriter(os), df.marshal,options);
296             }
297
298         } catch (Exception e) {
299             throw new APIException(e);
300         } finally {
301             tt.done();
302         }
303     }
304
305     
306     public void direct(InputStream is, OutputStream os, boolean ... options) throws APIException, IOException {
307         direct(new InputStreamReader(is),new OutputStreamWriter(os), options);
308     }
309
310     // // @Override
311     public RosettaData<T> option(int option) {
312         options[0] = (option&Data.PRETTY)==Data.PRETTY;
313         options[1] = (option&Data.FRAGMENT)==Data.FRAGMENT;
314         return this;
315     }
316
317 }