1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parseXml = exports.XElement = void 0;
|
4 | const sax = require("sax");
|
5 | const index_1 = require("./index");
|
6 | class XElement {
|
7 | constructor(name) {
|
8 | this.name = name;
|
9 | this.value = "";
|
10 | this.attributes = null;
|
11 | this.isCData = false;
|
12 | this.elements = null;
|
13 | if (!name) {
|
14 | throw index_1.newError("Element name cannot be empty", "ERR_XML_ELEMENT_NAME_EMPTY");
|
15 | }
|
16 | if (!isValidName(name)) {
|
17 | throw index_1.newError(`Invalid element name: ${name}`, "ERR_XML_ELEMENT_INVALID_NAME");
|
18 | }
|
19 | }
|
20 | attribute(name) {
|
21 | const result = this.attributes === null ? null : this.attributes[name];
|
22 | if (result == null) {
|
23 | throw index_1.newError(`No attribute "${name}"`, "ERR_XML_MISSED_ATTRIBUTE");
|
24 | }
|
25 | return result;
|
26 | }
|
27 | removeAttribute(name) {
|
28 | if (this.attributes !== null) {
|
29 | delete this.attributes[name];
|
30 | }
|
31 | }
|
32 | element(name, ignoreCase = false, errorIfMissed = null) {
|
33 | const result = this.elementOrNull(name, ignoreCase);
|
34 | if (result === null) {
|
35 | throw index_1.newError(errorIfMissed || `No element "${name}"`, "ERR_XML_MISSED_ELEMENT");
|
36 | }
|
37 | return result;
|
38 | }
|
39 | elementOrNull(name, ignoreCase = false) {
|
40 | if (this.elements === null) {
|
41 | return null;
|
42 | }
|
43 | for (const element of this.elements) {
|
44 | if (isNameEquals(element, name, ignoreCase)) {
|
45 | return element;
|
46 | }
|
47 | }
|
48 | return null;
|
49 | }
|
50 | getElements(name, ignoreCase = false) {
|
51 | if (this.elements === null) {
|
52 | return [];
|
53 | }
|
54 | return this.elements.filter(it => isNameEquals(it, name, ignoreCase));
|
55 | }
|
56 | elementValueOrEmpty(name, ignoreCase = false) {
|
57 | const element = this.elementOrNull(name, ignoreCase);
|
58 | return element === null ? "" : element.value;
|
59 | }
|
60 | }
|
61 | exports.XElement = XElement;
|
62 | const NAME_REG_EXP = new RegExp(/^[A-Za-z_][:A-Za-z0-9_-]*$/i);
|
63 | function isValidName(name) {
|
64 | return NAME_REG_EXP.test(name);
|
65 | }
|
66 | function isNameEquals(element, name, ignoreCase) {
|
67 | const elementName = element.name;
|
68 | return elementName === name || (ignoreCase === true && elementName.length === name.length && elementName.toLowerCase() === name.toLowerCase());
|
69 | }
|
70 | function parseXml(data) {
|
71 | let rootElement = null;
|
72 | const parser = sax.parser(true, {});
|
73 | const elements = [];
|
74 | parser.onopentag = saxElement => {
|
75 | const element = new XElement(saxElement.name);
|
76 | element.attributes = saxElement.attributes;
|
77 | if (rootElement === null) {
|
78 | rootElement = element;
|
79 | }
|
80 | else {
|
81 | const parent = elements[elements.length - 1];
|
82 | if (parent.elements == null) {
|
83 | parent.elements = [];
|
84 | }
|
85 | parent.elements.push(element);
|
86 | }
|
87 | elements.push(element);
|
88 | };
|
89 | parser.onclosetag = () => {
|
90 | elements.pop();
|
91 | };
|
92 | parser.ontext = text => {
|
93 | if (elements.length > 0) {
|
94 | elements[elements.length - 1].value = text;
|
95 | }
|
96 | };
|
97 | parser.oncdata = cdata => {
|
98 | const element = elements[elements.length - 1];
|
99 | element.value = cdata;
|
100 | element.isCData = true;
|
101 | };
|
102 | parser.onerror = err => {
|
103 | throw err;
|
104 | };
|
105 | parser.write(data);
|
106 | return rootElement;
|
107 | }
|
108 | exports.parseXml = parseXml;
|
109 |
|
\ | No newline at end of file |