Skip to content

Commit 2fb0877

Browse files
committed
Added method U.jsonFolderToXml(jsonFileFolder, xmlFileFolder, identStep)
1 parent 7403690 commit 2fb0877

2 files changed

Lines changed: 58 additions & 8 deletions

File tree

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

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,15 @@
2323
*/
2424
package com.github.underscore;
2525

26-
import java.io.FileInputStream;
27-
import java.io.FileOutputStream;
28-
import java.io.IOException;
29-
import java.io.InputStream;
30-
import java.io.OutputStream;
26+
import java.io.*;
3127
import java.net.URI;
3228
import java.net.URISyntaxException;
3329
import java.net.URL;
3430
import java.nio.channels.Channels;
3531
import java.nio.channels.ReadableByteChannel;
3632
import java.nio.charset.StandardCharsets;
37-
import java.nio.file.Files;
38-
import java.nio.file.Path;
39-
import java.nio.file.Paths;
33+
import java.nio.file.*;
34+
import java.nio.file.attribute.BasicFileAttributes;
4035
import java.util.ArrayList;
4136
import java.util.Arrays;
4237
import java.util.Collection;
@@ -2851,6 +2846,39 @@ public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws
28512846
fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES);
28522847
}
28532848

2849+
public static void jsonFolderToXml(
2850+
String jsonFileFolder, String xmlFileFolder, Xml.XmlStringBuilder.Step identStep)
2851+
throws IOException {
2852+
Path sourceRoot = Paths.get(jsonFileFolder);
2853+
Path targetRoot = Paths.get(xmlFileFolder);
2854+
Files.walkFileTree(sourceRoot, new SimpleFileVisitor<>() {
2855+
@Override
2856+
public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException {
2857+
covertJsonToXml(path, sourceRoot, targetRoot, identStep);
2858+
return FileVisitResult.CONTINUE;
2859+
}
2860+
});
2861+
}
2862+
2863+
public static void jsonFolderToXml(String jsonFileFolder, String xmlFileFolder) throws IOException {
2864+
jsonFolderToXml(jsonFileFolder, xmlFileFolder, Xml.XmlStringBuilder.Step.TWO_SPACES);
2865+
}
2866+
2867+
public static void covertJsonToXml(Path path, Path sourceRoot, Path targetRoot,
2868+
Xml.XmlStringBuilder.Step identStep) throws IOException {
2869+
Path relativePath = sourceRoot.relativize(path);
2870+
String fileName = relativePath.getFileName().toString();
2871+
String xmlFileName;
2872+
if (fileName.endsWith(".json")) {
2873+
xmlFileName = fileName.substring(0, fileName.length() - 5) + ".xml";
2874+
} else {
2875+
return;
2876+
}
2877+
Path targetPath = targetRoot.resolve(relativePath).getParent().resolve(xmlFileName);
2878+
Files.createDirectories(targetPath.getParent());
2879+
fileJsonToXml(path.toAbsolutePath().toString(), targetPath.toString(), identStep);
2880+
}
2881+
28542882
public static void streamJsonToXml(
28552883
InputStream jsonInputStream,
28562884
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)