2 * ============LICENSE_START====================================================
\r
4 * ===========================================================================
\r
5 * Copyright (c) 2018 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
22 package org.onap.aaf.misc.env.jaxb;
\r
24 import java.io.ByteArrayInputStream;
\r
25 import java.io.IOException;
\r
26 import java.io.InputStream;
\r
27 import java.io.OutputStream;
\r
28 import java.io.Reader;
\r
29 import java.io.Writer;
\r
31 import javax.xml.bind.JAXBException;
\r
33 import org.onap.aaf.misc.env.APIException;
\r
34 import org.onap.aaf.misc.env.Data;
\r
35 import org.onap.aaf.misc.env.Env;
\r
36 import org.onap.aaf.misc.env.EnvJAXB;
\r
37 import org.onap.aaf.misc.env.old.IOStringifier;
\r
38 import org.onap.aaf.misc.env.old.Objectifier;
\r
39 import org.onap.aaf.misc.env.old.Stringifier;
\r
42 * <i>Data</i> facilitates lazy marshaling of data with a pre-determined
\r
43 * marshaling mechanism.<p>
\r
45 * It stores either Object (defined by Generic {@literal <T>}) or String.<p>
\r
47 * On asking for Object of type {@literal <T>}, it will respond with the object
\r
48 * if it exists, or unmarshal the string and pass the result back.<p>
\r
50 * On asking for String, it will respond with the String
\r
51 * if it exists, or marshal the String and pass the result back.<p>
\r
57 public final class JAXBData<T> implements Data<T>{
\r
58 private Stringifier<T> stringifier;
\r
59 private Objectifier<T> objectifier;
\r
60 private String dataAsString;
\r
61 private T dataAsObject;
\r
62 private Class<T> tclass;
\r
63 private JAXBDF<T> df;
\r
64 private Env creatingEnv;
\r
65 private boolean options[] = new boolean[] {false, false};
\r
68 * Construct a Data Object with an appropriate Stringifier, Objectifier and Class to support
\r
76 JAXBData(Env env, JAXBDF<T> df, Stringifier<T> strfr, Objectifier<T> objfr, String text, Class<T> typeClass) {
\r
77 dataAsString = text;
\r
78 dataAsObject = null;
\r
79 stringifier = strfr;
\r
80 objectifier = objfr;
\r
88 * Construct a Data Object with an appropriate Stringifier, Objectifier and Object (which will
\r
96 @SuppressWarnings("unchecked")
\r
97 JAXBData(Env env, JAXBDF<T> df, Stringifier<T> strfr, Objectifier<T> objfr, T object) {
\r
98 dataAsString = null;
\r
99 dataAsObject = object;
\r
100 stringifier = strfr;
\r
101 objectifier = objfr;
\r
102 tclass = (Class<T>) object.getClass();
\r
108 * Respond with the String if it exists, or marshal the String and pass the result back.<p>
\r
110 * Explicitly use a specific Env for logging purposes
\r
114 * @throws APIException
\r
116 public String asString(EnvJAXB env) throws APIException {
\r
117 if(dataAsString!=null) {
\r
118 return dataAsString;
\r
120 return dataAsString = stringifier.stringify(env, dataAsObject);
\r
125 * Respond with the String if it exists, or marshal the String and pass the result back.
\r
127 * However, use the Env the Data Object was created with.
\r
130 * @throws APIException
\r
133 public String asString() throws APIException {
\r
134 if(dataAsString!=null) {
\r
135 return dataAsString;
\r
137 return dataAsString = stringifier.stringify(creatingEnv, dataAsObject,options);
\r
141 public Data<T> to(OutputStream os) throws APIException, IOException {
\r
142 if(dataAsString!=null) {
\r
143 os.write(dataAsString.getBytes());
\r
144 } else if (stringifier instanceof IOStringifier){
\r
145 ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, os, options);
\r
147 dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);
\r
148 os.write(dataAsString.getBytes());
\r
155 public JAXBData<T> to(Writer writer) throws APIException, IOException {
\r
156 if(dataAsString!=null) {
\r
157 writer.write(dataAsString);
\r
158 } else if (stringifier instanceof IOStringifier){
\r
159 ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, writer, options);
\r
161 dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);
\r
162 writer.write(dataAsString);
\r
168 public InputStream getInputStream() throws APIException {
\r
169 if(dataAsString==null) {
\r
170 dataAsString = stringifier.stringify(creatingEnv,dataAsObject,options);
\r
172 return new ByteArrayInputStream(dataAsString.getBytes());
\r
176 * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String
\r
177 * and pass the result back.<p>
\r
179 * Explicitly use a specific Env for logging purposes
\r
183 * @throws APIException
\r
186 public T asObject(EnvJAXB env) throws APIException {
\r
187 if(dataAsObject !=null) {
\r
188 return dataAsObject;
\r
190 // Some Java compilers need two statements here
\r
191 dataAsObject = objectifier.objectify(env, dataAsString);
\r
192 return dataAsObject;
\r
197 * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String
\r
198 * and pass the result back.<p>
\r
200 * However, use the Env the Data Object was created with.
\r
203 * @throws APIException
\r
206 public T asObject() throws APIException {
\r
207 if(dataAsObject !=null) {
\r
208 return dataAsObject;
\r
210 // Some Java compilers need two statements here
\r
211 dataAsObject = objectifier.objectify(creatingEnv, dataAsString);
\r
212 return dataAsObject;
\r
218 * Return the Class Type supported by this DataObject
\r
220 * @return {@literal Class<T>}
\r
223 public Class<T> getTypeClass() {
\r
229 * For Debugging Convenience, we marshal to String if possible.
\r
231 * Behavior is essentially the same as asString(), except asString() throws
\r
232 * an APIException. <p>
\r
233 * Since toString() must not throw exceptions, the function just catches and prints an
\r
234 * error, which is probably not the behavior desired.<p>
\r
236 * Therefore, use "asString()" where possible in actual Transactional code.
\r
238 * @see java.lang.Object#toString()
\r
241 public String toString() {
\r
242 if(dataAsString!=null) {
\r
243 return dataAsString;
\r
246 return dataAsString = stringifier.stringify(creatingEnv, dataAsObject);
\r
247 } catch (APIException e) {
\r
248 return "ERROR - Can't Stringify from Object " + e.getLocalizedMessage();
\r
253 public Data<T> load(T t) throws APIException {
\r
255 dataAsString = null;
\r
260 public Data<T> load(String str) throws APIException {
\r
261 dataAsObject = null;
\r
262 dataAsString = str;
\r
267 public Data<T> load(InputStream is) throws APIException {
\r
269 dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),is);
\r
270 dataAsString = null;
\r
271 } catch (JAXBException e) {
\r
272 throw new APIException(e);
\r
278 public Data<T> load(Reader rdr) throws APIException {
\r
280 dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),rdr);
\r
281 dataAsString = null;
\r
282 } catch (JAXBException e) {
\r
283 throw new APIException(e);
\r
290 public void direct(InputStream input, OutputStream output) throws APIException, IOException {
\r
291 byte b[] = new byte[128];
\r
294 count = input.read(b);
\r
295 if(count>0)output.write(b, 0, count);
\r
301 public Data<T> out(TYPE type) {
\r
302 // it's going to be XML regardless...
\r
308 public Data<T> in(TYPE type) {
\r
309 // Not Supported... will still be XML
\r
315 public Data<T> option(int option) {
\r
316 options[0] = (option&Data.PRETTY)==Data.PRETTY;
\r
317 options[1] = (option&Data.FRAGMENT)==Data.FRAGMENT;
\r