1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\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
11 * * http://www.apache.org/licenses/LICENSE-2.0
\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
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package com.att.rosetta.env;
\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
33 import javax.xml.bind.JAXBException;
\r
34 import javax.xml.namespace.QName;
\r
35 import javax.xml.validation.Schema;
\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
59 public class RosettaDF<T> extends BaseDataFactory implements DataFactory<T> {
\r
61 static InJson inJSON = new InJson();
\r
64 static OutJson outJSON = new OutJson();
\r
66 static OutRaw outRAW = new OutRaw();
\r
68 // Temporary until we write JAXB impl...
\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
82 * Private constructor to setup Type specific data manipulators
\r
86 * @throws SecurityException
\r
87 * @throws NoSuchFieldException
\r
88 * @throws ClassNotFoundException
\r
89 * @throws ParseException
\r
90 * @throws JAXBException
\r
92 // package on purpose
\r
93 RosettaDF(RosettaEnv env, Schema schema, String rootNs, Class<T> cls) throws APIException {
\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
103 defaultIn = inXML = new InXML(ji);
\r
104 defaultOut = outXML = new OutXML(ji);
\r
105 inType=outType=Data.TYPE.XML;
\r
107 } catch (Exception e) {
\r
108 throw new APIException(e);
\r
114 public RosettaData<T> newData() {
\r
115 RosettaData<T> data = new RosettaData<T>(env, this)
\r
118 .option(defOption);
\r
123 public RosettaData<T> newData(Env trans) {
\r
124 RosettaData<T> data = new RosettaData<T>(trans, this)
\r
127 .option(defOption);
\r
131 @SuppressWarnings("unchecked")
\r
133 public Class<T> getTypeClass() {
\r
134 return (Class<T>)jaxMar.getMarshalClass();
\r
137 public RosettaDF<T> in(Data.TYPE type) {
\r
139 defaultIn=getIn(type==Data.TYPE.DEFAULT?Data.TYPE.JSON:type);
\r
144 * If exists, first option is "Pretty", second is "Fragment"
\r
149 public RosettaDF<T> out(Data.TYPE type) {
\r
151 defaultOut = getOut(type==Data.TYPE.DEFAULT?Data.TYPE.JSON:type);
\r
155 public Parse<Reader,?> getIn(Data.TYPE type) {
\r
168 public Out getOut(Data.TYPE type) {
\r
183 public int logType(com.att.inno.env.Data.TYPE ot) {
\r
193 public RosettaEnv getEnv() {
\r
198 public Data.TYPE getInType() {
\r
202 public Data.TYPE getOutType() {
\r
206 public RosettaDF<T> option(int option) {
\r
207 defOption = option;
\r
213 * Assigning Root Marshal Object
\r
215 * Will wrap with DocMarshal Object if not already
\r
220 public RosettaDF<T> rootMarshal(Marshal<T> marshal) {
\r
221 if(marshal instanceof DocMarshal) {
\r
222 this.marshal = marshal;
\r
224 this.marshal = DocMarshal.root(marshal);
\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
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
238 out.extract(t, new OutputStreamWriter(os), marshal,options);
\r
240 } catch (Exception e) {
\r
241 throw new APIException(e);
\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
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
256 out.extract(t, writer, marshal,options);
\r
258 } catch (Exception e) {
\r
259 throw new APIException(e);
\r