Mass whitespace changes (Style Warnings)
[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 //    // @Override
62     public RosettaData<T> in(TYPE rosettaType) {
63         inType = rosettaType;
64         return this;
65     }
66     
67 //    // @Override
68     public RosettaData<T> out(TYPE rosettaType) {
69         outType = rosettaType;
70         return this;
71     }
72
73 //    // @Override
74     public RosettaData<T> load(Reader rdr) throws APIException {
75         Parse<Reader,?> in = df.getIn(inType);
76         TimeTaken tt = in.start(trans);
77         try {
78             saved.extract(rdr, (Writer)null, in);
79             xml=json=null;
80         } catch (Exception e) {
81             throw new APIException(e);
82         } finally {
83             tt.done();
84         }
85         return this;
86     }
87     
88     // @Override
89     public RosettaData<T> load(InputStream is) throws APIException {
90         Parse<Reader,?> in = df.getIn(inType);
91         TimeTaken tt = in.start(trans);
92         try {
93             saved.extract(new InputStreamReader(is), (Writer)null, in);
94             xml=json=null;
95         } catch (Exception e) {
96             throw new APIException(e);
97         } finally {
98             tt.done();
99         }
100         return this;
101     }
102
103     // @Override
104     public RosettaData<T> load(String str) throws APIException {
105         Parse<Reader,?> in = df.getIn(inType);
106         TimeTaken tt = in.start(trans);
107         try {
108             saved.extract(new StringReader(str), (Writer)null, in);
109             switch(inType) {
110                 case XML:
111                     xml = str;
112                     break;
113                 case JSON:
114                     json = str;
115                     break;
116                 default:
117                     
118                 }
119         } catch (Exception e) {
120             throw new APIException(e);
121         } finally {
122             tt.done();
123         }
124         return this;
125     }
126
127     // @Override
128     public RosettaData<T> load(T t) throws APIException {
129         Parse<?,?> in = df.getIn(inType);
130         TimeTaken tt = in.start(trans);
131         try {
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);
136             } else {
137                 saved.extract(t, (Writer)null, df.marshal);
138             }
139         } catch (Exception e) {
140             throw new APIException(e);
141         } finally {
142             tt.done();
143         }
144         return this;
145     }
146
147     public Saved getEvents() {
148         return saved;
149     }
150     
151     // @Override
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?
155         try {
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);
162         } finally {
163             tt.done();
164         }
165     }
166
167     // @Override
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?
171         try {
172             if (outType==TYPE.XML) {
173                 if (xml==null) {
174                     StringWriter sw = new StringWriter();
175                     out.extract(null, sw, saved, options);
176                     xml = sw.toString();
177                 }
178                 return xml;
179             } else {  // is JSON
180                 if (json==null) {
181                     StringWriter sw = new StringWriter();
182                     out.extract(null, sw, saved, options);
183                     json = sw.toString();
184                 }
185                 return json;
186             }
187         } catch (Exception e) {
188             throw new APIException(e);
189         } finally {
190             tt.done();
191         }
192     }
193
194
195     // @Override
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?
199         try {
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());
204             } else { 
205                 out.extract(null, os, saved, options);
206             }
207         } catch (Exception e) {
208             throw new APIException(e);
209         } finally {
210             tt.done();
211         }
212         return this;
213     }
214
215     // @Override
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?
219         try {
220             if (outType==TYPE.XML && xml!=null) {
221                 writer.append(xml);
222             } else if (outType==TYPE.JSON && json!=null) {
223                 writer.append(json);
224             } else { 
225                 out.extract(null, writer, saved, options);
226             }
227         } catch (Exception e) {
228             throw new APIException(e);
229         } finally {
230             tt.done();
231         }
232         return this;
233     }
234     
235     // @Override
236     public Class<T> getTypeClass() {
237         return df.getTypeClass();
238     }
239
240     private static final boolean[] emptyOption = new boolean[0];
241     
242     public void direct(InputStream is, OutputStream os) throws APIException, IOException {
243         direct(is,os,emptyOption);
244     }
245     
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?
250         try {
251             out.extract(reader, writer, in,options);
252         } catch (Exception e) {
253             throw new APIException(e);
254         } finally {
255             tt.done();
256         }
257     }
258
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?
262         try {
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);
267             } else {
268                 out.extract(t, writer, df.marshal,options);
269             }
270         } catch (Exception e) {
271             throw new APIException(e);
272         } finally {
273             tt.done();
274         }
275     }
276
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?
280         try {
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);
284                 } else {
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);
288                 }
289             } else {
290                 out.extract(t, new OutputStreamWriter(os), df.marshal,options);
291             }
292
293         } catch (Exception e) {
294             throw new APIException(e);
295         } finally {
296             tt.done();
297         }
298     }
299
300     
301     public void direct(InputStream is, OutputStream os, boolean ... options) throws APIException, IOException {
302         direct(new InputStreamReader(is),new OutputStreamWriter(os), options);
303     }
304
305     // // @Override
306     public RosettaData<T> option(int option) {
307         options[0] = (option&Data.PRETTY)==Data.PRETTY;
308         options[1] = (option&Data.FRAGMENT)==Data.FRAGMENT;
309         return this;
310     }
311
312 }