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