|
23 | 23 | */ |
24 | 24 | package com.github.underscore; |
25 | 25 |
|
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.*; |
31 | 27 | import java.net.URI; |
32 | 28 | import java.net.URISyntaxException; |
33 | 29 | import java.net.URL; |
34 | 30 | import java.nio.channels.Channels; |
35 | 31 | import java.nio.channels.ReadableByteChannel; |
36 | 32 | 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; |
40 | 35 | import java.util.ArrayList; |
41 | 36 | import java.util.Arrays; |
42 | 37 | import java.util.Collection; |
@@ -2851,6 +2846,39 @@ public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws |
2851 | 2846 | fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES); |
2852 | 2847 | } |
2853 | 2848 |
|
| 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 | + |
2854 | 2882 | public static void streamJsonToXml( |
2855 | 2883 | InputStream jsonInputStream, |
2856 | 2884 | OutputStream xmlOutputStream, |
|
0 commit comments