diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index 606829d6..a42f7510 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -25,7 +25,9 @@ import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.InputStream; import java.io.IOException; +import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; @@ -2793,6 +2795,20 @@ public static void fileXmlToJson(String xmlFileName, String jsonFileName) throws fileXmlToJson(xmlFileName, jsonFileName, Json.JsonStringBuilder.Step.TWO_SPACES); } + public static void streamXmlToJson(InputStream xmlInputStream, OutputStream jsonOutputStream, + Json.JsonStringBuilder.Step indentStep) throws IOException { + byte[] bytes = xmlInputStream.readAllBytes(); + String encoding = detectEncoding(bytes); + String xmlText = new String(removeBom(bytes), encoding); + String jsonText = xmlToJson(xmlText, indentStep); + String formattedJson = formatString(jsonText, System.lineSeparator()); + jsonOutputStream.write(formattedJson.getBytes(StandardCharsets.UTF_8)); + } + + public static void streamXmlToJson(InputStream xmlInputStream, OutputStream jsonOutputStream) throws IOException { + streamXmlToJson(xmlInputStream, jsonOutputStream, Json.JsonStringBuilder.Step.TWO_SPACES); + } + public static byte[] removeBom(byte[] bytes) { if ((bytes.length >= 3) && (bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65)) { return Arrays.copyOfRange(bytes, 3, bytes.length); diff --git a/src/test/java/com/github/underscore/UnderscoreTest.java b/src/test/java/com/github/underscore/UnderscoreTest.java index 63ed0238..da56466e 100644 --- a/src/test/java/com/github/underscore/UnderscoreTest.java +++ b/src/test/java/com/github/underscore/UnderscoreTest.java @@ -37,6 +37,9 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -1084,4 +1087,51 @@ void testFileXmlToJsonWithInvalidInput(@TempDir Path tempDir) { "Should throw IOException when input file doesn't exist" ); } + + @Test + void testStreamXmlToJson_validXml_writesJson() throws IOException { + String xml = "Test"; + InputStream xmlStream = new ByteArrayInputStream(xml.getBytes()); + ByteArrayOutputStream jsonStream = new ByteArrayOutputStream(); + U.streamXmlToJson(xmlStream, jsonStream); + String jsonOutput = jsonStream.toString("UTF-8"); + assertTrue(jsonOutput.contains("name"), "JSON output should contain 'name' field."); + assertTrue(jsonOutput.contains("Test"), "JSON output should contain 'Test' value."); + assertTrue(jsonOutput.startsWith("{"), "JSON output should start with '{'."); + assertTrue(jsonOutput.endsWith("}"), "JSON output should end with '}'."); + } + + @Test + void testStreamXmlToJson_emptyInput_producesEmptyOrError() { + InputStream xmlStream = new ByteArrayInputStream(new byte[0]); + ByteArrayOutputStream jsonStream = new ByteArrayOutputStream(); + Exception exception = assertThrows(Exception.class, () -> { + U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.TWO_SPACES); + }, "Should throw exception for empty input."); + String msg = exception.getMessage(); + assertNotNull(msg, "Exception message should not be null."); + } + + @Test + void testStreamXmlToJson_invalidXml_throwsException() { + // missing closing tag + String invalidXml = "Test"; + InputStream xmlStream = new ByteArrayInputStream(invalidXml.getBytes()); + ByteArrayOutputStream jsonStream = new ByteArrayOutputStream(); + Exception exception = assertThrows(Exception.class, () -> { + U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.TWO_SPACES); + }, "Should throw exception for invalid XML."); + String msg = exception.getMessage(); + assertNotNull(msg, "Exception message for invalid XML should not be null."); + } + + @Test + void testStreamXmlToJson_withIndentSteps_producesIndentedJson() throws IOException { + String xml = "value"; + InputStream xmlStream = new ByteArrayInputStream(xml.getBytes()); + ByteArrayOutputStream jsonStream = new ByteArrayOutputStream(); + U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.FOUR_SPACES); + String jsonOutput = jsonStream.toString("UTF-8"); + assertTrue(jsonOutput.contains(" "), "JSON output should be indented with four spaces."); + } }