|
26 | 26 | import java.io.FileInputStream;
|
27 | 27 | import java.io.FileOutputStream;
|
28 | 28 | import java.io.IOException;
|
| 29 | +import java.io.InputStream; |
| 30 | +import java.io.OutputStream; |
29 | 31 | import java.net.URI;
|
30 | 32 | import java.net.URISyntaxException;
|
31 | 33 | import java.net.URL;
|
32 | 34 | import java.nio.channels.Channels;
|
33 | 35 | import java.nio.channels.ReadableByteChannel;
|
34 | 36 | import java.nio.charset.StandardCharsets;
|
| 37 | +import java.nio.file.FileVisitResult; |
35 | 38 | import java.nio.file.Files;
|
| 39 | +import java.nio.file.Path; |
36 | 40 | import java.nio.file.Paths;
|
| 41 | +import java.nio.file.SimpleFileVisitor; |
| 42 | +import java.nio.file.attribute.BasicFileAttributes; |
37 | 43 | import java.util.ArrayList;
|
38 | 44 | import java.util.Arrays;
|
39 | 45 | import java.util.Collection;
|
@@ -100,6 +106,7 @@ public class U<T> extends Underscore<T> {
|
100 | 106 | java.util.regex.Pattern.compile(
|
101 | 107 | UPPER + "+(?=" + UPPER + LOWER + ")|" + UPPER + "?" + LOWER + "|" + UPPER
|
102 | 108 | + "+|\\d+");
|
| 109 | + private static final String ENCODING = "#encoding"; |
103 | 110 |
|
104 | 111 | static {
|
105 | 112 | String[] deburredLetters =
|
@@ -2781,6 +2788,206 @@ public static String xmlToJson(String xml, XmlToJsonMode mode) {
|
2781 | 2788 | return xmlToJson(xml, Json.JsonStringBuilder.Step.TWO_SPACES, mode);
|
2782 | 2789 | }
|
2783 | 2790 |
|
| 2791 | + public static void fileXmlToJson( |
| 2792 | + String xmlFileName, String jsonFileName, Json.JsonStringBuilder.Step identStep) |
| 2793 | + throws IOException { |
| 2794 | + final byte[] bytes = Files.readAllBytes(Paths.get(xmlFileName)); |
| 2795 | + String xmlText = new String(removeBom(bytes), detectEncoding(bytes)); |
| 2796 | + Files.write( |
| 2797 | + Paths.get(jsonFileName), |
| 2798 | + formatString(xmlToJson(xmlText, identStep), System.lineSeparator()) |
| 2799 | + .getBytes(StandardCharsets.UTF_8)); |
| 2800 | + } |
| 2801 | + |
| 2802 | + public static void fileXmlToJson(String xmlFileName, String jsonFileName) throws IOException { |
| 2803 | + fileXmlToJson(xmlFileName, jsonFileName, Json.JsonStringBuilder.Step.TWO_SPACES); |
| 2804 | + } |
| 2805 | + |
| 2806 | + public static void streamXmlToJson( |
| 2807 | + InputStream xmlInputStream, |
| 2808 | + OutputStream jsonOutputStream, |
| 2809 | + Json.JsonStringBuilder.Step indentStep) |
| 2810 | + throws IOException { |
| 2811 | + byte[] bytes = xmlInputStream.readAllBytes(); |
| 2812 | + String encoding = detectEncoding(bytes); |
| 2813 | + String xmlText = new String(removeBom(bytes), encoding); |
| 2814 | + String jsonText = xmlToJson(xmlText, indentStep); |
| 2815 | + String formattedJson = formatString(jsonText, System.lineSeparator()); |
| 2816 | + jsonOutputStream.write(formattedJson.getBytes(StandardCharsets.UTF_8)); |
| 2817 | + } |
| 2818 | + |
| 2819 | + public static void streamXmlToJson(InputStream xmlInputStream, OutputStream jsonOutputStream) |
| 2820 | + throws IOException { |
| 2821 | + streamXmlToJson(xmlInputStream, jsonOutputStream, Json.JsonStringBuilder.Step.TWO_SPACES); |
| 2822 | + } |
| 2823 | + |
| 2824 | + public static void fileJsonToXml( |
| 2825 | + String jsonFileName, String xmlFileName, Xml.XmlStringBuilder.Step identStep) |
| 2826 | + throws IOException { |
| 2827 | + final byte[] bytes = Files.readAllBytes(Paths.get(jsonFileName)); |
| 2828 | + String jsonText = new String(removeBom(bytes), detectEncoding(bytes)); |
| 2829 | + Object result = U.fromJson(jsonText); |
| 2830 | + Path xmlFilePath = Paths.get(xmlFileName); |
| 2831 | + String lineSeparator = System.lineSeparator(); |
| 2832 | + if (result instanceof Map) { |
| 2833 | + if (((Map) result).containsKey(ENCODING)) { |
| 2834 | + String encoding = String.valueOf(((Map) result).get(ENCODING)); |
| 2835 | + Files.write( |
| 2836 | + xmlFilePath, |
| 2837 | + formatString(Xml.toXml((Map) result, identStep), lineSeparator) |
| 2838 | + .getBytes(encoding)); |
| 2839 | + } else { |
| 2840 | + Files.write( |
| 2841 | + xmlFilePath, |
| 2842 | + formatString(Xml.toXml((Map) result, identStep), lineSeparator) |
| 2843 | + .getBytes(StandardCharsets.UTF_8)); |
| 2844 | + } |
| 2845 | + } else { |
| 2846 | + Files.write( |
| 2847 | + xmlFilePath, |
| 2848 | + formatString(Xml.toXml((List) result, identStep), lineSeparator) |
| 2849 | + .getBytes(StandardCharsets.UTF_8)); |
| 2850 | + } |
| 2851 | + } |
| 2852 | + |
| 2853 | + public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws IOException { |
| 2854 | + fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES); |
| 2855 | + } |
| 2856 | + |
| 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 | + |
| 2890 | + public static void streamJsonToXml( |
| 2891 | + InputStream jsonInputStream, |
| 2892 | + OutputStream xmlOutputStream, |
| 2893 | + Xml.XmlStringBuilder.Step identStep) |
| 2894 | + throws IOException { |
| 2895 | + byte[] bytes = jsonInputStream.readAllBytes(); |
| 2896 | + String jsonText = new String(removeBom(bytes), detectEncoding(bytes)); |
| 2897 | + Object jsonObject = U.fromJson(jsonText); |
| 2898 | + String lineSeparator = System.lineSeparator(); |
| 2899 | + String xml; |
| 2900 | + if (jsonObject instanceof Map) { |
| 2901 | + xml = formatString(Xml.toXml((Map<?, ?>) jsonObject, identStep), lineSeparator); |
| 2902 | + if (((Map) jsonObject).containsKey(ENCODING)) { |
| 2903 | + String encoding = String.valueOf(((Map) jsonObject).get(ENCODING)); |
| 2904 | + xmlOutputStream.write(xml.getBytes(encoding)); |
| 2905 | + } else { |
| 2906 | + xmlOutputStream.write(xml.getBytes(StandardCharsets.UTF_8)); |
| 2907 | + } |
| 2908 | + } else { |
| 2909 | + xml = formatString(Xml.toXml((List<?>) jsonObject, identStep), lineSeparator); |
| 2910 | + xmlOutputStream.write(xml.getBytes(StandardCharsets.UTF_8)); |
| 2911 | + } |
| 2912 | + } |
| 2913 | + |
| 2914 | + public static void streamJsonToXml(InputStream jsonInputStream, OutputStream xmlOutputStream) |
| 2915 | + throws IOException { |
| 2916 | + streamJsonToXml(jsonInputStream, xmlOutputStream, Xml.XmlStringBuilder.Step.TWO_SPACES); |
| 2917 | + } |
| 2918 | + |
| 2919 | + public static byte[] removeBom(byte[] bytes) { |
| 2920 | + if ((bytes.length >= 3) && (bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65)) { |
| 2921 | + return Arrays.copyOfRange(bytes, 3, bytes.length); |
| 2922 | + } |
| 2923 | + if ((bytes.length >= 2) && (bytes[0] == -1) && (bytes[1] == -2)) { |
| 2924 | + return Arrays.copyOfRange(bytes, 2, bytes.length); |
| 2925 | + } |
| 2926 | + if ((bytes.length >= 2) && (bytes[0] == -2) && (bytes[1] == -1)) { |
| 2927 | + return Arrays.copyOfRange(bytes, 2, bytes.length); |
| 2928 | + } |
| 2929 | + return bytes; |
| 2930 | + } |
| 2931 | + |
| 2932 | + public static String detectEncoding(byte[] buffer) { |
| 2933 | + if (buffer.length < 4) { |
| 2934 | + return "UTF8"; |
| 2935 | + } |
| 2936 | + String encoding = null; |
| 2937 | + int n = |
| 2938 | + ((buffer[0] & 0xFF) << 24) |
| 2939 | + | ((buffer[1] & 0xFF) << 16) |
| 2940 | + | ((buffer[2] & 0xFF) << 8) |
| 2941 | + | (buffer[3] & 0xFF); |
| 2942 | + switch (n) { |
| 2943 | + case 0x0000FEFF: |
| 2944 | + case 0x0000003C: |
| 2945 | + encoding = "UTF_32BE"; |
| 2946 | + break; |
| 2947 | + case 0x003C003F: |
| 2948 | + encoding = "UnicodeBigUnmarked"; |
| 2949 | + break; |
| 2950 | + case 0xFFFE0000: |
| 2951 | + case 0x3C000000: |
| 2952 | + encoding = "UTF_32LE"; |
| 2953 | + break; |
| 2954 | + // <? |
| 2955 | + case 0x3C003F00: |
| 2956 | + encoding = "UnicodeLittleUnmarked"; |
| 2957 | + break; |
| 2958 | + // <?xm |
| 2959 | + case 0x3C3F786D: |
| 2960 | + encoding = "UTF8"; |
| 2961 | + break; |
| 2962 | + default: |
| 2963 | + if ((n >>> 8) == 0xEFBBBF) { |
| 2964 | + encoding = "UTF8"; |
| 2965 | + break; |
| 2966 | + } |
| 2967 | + if ((n >>> 24) == 0x3C) { |
| 2968 | + break; |
| 2969 | + } |
| 2970 | + switch (n >>> 16) { |
| 2971 | + case 0xFFFE: |
| 2972 | + encoding = "UnicodeLittleUnmarked"; |
| 2973 | + break; |
| 2974 | + case 0xFEFF: |
| 2975 | + encoding = "UnicodeBigUnmarked"; |
| 2976 | + break; |
| 2977 | + default: |
| 2978 | + break; |
| 2979 | + } |
| 2980 | + } |
| 2981 | + return encoding == null ? "UTF8" : encoding; |
| 2982 | + } |
| 2983 | + |
| 2984 | + public static String formatString(String data, String lineSeparator) { |
| 2985 | + if ("\n".equals(lineSeparator)) { |
| 2986 | + return data; |
| 2987 | + } |
| 2988 | + return data.replace("\n", lineSeparator); |
| 2989 | + } |
| 2990 | + |
2784 | 2991 | public static String xmlOrJsonToJson(String xmlOrJson, Json.JsonStringBuilder.Step identStep) {
|
2785 | 2992 | TextType textType = getTextType(xmlOrJson);
|
2786 | 2993 | final String result;
|
|
0 commit comments