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.OutputStream;
\r
25 import java.io.StringWriter;
\r
26 import java.io.Writer;
\r
28 import javax.xml.bind.JAXBException;
\r
29 import javax.xml.namespace.QName;
\r
31 import org.onap.aaf.misc.env.APIException;
\r
32 import org.onap.aaf.misc.env.Env;
\r
33 import org.onap.aaf.misc.env.TimeTaken;
\r
34 import org.onap.aaf.misc.env.old.IOStringifier;
\r
36 public class JAXBStringifier<T> implements IOStringifier<T> {
\r
37 private JAXBmar jmar;
\r
39 public JAXBStringifier(Class<?>... classes) throws APIException {
\r
41 jmar = new JAXBmar(classes);
\r
42 } catch (JAXBException e) {
\r
43 throw new APIException(e);
\r
47 public JAXBStringifier(QName qname, Class<?>... classes)
\r
48 throws APIException {
\r
50 jmar = new JAXBmar(qname, classes);
\r
51 } catch (JAXBException e) {
\r
52 throw new APIException(e);
\r
56 // package on purpose
\r
57 JAXBStringifier(JAXBmar jmar) {
\r
62 public void stringify(Env env, T input, Writer writer, boolean ... options)
\r
63 throws APIException {
\r
64 TimeTaken tt = env.start("JAXB Marshal", Env.XML);
\r
66 jmar.marshal(env.debug(), input, writer, options);
\r
67 } catch (JAXBException e) {
\r
68 throw new APIException(e);
\r
75 public void stringify(Env env, T input, OutputStream os, boolean ... options)
\r
76 throws APIException {
\r
77 // TODO create an OutputStream that Counts?
\r
78 TimeTaken tt = env.start("JAXB Marshal", Env.XML);
\r
80 jmar.marshal(env.debug(), input, os, options);
\r
81 } catch (JAXBException e) {
\r
82 throw new APIException(e);
\r
89 public String stringify(Env env, T input, boolean ... options) throws APIException {
\r
90 TimeTaken tt = env.start("JAXB Marshal", Env.XML);
\r
91 StringWriter sw = new StringWriter();
\r
93 jmar.marshal(env.debug(), input, sw, options);
\r
94 String rv = sw.toString();
\r
95 tt.size(rv.length());
\r
97 } catch (JAXBException e) {
\r
99 throw new APIException(e);
\r
106 public void servicePrestart(Env env) throws APIException {
\r
110 public void threadPrestart(Env env) throws APIException {
\r
114 public void refresh(Env env) throws APIException {
\r
118 public void threadDestroy(Env env) throws APIException {
\r
122 public void serviceDestroy(Env env) throws APIException {
\r
126 public JAXBStringifier<T> pretty(boolean pretty) {
\r
127 jmar.pretty(pretty);
\r
132 public JAXBStringifier<T> asFragment(boolean fragment) {
\r
133 jmar.asFragment(fragment);
\r