From f45986cc007cf8adad2be80a0e8e60f0b71bb4f2 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 17 May 2025 20:35:36 +0300 Subject: [PATCH 1/4] Added method U.streamXmlToJson(xmlInputStream, jsonOutputStream, identStep) --- src/main/java/com/github/underscore/U.java | 12 +++++ .../com/github/underscore/UnderscoreTest.java | 52 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index 606829d6..ee7486e9 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,16 @@ 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 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..12742a45 100644 --- a/src/test/java/com/github/underscore/UnderscoreTest.java +++ b/src/test/java/com/github/underscore/UnderscoreTest.java @@ -37,6 +37,10 @@ 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.OutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; @@ -1084,4 +1088,52 @@ 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, Json.JsonStringBuilder.Step.TWO_SPACES); + 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."); + } } From 6900654f1940fbc8a5976d83439e83031dd169df Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 17 May 2025 20:44:07 +0300 Subject: [PATCH 2/4] Added method without ident step --- src/main/java/com/github/underscore/U.java | 4 ++++ src/test/java/com/github/underscore/UnderscoreTest.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index ee7486e9..a42f7510 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -2805,6 +2805,10 @@ public static void streamXmlToJson(InputStream xmlInputStream, OutputStream json 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 12742a45..7ed7a840 100644 --- a/src/test/java/com/github/underscore/UnderscoreTest.java +++ b/src/test/java/com/github/underscore/UnderscoreTest.java @@ -1094,7 +1094,7 @@ void testStreamXmlToJson_validXml_writesJson() throws IOException { String xml = "Test"; InputStream xmlStream = new ByteArrayInputStream(xml.getBytes()); ByteArrayOutputStream jsonStream = new ByteArrayOutputStream(); - U.streamXmlToJson(xmlStream, jsonStream, Json.JsonStringBuilder.Step.TWO_SPACES); + 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."); From 7f0a5a19840822dc599940d9fa7983c3937ecb2d Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 17 May 2025 20:45:28 +0300 Subject: [PATCH 3/4] Fixed sonar --- src/test/java/com/github/underscore/UnderscoreTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/github/underscore/UnderscoreTest.java b/src/test/java/com/github/underscore/UnderscoreTest.java index 7ed7a840..94710c08 100644 --- a/src/test/java/com/github/underscore/UnderscoreTest.java +++ b/src/test/java/com/github/underscore/UnderscoreTest.java @@ -40,7 +40,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; -import java.io.OutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; From 856480479669c38bafea353a2cf5fbea48a07439 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sat, 17 May 2025 20:50:45 +0300 Subject: [PATCH 4/4] Fixed format --- src/test/java/com/github/underscore/UnderscoreTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/com/github/underscore/UnderscoreTest.java b/src/test/java/com/github/underscore/UnderscoreTest.java index 94710c08..da56466e 100644 --- a/src/test/java/com/github/underscore/UnderscoreTest.java +++ b/src/test/java/com/github/underscore/UnderscoreTest.java @@ -1121,7 +1121,6 @@ void testStreamXmlToJson_invalidXml_throwsException() { 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."); }