Skip to content

Commit a8b6ea1

Browse files
nsenaveloichenningerORogel
authored
feat: add xml to json conversion methods for code lists (#89)
- feat: add xml to json conversion methods for code lists - refactor: a bit of clean up - build(test): add dependencies to do xml comparison - build(test): add dependencies to do json comparison - test: add unit tests for code list serialization --------- Co-authored-by: Loïc Henninger <[email protected]> Co-authored-by: Ophélie Rogel <[email protected]>
1 parent 6983c0d commit a8b6ea1

File tree

5 files changed

+269
-73
lines changed

5 files changed

+269
-73
lines changed

pom.xml

+14-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>fr.insee.pogues</groupId>
55
<artifactId>pogues-model</artifactId>
66
<packaging>jar</packaging>
7-
<version>1.2.0</version>
7+
<version>1.2.1</version>
88
<name>Pogues Model</name>
99
<description>Classes and converters for the Pogues model</description>
1010
<url>https://inseefr.github.io/Pogues-Model/</url>
@@ -90,6 +90,19 @@
9090
<version>${junit.version}</version>
9191
<scope>test</scope>
9292
</dependency>
93+
<dependency>
94+
<groupId>org.skyscreamer</groupId>
95+
<artifactId>jsonassert</artifactId>
96+
<version>1.5.1</version>
97+
<scope>test</scope>
98+
</dependency>
99+
<dependency>
100+
<groupId>org.xmlunit</groupId>
101+
<artifactId>xmlunit-assertj3</artifactId>
102+
<version>2.9.1</version>
103+
<scope>test</scope>
104+
</dependency>
105+
93106
<dependency>
94107
<groupId>org.apache.logging.log4j</groupId>
95108
<artifactId>log4j-slf4j2-impl</artifactId>
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
package fr.insee.pogues.conversion;
22

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;
155
import org.slf4j.Logger;
166
import org.slf4j.LoggerFactory;
17-
import org.eclipse.persistence.jaxb.MarshallerProperties;
187

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;
2314

2415
public class XMLToJSONTranslator {
2516

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;
2724

2825
public XMLToJSONTranslator() {
2926
this(false);
@@ -33,9 +30,7 @@ public XMLToJSONTranslator(boolean monitored) {
3330
this.monitored = monitored;
3431
}
3532

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 {
3934

4035
if (xmlFile == null)
4136
return null;
@@ -44,21 +39,21 @@ public String translate(File xmlFile) throws JAXBException, UnsupportedEncodingE
4439
return this.translate(xml);
4540
}
4641

47-
public String translate(String xmlString) throws JAXBException, UnsupportedEncodingException {
42+
public String translate(String xmlString) throws JAXBException {
4843

49-
if ((xmlString == null) || (xmlString.length() == 0))
44+
if ((xmlString == null) || (xmlString.isEmpty()))
5045
return null;
5146
StreamSource xml = new StreamSource(new StringReader(xmlString));
5247

5348
return this.translate(xml);
5449
}
5550

56-
public String translate(StreamSource xmlStream) throws JAXBException, UnsupportedEncodingException {
51+
public String translate(StreamSource xmlStream) throws JAXBException {
5752

5853
if (xmlStream == null)
5954
return null;
6055

61-
logger.debug("Preparing to translate from XML to JSON");
56+
logger.debug(START_DEBUG_MESSAGE);
6257

6358
JAXBContext context = JAXBContext.newInstance(Questionnaire.class);
6459
Unmarshaller unmarshaller = context.createUnmarshaller();
@@ -68,20 +63,20 @@ public String translate(StreamSource xmlStream) throws JAXBException, Unsupporte
6863
Questionnaire questionnaire = (Questionnaire) unmarshaller.unmarshal(xmlStream);
6964

7065
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);
7368
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
7469
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
7570

76-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
77-
marshaller.marshal(questionnaire, baos);
71+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
72+
marshaller.marshal(questionnaire, outputStream);
7873

79-
logger.debug("Translation complete");
74+
logger.debug(END_DEBUG_MESSAGE);
8075

81-
return baos.toString("UTF-8");
76+
return outputStream.toString(StandardCharsets.UTF_8);
8277
}
8378

84-
public String translateSequence(File xmlFile) throws JAXBException, UnsupportedEncodingException {
79+
public String translateSequence(File xmlFile) throws JAXBException {
8580

8681
if (xmlFile == null)
8782
return null;
@@ -90,21 +85,21 @@ public String translateSequence(File xmlFile) throws JAXBException, UnsupportedE
9085
return this.translateSequence(xml);
9186
}
9287

93-
public String translateSequence(String xmlString) throws JAXBException, UnsupportedEncodingException {
88+
public String translateSequence(String xmlString) throws JAXBException {
9489

95-
if ((xmlString == null) || (xmlString.length() == 0))
90+
if ((xmlString == null) || (xmlString.isEmpty()))
9691
return null;
9792
StreamSource xml = new StreamSource(new StringReader(xmlString));
9893

9994
return this.translateSequence(xml);
10095
}
10196

102-
public String translateSequence(StreamSource xmlStream) throws JAXBException, UnsupportedEncodingException {
97+
public String translateSequence(StreamSource xmlStream) throws JAXBException {
10398

10499
if (xmlStream == null)
105100
return null;
106101

107-
logger.debug("Preparing to translate from XML to JSON");
102+
logger.debug(START_DEBUG_MESSAGE);
108103

109104
JAXBContext context = JAXBContext.newInstance(SequenceType.class);
110105
Unmarshaller unmarshaller = context.createUnmarshaller();
@@ -115,20 +110,20 @@ public String translateSequence(StreamSource xmlStream) throws JAXBException, Un
115110
SequenceType sequence = jeSequence.getValue();
116111

117112
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);
120115
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
121116
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
122117

123-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
124-
marshaller.marshal(sequence, baos);
118+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
119+
marshaller.marshal(sequence, outputStream);
125120

126-
logger.debug("Translation complete");
121+
logger.debug(END_DEBUG_MESSAGE);
127122

128-
return baos.toString("UTF-8");
123+
return outputStream.toString(StandardCharsets.UTF_8);
129124
}
130125

131-
public String translateQuestion(File xmlFile) throws JAXBException, UnsupportedEncodingException {
126+
public String translateQuestion(File xmlFile) throws JAXBException {
132127

133128
if (xmlFile == null)
134129
return null;
@@ -137,21 +132,21 @@ public String translateQuestion(File xmlFile) throws JAXBException, UnsupportedE
137132
return this.translateQuestion(xml);
138133
}
139134

140-
public String translateQuestion(String xmlString) throws JAXBException, UnsupportedEncodingException {
135+
public String translateQuestion(String xmlString) throws JAXBException {
141136

142-
if ((xmlString == null) || (xmlString.length() == 0))
137+
if ((xmlString == null) || (xmlString.isEmpty()))
143138
return null;
144139
StreamSource xml = new StreamSource(new StringReader(xmlString));
145140

146141
return this.translateQuestion(xml);
147142
}
148143

149-
public String translateQuestion(StreamSource xmlStream) throws JAXBException, UnsupportedEncodingException {
144+
public String translateQuestion(StreamSource xmlStream) throws JAXBException {
150145

151146
if (xmlStream == null)
152147
return null;
153148

154-
logger.debug("Preparing to translate from XML to JSON");
149+
logger.debug(START_DEBUG_MESSAGE);
155150

156151
JAXBContext context = JAXBContext.newInstance(QuestionType.class);
157152
Unmarshaller unmarshaller = context.createUnmarshaller();
@@ -162,20 +157,20 @@ public String translateQuestion(StreamSource xmlStream) throws JAXBException, Un
162157
QuestionType question = jeQuestion.getValue();
163158

164159
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);
167162
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
168163
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
169164

170-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
171-
marshaller.marshal(question, baos);
165+
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
166+
marshaller.marshal(question, outputStream);
172167

173-
logger.debug("Translation complete");
168+
logger.debug(END_DEBUG_MESSAGE);
174169

175-
return baos.toString("UTF-8");
170+
return outputStream.toString(StandardCharsets.UTF_8);
176171
}
177172

178-
public String translateCodeLists(File xmlFile) throws JAXBException, UnsupportedEncodingException {
173+
public String translateCodeLists(File xmlFile) throws JAXBException {
179174

180175
if (xmlFile == null)
181176
return null;
@@ -184,21 +179,21 @@ public String translateCodeLists(File xmlFile) throws JAXBException, Unsupported
184179
return this.translateCodeLists(xml);
185180
}
186181

187-
public String translateCodeLists(String xmlString) throws JAXBException, UnsupportedEncodingException {
182+
public String translateCodeLists(String xmlString) throws JAXBException {
188183

189-
if ((xmlString == null) || (xmlString.length() == 0))
184+
if ((xmlString == null) || (xmlString.isEmpty()))
190185
return null;
191186
StreamSource xml = new StreamSource(new StringReader(xmlString));
192187

193188
return this.translateCodeLists(xml);
194189
}
195190

196-
public String translateCodeLists(StreamSource xmlStream) throws JAXBException, UnsupportedEncodingException {
191+
public String translateCodeLists(StreamSource xmlStream) throws JAXBException {
197192

198193
if (xmlStream == null)
199194
return null;
200195

201-
logger.debug("Preparing to translate from XML to JSON");
196+
logger.debug(START_DEBUG_MESSAGE);
202197

203198
JAXBContext context = JAXBContext.newInstance(CodeLists.class);
204199
Unmarshaller unmarshaller = context.createUnmarshaller();
@@ -208,29 +203,74 @@ public String translateCodeLists(StreamSource xmlStream) throws JAXBException, U
208203
CodeLists codeLists = (CodeLists) unmarshaller.unmarshal(xmlStream);
209204

210205
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);
213208
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, false);
214209
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
215210

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 {
218229

219-
logger.debug("Translation complete");
230+
if ((xmlString == null) || (xmlString.isEmpty()))
231+
return null;
232+
StreamSource xml = new StreamSource(new StringReader(xmlString));
220233

221-
return baos.toString("UTF-8");
234+
return this.translateCodeList(xml);
222235
}
223236

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+
}
225264

265+
private static class UnmarshallLogger extends Unmarshaller.Listener {
226266
@Override
227267
public void beforeUnmarshal(Object target, Object parent) {
228-
logger.debug("Before unmarshalling object " + target);
268+
logger.debug("Before unmarshalling object {}", target);
229269
}
230-
231270
@Override
232271
public void afterUnmarshal(Object target, Object parent) {
233-
logger.debug("After unmarshalling object " + target);
272+
logger.debug("After unmarshalling object {}", target);
234273
}
235274
}
275+
236276
}

0 commit comments

Comments
 (0)