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