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