9b8a2c9755dd11d7240c905bab2f5815789bb475
[aaf/authz.git] / misc / env / src / test / java / org / onap / aaf / misc / env / jaxb / JAXBStringifier.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.env.jaxb;
23
24 import java.io.OutputStream;
25 import java.io.StringWriter;
26 import java.io.Writer;
27
28 import javax.xml.bind.JAXBException;
29 import javax.xml.namespace.QName;
30
31 import org.onap.aaf.misc.env.APIException;
32 import org.onap.aaf.misc.env.Env;
33 import org.onap.aaf.misc.env.TimeTaken;
34 import org.onap.aaf.misc.env.old.IOStringifier;
35
36 public class JAXBStringifier<T> implements IOStringifier<T> {
37     private JAXBmar jmar;
38
39     public JAXBStringifier(Class<?>... classes) throws APIException {
40         try {
41             jmar = new JAXBmar(classes);
42         } catch (JAXBException e) {
43             throw new APIException(e);
44         }
45     }
46
47     public JAXBStringifier(QName qname, Class<?>... classes)
48             throws APIException {
49         try {
50             jmar = new JAXBmar(qname, classes);
51         } catch (JAXBException e) {
52             throw new APIException(e);
53         }
54     }
55     
56     // package on purpose
57     JAXBStringifier(JAXBmar jmar) {
58         this.jmar = jmar;
59     }
60
61     // // @Override
62     public void stringify(Env env, T input, Writer writer, boolean ... options)
63             throws APIException {
64         TimeTaken tt = env.start("JAXB Marshal", Env.XML);
65         try {
66             jmar.marshal(env.debug(), input, writer, options);
67         } catch (JAXBException e) {
68             throw new APIException(e);
69         } finally {
70             tt.done();
71         }
72     }
73
74     // @Override
75     public void stringify(Env env, T input, OutputStream os, boolean ... options)
76             throws APIException {
77         // TODO create an OutputStream that Counts?
78         TimeTaken tt = env.start("JAXB Marshal", Env.XML);
79         try {
80             jmar.marshal(env.debug(), input, os, options);
81         } catch (JAXBException e) {
82             throw new APIException(e);
83         } finally {
84             tt.done();
85         }
86     }
87
88     // @Override
89     public String stringify(Env env, T input, boolean ... options) throws APIException {
90         TimeTaken tt = env.start("JAXB Marshal", Env.XML);
91         StringWriter sw = new StringWriter();
92         try {
93             jmar.marshal(env.debug(), input, sw, options);
94             String rv = sw.toString();
95             tt.size(rv.length());
96             return rv;
97         } catch (JAXBException e) {
98             tt.size(0);
99             throw new APIException(e);
100         } finally {
101             tt.done();
102         }
103     }
104
105     // // @Override
106     public void servicePrestart(Env env) throws APIException {
107     }
108
109     // // @Override
110     public void threadPrestart(Env env) throws APIException {
111     }
112
113     // // @Override
114     public void refresh(Env env) throws APIException {
115     }
116
117     // // @Override
118     public void threadDestroy(Env env) throws APIException {
119     }
120
121     // // @Override
122     public void serviceDestroy(Env env) throws APIException {
123     }
124
125     // @Override
126     public JAXBStringifier<T> pretty(boolean pretty) {
127         jmar.pretty(pretty);
128         return this;
129     }
130
131     // @Override
132     public JAXBStringifier<T> asFragment(boolean fragment) {
133         jmar.asFragment(fragment);
134         return this;
135     }
136
137 }