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.rosetta.env;
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;
32 import javax.xml.bind.JAXBException;
33 import javax.xml.namespace.QName;
34 import javax.xml.validation.Schema;
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;
58 public class RosettaDF<T> extends BaseDataFactory implements DataFactory<T> {
60 static InJson inJSON = new InJson();
63 static OutJson outJSON = new OutJson();
65 static OutRaw outRAW = new OutRaw();
67 // Temporary until we write JAXB impl...
71 private Parse<Reader,?> defaultIn;
72 private Out defaultOut;
73 private RosettaEnv env;
76 private int defOption;
77 Marshal<T> marshal = null;
81 * Private constructor to setup Type specific data manipulators
85 * @throws SecurityException
86 * @throws NoSuchFieldException
87 * @throws ClassNotFoundException
88 * @throws ParseException
89 * @throws JAXBException
92 RosettaDF(RosettaEnv env, Schema schema, String rootNs, Class<T> cls) throws APIException {
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);
102 defaultIn = inXML = new InXML(ji);
103 defaultOut = outXML = new OutXML(ji);
104 inType=outType=Data.TYPE.XML;
106 } catch (Exception e) {
107 throw new APIException(e);
113 public RosettaData<T> newData() {
114 RosettaData<T> data = new RosettaData<T>(env, this)
122 public RosettaData<T> newData(Env trans) {
123 RosettaData<T> data = new RosettaData<T>(trans, this)
130 @SuppressWarnings("unchecked")
132 public Class<T> getTypeClass() {
133 return (Class<T>)jaxMar.getMarshalClass();
136 public RosettaDF<T> in(Data.TYPE type) {
138 defaultIn=getIn(type==Data.TYPE.DEFAULT?Data.TYPE.JSON:type);
143 * If exists, first option is "Pretty", second is "Fragment"
148 public RosettaDF<T> out(Data.TYPE type) {
150 defaultOut = getOut(type==Data.TYPE.DEFAULT?Data.TYPE.JSON:type);
154 public Parse<Reader,?> getIn(Data.TYPE type) {
167 public Out getOut(Data.TYPE type) {
182 public int logType(org.onap.aaf.misc.env.Data.TYPE ot) {
192 public RosettaEnv getEnv() {
197 public Data.TYPE getInType() {
201 public Data.TYPE getOutType() {
205 public RosettaDF<T> option(int option) {
212 * Assigning Root Marshal Object
214 * Will wrap with DocMarshal Object if not already
219 public RosettaDF<T> rootMarshal(Marshal<T> marshal) {
220 if(marshal instanceof DocMarshal) {
221 this.marshal = marshal;
223 this.marshal = DocMarshal.root(marshal);
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?
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);
237 out.extract(t, new OutputStreamWriter(os), marshal,options);
239 } catch (Exception e) {
240 throw new APIException(e);
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?
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);
255 out.extract(t, writer, marshal,options);
257 } catch (Exception e) {
258 throw new APIException(e);