-
Notifications
You must be signed in to change notification settings - Fork 1
/
XML.mjs
447 lines (415 loc) · 12.3 KB
/
XML.mjs
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// refer: https://github.com/Peng-YM/QuanX/blob/master/Tools/XMLParser/xml-parser.js
// refer: https://goessner.net/download/prj/jsonxml/
export default class XML {
static name = "XML";
static version = "0.4.2";
static about = () => console.log(`\n🟧 ${this.name} v${this.version}\n`);
static #ATTRIBUTE_KEY = "@";
static #CHILD_NODE_KEY = "#";
static #UNESCAPE = {
"&": "&",
"<": "<",
">": ">",
"'": "'",
""": '"'
};
static #ESCAPE = {
"&": "&",
"<": "<",
">": ">",
"'": "'",
'"': """
};
static parse(xml = new String, reviver = "") {
const UNESCAPE = this.#UNESCAPE;
const ATTRIBUTE_KEY = this.#ATTRIBUTE_KEY;
const CHILD_NODE_KEY = this.#CHILD_NODE_KEY;
const DOM = toDOM(xml);
let json = fromXML(DOM, reviver);
return json;
/***************** Fuctions *****************/
function toDOM(text) {
const list = text.replace(/^[ \t]+/gm, "")
.split(/<([^!<>?](?:'[\S\s]*?'|"[\S\s]*?"|[^'"<>])*|!(?:--[\S\s]*?--|\[[^\[\]'"<>]+\[[\S\s]*?]]|DOCTYPE[^\[<>]*?\[[\S\s]*?]|(?:ENTITY[^"<>]*?"[\S\s]*?")?[\S\s]*?)|\?[\S\s]*?\?)>/);
const length = list.length;
// root element
const root = { children: [] };
let elem = root;
// dom tree stack
const stack = [];
// parse
for (let i = 0; i < length;) {
// text node
const str = list[i++];
if (str) appendText(str);
// child node
const tag = list[i++];
if (tag) parseNode(tag);
}
return root;
/***************** Fuctions *****************/
function parseNode(tag) {
const tags = tag.split(" ");
const name = tags.shift();
const length = tags.length;
let child = {};
switch (name[0]) {
case "/":
// close tag
const closed = tag.replace(/^\/|[\s\/].*$/g, "").toLowerCase();
while (stack.length) {
const tagName = elem?.name?.toLowerCase?.();
elem = stack.pop();
if (tagName === closed) break;
}
break;
case "?":
// XML declaration
child.name = name;
child.raw = tags.join(" ");
appendChild(child);
break;
case "!":
if (/!\[CDATA\[(.+)\]\]/.test(tag)) {
// CDATA section
child.name = "!CDATA";
//child.raw = tag.slice(9, -2);
child.raw = tag.match(/!\[CDATA\[(?<raw>.+)\]\]/)?.groups?.raw;
//appendText(tag.slice(9, -2));
} else if (/!--(.+)--/.test(tag)) {
// Comment section
child.name = "!--";
child.raw = tag.match(/!--(?<raw>.+)--/)?.groups?.raw;
} else {
// Comment section
child.name = name;
child.raw = tags.join(" ");
};
appendChild(child);
break;
default:
child = openTag(tag);
appendChild(child);
switch ((tags?.[length - 1] ?? name).slice(-1)) {
case "/":
//child.hasChild = false; // emptyTag
delete child.children; // emptyTag
break;
default:
switch (name) {
case "link":
//child.hasChild = false; // emptyTag
delete child.children; // emptyTag
break;
default:
stack.push(elem); // openTag
elem = child;
break;
};
break;
};
break;
};
function openTag(tag) {
const elem = { children: [] };
tag = tag.replace(/\s*\/?$/, "");
const pos = tag.search(/[\s='"\/]/);
if (pos < 0) {
elem.name = tag;
} else {
elem.name = tag.substr(0, pos);
elem.tag = tag.substr(pos);
}
return elem;
};
};
function appendText(str) {
//str = removeSpaces(str);
str = removeBreakLine(str);
//str = str?.trim?.();
if (str) appendChild(unescapeXML(str));
function removeBreakLine(str) {
return str?.replace?.(/^(\r\n|\r|\n|\t)+|(\r\n|\r|\n|\t)+$/g, "");
}
}
function appendChild(child) {
elem.children.push(child);
}
};
/***************** Fuctions *****************/
function fromPlist(elem, reviver) {
let object;
switch (typeof elem) {
case "string":
case "undefined":
object = elem;
break;
case "object":
//default:
const name = elem.name;
const children = elem.children;
object = {};
switch (name) {
case "plist":
let plist = fromPlist(children[0], reviver);
object = Object.assign(object, plist)
break;
case "dict":
let dict = children.map(child => fromPlist(child, reviver));
dict = chunk(dict, 2);
object = Object.fromEntries(dict);
break;
case "array":
if (!Array.isArray(object)) object = [];
object = children.map(child => fromPlist(child, reviver));
break;
case "key":
const key = children[0];
object = key;
break;
case "true":
case "false":
const boolean = name;
object = JSON.parse(boolean);
break;
case "integer":
const integer = children[0];
//object = parseInt(integer);
object = BigInt(integer);
break;
case "real":
const real = children[0];
//const digits = real.split(".")[1]?.length || 0;
object = parseFloat(real)//.toFixed(digits);
break;
case "string":
const string = children[0];
object = string;
break;
};
if (reviver) object = reviver(name || "", object);
break;
}
return object;
/**
* Chunk Array
* @author VirgilClyne
* @param {Array} source - source
* @param {Number} length - number
* @return {Array<*>} target
*/
function chunk(source, length) {
var index = 0, target = [];
while (index < source.length) target.push(source.slice(index, index += length));
return target;
};
}
function fromXML(elem, reviver) {
let object;
switch (typeof elem) {
case "string":
case "undefined":
object = elem;
break;
case "object":
//default:
const raw = elem.raw;
const name = elem.name;
const tag = elem.tag;
const children = elem.children;
if (raw) object = raw;
else if (tag) object = parseAttribute(tag, reviver);
else if (!children) object = { [name]: undefined };
else object = {};
if (name === "plist") object = Object.assign(object, fromPlist(children[0], reviver));
else children?.forEach?.((child, i) => {
if (typeof child === "string") addObject(object, CHILD_NODE_KEY, fromXML(child, reviver), undefined)
else if (!child.tag && !child.children && !child.raw) addObject(object, child.name, fromXML(child, reviver), children?.[i - 1]?.name)
else addObject(object, child.name, fromXML(child, reviver), undefined)
});
if (children && children.length === 0) addObject(object, CHILD_NODE_KEY, null, undefined);
/*
if (Object.keys(object).length === 0) {
if (elem.name) object[elem.name] = (elem.hasChild === false) ? null : "";
else object = (elem.hasChild === false) ? null : "";
}
*/
//if (Object.keys(object).length === 0) addObject(object, elem.name, (elem.hasChild === false) ? null : "");
//if (Object.keys(object).length === 0) object = (elem.hasChild === false) ? undefined : "";
if (reviver) object = reviver(name || "", object);
break;
}
return object;
/***************** Fuctions *****************/
function parseAttribute(tag, reviver) {
if (!tag) return;
const list = tag.split(/([^\s='"]+(?:\s*=\s*(?:'[\S\s]*?'|"[\S\s]*?"|[^\s'"]*))?)/);
const length = list.length;
let attributes, val;
for (let i = 0; i < length; i++) {
let str = removeSpaces(list[i]);
//let str = removeBreakLine(list[i]);
//let str = list[i]?.trim?.();
if (!str) continue;
if (!attributes) {
attributes = {};
}
const pos = str.indexOf("=");
if (pos < 0) {
// bare attribute
str = ATTRIBUTE_KEY + str;
val = null;
} else {
// attribute key/value pair
val = str.substr(pos + 1).replace(/^\s+/, "");
str = ATTRIBUTE_KEY + str.substr(0, pos).replace(/\s+$/, "");
// quote: foo="FOO" bar='BAR'
const firstChar = val[0];
const lastChar = val[val.length - 1];
if (firstChar === lastChar && (firstChar === "'" || firstChar === '"')) {
val = val.substr(1, val.length - 2);
}
val = unescapeXML(val);
}
if (reviver) val = reviver(str, val);
addObject(attributes, str, val);
}
return attributes;
function removeSpaces(str) {
//return str && str.replace(/^\s+|\s+$/g, "");
return str?.trim?.();
}
}
function addObject(object, key, val, prevKey = key) {
if (typeof val === "undefined") return;
else {
const prev = object[prevKey];
//const curr = object[key];
if (Array.isArray(prev)) prev.push(val);
else if (prev) object[prevKey] = [prev, val];
else object[key] = val;
}
}
}
function unescapeXML(str) {
return str.replace(/(&(?:lt|gt|amp|apos|quot|#(?:\d{1,6}|x[0-9a-fA-F]{1,5}));)/g, function (str) {
if (str[1] === "#") {
const code = (str[2] === "x") ? parseInt(str.substr(3), 16) : parseInt(str.substr(2), 10);
if (code > -1) return String.fromCharCode(code);
}
return UNESCAPE[str] || str;
});
}
};
static stringify(json = new Object, tab = "") {
const ESCAPE = this.#ESCAPE;
const ATTRIBUTE_KEY = this.#ATTRIBUTE_KEY;
const CHILD_NODE_KEY = this.#CHILD_NODE_KEY;
let XML = "";
for (let elem in json) XML += toXml(json[elem], elem, "");
XML = tab ? XML.replace(/\t/g, tab) : XML.replace(/\t|\n/g, "");
return XML;
/***************** Fuctions *****************/
function toXml(Elem, Name, Ind) {
let xml = "";
switch (typeof Elem) {
case "object":
if (Array.isArray(Elem)) {
xml = Elem.reduce(
(prevXML, currXML) => prevXML += `${Ind}${toXml(currXML, Name, `${Ind}\t`)}\n`,
""
);
} else {
let attribute = "";
let hasChild = false;
for (let name in Elem) {
if (name[0] === ATTRIBUTE_KEY) {
attribute += ` ${name.substring(1)}=\"${Elem[name].toString()}\"`;
delete Elem[name];
} else if (Elem[name] === undefined) Name = name;
else hasChild = true;
}
xml += `${Ind}<${Name}${attribute}${(hasChild || Name === "link") ? "" : "/"}>`;
if (hasChild) {
if (Name === "plist") xml += toPlist(Elem, Name, `${Ind}\t`);
else {
for (let name in Elem) {
switch (name) {
case CHILD_NODE_KEY:
xml += Elem[name] ?? "";
break;
default:
xml += toXml(Elem[name], name, `${Ind}\t`);
break;
};
};
};
xml += (xml.slice(-1) === "\n" ? Ind : "") + `</${Name}>`;
};
};
break;
case "string":
switch (Name) {
case "?xml":
xml += `${Ind}<${Name} ${Elem.toString()}>`;
break;
case "?":
xml += `${Ind}<${Name}${Elem.toString()}${Name}>`;
break;
case "!--":
xml += `${Ind}<!--${Elem.toString()}-->`;
break;
case "!DOCTYPE":
xml += `${Ind}<${Name} ${Elem.toString()}>`;
break;
case "!CDATA":
xml += `${Ind}<![CDATA[${Elem.toString()}]]>`;
break;
case CHILD_NODE_KEY:
xml += Elem;
break;
default:
xml += `${Ind}<${Name}>${Elem.toString()}</${Name}>`;
break;
};
break;
case "undefined":
xml += Ind + `<${Name.toString()}/>`;
break;
};
return xml;
};
function toPlist(Elem, Name, Ind) {
let plist = "";
switch (typeof Elem) {
case "boolean":
plist = `${Ind}<${Elem.toString()}/>`;
break;
case "number":
plist = `${Ind}<real>${Elem.toString()}</real>`;
break;
case "bigint":
plist = `${Ind}<integer>${Elem.toString()}</integer>`;
break;
case "string":
plist = `${Ind}<string>${Elem.toString()}</string>`;
break;
case "object":
let array = "";
if (Array.isArray(Elem)) {
for (var i = 0, n = Elem.length; i < n; i++) array += `${Ind}${toPlist(Elem[i], Name, `${Ind}\t`)}`;
plist = `${Ind}<array>${array}${Ind}</array>`;
} else {
let dict = "";
Object.entries(Elem).forEach(([key, value]) => {
dict += `${Ind}<key>${key}</key>`;
dict += toPlist(value, key, Ind);
});
plist = `${Ind}<dict>${dict}${Ind}</dict>`;
};
break;
}
return plist;
};
};
}