1
1
package fr .insee .pogues .conversion ;
2
2
3
- import java .io .ByteArrayOutputStream ;
4
- import java .io .File ;
5
- import java .io .StringReader ;
6
- import java .io .UnsupportedEncodingException ;
7
-
8
- import javax .xml .bind .JAXBContext ;
9
- import javax .xml .bind .JAXBElement ;
10
- import javax .xml .bind .JAXBException ;
11
- import javax .xml .bind .Marshaller ;
12
- import javax .xml .bind .Unmarshaller ;
13
- import javax .xml .transform .stream .StreamSource ;
14
-
3
+ import fr .insee .pogues .model .*;
4
+ import org .eclipse .persistence .jaxb .MarshallerProperties ;
15
5
import org .slf4j .Logger ;
16
6
import org .slf4j .LoggerFactory ;
17
- import org .eclipse .persistence .jaxb .MarshallerProperties ;
18
7
19
- import fr .insee .pogues .model .CodeLists ;
20
- import fr .insee .pogues .model .QuestionType ;
21
- import fr .insee .pogues .model .Questionnaire ;
22
- import fr .insee .pogues .model .SequenceType ;
8
+ import javax .xml .bind .*;
9
+ import javax .xml .transform .stream .StreamSource ;
10
+ import java .io .ByteArrayOutputStream ;
11
+ import java .io .File ;
12
+ import java .io .StringReader ;
13
+ import java .nio .charset .StandardCharsets ;
23
14
24
15
public class XMLToJSONTranslator {
25
16
26
- private boolean monitored ;
17
+ private static final Logger logger = LoggerFactory .getLogger (XMLToJSONTranslator .class );
18
+ public static final String START_DEBUG_MESSAGE = "Preparing to translate from XML to JSON" ;
19
+ public static final String END_DEBUG_MESSAGE = "Translation complete" ;
20
+ public static final String JSON_CONTENT_TYPE = "application/json" ;
21
+ public static final String UTF_8_ENCODING = "UTF-8" ;
22
+
23
+ private final boolean monitored ;
27
24
28
25
public XMLToJSONTranslator () {
29
26
this (false );
@@ -33,9 +30,7 @@ public XMLToJSONTranslator(boolean monitored) {
33
30
this .monitored = monitored ;
34
31
}
35
32
36
- private static final Logger logger = LoggerFactory .getLogger (XMLToJSONTranslator .class );
37
-
38
- public String translate (File xmlFile ) throws JAXBException , UnsupportedEncodingException {
33
+ public String translate (File xmlFile ) throws JAXBException {
39
34
40
35
if (xmlFile == null )
41
36
return null ;
@@ -44,21 +39,21 @@ public String translate(File xmlFile) throws JAXBException, UnsupportedEncodingE
44
39
return this .translate (xml );
45
40
}
46
41
47
- public String translate (String xmlString ) throws JAXBException , UnsupportedEncodingException {
42
+ public String translate (String xmlString ) throws JAXBException {
48
43
49
- if ((xmlString == null ) || (xmlString .length () == 0 ))
44
+ if ((xmlString == null ) || (xmlString .isEmpty () ))
50
45
return null ;
51
46
StreamSource xml = new StreamSource (new StringReader (xmlString ));
52
47
53
48
return this .translate (xml );
54
49
}
55
50
56
- public String translate (StreamSource xmlStream ) throws JAXBException , UnsupportedEncodingException {
51
+ public String translate (StreamSource xmlStream ) throws JAXBException {
57
52
58
53
if (xmlStream == null )
59
54
return null ;
60
55
61
- logger .debug ("Preparing to translate from XML to JSON" );
56
+ logger .debug (START_DEBUG_MESSAGE );
62
57
63
58
JAXBContext context = JAXBContext .newInstance (Questionnaire .class );
64
59
Unmarshaller unmarshaller = context .createUnmarshaller ();
@@ -68,20 +63,20 @@ public String translate(StreamSource xmlStream) throws JAXBException, Unsupporte
68
63
Questionnaire questionnaire = (Questionnaire ) unmarshaller .unmarshal (xmlStream );
69
64
70
65
Marshaller marshaller = context .createMarshaller ();
71
- marshaller .setProperty (MarshallerProperties .MEDIA_TYPE , "application/json" );
72
- marshaller .setProperty (Marshaller .JAXB_ENCODING , "UTF-8" );
66
+ marshaller .setProperty (MarshallerProperties .MEDIA_TYPE , JSON_CONTENT_TYPE );
67
+ marshaller .setProperty (Marshaller .JAXB_ENCODING , UTF_8_ENCODING );
73
68
marshaller .setProperty (MarshallerProperties .JSON_INCLUDE_ROOT , false );
74
69
marshaller .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , true );
75
70
76
- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
77
- marshaller .marshal (questionnaire , baos );
71
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
72
+ marshaller .marshal (questionnaire , outputStream );
78
73
79
- logger .debug ("Translation complete" );
74
+ logger .debug (END_DEBUG_MESSAGE );
80
75
81
- return baos .toString ("UTF-8" );
76
+ return outputStream .toString (StandardCharsets . UTF_8 );
82
77
}
83
78
84
- public String translateSequence (File xmlFile ) throws JAXBException , UnsupportedEncodingException {
79
+ public String translateSequence (File xmlFile ) throws JAXBException {
85
80
86
81
if (xmlFile == null )
87
82
return null ;
@@ -90,21 +85,21 @@ public String translateSequence(File xmlFile) throws JAXBException, UnsupportedE
90
85
return this .translateSequence (xml );
91
86
}
92
87
93
- public String translateSequence (String xmlString ) throws JAXBException , UnsupportedEncodingException {
88
+ public String translateSequence (String xmlString ) throws JAXBException {
94
89
95
- if ((xmlString == null ) || (xmlString .length () == 0 ))
90
+ if ((xmlString == null ) || (xmlString .isEmpty () ))
96
91
return null ;
97
92
StreamSource xml = new StreamSource (new StringReader (xmlString ));
98
93
99
94
return this .translateSequence (xml );
100
95
}
101
96
102
- public String translateSequence (StreamSource xmlStream ) throws JAXBException , UnsupportedEncodingException {
97
+ public String translateSequence (StreamSource xmlStream ) throws JAXBException {
103
98
104
99
if (xmlStream == null )
105
100
return null ;
106
101
107
- logger .debug ("Preparing to translate from XML to JSON" );
102
+ logger .debug (START_DEBUG_MESSAGE );
108
103
109
104
JAXBContext context = JAXBContext .newInstance (SequenceType .class );
110
105
Unmarshaller unmarshaller = context .createUnmarshaller ();
@@ -115,20 +110,20 @@ public String translateSequence(StreamSource xmlStream) throws JAXBException, Un
115
110
SequenceType sequence = jeSequence .getValue ();
116
111
117
112
Marshaller marshaller = context .createMarshaller ();
118
- marshaller .setProperty (MarshallerProperties .MEDIA_TYPE , "application/json" );
119
- marshaller .setProperty (Marshaller .JAXB_ENCODING , "UTF-8" );
113
+ marshaller .setProperty (MarshallerProperties .MEDIA_TYPE , JSON_CONTENT_TYPE );
114
+ marshaller .setProperty (Marshaller .JAXB_ENCODING , UTF_8_ENCODING );
120
115
marshaller .setProperty (MarshallerProperties .JSON_INCLUDE_ROOT , false );
121
116
marshaller .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , true );
122
117
123
- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
124
- marshaller .marshal (sequence , baos );
118
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
119
+ marshaller .marshal (sequence , outputStream );
125
120
126
- logger .debug ("Translation complete" );
121
+ logger .debug (END_DEBUG_MESSAGE );
127
122
128
- return baos .toString ("UTF-8" );
123
+ return outputStream .toString (StandardCharsets . UTF_8 );
129
124
}
130
125
131
- public String translateQuestion (File xmlFile ) throws JAXBException , UnsupportedEncodingException {
126
+ public String translateQuestion (File xmlFile ) throws JAXBException {
132
127
133
128
if (xmlFile == null )
134
129
return null ;
@@ -137,21 +132,21 @@ public String translateQuestion(File xmlFile) throws JAXBException, UnsupportedE
137
132
return this .translateQuestion (xml );
138
133
}
139
134
140
- public String translateQuestion (String xmlString ) throws JAXBException , UnsupportedEncodingException {
135
+ public String translateQuestion (String xmlString ) throws JAXBException {
141
136
142
- if ((xmlString == null ) || (xmlString .length () == 0 ))
137
+ if ((xmlString == null ) || (xmlString .isEmpty () ))
143
138
return null ;
144
139
StreamSource xml = new StreamSource (new StringReader (xmlString ));
145
140
146
141
return this .translateQuestion (xml );
147
142
}
148
143
149
- public String translateQuestion (StreamSource xmlStream ) throws JAXBException , UnsupportedEncodingException {
144
+ public String translateQuestion (StreamSource xmlStream ) throws JAXBException {
150
145
151
146
if (xmlStream == null )
152
147
return null ;
153
148
154
- logger .debug ("Preparing to translate from XML to JSON" );
149
+ logger .debug (START_DEBUG_MESSAGE );
155
150
156
151
JAXBContext context = JAXBContext .newInstance (QuestionType .class );
157
152
Unmarshaller unmarshaller = context .createUnmarshaller ();
@@ -162,20 +157,20 @@ public String translateQuestion(StreamSource xmlStream) throws JAXBException, Un
162
157
QuestionType question = jeQuestion .getValue ();
163
158
164
159
Marshaller marshaller = context .createMarshaller ();
165
- marshaller .setProperty (MarshallerProperties .MEDIA_TYPE , "application/json" );
166
- marshaller .setProperty (Marshaller .JAXB_ENCODING , "UTF-8" );
160
+ marshaller .setProperty (MarshallerProperties .MEDIA_TYPE , JSON_CONTENT_TYPE );
161
+ marshaller .setProperty (Marshaller .JAXB_ENCODING , UTF_8_ENCODING );
167
162
marshaller .setProperty (MarshallerProperties .JSON_INCLUDE_ROOT , false );
168
163
marshaller .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , true );
169
164
170
- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
171
- marshaller .marshal (question , baos );
165
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
166
+ marshaller .marshal (question , outputStream );
172
167
173
- logger .debug ("Translation complete" );
168
+ logger .debug (END_DEBUG_MESSAGE );
174
169
175
- return baos .toString ("UTF-8" );
170
+ return outputStream .toString (StandardCharsets . UTF_8 );
176
171
}
177
172
178
- public String translateCodeLists (File xmlFile ) throws JAXBException , UnsupportedEncodingException {
173
+ public String translateCodeLists (File xmlFile ) throws JAXBException {
179
174
180
175
if (xmlFile == null )
181
176
return null ;
@@ -184,21 +179,21 @@ public String translateCodeLists(File xmlFile) throws JAXBException, Unsupported
184
179
return this .translateCodeLists (xml );
185
180
}
186
181
187
- public String translateCodeLists (String xmlString ) throws JAXBException , UnsupportedEncodingException {
182
+ public String translateCodeLists (String xmlString ) throws JAXBException {
188
183
189
- if ((xmlString == null ) || (xmlString .length () == 0 ))
184
+ if ((xmlString == null ) || (xmlString .isEmpty () ))
190
185
return null ;
191
186
StreamSource xml = new StreamSource (new StringReader (xmlString ));
192
187
193
188
return this .translateCodeLists (xml );
194
189
}
195
190
196
- public String translateCodeLists (StreamSource xmlStream ) throws JAXBException , UnsupportedEncodingException {
191
+ public String translateCodeLists (StreamSource xmlStream ) throws JAXBException {
197
192
198
193
if (xmlStream == null )
199
194
return null ;
200
195
201
- logger .debug ("Preparing to translate from XML to JSON" );
196
+ logger .debug (START_DEBUG_MESSAGE );
202
197
203
198
JAXBContext context = JAXBContext .newInstance (CodeLists .class );
204
199
Unmarshaller unmarshaller = context .createUnmarshaller ();
@@ -208,29 +203,74 @@ public String translateCodeLists(StreamSource xmlStream) throws JAXBException, U
208
203
CodeLists codeLists = (CodeLists ) unmarshaller .unmarshal (xmlStream );
209
204
210
205
Marshaller marshaller = context .createMarshaller ();
211
- marshaller .setProperty (MarshallerProperties .MEDIA_TYPE , "application/json" );
212
- marshaller .setProperty (Marshaller .JAXB_ENCODING , "UTF-8" );
206
+ marshaller .setProperty (MarshallerProperties .MEDIA_TYPE , JSON_CONTENT_TYPE );
207
+ marshaller .setProperty (Marshaller .JAXB_ENCODING , UTF_8_ENCODING );
213
208
marshaller .setProperty (MarshallerProperties .JSON_INCLUDE_ROOT , false );
214
209
marshaller .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , true );
215
210
216
- ByteArrayOutputStream baos = new ByteArrayOutputStream ();
217
- marshaller .marshal (codeLists , baos );
211
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
212
+ marshaller .marshal (codeLists , outputStream );
213
+
214
+ logger .debug (END_DEBUG_MESSAGE );
215
+
216
+ return outputStream .toString (StandardCharsets .UTF_8 );
217
+ }
218
+
219
+ public String translateCodeList (File xmlFile ) throws JAXBException {
220
+
221
+ if (xmlFile == null )
222
+ return null ;
223
+ StreamSource xml = new StreamSource (xmlFile );
224
+
225
+ return this .translateCodeList (xml );
226
+ }
227
+
228
+ public String translateCodeList (String xmlString ) throws JAXBException {
218
229
219
- logger .debug ("Translation complete" );
230
+ if ((xmlString == null ) || (xmlString .isEmpty ()))
231
+ return null ;
232
+ StreamSource xml = new StreamSource (new StringReader (xmlString ));
220
233
221
- return baos . toString ( "UTF-8" );
234
+ return this . translateCodeList ( xml );
222
235
}
223
236
224
- private class UnmarshallLogger extends Unmarshaller .Listener {
237
+ public String translateCodeList (StreamSource xmlStream ) throws JAXBException {
238
+
239
+ if (xmlStream == null )
240
+ return null ;
241
+
242
+ logger .debug (START_DEBUG_MESSAGE );
243
+
244
+ JAXBContext context = JAXBContext .newInstance (CodeLists .class );
245
+ Unmarshaller unmarshaller = context .createUnmarshaller ();
246
+ if (monitored )
247
+ unmarshaller .setListener (new UnmarshallLogger ());
248
+
249
+ CodeList codeList = (CodeList ) unmarshaller .unmarshal (xmlStream );
250
+
251
+ Marshaller marshaller = context .createMarshaller ();
252
+ marshaller .setProperty (MarshallerProperties .MEDIA_TYPE , JSON_CONTENT_TYPE );
253
+ marshaller .setProperty (Marshaller .JAXB_ENCODING , UTF_8_ENCODING );
254
+ marshaller .setProperty (MarshallerProperties .JSON_INCLUDE_ROOT , false );
255
+ marshaller .setProperty (Marshaller .JAXB_FORMATTED_OUTPUT , true );
256
+
257
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream ();
258
+ marshaller .marshal (codeList , outputStream );
259
+
260
+ logger .debug (END_DEBUG_MESSAGE );
261
+
262
+ return outputStream .toString (StandardCharsets .UTF_8 );
263
+ }
225
264
265
+ private static class UnmarshallLogger extends Unmarshaller .Listener {
226
266
@ Override
227
267
public void beforeUnmarshal (Object target , Object parent ) {
228
- logger .debug ("Before unmarshalling object " + target );
268
+ logger .debug ("Before unmarshalling object {}" , target );
229
269
}
230
-
231
270
@ Override
232
271
public void afterUnmarshal (Object target , Object parent ) {
233
- logger .debug ("After unmarshalling object " + target );
272
+ logger .debug ("After unmarshalling object {}" , target );
234
273
}
235
274
}
275
+
236
276
}
0 commit comments