@hpcc-js/util 3.5.4 → 3.5.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/saxParser.ts CHANGED
@@ -1,135 +1,135 @@
1
- import { StringAnyMap } from "./dictionary.ts";
2
- import { Stack } from "./stack.ts";
3
-
4
- export class XMLNode {
5
- name: string = "";
6
- $: StringAnyMap = {};
7
- protected _children: XMLNode[] = [];
8
- content: string = "";
9
-
10
- constructor(name: string) {
11
- this.name = name;
12
- }
13
-
14
- appendAttribute(key: string, val: string) {
15
- this.$[key] = val;
16
- }
17
-
18
- appendContent(content: string) {
19
- this.content += content;
20
- }
21
-
22
- appendChild(child: XMLNode) {
23
- this._children.push(child);
24
- }
25
-
26
- children(tag?: string): XMLNode[] {
27
- if (tag === undefined) {
28
- return this._children;
29
- }
30
- return this._children.filter((xmlNode) => {
31
- return xmlNode.name === tag;
32
- });
33
- }
34
- }
35
-
36
- export class SAXStackParser {
37
- root?: XMLNode;
38
- stack: Stack<XMLNode> = new Stack<XMLNode>();
39
-
40
- constructor() {
41
- }
42
-
43
- private walkDoc(node: any) {
44
- const xmlNode = this._startXMLNode(node);
45
- if (node.attributes) {
46
- for (let i = 0; i < node.attributes.length; ++i) {
47
- const attribute = node.attributes.item(i);
48
- this.attributes(attribute.nodeName, attribute.nodeValue);
49
- }
50
- }
51
- this.startXMLNode(xmlNode);
52
- if (node.childNodes) {
53
- for (let i = 0; i < node.childNodes.length; ++i) {
54
- const childNode = node.childNodes.item(i);
55
- if (childNode.nodeType === childNode.TEXT_NODE) {
56
- this.characters(childNode.nodeValue!);
57
- } else {
58
- this.walkDoc(childNode);
59
- }
60
- }
61
- }
62
- this.endXMLNode(this.stack.pop()!);
63
- }
64
-
65
- private _startXMLNode(node: Node): XMLNode {
66
- const newNode = new XMLNode(node.nodeName);
67
- if (!this.stack.depth()) {
68
- this.root = newNode;
69
- } else {
70
- this.stack.top()!.appendChild(newNode);
71
- }
72
- return this.stack.push(newNode);
73
- }
74
-
75
- parse(xml: string) {
76
- const domParser = new DOMParser();
77
- const doc = domParser.parseFromString(xml, "application/xml");
78
- this.startDocument();
79
- this.walkDoc(doc);
80
- this.endDocument();
81
- }
82
-
83
- // Callbacks ---
84
- startDocument() {
85
- }
86
-
87
- endDocument() {
88
- }
89
-
90
- startXMLNode(node: XMLNode) {
91
- }
92
-
93
- endXMLNode(node: XMLNode) {
94
- }
95
-
96
- attributes(key: string, val: any) {
97
- this.stack.top()!.appendAttribute(key, val);
98
- }
99
-
100
- characters(text: string) {
101
- this.stack.top()!.appendContent(text);
102
- }
103
- }
104
-
105
- class XML2JSONParser extends SAXStackParser {
106
- startXMLNode(node: XMLNode) {
107
- super.startXMLNode(node);
108
- switch (node.name) {
109
- case "xs:element":
110
- break;
111
- case "xs:simpleType":
112
- break;
113
- default:
114
- break;
115
- }
116
- }
117
-
118
- endXMLNode(node: XMLNode) {
119
- switch (node.name) {
120
- case "xs:element":
121
- break;
122
- case "xs:simpleType":
123
- break;
124
- default:
125
- break;
126
- }
127
- super.endXMLNode(node);
128
- }
129
- }
130
-
131
- export function xml2json(xml: string): XMLNode | undefined {
132
- const saxParser = new XML2JSONParser();
133
- saxParser.parse(xml);
134
- return saxParser.root;
135
- }
1
+ import { StringAnyMap } from "./dictionary.ts";
2
+ import { Stack } from "./stack.ts";
3
+
4
+ export class XMLNode {
5
+ name: string = "";
6
+ $: StringAnyMap = {};
7
+ protected _children: XMLNode[] = [];
8
+ content: string = "";
9
+
10
+ constructor(name: string) {
11
+ this.name = name;
12
+ }
13
+
14
+ appendAttribute(key: string, val: string) {
15
+ this.$[key] = val;
16
+ }
17
+
18
+ appendContent(content: string) {
19
+ this.content += content;
20
+ }
21
+
22
+ appendChild(child: XMLNode) {
23
+ this._children.push(child);
24
+ }
25
+
26
+ children(tag?: string): XMLNode[] {
27
+ if (tag === undefined) {
28
+ return this._children;
29
+ }
30
+ return this._children.filter((xmlNode) => {
31
+ return xmlNode.name === tag;
32
+ });
33
+ }
34
+ }
35
+
36
+ export class SAXStackParser {
37
+ root?: XMLNode;
38
+ stack: Stack<XMLNode> = new Stack<XMLNode>();
39
+
40
+ constructor() {
41
+ }
42
+
43
+ private walkDoc(node: any) {
44
+ const xmlNode = this._startXMLNode(node);
45
+ if (node.attributes) {
46
+ for (let i = 0; i < node.attributes.length; ++i) {
47
+ const attribute = node.attributes.item(i);
48
+ this.attributes(attribute.nodeName, attribute.nodeValue);
49
+ }
50
+ }
51
+ this.startXMLNode(xmlNode);
52
+ if (node.childNodes) {
53
+ for (let i = 0; i < node.childNodes.length; ++i) {
54
+ const childNode = node.childNodes.item(i);
55
+ if (childNode.nodeType === childNode.TEXT_NODE) {
56
+ this.characters(childNode.nodeValue!);
57
+ } else {
58
+ this.walkDoc(childNode);
59
+ }
60
+ }
61
+ }
62
+ this.endXMLNode(this.stack.pop()!);
63
+ }
64
+
65
+ private _startXMLNode(node: Node): XMLNode {
66
+ const newNode = new XMLNode(node.nodeName);
67
+ if (!this.stack.depth()) {
68
+ this.root = newNode;
69
+ } else {
70
+ this.stack.top()!.appendChild(newNode);
71
+ }
72
+ return this.stack.push(newNode);
73
+ }
74
+
75
+ parse(xml: string) {
76
+ const domParser = new DOMParser();
77
+ const doc = domParser.parseFromString(xml, "application/xml");
78
+ this.startDocument();
79
+ this.walkDoc(doc);
80
+ this.endDocument();
81
+ }
82
+
83
+ // Callbacks ---
84
+ startDocument() {
85
+ }
86
+
87
+ endDocument() {
88
+ }
89
+
90
+ startXMLNode(node: XMLNode) {
91
+ }
92
+
93
+ endXMLNode(node: XMLNode) {
94
+ }
95
+
96
+ attributes(key: string, val: any) {
97
+ this.stack.top()!.appendAttribute(key, val);
98
+ }
99
+
100
+ characters(text: string) {
101
+ this.stack.top()!.appendContent(text);
102
+ }
103
+ }
104
+
105
+ class XML2JSONParser extends SAXStackParser {
106
+ startXMLNode(node: XMLNode) {
107
+ super.startXMLNode(node);
108
+ switch (node.name) {
109
+ case "xs:element":
110
+ break;
111
+ case "xs:simpleType":
112
+ break;
113
+ default:
114
+ break;
115
+ }
116
+ }
117
+
118
+ endXMLNode(node: XMLNode) {
119
+ switch (node.name) {
120
+ case "xs:element":
121
+ break;
122
+ case "xs:simpleType":
123
+ break;
124
+ default:
125
+ break;
126
+ }
127
+ super.endXMLNode(node);
128
+ }
129
+ }
130
+
131
+ export function xml2json(xml: string): XMLNode | undefined {
132
+ const saxParser = new XML2JSONParser();
133
+ saxParser.parse(xml);
134
+ return saxParser.root;
135
+ }
package/src/stack.ts CHANGED
@@ -1,41 +1,41 @@
1
- /**
2
- * A generic Stack
3
- */
4
- export class Stack<T> {
5
- private stack: T[] = [];
6
-
7
- /**
8
- * Push element onto the stack
9
- *
10
- * @param e - element to push
11
- */
12
- push(e: T) {
13
- this.stack.push(e);
14
- return e;
15
- }
16
-
17
- /**
18
- * Pop element off the stack
19
- */
20
- pop(): T | undefined {
21
- return this.stack.pop();
22
- }
23
-
24
- /**
25
- * Top item on the stack
26
- *
27
- * @returns Top element on the stack
28
- */
29
- top(): T | undefined {
30
- return this.stack.length ? this.stack[this.stack.length - 1] : undefined;
31
- }
32
-
33
- /**
34
- * Depth of stack
35
- *
36
- * @returns Depth
37
- */
38
- depth(): number {
39
- return this.stack.length;
40
- }
41
- }
1
+ /**
2
+ * A generic Stack
3
+ */
4
+ export class Stack<T> {
5
+ private stack: T[] = [];
6
+
7
+ /**
8
+ * Push element onto the stack
9
+ *
10
+ * @param e - element to push
11
+ */
12
+ push(e: T) {
13
+ this.stack.push(e);
14
+ return e;
15
+ }
16
+
17
+ /**
18
+ * Pop element off the stack
19
+ */
20
+ pop(): T | undefined {
21
+ return this.stack.pop();
22
+ }
23
+
24
+ /**
25
+ * Top item on the stack
26
+ *
27
+ * @returns Top element on the stack
28
+ */
29
+ top(): T | undefined {
30
+ return this.stack.length ? this.stack[this.stack.length - 1] : undefined;
31
+ }
32
+
33
+ /**
34
+ * Depth of stack
35
+ *
36
+ * @returns Depth
37
+ */
38
+ depth(): number {
39
+ return this.stack.length;
40
+ }
41
+ }