2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.misc.env.jaxb;
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;
30 import javax.xml.bind.JAXBException;
31 import javax.xml.namespace.QName;
32 import javax.xml.validation.Schema;
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;
44 public class JAXBDF<T> extends BaseDataFactory implements OldDataFactory<T>,IOObjectifier<T>, IOStringifier<T> {
50 public JAXBDF(EnvJAXB env, Class<?> ... classes) throws APIException {
53 jumar = new JAXBumar(classes);
54 jmar = new JAXBmar(classes) ;
55 } catch (JAXBException e) {
56 throw new APIException(e);
60 public JAXBDF(EnvJAXB env, Schema schema, Class<?> ... classes) throws APIException {
63 jumar = new JAXBumar(schema, classes);
64 jmar = new JAXBmar(classes);
65 } catch (JAXBException e) {
66 throw new APIException(e);
70 public JAXBDF(EnvJAXB env, QName qname, Class<?> ... classes) throws APIException {
73 jumar = new JAXBumar(classes);
74 jmar = new JAXBmar(qname, classes);
75 } catch (JAXBException e) {
76 throw new APIException(e);
80 public JAXBDF(EnvJAXB env, Schema schema, QName qname, Class<?> ... classes) throws APIException {
83 jumar = new JAXBumar(schema, classes);
84 jmar = new JAXBmar(qname, classes);
85 } catch (JAXBException e) {
86 throw new APIException(e);
91 public T newInstance() throws APIException {
93 return jumar.newInstance();
94 } catch (Exception e) {
95 throw new APIException(e);
100 public IOStringifier<T> pretty(boolean pretty) {
106 public IOStringifier<T> asFragment(boolean fragment) {
107 jmar.asFragment(fragment);
112 public void servicePrestart(Env env) throws APIException {
116 public void threadPrestart(Env env) throws APIException {
120 public void refresh(Env env) throws APIException {
124 public void threadDestroy(Env env) throws APIException {
128 public void serviceDestroy(Env env) throws APIException {
131 @SuppressWarnings("unchecked")
133 public Data<T> newData() {
134 return new JAXBData<T>(primaryEnv, this, new JAXBStringifier<T>(jmar), new JAXBObjectifier<T>(jumar),"",(Class<T>)jmar.getMarshalClass());
137 @SuppressWarnings("unchecked")
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());
144 public Data<T> newData(T type) {
145 return new JAXBData<T>(primaryEnv, this, new JAXBStringifier<T>(jmar), new JAXBObjectifier<T>(jumar), type);
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.
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);
160 @SuppressWarnings("unchecked")
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());
166 /////////// Old DataFactory Interface
168 public String stringify(T type) throws APIException {
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);
179 public void stringify(T type, Writer writer) throws APIException {
181 jmar.marshal(primaryEnv.debug(), type, writer);
182 } catch (JAXBException e) {
183 throw new APIException(e);
188 public void stringify(T type, OutputStream os) throws APIException {
190 jmar.marshal(primaryEnv.debug(), type, os);
191 } catch (JAXBException e) {
192 throw new APIException(e);
196 /////////// New DataFactory Interface
198 public String stringify(Env env, T input, boolean ... options) throws APIException {
200 StringWriter sw = new StringWriter();
201 TimeTaken tt = env.start("JAXB Stringify", Env.XML);
203 jmar.marshal(env.debug(), input, sw, options);
207 String str = sw.toString();
208 tt.size(str.getBytes().length);
210 } catch (JAXBException e) {
211 throw new APIException(e);
216 public void stringify(Env env, T input, Writer writer, boolean ... options) throws APIException {
217 TimeTaken tt = env.start("JAXB Stringify", Env.XML);
219 jmar.marshal(env.debug(), input, writer, options);
220 } catch (JAXBException e) {
221 throw new APIException(e);
228 public void stringify(Env env, T input, OutputStream os, boolean ... options) throws APIException {
229 TimeTaken tt = env.start("JAXB Stringify", Env.XML);
231 jmar.marshal(env.debug(), input, os, options);
232 } catch (JAXBException e) {
233 throw new APIException(e);
240 public T objectify(Env env, Reader rdr) throws APIException {
241 TimeTaken tt = env.start("JAXB Objectify", Env.XML);
243 return jumar.unmarshal(env.debug(), rdr);
244 } catch (JAXBException e) {
245 throw new APIException(e);
252 public T objectify(Reader rdr) throws APIException {
254 return jumar.unmarshal(primaryEnv.debug(), rdr);
255 } catch (JAXBException e) {
256 throw new APIException(e);
261 public T objectify(Env env, InputStream is) throws APIException {
262 TimeTaken tt = env.start("JAXB Objectify", Env.XML);
264 return jumar.unmarshal(env.debug(), is);
265 } catch (JAXBException e) {
266 throw new APIException(e);
273 public T objectify(InputStream is) throws APIException {
275 return jumar.unmarshal(primaryEnv.debug(), is);
276 } catch (JAXBException e) {
277 throw new APIException(e);
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);
286 return jumar.unmarshal(env.debug(), input);
287 } catch (JAXBException e) {
288 throw new APIException(e);
295 public T objectify(String text) throws APIException {
297 return jumar.unmarshal(primaryEnv.debug(), text);
298 } catch (JAXBException e) {
299 throw new APIException(e);
303 @SuppressWarnings("unchecked")
305 public Class<T> getTypeClass() {
306 return (Class<T>)jmar.getMarshalClass();