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.inno.env.jaxb;
\r
25 import java.io.OutputStream;
\r
26 import java.io.StringWriter;
\r
27 import java.io.Writer;
\r
29 import javax.xml.bind.JAXBException;
\r
30 import javax.xml.namespace.QName;
\r
32 import com.att.inno.env.APIException;
\r
33 import com.att.inno.env.Env;
\r
34 import com.att.inno.env.TimeTaken;
\r
35 import com.att.inno.env.old.IOStringifier;
\r
37 public class JAXBStringifier<T> implements IOStringifier<T> {
\r
38 private JAXBmar jmar;
\r
40 public JAXBStringifier(Class<?>... classes) throws APIException {
\r
42 jmar = new JAXBmar(classes);
\r
43 } catch (JAXBException e) {
\r
44 throw new APIException(e);
\r
48 public JAXBStringifier(QName qname, Class<?>... classes)
\r
49 throws APIException {
\r
51 jmar = new JAXBmar(qname, classes);
\r
52 } catch (JAXBException e) {
\r
53 throw new APIException(e);
\r
57 // package on purpose
\r
58 JAXBStringifier(JAXBmar jmar) {
\r
63 public void stringify(Env env, T input, Writer writer, boolean ... options)
\r
64 throws APIException {
\r
65 TimeTaken tt = env.start("JAXB Marshal", Env.XML);
\r
67 jmar.marshal(env.debug(), input, writer, options);
\r
68 } catch (JAXBException e) {
\r
69 throw new APIException(e);
\r
76 public void stringify(Env env, T input, OutputStream os, boolean ... options)
\r
77 throws APIException {
\r
78 // TODO create an OutputStream that Counts?
\r
79 TimeTaken tt = env.start("JAXB Marshal", Env.XML);
\r
81 jmar.marshal(env.debug(), input, os, options);
\r
82 } catch (JAXBException e) {
\r
83 throw new APIException(e);
\r
90 public String stringify(Env env, T input, boolean ... options) throws APIException {
\r
91 TimeTaken tt = env.start("JAXB Marshal", Env.XML);
\r
92 StringWriter sw = new StringWriter();
\r
94 jmar.marshal(env.debug(), input, sw, options);
\r
95 String rv = sw.toString();
\r
96 tt.size(rv.length());
\r
98 } catch (JAXBException e) {
\r
100 throw new APIException(e);
\r
107 public void servicePrestart(Env env) throws APIException {
\r
111 public void threadPrestart(Env env) throws APIException {
\r
115 public void refresh(Env env) throws APIException {
\r
119 public void threadDestroy(Env env) throws APIException {
\r
123 public void serviceDestroy(Env env) throws APIException {
\r
127 public JAXBStringifier<T> pretty(boolean pretty) {
\r
128 jmar.pretty(pretty);
\r
133 public JAXBStringifier<T> asFragment(boolean fragment) {
\r
134 jmar.asFragment(fragment);
\r