Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/main/java/com/github/underscore/U.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/github/underscore/UnderscoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
+ System.lineSeparator()
+ "<root></root>",
xmlStr,
"Should write XML using UTF-8 when #encoding key not present");
}

@Test
void testListResult(@TempDir Path tempDir) throws IOException {
// Arrange
Expand Down
Loading