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