Xml2Js.java
8.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.zip.Deflater;
public class Xml2Js
{
/**
*
*/
protected static final int IO_BUFFER_SIZE = 4 * 1024;
/**
*
*/
public static String CHARSET_FOR_URL_ENCODING = "UTF-8";
/**
*
* @param path
* @return
*/
public List<String> walk(File base, File root) throws IOException
{
if (root == null)
{
root = base;
}
List<String> result = new LinkedList<String>();
String basePath = base.getCanonicalPath();
File[] list = root.listFiles();
if (list != null)
{
for (File f : list)
{
if (f.isDirectory())
{
result.addAll(walk(base, f));
}
else if (f.getCanonicalPath().toLowerCase().endsWith(".xml"))
{
String name = f.getCanonicalPath()
.substring(basePath.length() + 1);
result.add(
"f['" + name.replace("\\", "/") + "'] = '" + processFile(f) + "';\n");
}
}
}
return result;
}
/**
*
* @param file
* @return
* @throws IOException
*/
public static String processFile(File file) throws IOException
{
System.out.println("Processing " + file.getCanonicalPath() + "...");
Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION, true);
byte[] inBytes = encodeURIComponent(
readInputStream(new FileInputStream(file)),
CHARSET_FOR_URL_ENCODING).getBytes("UTF-8");
deflater.setInput(inBytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(
inBytes.length);
deflater.finish();
byte[] buffer = new byte[IO_BUFFER_SIZE];
while (!deflater.finished())
{
int count = deflater.deflate(buffer); // returns the generated code... index
outputStream.write(buffer, 0, count);
}
outputStream.close();
return encodeToString(outputStream.toByteArray(), false);
}
/**
*
* @param stream
* @return
* @throws IOException
*/
public static String readInputStream(InputStream stream) throws IOException
{
BufferedReader reader = new BufferedReader(
new InputStreamReader(stream));
StringBuffer result = new StringBuffer();
String tmp = reader.readLine();
while (tmp != null)
{
result.append(tmp.trim());
tmp = reader.readLine();
}
reader.close();
return result.toString();
}
public static String encodeURIComponent(String s, String charset)
{
if (s == null)
{
return null;
}
else
{
String result;
try
{
result = URLEncoder.encode(s, charset).replaceAll("\\+", "%20")
.replaceAll("\\%21", "!").replaceAll("\\%27", "'")
.replaceAll("\\%28", "(").replaceAll("\\%29", ")")
.replaceAll("\\%7E", "~");
}
catch (UnsupportedEncodingException e)
{
// This exception should never occur
result = s;
}
return result;
}
}
private static final char[] CA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
.toCharArray();
private static final int[] IA = new int[256];
static
{
Arrays.fill(IA, -1);
for (int i = 0, iS = CA.length; i < iS; i++)
IA[CA[i]] = i;
IA['='] = 0;
}
// ****************************************************************************************
// * char[] version
// ****************************************************************************************
/** Encodes a raw byte array into a BASE64 <code>char[]</code> representation i accordance with RFC 2045.
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
* little faster.
* @return A BASE64 encoded array. Never <code>null</code>.
*/
public final static char[] encodeToChar(byte[] sArr, boolean lineSep)
{
// Check special case
int sLen = sArr != null ? sArr.length : 0;
if (sLen == 0)
return new char[0];
int eLen = (sLen / 3) * 3; // Length of even 24-bits.
int cCnt = ((sLen - 1) / 3 + 1) << 2; // Returned character count
int dLen = cCnt + (lineSep ? (cCnt - 1) / 76 << 1 : 0); // Length of returned array
char[] dArr = new char[dLen];
// Encode even 24-bits
for (int s = 0, d = 0, cc = 0; s < eLen;)
{
// Copy next three bytes into lower 24 bits of int, paying attension to sign.
int i = (sArr[s++] & 0xff) << 16 | (sArr[s++] & 0xff) << 8
| (sArr[s++] & 0xff);
// Encode the int into four chars
dArr[d++] = CA[(i >>> 18) & 0x3f];
dArr[d++] = CA[(i >>> 12) & 0x3f];
dArr[d++] = CA[(i >>> 6) & 0x3f];
dArr[d++] = CA[i & 0x3f];
// Add optional line separator
if (lineSep && ++cc == 19 && d < dLen - 2)
{
dArr[d++] = '\r';
dArr[d++] = '\n';
cc = 0;
}
}
// Pad and encode last bits if source isn't even 24 bits.
int left = sLen - eLen; // 0 - 2.
if (left > 0)
{
// Prepare the int
int i = ((sArr[eLen] & 0xff) << 10)
| (left == 2 ? ((sArr[sLen - 1] & 0xff) << 2) : 0);
// Set last four chars
dArr[dLen - 4] = CA[i >> 12];
dArr[dLen - 3] = CA[(i >>> 6) & 0x3f];
dArr[dLen - 2] = left == 2 ? CA[i & 0x3f] : '=';
dArr[dLen - 1] = '=';
}
return dArr;
}
// ****************************************************************************************
// * String version
// ****************************************************************************************
/** Encodes a raw byte array into a BASE64 <code>String</code> representation i accordance with RFC 2045.
* @param sArr The bytes to convert. If <code>null</code> or length 0 an empty array will be returned.
* @param lineSep Optional "\r\n" after 76 characters, unless end of file.<br>
* No line separator will be in breach of RFC 2045 which specifies max 76 per line but will be a
* little faster.
* @return A BASE64 encoded array. Never <code>null</code>.
*/
public final static String encodeToString(byte[] sArr, boolean lineSep)
{
// Reuse char[] since we can't create a String incrementally anyway and StringBuffer/Builder would be slower.
return new String(encodeToChar(sArr, lineSep));
}
/**
* Main
*/
public static void main(String[] args)
{
if (args.length < 2)
{
System.out.println("Usage: xml2js path file");
}
else
{
try
{
Xml2Js fw = new Xml2Js();
// Generates result
StringBuffer result = new StringBuffer();
result.append("(function() {\nvar f = {};\n");
List<String> files = fw
.walk(new File(new File(".").getCanonicalPath()
+ File.separator + args[0]), null);
Iterator<String> it = files.iterator();
while (it.hasNext())
{
result.append(it.next());
}
result.append("\n");
result.append("var l = mxStencilRegistry.loadStencil;\n\n");
result.append(
"mxStencilRegistry.loadStencil = function(filename, fn)\n{\n");
result.append(" var t = f[filename.substring(STENCIL_PATH.length + 1)];\n");
result.append(" var s = null;\n");
result.append(" if (t != null) {\n");
result.append(" t = pako.inflateRaw(Uint8Array.from(atob(t), function (c) {\n");
result.append(" return c.charCodeAt(0);\n");
result.append(" }), {to: 'string'});\n");
result.append(" s = decodeURIComponent(t);\n");
result.append(" }\n");
result.append(" if (fn != null && s != null) {\n");
result.append(
" window.setTimeout(function(){fn(mxUtils.parseXml(s))}, 0);\n");
result.append(" } else {\n");
result.append(
" return (s != null) ? mxUtils.parseXml(s) : l.apply(this, arguments)\n");
result.append(" }\n");
result.append("};\n");
result.append("})();\n");
FileWriter writer = new FileWriter(
new File(new File(".").getCanonicalPath()
+ File.separator + args[1]));
writer.write(result.toString());
writer.flush();
writer.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
}