AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / rosetta / src / main / java / org / onap / aaf / misc / rosetta / env / RosettaDF.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.OutputStream;
26 import java.io.OutputStreamWriter;
27 import java.io.Reader;
28 import java.io.StringReader;
29 import java.io.StringWriter;
30 import java.io.Writer;
31
32 import javax.xml.bind.JAXBException;
33 import javax.xml.namespace.QName;
34 import javax.xml.validation.Schema;
35
36 import org.onap.aaf.misc.env.APIException;
37 import org.onap.aaf.misc.env.BaseDataFactory;
38 import org.onap.aaf.misc.env.Data;
39 import org.onap.aaf.misc.env.DataFactory;
40 import org.onap.aaf.misc.env.Env;
41 import org.onap.aaf.misc.env.TimeTaken;
42 import org.onap.aaf.misc.env.Trans;
43 import org.onap.aaf.misc.env.Data.TYPE;
44 import org.onap.aaf.misc.env.jaxb.JAXBmar;
45 import org.onap.aaf.misc.env.jaxb.JAXBumar;
46 import org.onap.aaf.misc.rosetta.InJson;
47 import org.onap.aaf.misc.rosetta.InXML;
48 import org.onap.aaf.misc.rosetta.JaxInfo;
49 import org.onap.aaf.misc.rosetta.Marshal;
50 import org.onap.aaf.misc.rosetta.Out;
51 import org.onap.aaf.misc.rosetta.OutJson;
52 import org.onap.aaf.misc.rosetta.OutRaw;
53 import org.onap.aaf.misc.rosetta.OutXML;
54 import org.onap.aaf.misc.rosetta.Parse;
55 import org.onap.aaf.misc.rosetta.ParseException;
56 import org.onap.aaf.misc.rosetta.marshal.DocMarshal;
57
58 public class RosettaDF<T> extends BaseDataFactory implements DataFactory<T>  {
59         
60         static InJson inJSON = new InJson();
61         InXML  inXML;
62
63         static OutJson outJSON = new OutJson();
64         OutXML outXML;
65         static OutRaw outRAW = new OutRaw();
66         
67         // Temporary until we write JAXB impl...
68         JAXBmar jaxMar;
69         JAXBumar jaxUmar;
70         
71         private Parse<Reader,?> defaultIn;
72         private Out defaultOut;
73         private RosettaEnv env;
74         private TYPE inType;
75         private TYPE outType;
76         private int defOption;
77         Marshal<T> marshal = null;
78         
79
80         /**
81          * Private constructor to setup Type specific data manipulators
82          * @param schema
83          * @param rootNs
84          * @param cls
85          * @throws SecurityException
86          * @throws NoSuchFieldException
87          * @throws ClassNotFoundException
88          * @throws ParseException
89          * @throws JAXBException
90          */
91         // package on purpose
92         RosettaDF(RosettaEnv env, Schema schema, String rootNs, Class<T> cls) throws APIException {
93                 this.env = env;
94                 try {
95                 // Note: rootNs can be null, in order to derive content from Class.  
96                 JaxInfo ji = rootNs==null?JaxInfo.build(cls):JaxInfo.build(cls,rootNs);
97                 // Note: JAXBmar sets qname to null if not exists
98                 jaxMar = new JAXBmar(rootNs==null?null:new QName("xmlns",rootNs),cls);
99                 // Note: JAXBumar sets schema to null if not exists
100                 jaxUmar = new JAXBumar(schema, cls);
101                 
102                 defaultIn = inXML = new InXML(ji);
103                 defaultOut = outXML = new OutXML(ji);
104                 inType=outType=Data.TYPE.XML;
105                 defOption = 0;
106                 } catch (Exception e) {
107                         throw new APIException(e);
108                 }
109         }
110         
111
112         // @Override
113         public RosettaData<T> newData() {
114                 RosettaData<T> data = new RosettaData<T>(env, this)                     
115                         .in(inType)
116                         .out(outType)
117                         .option(defOption);
118                 return data;
119         }
120
121         // @Override
122         public RosettaData<T> newData(Env trans) {
123                 RosettaData<T> data = new RosettaData<T>(trans, this)
124                         .in(inType)
125                         .out(outType)
126                         .option(defOption);
127                 return data;
128         }
129
130         @SuppressWarnings("unchecked")
131         // @Override
132         public Class<T> getTypeClass() {
133                 return (Class<T>)jaxMar.getMarshalClass();
134         }
135
136         public RosettaDF<T> in(Data.TYPE type) {
137                 inType = type;
138                 defaultIn=getIn(type==Data.TYPE.DEFAULT?Data.TYPE.JSON:type);
139                 return this;
140         }
141
142         /**
143          * If exists, first option is "Pretty", second is "Fragment"
144          * 
145          * @param options
146          * @return
147          */
148         public RosettaDF<T> out(Data.TYPE type) {
149                 outType = type;
150                 defaultOut = getOut(type==Data.TYPE.DEFAULT?Data.TYPE.JSON:type);
151                 return this;
152         }
153         
154         public Parse<Reader,?> getIn(Data.TYPE type) {
155                 switch(type) {
156                         case DEFAULT:
157                                 return defaultIn;
158                         case JSON:
159                                 return inJSON;
160                         case XML:
161                                 return inXML;
162                         default:
163                                 return defaultIn;
164                 }
165         }
166         
167         public Out getOut(Data.TYPE type) {
168                 switch(type) {
169                         case DEFAULT:
170                                 return defaultOut;
171                         case JSON:
172                                 return outJSON;
173                         case XML:
174                                 return outXML;
175                         case RAW:
176                                 return outRAW;
177                         default:
178                                 return defaultOut;
179                 }
180         }
181         
182         public int logType(org.onap.aaf.misc.env.Data.TYPE ot) {
183                 switch(ot) {
184                         case JSON:
185                                 return Env.JSON;
186                         default:
187                                 return Env.XML;
188                 }
189         }
190
191
192         public RosettaEnv getEnv() {
193                 return env;
194         }
195
196
197         public Data.TYPE getInType() {
198                 return inType;
199         }
200
201         public Data.TYPE getOutType() {
202                 return outType;
203         }
204
205         public RosettaDF<T> option(int option) {
206                 defOption = option;
207                 
208                 return this;
209         }
210
211         /**
212          * Assigning Root Marshal Object
213          * 
214          * Will wrap with DocMarshal Object if not already
215          * 
216          * @param marshal
217          * @return
218          */
219         public RosettaDF<T> rootMarshal(Marshal<T> marshal) {
220                 if(marshal instanceof DocMarshal) {
221                         this.marshal = marshal;
222                 } else {
223                         this.marshal = DocMarshal.root(marshal);
224                 }
225                 return this;
226         }
227         
228         public void direct(Trans trans, T t, OutputStream os, boolean ... options) throws APIException, IOException {
229                 Out out = getOut(outType);
230                 TimeTaken tt = trans.start(out.logName(),logType(outType)); // determine from Out.. without dependency on Env?
231                 try {
232                         if(marshal==null) { // Unknown marshaller... do working XML marshal/extraction
233                                 StringWriter sw = new StringWriter();
234                                 jaxMar.marshal(trans.debug(), t, sw, options);
235                                 out.extract(new StringReader(sw.toString()), new OutputStreamWriter(os), inXML,options);
236                         } else {
237                                 out.extract(t, new OutputStreamWriter(os), marshal,options);
238                         }
239                 } catch (Exception e) {
240                         throw new APIException(e);
241                 } finally {
242                         tt.done();
243                 }
244         }
245
246         public void direct(Trans trans, T t, Writer writer, boolean ... options) throws APIException, IOException {
247                 Out out = getOut(outType);
248                 TimeTaken tt = trans.start(out.logName(),logType(outType)); // determine from Out.. without dependency on Env?
249                 try {
250                         if(marshal==null) { // Unknown marshaller... do working XML marshal/extraction
251                                 StringWriter sw = new StringWriter();
252                                 jaxMar.marshal(trans.debug(), t, sw, options);
253                                 out.extract(new StringReader(sw.toString()), writer, inXML,options);
254                         } else {
255                                 out.extract(t, writer, marshal,options);
256                         }
257                 } catch (Exception e) {
258                         throw new APIException(e);
259                 } finally {
260                         tt.done();
261                 }
262         }
263
264
265 }