diff --git a/src/main/java/com/github/underscore/U.java b/src/main/java/com/github/underscore/U.java index 7f545360..96284906 100644 --- a/src/main/java/com/github/underscore/U.java +++ b/src/main/java/com/github/underscore/U.java @@ -34,9 +34,12 @@ import java.nio.channels.Channels; import java.nio.channels.ReadableByteChannel; import java.nio.charset.StandardCharsets; +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; import java.util.Collection; @@ -2851,6 +2854,39 @@ public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES); } + public static void jsonFolderToXml( + String jsonFolder, String xmlFolder, Xml.XmlStringBuilder.Step identStep) + throws IOException { + 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 { + covertJsonToXml(path, sourceRoot, targetRoot, identStep); + return FileVisitResult.CONTINUE; + } + }); + } + + 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, + 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