|
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;
|
35 | 37 | import java.nio.file.Files;
|
| 38 | +import java.nio.file.Path; |
36 | 39 | import java.nio.file.Paths;
|
37 | 40 | import java.util.ArrayList;
|
38 | 41 | import java.util.Arrays;
|
@@ -100,6 +103,7 @@ public class U<T> extends Underscore<T> {
|
100 | 103 | java.util.regex.Pattern.compile(
|
101 | 104 | UPPER + "+(?=" + UPPER + LOWER + ")|" + UPPER + "?" + LOWER + "|" + UPPER
|
102 | 105 | + "+|\\d+");
|
| 106 | + private static final String ENCODING = "#encoding"; |
103 | 107 |
|
104 | 108 | static {
|
105 | 109 | String[] deburredLetters =
|
@@ -2781,6 +2785,173 @@ public static String xmlToJson(String xml, XmlToJsonMode mode) {
|
2781 | 2785 | return xmlToJson(xml, Json.JsonStringBuilder.Step.TWO_SPACES, mode);
|
2782 | 2786 | }
|
2783 | 2787 |
|
| 2788 | + public static void fileXmlToJson( |
| 2789 | + String xmlFileName, String jsonFileName, Json.JsonStringBuilder.Step identStep) |
| 2790 | + throws IOException { |
| 2791 | + final byte[] bytes = Files.readAllBytes(Paths.get(xmlFileName)); |
| 2792 | + String xmlText = new String(removeBom(bytes), detectEncoding(bytes)); |
| 2793 | + Files.write( |
| 2794 | + Paths.get(jsonFileName), |
| 2795 | + formatString(xmlToJson(xmlText, identStep), System.lineSeparator()) |
| 2796 | + .getBytes(StandardCharsets.UTF_8)); |
| 2797 | + } |
| 2798 | + |
| 2799 | + public static void fileXmlToJson(String xmlFileName, String jsonFileName) throws IOException { |
| 2800 | + fileXmlToJson(xmlFileName, jsonFileName, Json.JsonStringBuilder.Step.TWO_SPACES); |
| 2801 | + } |
| 2802 | + |
| 2803 | + public static void streamXmlToJson( |
| 2804 | + InputStream xmlInputStream, |
| 2805 | + OutputStream jsonOutputStream, |
| 2806 | + Json.JsonStringBuilder.Step indentStep) |
| 2807 | + throws IOException { |
| 2808 | + byte[] bytes = xmlInputStream.readAllBytes(); |
| 2809 | + String encoding = detectEncoding(bytes); |
| 2810 | + String xmlText = new String(removeBom(bytes), encoding); |
| 2811 | + String jsonText = xmlToJson(xmlText, indentStep); |
| 2812 | + String formattedJson = formatString(jsonText, System.lineSeparator()); |
| 2813 | + jsonOutputStream.write(formattedJson.getBytes(StandardCharsets.UTF_8)); |
| 2814 | + } |
| 2815 | + |
| 2816 | + public static void streamXmlToJson(InputStream xmlInputStream, OutputStream jsonOutputStream) |
| 2817 | + throws IOException { |
| 2818 | + streamXmlToJson(xmlInputStream, jsonOutputStream, Json.JsonStringBuilder.Step.TWO_SPACES); |
| 2819 | + } |
| 2820 | + |
| 2821 | + public static void fileJsonToXml( |
| 2822 | + String jsonFileName, String xmlFileName, Xml.XmlStringBuilder.Step identStep) |
| 2823 | + throws IOException { |
| 2824 | + final byte[] bytes = Files.readAllBytes(Paths.get(jsonFileName)); |
| 2825 | + String jsonText = new String(removeBom(bytes), detectEncoding(bytes)); |
| 2826 | + Object result = U.fromJson(jsonText); |
| 2827 | + Path xmlFilePath = Paths.get(xmlFileName); |
| 2828 | + String lineSeparator = System.lineSeparator(); |
| 2829 | + if (result instanceof Map) { |
| 2830 | + if (((Map) result).containsKey(ENCODING)) { |
| 2831 | + String encoding = String.valueOf(((Map) result).get(ENCODING)); |
| 2832 | + Files.write( |
| 2833 | + xmlFilePath, |
| 2834 | + formatString(Xml.toXml((Map) result, identStep), lineSeparator) |
| 2835 | + .getBytes(encoding)); |
| 2836 | + } else { |
| 2837 | + Files.write( |
| 2838 | + xmlFilePath, |
| 2839 | + formatString(Xml.toXml((Map) result, identStep), lineSeparator) |
| 2840 | + .getBytes(StandardCharsets.UTF_8)); |
| 2841 | + } |
| 2842 | + } else { |
| 2843 | + Files.write( |
| 2844 | + xmlFilePath, |
| 2845 | + formatString(Xml.toXml((List) result, identStep), lineSeparator) |
| 2846 | + .getBytes(StandardCharsets.UTF_8)); |
| 2847 | + } |
| 2848 | + } |
| 2849 | + |
| 2850 | + public static void fileJsonToXml(String jsonFileName, String xmlFileName) throws IOException { |
| 2851 | + fileJsonToXml(jsonFileName, xmlFileName, Xml.XmlStringBuilder.Step.TWO_SPACES); |
| 2852 | + } |
| 2853 | + |
| 2854 | + public static void streamJsonToXml( |
| 2855 | + InputStream jsonInputStream, |
| 2856 | + OutputStream xmlOutputStream, |
| 2857 | + Xml.XmlStringBuilder.Step identStep) |
| 2858 | + throws IOException { |
| 2859 | + byte[] bytes = jsonInputStream.readAllBytes(); |
| 2860 | + String jsonText = new String(removeBom(bytes), detectEncoding(bytes)); |
| 2861 | + Object jsonObject = U.fromJson(jsonText); |
| 2862 | + String lineSeparator = System.lineSeparator(); |
| 2863 | + String xml; |
| 2864 | + if (jsonObject instanceof Map) { |
| 2865 | + xml = formatString(Xml.toXml((Map<?, ?>) jsonObject, identStep), lineSeparator); |
| 2866 | + if (((Map) jsonObject).containsKey(ENCODING)) { |
| 2867 | + String encoding = String.valueOf(((Map) jsonObject).get(ENCODING)); |
| 2868 | + xmlOutputStream.write(xml.getBytes(encoding)); |
| 2869 | + } else { |
| 2870 | + xmlOutputStream.write(xml.getBytes(StandardCharsets.UTF_8)); |
| 2871 | + } |
| 2872 | + } else { |
| 2873 | + xml = formatString(Xml.toXml((List<?>) jsonObject, identStep), lineSeparator); |
| 2874 | + xmlOutputStream.write(xml.getBytes(StandardCharsets.UTF_8)); |
| 2875 | + } |
| 2876 | + } |
| 2877 | + |
| 2878 | + public static void streamJsonToXml(InputStream jsonInputStream, OutputStream xmlOutputStream) |
| 2879 | + throws IOException { |
| 2880 | + streamJsonToXml(jsonInputStream, xmlOutputStream, Xml.XmlStringBuilder.Step.TWO_SPACES); |
| 2881 | + } |
| 2882 | + |
| 2883 | + public static byte[] removeBom(byte[] bytes) { |
| 2884 | + if ((bytes.length >= 3) && (bytes[0] == -17) && (bytes[1] == -69) && (bytes[2] == -65)) { |
| 2885 | + return Arrays.copyOfRange(bytes, 3, bytes.length); |
| 2886 | + } |
| 2887 | + if ((bytes.length >= 2) && (bytes[0] == -1) && (bytes[1] == -2)) { |
| 2888 | + return Arrays.copyOfRange(bytes, 2, bytes.length); |
| 2889 | + } |
| 2890 | + if ((bytes.length >= 2) && (bytes[0] == -2) && (bytes[1] == -1)) { |
| 2891 | + return Arrays.copyOfRange(bytes, 2, bytes.length); |
| 2892 | + } |
| 2893 | + return bytes; |
| 2894 | + } |
| 2895 | + |
| 2896 | + public static String detectEncoding(byte[] buffer) { |
| 2897 | + if (buffer.length < 4) { |
| 2898 | + return "UTF8"; |
| 2899 | + } |
| 2900 | + String encoding = null; |
| 2901 | + int n = |
| 2902 | + ((buffer[0] & 0xFF) << 24) |
| 2903 | + | ((buffer[1] & 0xFF) << 16) |
| 2904 | + | ((buffer[2] & 0xFF) << 8) |
| 2905 | + | (buffer[3] & 0xFF); |
| 2906 | + switch (n) { |
| 2907 | + case 0x0000FEFF: |
| 2908 | + case 0x0000003C: |
| 2909 | + encoding = "UTF_32BE"; |
| 2910 | + break; |
| 2911 | + case 0x003C003F: |
| 2912 | + encoding = "UnicodeBigUnmarked"; |
| 2913 | + break; |
| 2914 | + case 0xFFFE0000: |
| 2915 | + case 0x3C000000: |
| 2916 | + encoding = "UTF_32LE"; |
| 2917 | + break; |
| 2918 | + // <? |
| 2919 | + case 0x3C003F00: |
| 2920 | + encoding = "UnicodeLittleUnmarked"; |
| 2921 | + break; |
| 2922 | + // <?xm |
| 2923 | + case 0x3C3F786D: |
| 2924 | + encoding = "UTF8"; |
| 2925 | + break; |
| 2926 | + default: |
| 2927 | + if ((n >>> 8) == 0xEFBBBF) { |
| 2928 | + encoding = "UTF8"; |
| 2929 | + break; |
| 2930 | + } |
| 2931 | + if ((n >>> 24) == 0x3C) { |
| 2932 | + break; |
| 2933 | + } |
| 2934 | + switch (n >>> 16) { |
| 2935 | + case 0xFFFE: |
| 2936 | + encoding = "UnicodeLittleUnmarked"; |
| 2937 | + break; |
| 2938 | + case 0xFEFF: |
| 2939 | + encoding = "UnicodeBigUnmarked"; |
| 2940 | + break; |
| 2941 | + default: |
| 2942 | + break; |
| 2943 | + } |
| 2944 | + } |
| 2945 | + return encoding == null ? "UTF8" : encoding; |
| 2946 | + } |
| 2947 | + |
| 2948 | + public static String formatString(String data, String lineSeparator) { |
| 2949 | + if ("\n".equals(lineSeparator)) { |
| 2950 | + return data; |
| 2951 | + } |
| 2952 | + return data.replace("\n", lineSeparator); |
| 2953 | + } |
| 2954 | + |
2784 | 2955 | public static String xmlOrJsonToJson(String xmlOrJson, Json.JsonStringBuilder.Step identStep) {
|
2785 | 2956 | TextType textType = getTextType(xmlOrJson);
|
2786 | 2957 | final String result;
|
|
0 commit comments