-
Notifications
You must be signed in to change notification settings - Fork 41
/
xml-parser.js
330 lines (290 loc) · 8.55 KB
/
xml-parser.js
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"use strict";
const CHAR_SINGLE_QUOTE = 39;
const CHAR_DOUBLE_QUOTE = 34;
const CHAR_LESS_THAN = 60;
const CHAR_GREATER_THAN = 62;
const CHAR_COLON = 58;
const CHAR_SPACE = 32;
const CHAR_TAB = 9;
const CHAR_CR = 13;
const CHAR_LF = 10;
const CHAR_SLASH = 47;
const CHAR_EXMARK = 33;
const CHAR_QMARK = 63;
const CHAR_EQUAL = 61;
const STATE_LESS_THAN = 1;
const STATE_SINGLE_QUOTE = 2;
const STATE_DOUBLE_QUOTE = 3;
function parseXmlDeclaration(buffer) {
for (const enc of ["utf16le", "utf8", "latin1", "ascii"]) {
let str = buffer.toString(enc, 0, 150);
if (str.startsWith("<?xml")) {
str = str.split("\n")[0].trim();
try {
return parseAttrs(str.slice(5, -2));
} catch (err) {
// Ignore
}
}
}
return null;
}
function parseAttrs(string) {
const attrs = [];
const len = string.length;
let state = 0;
let name = "";
let namespace = "";
let localName = "";
let idx = 0;
let colonIdx = 0;
for (let i = 0; i < len; ++i) {
const c = string.charCodeAt(i);
switch (c) {
case CHAR_SINGLE_QUOTE:
case CHAR_DOUBLE_QUOTE:
if (state === c) {
state = 0;
if (name) {
const value = string.slice(idx + 1, i);
const e = {
name: name,
namespace: namespace,
localName: localName,
value: value
};
attrs.push(e);
name = "";
idx = i + 1;
}
} else {
state = c;
idx = i;
}
continue;
case CHAR_COLON:
if (idx >= colonIdx) colonIdx = i;
continue;
case CHAR_EQUAL:
if (name) throw new Error(`Unexpected character at ${i}`);
name = string.slice(idx, i).trim();
// TODO validate name
if (colonIdx > idx) {
namespace = string.slice(idx, colonIdx).trim();
localName = string.slice(colonIdx + 1, i).trim();
} else {
namespace = "";
localName = name;
}
}
}
if (name) throw new Error(`Attribute must have value at ${idx}`);
const tail = string.slice(idx);
if (tail.trim()) throw new Error(`Unexpected string at ${len - tail.length}`);
return attrs;
}
function decodeEntities(string) {
return string.replace(/&[0-9a-z#]+;/gi, match => {
switch (match) {
case """:
return '"';
case "&":
return "&";
case "'":
return "'";
case "<":
return "<";
case ">":
return ">";
default:
if (match.startsWith("&#x")) {
const str = match.slice(3, -1).toLowerCase();
const n = parseInt(str, 16);
if (str.endsWith(n.toString(16))) return String.fromCharCode(n);
} else if (match.startsWith("&#")) {
const str = match.slice(2, -1);
const n = parseInt(str);
if (str.endsWith(n.toString())) return String.fromCharCode(n);
}
}
return match;
});
}
function encodeEntities(string) {
const entities = {
"&": "&",
'"': """,
"'": "'",
"<": "<",
">": ">"
};
return string.replace(/[&"'<>]/g, m => entities[m]);
}
function parseXml(string) {
const len = string.length;
let state1 = 0;
let state1Index = 0;
let state2 = 0;
let state2Index = 0;
const root = {
name: "root",
namespace: "",
localName: "root",
attrs: "",
text: "",
bodyIndex: 0,
children: []
};
const stack = [root];
for (let i = 0; i < len; ++i) {
switch (string.charCodeAt(i)) {
case CHAR_SINGLE_QUOTE:
switch (state1 & 0xff) {
case STATE_SINGLE_QUOTE:
state1 = state2;
state1Index = state2Index;
state2 = 0;
continue;
case STATE_LESS_THAN:
state2 = state1;
state2Index = state1Index;
state1 = STATE_SINGLE_QUOTE;
state1Index = i;
continue;
}
continue;
case CHAR_DOUBLE_QUOTE:
switch (state1 & 0xff) {
case STATE_DOUBLE_QUOTE:
state1 = state2;
state1Index = state2Index;
state2 = 0;
continue;
case STATE_LESS_THAN:
state2 = state1;
state2Index = state1Index;
state1 = STATE_DOUBLE_QUOTE;
state1Index = i;
continue;
}
continue;
case CHAR_LESS_THAN:
if ((state1 & 0xff) === 0) {
state2 = state1;
state2Index = state1Index;
state1 = STATE_LESS_THAN;
state1Index = i;
}
continue;
case CHAR_COLON:
if ((state1 & 0xff) === STATE_LESS_THAN) {
const colonIndex = (state1 >> 8) & 0xff;
if (colonIndex === 0) state1 ^= ((i - state1Index) & 0xff) << 8;
}
continue;
case CHAR_SPACE:
case CHAR_TAB:
case CHAR_CR:
case CHAR_LF:
if ((state1 & 0xff) === STATE_LESS_THAN) {
const wsIndex = (state1 >> 16) & 0xff;
if (wsIndex === 0) state1 ^= ((i - state1Index) & 0xff) << 16;
}
continue;
case CHAR_GREATER_THAN:
if ((state1 & 0xff) === STATE_LESS_THAN) {
const secondChar = string.charCodeAt(state1Index + 1);
const wsIndex = (state1 >> 16) & 0xff;
let name,
colonIndex,
e,
parent,
selfClosing,
localName,
namespace;
switch (secondChar) {
case CHAR_SLASH:
e = stack.pop();
name =
wsIndex === 0
? string.slice(state1Index + 2, i)
: string.slice(state1Index + 2, state1Index + wsIndex);
if (e.name !== name)
throw new Error(`Unmatched closing tag at ${i}`);
if (!e.children.length)
e.text = string.slice(e.bodyIndex, state1Index);
state1 = state2;
state1Index = state2Index;
state2 = 0;
continue;
case CHAR_EXMARK:
if (string.startsWith("![CDATA[", state1Index + 1)) {
if (string.endsWith("]]", i))
throw new Error(`CDATA nodes are not supported at ${i}`);
} else if (string.startsWith("!--", state1Index + 1)) {
// Comment node, ignore
if (string.endsWith("--", i)) {
state1 = state2;
state1Index = state2Index;
state2 = 0;
}
}
continue;
case CHAR_QMARK:
if (string.charCodeAt(i - 1) === CHAR_QMARK) {
// XML declaration node, ignore
state1 = state2;
state1Index = state2Index;
state2 = 0;
}
continue;
default:
selfClosing = +(string.charCodeAt(i - 1) === CHAR_SLASH);
parent = stack[stack.length - 1];
colonIndex = (state1 >> 8) & 0xff;
name =
wsIndex === 0
? string.slice(state1Index + 1, i - selfClosing)
: string.slice(state1Index + 1, state1Index + wsIndex);
if (colonIndex && (!wsIndex || colonIndex < wsIndex)) {
localName = name.slice(colonIndex);
namespace = name.slice(0, colonIndex - 1);
} else {
localName = name;
namespace = "";
}
e = {
name: name,
namespace: namespace,
localName: localName,
attrs: wsIndex
? string.slice(state1Index + wsIndex + 1, i - selfClosing)
: "",
text: "",
bodyIndex: i + 1,
children: []
};
parent.children.push(e);
if (!selfClosing) stack.push(e);
state1 = state2;
state1Index = state2Index;
state2 = 0;
continue;
}
}
continue;
}
}
if (state1) throw new Error(`Unclosed token at ${state1Index}`);
if (stack.length > 1) {
const e = stack[stack.length - 1];
throw new Error(`Unclosed XML element at ${e.bodyIndex}`);
}
if (!root.children.length) root.text = string;
return root;
}
exports.parseXmlDeclaration = parseXmlDeclaration;
exports.parseAttrs = parseAttrs;
exports.decodeEntities = decodeEntities;
exports.encodeEntities = encodeEntities;
exports.parseXml = parseXml;