|
1 | 1 | package fr.insee.lunatic.conversion.data; |
2 | 2 |
|
3 | | -import java.io.ByteArrayInputStream; |
4 | | -import java.io.File; |
5 | | -import java.io.InputStream; |
6 | | -import java.io.OutputStream; |
| 3 | +import java.io.*; |
7 | 4 | import java.nio.charset.StandardCharsets; |
8 | 5 | import java.nio.file.Files; |
9 | 6 |
|
| 7 | +import fr.insee.lunatic.conversion.ConversionException; |
10 | 8 | import org.apache.commons.io.FileUtils; |
11 | 9 | import org.slf4j.Logger; |
12 | 10 | import org.slf4j.LoggerFactory; |
|
21 | 19 | * |
22 | 20 | */ |
23 | 21 | public class JSONLunaticDataToXML { |
24 | | - |
25 | | - private static XslTransformation saxonService = new XslTransformation(); |
26 | | - |
| 22 | + private static final XslTransformation saxonService = new XslTransformation(); |
27 | 23 | private static final Logger logger = LoggerFactory.getLogger(JSONLunaticDataToXML.class); |
28 | 24 |
|
29 | | - public File transform(File input) throws Exception { |
30 | | - |
| 25 | + /** |
| 26 | + * |
| 27 | + * @param jsonInputFile json file in lunatic format |
| 28 | + * @return xml file in lunatic format |
| 29 | + * @throws ConversionException exception occurring during conversion |
| 30 | + */ |
| 31 | + public File transform(File jsonInputFile) throws Exception { |
31 | 32 | File outputFile = Files.createTempFile("json2xml", ".xml").toFile(); |
32 | | - |
33 | | - |
34 | | - logger.debug("Output folder : " + outputFile.getAbsolutePath()); |
35 | | - |
36 | | - String jsonString = FileUtils.readFileToString(input, StandardCharsets.UTF_8); |
37 | | - InputStream inputStream = new ByteArrayInputStream(wrapJsonWithXml(jsonString).getBytes(StandardCharsets.UTF_8)); |
38 | | - OutputStream outputStream = FileUtils.openOutputStream(outputFile); |
39 | | - |
40 | | - InputStream XSL = XMLLunaticDataToJSON.class.getClassLoader() |
41 | | - .getResourceAsStream(Constants.DATA_TRANSFORMATION_JSON_2_XML); |
42 | | - try { |
43 | | - saxonService.transformWithSimpleXSLSheet(inputStream,outputStream, XSL); |
| 33 | + logger.debug("Output folder : {}", outputFile.getAbsolutePath()); |
| 34 | + String jsonString = FileUtils.readFileToString(jsonInputFile, StandardCharsets.UTF_8); |
| 35 | + try( |
| 36 | + InputStream inputStream = new ByteArrayInputStream(wrapJsonWithXml(jsonString).getBytes(StandardCharsets.UTF_8)); |
| 37 | + OutputStream outputStream = FileUtils.openOutputStream(outputFile); |
| 38 | + InputStream xslStream = XMLLunaticDataToJSON.class.getClassLoader() |
| 39 | + .getResourceAsStream(Constants.DATA_TRANSFORMATION_JSON_2_XML) |
| 40 | + ) { |
| 41 | + saxonService.transformWithSimpleXSLSheet(inputStream,outputStream, xslStream); |
44 | 42 | }catch(Exception e) { |
45 | | - String errorMessage = "An error was occured during the json to xml transformation. "+e.getMessage(); |
46 | | - logger.error(errorMessage); |
47 | | - throw new Exception(errorMessage); |
| 43 | + throw new ConversionException("Error when converting json to xml", e); |
48 | 44 | } |
| 45 | + return outputFile; |
| 46 | + } |
49 | 47 |
|
50 | | - inputStream.close(); |
51 | | - outputStream.close(); |
52 | | - XSL.close(); |
| 48 | + /** |
| 49 | + * Transformation of a lunatic json data to a lunatic xml data |
| 50 | + * |
| 51 | + * @param jsonInputStream data in a json format input stream |
| 52 | + * @return data in a xml format |
| 53 | + * @throws Exception when exceptions occurred |
| 54 | + */ |
| 55 | + public OutputStream transform(InputStream jsonInputStream) throws Exception { |
| 56 | + try (InputStream xslStream = XMLLunaticDataToJSON.class.getClassLoader() |
| 57 | + .getResourceAsStream(Constants.DATA_TRANSFORMATION_JSON_2_XML); |
| 58 | + InputStream wrappedInput = wrapJsonStreamWithXml(jsonInputStream)) { |
53 | 59 |
|
54 | | - return outputFile; |
| 60 | + ByteArrayOutputStream xmlOutputStream = new ByteArrayOutputStream(); |
| 61 | + saxonService.transformWithSimpleXSLSheet(wrappedInput, xmlOutputStream, xslStream); |
| 62 | + return xmlOutputStream; |
| 63 | + } catch (Exception e) { |
| 64 | + throw new ConversionException("Error when converting json to xml", e); |
| 65 | + } |
55 | 66 | } |
56 | 67 |
|
57 | 68 | public String transform(String jsonString) throws Exception { |
58 | 69 |
|
59 | 70 | File outputFile = Files.createTempFile("json2xml", ".xml").toFile(); |
60 | 71 |
|
| 72 | + logger.debug("Output folder : {}", outputFile.getAbsolutePath()); |
61 | 73 |
|
62 | | - logger.debug("Output folder : " + outputFile.getAbsolutePath()); |
63 | | - |
64 | | - InputStream inputStream = new ByteArrayInputStream(wrapJsonWithXml(jsonString).getBytes(StandardCharsets.UTF_8)); |
65 | | - OutputStream outputStream = FileUtils.openOutputStream(outputFile); |
66 | | - |
67 | | - InputStream XSL = XMLLunaticDataToJSON.class.getClassLoader() |
68 | | - .getResourceAsStream(Constants.DATA_TRANSFORMATION_JSON_2_XML); |
69 | | - try { |
70 | | - saxonService.transformWithSimpleXSLSheet(inputStream,outputStream, XSL); |
| 74 | + try( |
| 75 | + InputStream inputStream = new ByteArrayInputStream(wrapJsonWithXml(jsonString) |
| 76 | + .getBytes(StandardCharsets.UTF_8)); |
| 77 | + OutputStream outputStream = FileUtils.openOutputStream(outputFile); |
| 78 | + InputStream xslStream = XMLLunaticDataToJSON.class.getClassLoader() |
| 79 | + .getResourceAsStream(Constants.DATA_TRANSFORMATION_JSON_2_XML) |
| 80 | + ) { |
| 81 | + saxonService.transformWithSimpleXSLSheet(inputStream,outputStream, xslStream); |
71 | 82 | }catch(Exception e) { |
72 | | - String errorMessage = "An error was occured during the json to xml transformation. "+e.getMessage(); |
73 | | - logger.error(errorMessage); |
74 | | - throw new Exception(errorMessage); |
| 83 | + throw new ConversionException("Error when converting json to xml", e); |
75 | 84 | } |
76 | | - |
77 | | - inputStream.close(); |
78 | | - outputStream.close(); |
79 | | - XSL.close(); |
80 | | - |
81 | 85 | return FileUtils.readFileToString(outputFile, StandardCharsets.UTF_8); |
82 | 86 | } |
83 | 87 |
|
84 | | - public String wrapJsonWithXml(String json) { |
| 88 | + private String wrapJsonWithXml(String json) { |
85 | 89 | return "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Data>"+preProcessJson2XML(json)+"</Data>"; |
86 | 90 | } |
| 91 | + |
| 92 | + /** |
| 93 | + * Wraps the content of a JSON InputStream into a valid XML structure. |
| 94 | + * |
| 95 | + * @param jsonInputStream the original JSON stream |
| 96 | + * @return a new InputStream containing the wrapped XML |
| 97 | + * @throws IOException if reading the stream fails |
| 98 | + */ |
| 99 | + private InputStream wrapJsonStreamWithXml(InputStream jsonInputStream) throws IOException { |
| 100 | + String json = new String(jsonInputStream.readAllBytes(), StandardCharsets.UTF_8); |
| 101 | + String xml = wrapJsonWithXml(json); |
| 102 | + return new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)); |
| 103 | + } |
87 | 104 |
|
88 | | - public String preProcessJson2XML(String json) { |
| 105 | + private String preProcessJson2XML(String json) { |
89 | 106 | return json.replaceAll("&", "&") |
90 | 107 | .replaceAll("<", "<") |
91 | 108 | .replaceAll(">", ">"); |
|
0 commit comments