@@ -2781,6 +2781,103 @@ public static String xmlToJson(String xml, XmlToJsonMode mode) {
27812781 return xmlToJson (xml , Json .JsonStringBuilder .Step .TWO_SPACES , mode );
27822782 }
27832783
2784+ public static void fileXmlToJson (String xmlFileName , String jsonFileName , Json .JsonStringBuilder .Step identStep )
2785+ throws IOException {
2786+ final byte [] bytes = Files .readAllBytes (Paths .get (xmlFileName ));
2787+ String xmlText = new String (removeBom (bytes ), detectEncoding (bytes ));
2788+ Files .write (Paths .get (jsonFileName ), formatString (xmlToJson (xmlText , identStep ),
2789+ System .lineSeparator ()).getBytes (StandardCharsets .UTF_8 ));
2790+ }
2791+
2792+ public static void fileXmlToJson (String xmlFileName , String jsonFileName ) throws IOException {
2793+ fileXmlToJson (xmlFileName , jsonFileName , Json .JsonStringBuilder .Step .TWO_SPACES );
2794+ }
2795+
2796+ public static byte [] removeBom (byte [] bytes ) {
2797+ if ((bytes .length >= 3 ) && (bytes [0 ] == -17 ) && (bytes [1 ] == -69 ) && (bytes [2 ] == -65 )) {
2798+ return Arrays .copyOfRange (bytes , 3 , bytes .length );
2799+ }
2800+ if ((bytes .length >= 2 ) && (bytes [0 ] == -1 ) && (bytes [1 ] == -2 )) {
2801+ return Arrays .copyOfRange (bytes , 2 , bytes .length );
2802+ }
2803+ if ((bytes .length >= 2 ) && (bytes [0 ] == -2 ) && (bytes [1 ] == -1 )) {
2804+ return Arrays .copyOfRange (bytes , 2 , bytes .length );
2805+ }
2806+ return bytes ;
2807+ }
2808+
2809+ public static String detectEncoding (byte [] buffer ) {
2810+ if (buffer .length < 4 ) {
2811+ return "UTF8" ;
2812+ }
2813+ String encoding = null ;
2814+ int n = ((buffer [0 ] & 0xFF ) << 24 ) |
2815+ ((buffer [1 ] & 0xFF ) << 16 ) |
2816+ ((buffer [2 ] & 0xFF ) << 8 ) |
2817+ (buffer [3 ] & 0xFF );
2818+ switch ( n ) {
2819+ case 0x0000FEFF :
2820+ encoding = "UTF_32BE" ;
2821+ break ;
2822+ case 0x0000003C :
2823+ encoding = "UTF_32BE" ;
2824+ break ;
2825+ case 0x003C003F :
2826+ encoding = "UnicodeBigUnmarked" ;
2827+ break ;
2828+ case 0xFFFE0000 :
2829+ encoding = "UTF_32LE" ;
2830+ break ;
2831+ case 0x3C000000 :
2832+ encoding = "UTF_32LE" ;
2833+ break ;
2834+ case 0x3C003F00 : // <?
2835+ encoding = "UnicodeLittleUnmarked" ;
2836+ break ;
2837+ case 0x3C3F786D : // <?xm
2838+ encoding = "UTF8" ;
2839+ break ;
2840+ default :
2841+ if ( (n >>> 8 ) == 0xEFBBBF ) {
2842+ encoding = "UTF8" ;
2843+ break ;
2844+ }
2845+ if ( (n >>> 24 ) == 0x3C ) {
2846+ break ;
2847+ }
2848+ switch ( n >>> 16 ) {
2849+ case 0xFFFE :
2850+ encoding = "UnicodeLittleUnmarked" ;
2851+ break ;
2852+ case 0xFEFF :
2853+ encoding = "UnicodeBigUnmarked" ;
2854+ break ;
2855+ }
2856+ }
2857+ return encoding == null ? "UTF8" : encoding ;
2858+ }
2859+
2860+ public static String formatString (String data , String lineSeparator ) {
2861+ if ("\n " .equals (lineSeparator )) {
2862+ return data ;
2863+ }
2864+ return data .replace ("\n " , lineSeparator );
2865+ }
2866+
2867+ private static String getLineSeparator (String destFormat ) {
2868+ final String result ;
2869+ if ("mac" .equalsIgnoreCase (destFormat )) {
2870+ result = "\r " ;
2871+ } else if ("windows" .equalsIgnoreCase (destFormat )) {
2872+ result = "\r \n " ;
2873+ } else if ("unix" .equalsIgnoreCase (destFormat )) {
2874+ result = "\n " ;
2875+ } else {
2876+ result = System .lineSeparator ();
2877+ }
2878+ return result ;
2879+ }
2880+
27842881 public static String xmlOrJsonToJson (String xmlOrJson , Json .JsonStringBuilder .Step identStep ) {
27852882 TextType textType = getTextType (xmlOrJson );
27862883 final String result ;
0 commit comments