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 dataAsString = stringifier.stringify(env, dataAsObject);
\r
121 return dataAsString;
\r
126 * Respond with the String if it exists, or marshal the String and pass the result back.
\r
128 * However, use the Env the Data Object was created with.
\r
131 * @throws APIException
\r
134 public String asString() throws APIException {
\r
135 if (dataAsString!=null) {
\r
136 return dataAsString;
\r
138 dataAsString = stringifier.stringify(creatingEnv, dataAsObject,options);
\r
139 return dataAsString;
\r
143 public Data<T> to(OutputStream os) throws APIException, IOException {
\r
144 if (dataAsString!=null) {
\r
145 os.write(dataAsString.getBytes());
\r
146 } else if (stringifier instanceof IOStringifier){
\r
147 ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, os, options);
\r
149 dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);
\r
150 os.write(dataAsString.getBytes());
\r
157 public JAXBData<T> to(Writer writer) throws APIException, IOException {
\r
158 if (dataAsString!=null) {
\r
159 writer.write(dataAsString);
\r
160 } else if (stringifier instanceof IOStringifier){
\r
161 ((IOStringifier<T>)stringifier).stringify(creatingEnv, dataAsObject, writer, options);
\r
163 dataAsString = stringifier.stringify(creatingEnv, dataAsObject, options);
\r
164 writer.write(dataAsString);
\r
170 public InputStream getInputStream() throws APIException {
\r
171 if (dataAsString==null) {
\r
172 dataAsString = stringifier.stringify(creatingEnv,dataAsObject,options);
\r
174 return new ByteArrayInputStream(dataAsString.getBytes());
\r
178 * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String
\r
179 * and pass the result back.<p>
\r
181 * Explicitly use a specific Env for logging purposes
\r
185 * @throws APIException
\r
188 public T asObject(EnvJAXB env) throws APIException {
\r
189 if (dataAsObject !=null) {
\r
190 return dataAsObject;
\r
192 // Some Java compilers need two statements here
\r
193 dataAsObject = objectifier.objectify(env, dataAsString);
\r
194 return dataAsObject;
\r
199 * Respond with the Object of type {@literal <T>} if it exists, or unmarshal from String
\r
200 * and pass the result back.<p>
\r
202 * However, use the Env the Data Object was created with.
\r
205 * @throws APIException
\r
208 public T asObject() throws APIException {
\r
209 if (dataAsObject !=null) {
\r
210 return dataAsObject;
\r
212 // Some Java compilers need two statements here
\r
213 dataAsObject = objectifier.objectify(creatingEnv, dataAsString);
\r
214 return dataAsObject;
\r
220 * Return the Class Type supported by this DataObject
\r
222 * @return {@literal Class<T>}
\r
225 public Class<T> getTypeClass() {
\r
231 * For Debugging Convenience, we marshal to String if possible.
\r
233 * Behavior is essentially the same as asString(), except asString() throws
\r
234 * an APIException. <p>
\r
235 * Since toString() must not throw exceptions, the function just catches and prints an
\r
236 * error, which is probably not the behavior desired.<p>
\r
238 * Therefore, use "asString()" where possible in actual Transactional code.
\r
240 * @see java.lang.Object#toString()
\r
243 public String toString() {
\r
244 if (dataAsString!=null) {
\r
245 return dataAsString;
\r
248 dataAsString = stringifier.stringify(creatingEnv, dataAsObject);
\r
249 return dataAsString;
\r
250 } catch (APIException e) {
\r
251 return "ERROR - Can't Stringify from Object " + e.getLocalizedMessage();
\r
256 public Data<T> load(T t) throws APIException {
\r
258 dataAsString = null;
\r
263 public Data<T> load(String str) throws APIException {
\r
264 dataAsObject = null;
\r
265 dataAsString = str;
\r
270 public Data<T> load(InputStream is) throws APIException {
\r
272 dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),is);
\r
273 dataAsString = null;
\r
274 } catch (JAXBException e) {
\r
275 throw new APIException(e);
\r
281 public Data<T> load(Reader rdr) throws APIException {
\r
283 dataAsObject = df.jumar.unmarshal(creatingEnv.debug(),rdr);
\r
284 dataAsString = null;
\r
285 } catch (JAXBException e) {
\r
286 throw new APIException(e);
\r
293 public void direct(InputStream input, OutputStream output) throws APIException, IOException {
\r
294 byte b[] = new byte[128];
\r
297 count = input.read(b);
\r
298 if (count>0)output.write(b, 0, count);
\r
299 } while (count>=0);
\r
304 public Data<T> out(TYPE type) {
\r
305 // it's going to be XML regardless...
\r
311 public Data<T> in(TYPE type) {
\r
312 // Not Supported... will still be XML
\r
318 public Data<T> option(int option) {
\r
319 options[0] = (option&Data.PRETTY)==Data.PRETTY;
\r
320 options[1] = (option&Data.FRAGMENT)==Data.FRAGMENT;
\r