Skip to content

Commit 41c513b

Browse files
authored
Added method U.fileJsonToXml(jsonFileName, xmlFileName, identStep)
1 parent 69d883e commit 41c513b

File tree

2 files changed

+117
-12
lines changed

2 files changed

+117
-12
lines changed

src/main/java/com/github/underscore/U.java

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.nio.channels.ReadableByteChannel;
3636
import java.nio.charset.StandardCharsets;
3737
import java.nio.file.Files;
38+
import java.nio.file.Path;
3839
import java.nio.file.Paths;
3940
import java.util.ArrayList;
4041
import java.util.Arrays;
@@ -2816,6 +2817,39 @@ public static void streamXmlToJson(InputStream xmlInputStream, OutputStream json
28162817
streamXmlToJson(xmlInputStream, jsonOutputStream, Json.JsonStringBuilder.Step.TWO_SPACES);
28172818
}
28182819

2820+
public static void fileJsonToXml(
2821+
String jsonFileName, String xmlFileName, Xml.XmlStringBuilder.Step identStep)
2822+
throws IOException {
2823+
final byte[] bytes = Files.readAllBytes(Paths.get(jsonFileName));
2824+
String jsonText = new String(removeBom(bytes), detectEncoding(bytes));
2825+
Object result = U.fromJson(jsonText);
2826+
Path xmlFilePath = Paths.get(xmlFileName);
2827+
String lineSeparator = System.lineSeparator();
2828+
if (result instanceof Map) {
2829+
if (((Map) result).containsKey("#encoding")) {
2830+
String encoding = String.valueOf(((Map) result).get("#encoding"));
2831+
Files.write(
2832+
xmlFilePath,
2833+
formatString(Xml.toXml((Map) result, identStep), lineSeparator)
2834+
.getBytes(encoding));
2835+
} else {
2836+
Files.write(
2837+
xmlFilePath,
2838+
formatString(Xml.toXml((Map) result, identStep), lineSeparator)
2839+
.getBytes(StandardCharsets.UTF_8));
2840+
}
2841+
} else {
2842+
Files.write(
2843+
xmlFilePath,
2844+
formatString(Xml.toXml((List) result, identStep), lineSeparator)
2845+
.getBytes(StandardCharsets.UTF_8));
2846+
}
2847+
}
2848+
2849+
public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws IOException {
2850+
fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES);
2851+
}
2852+
28192853
public static byte[] removeBom(byte[] bytes) {
28202854
if ((bytes.length >= 3) && (bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65)) {
28212855
return Arrays.copyOfRange(bytes, 3, bytes.length);
@@ -2848,8 +2882,6 @@ public static String detectEncoding(byte[] buffer) {
28482882
encoding = "UnicodeBigUnmarked";
28492883
break;
28502884
case 0xFFFE0000:
2851-
encoding = "UTF_32LE";
2852-
break;
28532885
case 0x3C000000:
28542886
encoding = "UTF_32LE";
28552887
break;

src/test/java/com/github/underscore/UnderscoreTest.java

Lines changed: 83 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ void testStreamXmlToJson_validXml_writesJson() throws IOException {
11031103
InputStream xmlStream = new ByteArrayInputStream(xml.getBytes());
11041104
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
11051105
U.streamXmlToJson(xmlStream, jsonStream);
1106-
String jsonOutput = jsonStream.toString("UTF-8");
1106+
String jsonOutput = jsonStream.toString(StandardCharsets.UTF_8);
11071107
assertTrue(jsonOutput.contains("name"), "JSON output should contain 'name' field.");
11081108
assertTrue(jsonOutput.contains("Test"), "JSON output should contain 'Test' value.");
11091109
assertTrue(jsonOutput.startsWith("{"), "JSON output should start with '{'.");
@@ -1117,10 +1117,11 @@ void testStreamXmlToJson_emptyInput_producesEmptyOrError() {
11171117
Exception exception =
11181118
assertThrows(
11191119
Exception.class,
1120-
() -> {
1121-
U.streamXmlToJson(
1122-
xmlStream, jsonStream, Json.JsonStringBuilder.Step.TWO_SPACES);
1123-
},
1120+
() ->
1121+
U.streamXmlToJson(
1122+
xmlStream,
1123+
jsonStream,
1124+
Json.JsonStringBuilder.Step.TWO_SPACES),
11241125
"Should throw exception for empty input.");
11251126
String msg = exception.getMessage();
11261127
assertNotNull(msg, "Exception message should not be null.");
@@ -1135,10 +1136,11 @@ void testStreamXmlToJson_invalidXml_throwsException() {
11351136
Exception exception =
11361137
assertThrows(
11371138
Exception.class,
1138-
() -> {
1139-
U.streamXmlToJson(
1140-
xmlStream, jsonStream, Json.JsonStringBuilder.Step.TWO_SPACES);
1141-
},
1139+
() ->
1140+
U.streamXmlToJson(
1141+
xmlStream,
1142+
jsonStream,
1143+
Json.JsonStringBuilder.Step.TWO_SPACES),
11421144
"Should throw exception for invalid XML.");
11431145
String msg = exception.getMessage();
11441146
assertNotNull(msg, "Exception message for invalid XML should not be null.");
@@ -1150,7 +1152,78 @@ void testStreamXmlToJson_withIndentSteps_producesIndentedJson() throws IOExcepti
11501152
InputStream xmlStream = new ByteArrayInputStream(xml.getBytes());
11511153
ByteArrayOutputStream jsonStream = new ByteArrayOutputStream();
11521154
U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.FOUR_SPACES);
1153-
String jsonOutput = jsonStream.toString("UTF-8");
1155+
String jsonOutput = jsonStream.toString(StandardCharsets.UTF_8);
11541156
assertTrue(jsonOutput.contains(" "), "JSON output should be indented with four spaces.");
11551157
}
1158+
1159+
@Test
1160+
void testMapWithEncodingKey(@TempDir Path tempDir) throws IOException {
1161+
// Arrange
1162+
Path jsonFile = tempDir.resolve("in.json");
1163+
Path xmlFile = tempDir.resolve("out.xml");
1164+
String encoding = "UTF-16";
1165+
// Write json
1166+
String jsonText = "{\"#encoding\":\"" + encoding + "\"}";
1167+
Files.write(jsonFile, jsonText.getBytes(StandardCharsets.UTF_8));
1168+
// Act
1169+
U.fileJsonToXml(jsonFile.toString(), xmlFile.toString());
1170+
// Assert
1171+
byte[] xmlBytes = Files.readAllBytes(xmlFile);
1172+
String xmlStr = new String(xmlBytes, encoding);
1173+
assertEquals(
1174+
"<?xml version=\"1.0\" encoding=\"UTF-16\"?>"
1175+
+ System.lineSeparator()
1176+
+ "<root></root>",
1177+
xmlStr,
1178+
"Should write XML with provided encoding when #encoding key present");
1179+
}
1180+
1181+
@Test
1182+
void testMapWithoutEncodingKey(@TempDir Path tempDir) throws IOException {
1183+
// Arrange
1184+
Path jsonFile = tempDir.resolve("in.json");
1185+
Path xmlFile = tempDir.resolve("out.xml");
1186+
String jsonText = "{}";
1187+
Files.write(jsonFile, jsonText.getBytes(StandardCharsets.UTF_8));
1188+
// Act
1189+
U.fileJsonToXml(
1190+
jsonFile.toString(), xmlFile.toString(), Xml.XmlStringBuilder.Step.TWO_SPACES);
1191+
// Assert
1192+
byte[] xmlBytes = Files.readAllBytes(xmlFile);
1193+
String xmlStr = new String(xmlBytes, StandardCharsets.UTF_8);
1194+
assertEquals(
1195+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1196+
+ System.lineSeparator()
1197+
+ "<root></root>",
1198+
xmlStr,
1199+
"Should write XML using UTF-8 when #encoding key not present");
1200+
}
1201+
1202+
@Test
1203+
void testListResult(@TempDir Path tempDir) throws IOException {
1204+
// Arrange
1205+
Path jsonFile = tempDir.resolve("in.json");
1206+
Path xmlFile = tempDir.resolve("out.xml");
1207+
Files.write(jsonFile, "[1,2,3]".getBytes(StandardCharsets.UTF_8));
1208+
// Act
1209+
U.fileJsonToXml(
1210+
jsonFile.toString(), xmlFile.toString(), Xml.XmlStringBuilder.Step.TWO_SPACES);
1211+
// Assert
1212+
byte[] xmlBytes = Files.readAllBytes(xmlFile);
1213+
String xmlStr = new String(xmlBytes, StandardCharsets.UTF_8);
1214+
assertEquals(
1215+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1216+
+ System.lineSeparator()
1217+
+ "<root>"
1218+
+ System.lineSeparator()
1219+
+ " <element number=\"true\">1</element>"
1220+
+ System.lineSeparator()
1221+
+ " <element number=\"true\">2</element>"
1222+
+ System.lineSeparator()
1223+
+ " <element number=\"true\">3</element>"
1224+
+ System.lineSeparator()
1225+
+ "</root>",
1226+
xmlStr,
1227+
"Should write XML using UTF-8 when result is a List");
1228+
}
11561229
}

0 commit comments

Comments
 (0)