AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / env / src / main / java / org / onap / aaf / misc / env / jaxb / JAXBDF.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.env.jaxb;
23
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.Reader;
27 import java.io.StringWriter;
28 import java.io.Writer;
29
30 import javax.xml.bind.JAXBException;
31 import javax.xml.namespace.QName;
32 import javax.xml.validation.Schema;
33
34 import org.onap.aaf.misc.env.APIException;
35 import org.onap.aaf.misc.env.BaseDataFactory;
36 import org.onap.aaf.misc.env.Data;
37 import org.onap.aaf.misc.env.Env;
38 import org.onap.aaf.misc.env.EnvJAXB;
39 import org.onap.aaf.misc.env.TimeTaken;
40 import org.onap.aaf.misc.env.old.IOObjectifier;
41 import org.onap.aaf.misc.env.old.IOStringifier;
42 import org.onap.aaf.misc.env.old.OldDataFactory;
43
44 public class JAXBDF<T> extends BaseDataFactory implements OldDataFactory<T>,IOObjectifier<T>, IOStringifier<T> {
45         // Package on purpose
46         EnvJAXB primaryEnv;
47         JAXBumar jumar;
48         JAXBmar jmar;
49
50         public JAXBDF(EnvJAXB env, Class<?> ... classes) throws APIException {
51                 try {
52                         primaryEnv = env;
53                         jumar = new JAXBumar(classes);
54                         jmar = new JAXBmar(classes) ;
55                 } catch (JAXBException e) {
56                         throw new APIException(e);
57                 }
58         }
59         
60         public JAXBDF(EnvJAXB env, Schema schema, Class<?> ... classes) throws APIException {
61                 try {
62                         primaryEnv = env;
63                         jumar = new JAXBumar(schema, classes);
64                         jmar = new JAXBmar(classes);
65                 } catch (JAXBException e) {
66                         throw new APIException(e);
67                 }
68         }
69         
70         public JAXBDF(EnvJAXB env, QName qname, Class<?> ... classes) throws APIException {
71                 try {
72                         primaryEnv = env;
73                         jumar = new JAXBumar(classes);
74                         jmar = new JAXBmar(qname, classes);
75                 } catch (JAXBException e) {
76                         throw new APIException(e);
77                 }
78         }
79
80         public JAXBDF(EnvJAXB env, Schema schema, QName qname, Class<?> ... classes) throws APIException {
81                 try {
82                         primaryEnv = env;
83                         jumar = new JAXBumar(schema, classes);
84                         jmar = new JAXBmar(qname, classes);
85                 } catch (JAXBException e) {
86                         throw new APIException(e);
87                 }
88         }
89         
90         // @Override
91         public T newInstance() throws APIException {
92                 try {
93                         return jumar.newInstance();
94                 } catch (Exception e) {
95                         throw new APIException(e);
96                 }
97         }
98
99         // @Override
100         public IOStringifier<T> pretty(boolean pretty) {
101                 jmar.pretty(pretty);
102                 return this;
103         }
104
105         // @Override
106         public IOStringifier<T> asFragment(boolean fragment) {
107                 jmar.asFragment(fragment);
108                 return this;
109         }
110
111         // @Override
112         public void servicePrestart(Env env) throws APIException {
113         }
114
115         // @Override
116         public void threadPrestart(Env env) throws APIException {
117         }
118
119         // @Override
120         public void refresh(Env env) throws APIException {
121         }
122
123         // @Override
124         public void threadDestroy(Env env) throws APIException {
125         }
126
127         // @Override
128         public void serviceDestroy(Env env) throws APIException {
129         }
130
131         @SuppressWarnings("unchecked")
132         // @Override
133         public Data<T> newData() {
134                 return new JAXBData<T>(primaryEnv, this, new JAXBStringifier<T>(jmar), new JAXBObjectifier<T>(jumar),"",(Class<T>)jmar.getMarshalClass());
135         }
136
137         @SuppressWarnings("unchecked")
138         // @Override
139         public Data<T> newData(Env env) {
140                 return new JAXBData<T>(env, this,new JAXBStringifier<T>(jmar), new JAXBObjectifier<T>(jumar),"",(Class<T>)jmar.getMarshalClass());
141         }
142
143         // @Override
144         public Data<T> newData(T type) {
145                 return new JAXBData<T>(primaryEnv, this, new JAXBStringifier<T>(jmar), new JAXBObjectifier<T>(jumar), type);
146         }
147
148         // @Override
149         public Data<T> newDataFromStream(Env env, InputStream input) throws APIException {
150                 //TODO Write an unvalidated String using STAX checking for end of Doc?
151                 // perhaps key evaluation as well.
152                 try {
153                         T t = jumar.unmarshal(env.debug(), input);
154                         return new JAXBData<T>(primaryEnv, this, new JAXBStringifier<T>(jmar), new JAXBObjectifier<T>(jumar),t);
155                 } catch(JAXBException e) {
156                         throw new APIException(e);
157                 }
158         }
159
160         @SuppressWarnings("unchecked")
161         // @Override
162         public Data<T> newDataFromString(String string) {
163                 return new JAXBData<T>(primaryEnv, this,new JAXBStringifier<T>(jmar), new JAXBObjectifier<T>(jumar), string,(Class<T>)jmar.getMarshalClass());
164         }
165
166         /////////// Old DataFactory Interface 
167         // @Override
168         public String stringify(T type) throws APIException {
169                 try {
170                         StringWriter sw = new StringWriter();
171                         jmar.marshal(primaryEnv.debug(), type, sw);
172                         return sw.toString();
173                 } catch (JAXBException e) {
174                         throw new APIException(e);
175                 }       
176         }
177
178         // @Override
179         public void stringify(T type, Writer writer) throws APIException {
180                 try {
181                         jmar.marshal(primaryEnv.debug(), type, writer);
182                 } catch (JAXBException e) {
183                         throw new APIException(e);
184                 }       
185         }
186
187         // @Override
188         public void stringify(T type, OutputStream os) throws APIException {
189                 try {
190                         jmar.marshal(primaryEnv.debug(), type, os);
191                 } catch (JAXBException e) {
192                         throw new APIException(e);
193                 }       
194         }
195
196         /////////// New DataFactory Interface 
197         // @Override
198         public String stringify(Env env, T input, boolean ... options) throws APIException {
199                 try {
200                         StringWriter sw = new StringWriter();
201                         TimeTaken tt = env.start("JAXB Stringify", Env.XML);
202                         try {
203                                 jmar.marshal(env.debug(), input, sw, options);
204                         } finally {
205                                 tt.done();
206                         }
207                         String str = sw.toString();
208                         tt.size(str.getBytes().length);
209                         return str;
210                 } catch (JAXBException e) {
211                         throw new APIException(e);
212                 }
213         }
214
215         // @Override
216         public void stringify(Env env, T input, Writer writer, boolean ... options) throws APIException {
217                 TimeTaken tt = env.start("JAXB Stringify", Env.XML);
218                 try {
219                         jmar.marshal(env.debug(), input, writer, options);
220                 } catch (JAXBException e) {
221                         throw new APIException(e);
222                 } finally {
223                         tt.done();
224                 }
225         }
226
227         // @Override
228         public void stringify(Env env, T input, OutputStream os, boolean ... options) throws APIException {
229                 TimeTaken tt = env.start("JAXB Stringify", Env.XML);
230                 try {
231                         jmar.marshal(env.debug(), input, os, options);
232                 } catch (JAXBException e) {
233                         throw new APIException(e);
234                 } finally {
235                         tt.done();
236                 }
237         }
238
239         // @Override
240         public T objectify(Env env, Reader rdr) throws APIException {
241                 TimeTaken tt = env.start("JAXB Objectify", Env.XML);
242                 try {
243                         return jumar.unmarshal(env.debug(), rdr);
244                 } catch (JAXBException e) {
245                         throw new APIException(e);
246                 } finally {
247                         tt.done();
248                 }
249         }
250
251         // @Override
252         public T objectify(Reader rdr) throws APIException {
253                 try {
254                         return jumar.unmarshal(primaryEnv.debug(), rdr);
255                 } catch (JAXBException e) {
256                         throw new APIException(e);
257                 }       
258         }
259
260         // @Override
261         public T objectify(Env env, InputStream is) throws APIException {
262                 TimeTaken tt = env.start("JAXB Objectify", Env.XML);
263                 try {
264                         return jumar.unmarshal(env.debug(), is);
265                 } catch (JAXBException e) {
266                         throw new APIException(e);
267                 } finally {
268                         tt.done();
269                 }
270         }
271
272         // @Override
273         public T objectify(InputStream is) throws APIException {
274                 try {
275                         return jumar.unmarshal(primaryEnv.debug(), is);
276                 } catch (JAXBException e) {
277                         throw new APIException(e);
278                 }       
279         }
280
281         // @Override
282         public T objectify(Env env, String input) throws APIException {
283                 TimeTaken tt = env.start("JAXB Objectify", Env.XML);
284                 tt.size(input.getBytes().length);
285                 try {
286                         return jumar.unmarshal(env.debug(), input);
287                 } catch (JAXBException e) {
288                         throw new APIException(e);
289                 } finally {
290                         tt.done();
291                 }
292         }
293
294         // @Override
295         public T objectify(String text) throws APIException {
296                 try {
297                         return jumar.unmarshal(primaryEnv.debug(), text);
298                 } catch (JAXBException e) {
299                         throw new APIException(e);
300                 }       
301         }
302
303         @SuppressWarnings("unchecked")
304         // @Override
305         public Class<T> getTypeClass() {
306                 return (Class<T>)jmar.getMarshalClass();
307         }
308
309 }