Skip to content

Commit fb53fd6

Browse files
authored
Added method U.jsonFolderToXml(jsonFolder, xmlFolder, identStep)
1 parent 2c073b3 commit fb53fd6

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@
3434
import java.nio.channels.Channels;
3535
import java.nio.channels.ReadableByteChannel;
3636
import java.nio.charset.StandardCharsets;
37+
import java.nio.file.FileVisitResult;
3738
import java.nio.file.Files;
3839
import java.nio.file.Path;
3940
import java.nio.file.Paths;
41+
import java.nio.file.SimpleFileVisitor;
42+
import java.nio.file.attribute.BasicFileAttributes;
4043
import java.util.ArrayList;
4144
import java.util.Arrays;
4245
import java.util.Collection;
@@ -2851,6 +2854,39 @@ public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws
28512854
fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES);
28522855
}
28532856

2857+
public static void jsonFolderToXml(
2858+
String jsonFolder, String xmlFolder, Xml.XmlStringBuilder.Step identStep)
2859+
throws IOException {
2860+
Path sourceRoot = Paths.get(jsonFolder);
2861+
Path targetRoot = Paths.get(xmlFolder);
2862+
Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() {
2863+
@Override
2864+
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
2865+
covertJsonToXml(path, sourceRoot, targetRoot, identStep);
2866+
return FileVisitResult.CONTINUE;
2867+
}
2868+
});
2869+
}
2870+
2871+
public static void jsonFolderToXml(String jsonFolder, String xmlFolder) throws IOException {
2872+
jsonFolderToXml(jsonFolder, xmlFolder, Xml.XmlStringBuilder.Step.TWO_SPACES);
2873+
}
2874+
2875+
public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot,
2876+
Xml.XmlStringBuilder.Step identStep) throws IOException {
2877+
Path relativePath = sourceRoot.relativize(path);
2878+
String fileName = relativePath.getFileName().toString();
2879+
String xmlFileName;
2880+
if (fileName.endsWith(".json")) {
2881+
xmlFileName = fileName.substring(0, fileName.length() - 5) + ".xml";
2882+
} else {
2883+
return;
2884+
}
2885+
Path targetPath = targetRoot.resolve(relativePath).getParent().resolve(xmlFileName);
2886+
Files.createDirectories(targetPath.getParent());
2887+
fileJsonToXml(path.toAbsolutePath().toString(), targetPath.toString(), identStep);
2888+
}
2889+
28542890
public static void streamJsonToXml(
28552891
InputStream jsonInputStream,
28562892
OutputStream xmlOutputStream,

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,28 @@ void testMapWithoutEncodingKey(@TempDir Path tempDir) throws IOException {
11991199
"Should write XML using UTF-8 when #encoding key not present");
12001200
}
12011201

1202+
@Test
1203+
void testJsonFolderToXml(@TempDir Path tempDir) throws IOException {
1204+
// Arrange
1205+
Path jsonFile = tempDir.resolve("in.json");
1206+
Path xmlFile = tempDir.resolve("in.xml");
1207+
String jsonText = "{}";
1208+
Files.write(jsonFile, jsonText.getBytes(StandardCharsets.UTF_8));
1209+
Files.write(xmlFile, jsonText.getBytes(StandardCharsets.UTF_8));
1210+
// Act
1211+
U.jsonFolderToXml(
1212+
jsonFile.getParent().toString(), xmlFile.getParent().toString());
1213+
// Assert
1214+
byte[] xmlBytes = Files.readAllBytes(xmlFile);
1215+
String xmlStr = new String(xmlBytes, StandardCharsets.UTF_8);
1216+
assertEquals(
1217+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
1218+
+ System.lineSeparator()
1219+
+ "<root></root>",
1220+
xmlStr,
1221+
"Should write XML using UTF-8 when #encoding key not present");
1222+
}
1223+
12021224
@Test
12031225
void testListResult(@TempDir Path tempDir) throws IOException {
12041226
// Arrange

0 commit comments

Comments
 (0)