From 2fb0877e2c7ac2c330b3d91a590461c806d265a1 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 27 Jul 2025 19:05:45 +0300 Subject: [PATCH 1/3] Added method U.jsonFolderToXml(jsonFileFolder, xmlFileFolder, identStep) --- src/main/java/com/github/underscore/U.java | 44 +++++++++++++++---- .../com/github/underscore/UnderscoreTest.java | 22 ++++++++++ 2 files changed, 58 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index 7f545360..d27bd4ac 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -23,20 +23,15 @@ */ package com.github.underscore; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.io.*; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; +import java.nio.file.*; +import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -2851,6 +2846,39 @@ public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES); } + public static void jsonFolderToXml( + String jsonFileFolder, String xmlFileFolder, Xml.XmlStringBuilder.Step identStep) + throws IOException { + Path sourceRoot = Paths.get(jsonFileFolder); + Path targetRoot = Paths.get(xmlFileFolder); + Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() { + @Override + public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { + covertJsonToXml(path, sourceRoot, targetRoot, identStep); + return FileVisitResult.CONTINUE; + } + }); + } + + public static void jsonFolderToXml(String jsonFileFolder, String xmlFileFolder) throws IOException { + jsonFolderToXml(jsonFileFolder, xmlFileFolder, Xml.XmlStringBuilder.Step.TWO_SPACES); + } + + public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot, + Xml.XmlStringBuilder.Step identStep) throws IOException { + Path relativePath = sourceRoot.relativize(path); + String fileName = relativePath.getFileName().toString(); + String xmlFileName; + if (fileName.endsWith(".json")) { + xmlFileName = fileName.substring(0, fileName.length() - 5) + ".xml"; + } else { + return; + } + Path targetPath = targetRoot.resolve(relativePath).getParent().resolve(xmlFileName); + Files.createDirectories(targetPath.getParent()); + fileJsonToXml(path.toAbsolutePath().toString(), targetPath.toString(), identStep); + } + public static void streamJsonToXml( InputStream jsonInputStream, OutputStream xmlOutputStream, diff --git a/src/test/java/com/github/underscore/UnderscoreTest.java b/src/test/java/com/github/underscore/UnderscoreTest.java index f018e03c..ebd84190 100644 --- a/src/test/java/com/github/underscore/UnderscoreTest.java +++ b/src/test/java/com/github/underscore/UnderscoreTest.java @@ -1199,6 +1199,28 @@ void testMapWithoutEncodingKey(@TempDir Path tempDir) throws IOException { "Should write XML using UTF-8 when #encoding key not present"); } + @Test + void testJsonFolderToXml(@TempDir Path tempDir) throws IOException { + // Arrange + Path jsonFile = tempDir.resolve("in.json"); + Path xmlFile = tempDir.resolve("in.xml"); + String jsonText = "{}"; + Files.write(jsonFile, jsonText.getBytes(StandardCharsets.UTF_8)); + Files.write(xmlFile, jsonText.getBytes(StandardCharsets.UTF_8)); + // Act + U.jsonFolderToXml( + jsonFile.getParent().toString(), xmlFile.getParent().toString()); + // Assert + byte[] xmlBytes = Files.readAllBytes(xmlFile); + String xmlStr = new String(xmlBytes, StandardCharsets.UTF_8); + assertEquals( + "" + + System.lineSeparator() + + "", + xmlStr, + "Should write XML using UTF-8 when #encoding key not present"); + } + @Test void testListResult(@TempDir Path tempDir) throws IOException { // Arrange From 8c96c550be9d94f26a639849492502e26f034935 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 27 Jul 2025 19:07:00 +0300 Subject: [PATCH 2/3] Fixed imports --- src/main/java/com/github/underscore/U.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index d27bd4ac..ac4d3146 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -23,14 +23,22 @@ */ package com.github.underscore; -import java.io.*; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.net.URI; import java.net.URISyntaxException; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.charset.StandardCharsets; -import java.nio.file.*; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Arrays; From 53b371a3d60111b0e679c97a0ba0c86dd0fd5c85 Mon Sep 17 00:00:00 2001 From: Valentyn Kolesnikov Date: Sun, 27 Jul 2025 19:09:04 +0300 Subject: [PATCH 3/3] Renames variables --- src/main/java/com/github/underscore/U.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index ac4d3146..96284906 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -2855,10 +2855,10 @@ public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws } public static void jsonFolderToXml( - String jsonFileFolder, String xmlFileFolder, Xml.XmlStringBuilder.Step identStep) + String jsonFolder, String xmlFolder, Xml.XmlStringBuilder.Step identStep) throws IOException { - Path sourceRoot = Paths.get(jsonFileFolder); - Path targetRoot = Paths.get(xmlFileFolder); + Path sourceRoot = Paths.get(jsonFolder); + Path targetRoot = Paths.get(xmlFolder); Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { @@ -2868,8 +2868,8 @@ public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IO }); } - public static void jsonFolderToXml(String jsonFileFolder, String xmlFileFolder) throws IOException { - jsonFolderToXml(jsonFileFolder, xmlFileFolder, Xml.XmlStringBuilder.Step.TWO_SPACES); + public static void jsonFolderToXml(String jsonFolder, String xmlFolder) throws IOException { + jsonFolderToXml(jsonFolder, xmlFolder, Xml.XmlStringBuilder.Step.TWO_SPACES); } public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot,