Xml2Js.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import java.io.BufferedReader;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.io.UnsupportedEncodingException;
  10. import java.net.URLEncoder;
  11. import java.util.Arrays;
  12. import java.util.HashSet;
  13. import java.util.Iterator;
  14. import java.util.LinkedList;
  15. import java.util.List;
  16. import java.util.Set;
  17. import java.util.zip.Deflater;
  18. import java.util.Base64;
  19. public class Xml2Js
  20. {
  21. /**
  22. *
  23. */
  24. protected static final int IO_BUFFER_SIZE = 4 * 1024;
  25. /**
  26. *
  27. */
  28. public static String CHARSET_FOR_URL_ENCODING = "UTF-8";
  29. /**
  30. *
  31. * @param path
  32. * @return
  33. */
  34. public List<String> walk(File base, File root) throws IOException
  35. {
  36. if (root == null)
  37. {
  38. root = base;
  39. }
  40. List<String> result = new LinkedList<String>();
  41. String basePath = base.getCanonicalPath();
  42. File[] list = root.listFiles();
  43. if (list != null)
  44. {
  45. for (File f : list)
  46. {
  47. if (f.isDirectory())
  48. {
  49. result.addAll(walk(base, f));
  50. }
  51. else if (f.getCanonicalPath().toLowerCase().endsWith(".xml"))
  52. {
  53. String name = f.getCanonicalPath()
  54. .substring(basePath.length() + 1);
  55. result.add(
  56. "f['" + name + "'] = '" + processFile(f) + "';\n");
  57. }
  58. }
  59. }
  60. return result;
  61. }
  62. /**
  63. *
  64. * @param file
  65. * @return
  66. * @throws IOException
  67. */
  68. public static String processFile(File file) throws IOException
  69. {
  70. Deflater deflater = new Deflater(Deflater.BEST_COMPRESSION, true);
  71. byte[] inBytes = readInputStream(new FileInputStream(file)).getBytes("UTF-8");
  72. deflater.setInput(inBytes);
  73. ByteArrayOutputStream outputStream = new ByteArrayOutputStream(
  74. inBytes.length);
  75. deflater.finish();
  76. byte[] buffer = new byte[IO_BUFFER_SIZE];
  77. while (!deflater.finished())
  78. {
  79. int count = deflater.deflate(buffer); // returns the generated code... index
  80. outputStream.write(buffer, 0, count);
  81. }
  82. outputStream.close();
  83. return Base64.getEncoder().encodeToString(outputStream.toByteArray());
  84. }
  85. /**
  86. *
  87. * @param stream
  88. * @return
  89. * @throws IOException
  90. */
  91. public static String readInputStream(InputStream stream) throws IOException
  92. {
  93. BufferedReader reader = new BufferedReader(
  94. new InputStreamReader(stream));
  95. StringBuffer result = new StringBuffer();
  96. String tmp = reader.readLine();
  97. while (tmp != null)
  98. {
  99. result.append(tmp.trim());
  100. tmp = reader.readLine();
  101. }
  102. reader.close();
  103. return result.toString();
  104. }
  105. public static String encodeURIComponent(String s, String charset)
  106. {
  107. if (s == null)
  108. {
  109. return null;
  110. }
  111. else
  112. {
  113. String result;
  114. try
  115. {
  116. result = URLEncoder.encode(s, charset).replaceAll("\\+", "%20")
  117. .replaceAll("\\%21", "!").replaceAll("\\%27", "'")
  118. .replaceAll("\\%28", "(").replaceAll("\\%29", ")")
  119. .replaceAll("\\%7E", "~");
  120. }
  121. catch (UnsupportedEncodingException e)
  122. {
  123. // This exception should never occur
  124. result = s;
  125. }
  126. return result;
  127. }
  128. }
  129. private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  130. .toCharArray();
  131. private static final int[] IA = new int[256];
  132. static
  133. {
  134. Arrays.fill(IA, -1);
  135. for (int i = 0, iS = CA.length; i < iS; i++)
  136. IA[CA[i]] = i;
  137. IA['='] = 0;
  138. }
  139. /**
  140. * Main
  141. */
  142. public static void main(String[] args)
  143. {
  144. if (args.length < 2)
  145. {
  146. System.out.println("Usage: xml2js path file");
  147. }
  148. else
  149. {
  150. try
  151. {
  152. Xml2Js fw = new Xml2Js();
  153. // Generates result
  154. StringBuffer result = new StringBuffer();
  155. result.append("(function() {\nvar f = {};\n");
  156. List<String> files = fw
  157. .walk(new File(new File(".").getCanonicalPath()
  158. + File.separator + args[0]), null);
  159. Iterator<String> it = files.iterator();
  160. while (it.hasNext())
  161. {
  162. result.append(it.next());
  163. }
  164. result.append("\n");
  165. result.append("var l = mxStencilRegistry.loadStencil;\n\n");
  166. result.append(
  167. "mxStencilRegistry.loadStencil = function(filename, fn)\n{\n");
  168. result.append(" var t = f[filename.substring(STENCIL_PATH.length + 1)];\n");
  169. result.append(" var s = null;\n");
  170. result.append(" if (t != null) {\n");
  171. result.append(" s = pako.inflateRaw(Uint8Array.from(atob(t), function (c) {\n");
  172. result.append(" return c.charCodeAt(0);\n");
  173. result.append(" }), {to: 'string'});\n");
  174. result.append(" }\n");
  175. result.append(" if (fn != null && s != null) {\n");
  176. result.append(
  177. " window.setTimeout(function(){fn(mxUtils.parseXml(s))}, 0);\n");
  178. result.append(" } else {\n");
  179. result.append(
  180. " return (s != null) ? mxUtils.parseXml(s) : l.apply(this, arguments)\n");
  181. result.append(" }\n");
  182. result.append("};\n");
  183. result.append("})();\n");
  184. FileWriter writer = new FileWriter(
  185. new File(new File(".").getCanonicalPath()
  186. + File.separator + args[1]));
  187. writer.write(result.toString());
  188. writer.flush();
  189. writer.close();
  190. }
  191. catch (IOException ex)
  192. {
  193. ex.printStackTrace();
  194. }
  195. }
  196. }
  197. }