node-opcua-xml2json 2.63.1 → 2.64.1
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/dist/source/definition_parser.d.ts +3 -3
- package/dist/source/definition_parser.js +83 -83
- package/dist/source/extension_object_parser.d.ts +17 -17
- package/dist/source/extension_object_parser.js +239 -239
- package/dist/source/fragmentCloner.d.ts +20 -0
- package/dist/source/fragmentCloner.js +55 -0
- package/dist/source/fragmentCloner.js.map +1 -0
- package/dist/source/fragment_cloner.d.ts +13 -13
- package/dist/source/fragment_cloner.js +44 -44
- package/dist/source/fragment_cloner_parser.d.ts +8 -8
- package/dist/source/fragment_cloner_parser.js +15 -15
- package/dist/source/index.d.ts +6 -6
- package/dist/source/index.js +18 -18
- package/dist/source/nodejs/xml2json_fs.d.ts +10 -10
- package/dist/source/nodejs/xml2json_fs.js +38 -38
- package/dist/source/xml2JsonPojo.d.ts +8 -0
- package/dist/source/xml2JsonPojo.js +109 -0
- package/dist/source/xml2JsonPojo.js.map +1 -0
- package/dist/source/xml2Json_pojo_tools.d.ts +19 -19
- package/dist/source/xml2Json_pojo_tools.js +88 -88
- package/dist/source/xml2json.d.ts +177 -177
- package/dist/source/xml2json.js +295 -295
- package/dist/source/xml2json_pojo.d.ts +7 -7
- package/dist/source/xml2json_pojo.js +28 -28
- package/package.json +6 -6
package/dist/source/xml2json.js
CHANGED
|
@@ -1,296 +1,296 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @module node-opcua-xml2json
|
|
4
|
-
* node -> see if https://github.com/isaacs/sax-js could be used instead
|
|
5
|
-
*/
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.Xml2Json = exports.ReaderState = exports.ReaderStateBase = void 0;
|
|
8
|
-
// tslint:disable:max-classes-per-file
|
|
9
|
-
// tslint:disable:no-var-requires
|
|
10
|
-
// tslint:disable:unified-signatures
|
|
11
|
-
const node_opcua_assert_1 = require("node-opcua-assert");
|
|
12
|
-
const LtxParser = require("ltx/lib/parsers/ltx.js");
|
|
13
|
-
/**
|
|
14
|
-
* @static
|
|
15
|
-
* @private
|
|
16
|
-
* @method _coerceParser
|
|
17
|
-
* @param parser {map<ReaderState|options>}
|
|
18
|
-
* @return {map}
|
|
19
|
-
*/
|
|
20
|
-
function _coerceParser(parser) {
|
|
21
|
-
for (const name of Object.keys(parser)) {
|
|
22
|
-
if (parser[name] && !(parser[name] instanceof ReaderState)) {
|
|
23
|
-
// this is to prevent recursion
|
|
24
|
-
const tmp = parser[name];
|
|
25
|
-
delete parser[name];
|
|
26
|
-
parser[name] = new ReaderState(tmp);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
return parser;
|
|
30
|
-
}
|
|
31
|
-
class ReaderStateBase {
|
|
32
|
-
}
|
|
33
|
-
exports.ReaderStateBase = ReaderStateBase;
|
|
34
|
-
/**
|
|
35
|
-
* @class ReaderState
|
|
36
|
-
* @private
|
|
37
|
-
* @param options
|
|
38
|
-
* @param [options.parser=null] {map<ReaderState|options}}
|
|
39
|
-
* @param [options.init|null]
|
|
40
|
-
* @param [options.finish]
|
|
41
|
-
* @param [options.startElement]
|
|
42
|
-
* @param [options.endElement]
|
|
43
|
-
*/
|
|
44
|
-
class ReaderState extends ReaderStateBase {
|
|
45
|
-
constructor(options) {
|
|
46
|
-
super();
|
|
47
|
-
this.chunks = [];
|
|
48
|
-
this.text = "";
|
|
49
|
-
this.name = "";
|
|
50
|
-
this.level = -1;
|
|
51
|
-
this.currentLevel = -1;
|
|
52
|
-
// ensure options object has only expected properties
|
|
53
|
-
options.parser = options.parser || {};
|
|
54
|
-
if (!(options instanceof ReaderStateBase)) {
|
|
55
|
-
this._init = options.init;
|
|
56
|
-
this._finish = options.finish;
|
|
57
|
-
this._startElement = options.startElement;
|
|
58
|
-
this._endElement = options.endElement;
|
|
59
|
-
}
|
|
60
|
-
this.parser = _coerceParser(options.parser);
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* @method _on_init
|
|
64
|
-
* @param elementName - the name of the element
|
|
65
|
-
* @param attrs
|
|
66
|
-
* @param parent
|
|
67
|
-
* @param level
|
|
68
|
-
* @param engine
|
|
69
|
-
* @protected
|
|
70
|
-
*/
|
|
71
|
-
_on_init(elementName, attrs, parent, level, engine) {
|
|
72
|
-
this.name = elementName;
|
|
73
|
-
this.parent = parent;
|
|
74
|
-
this.engine = engine;
|
|
75
|
-
this.data = {};
|
|
76
|
-
this.level = level;
|
|
77
|
-
this.currentLevel = this.level;
|
|
78
|
-
this.attrs = attrs;
|
|
79
|
-
(0, node_opcua_assert_1.assert)(this.attrs);
|
|
80
|
-
if (this._init) {
|
|
81
|
-
this._init(elementName, attrs, parent, engine);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
_on_finish() {
|
|
85
|
-
if (this._finish) {
|
|
86
|
-
this._finish();
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* @param level
|
|
91
|
-
* @param elementName - the name of the element
|
|
92
|
-
* @param attrs
|
|
93
|
-
* @protected
|
|
94
|
-
*/
|
|
95
|
-
_on_startElement(level, elementName, attrs) {
|
|
96
|
-
this.currentLevel = level;
|
|
97
|
-
// console.log("wxxxx _on_startElement#" + this.name, elementName, this.currentLevel);
|
|
98
|
-
this.chunks = [];
|
|
99
|
-
this.text = "";
|
|
100
|
-
if (this._startElement) {
|
|
101
|
-
this._startElement(elementName, attrs);
|
|
102
|
-
}
|
|
103
|
-
if (this.engine && Object.prototype.hasOwnProperty.call(this.parser, elementName)) {
|
|
104
|
-
// console.log("promoting ", elementName, this.level);
|
|
105
|
-
this.engine._promote(this.parser[elementName], level, elementName, attrs);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
_on_endElement2(level, elementName) {
|
|
109
|
-
if (this._endElement) {
|
|
110
|
-
this._endElement(elementName);
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* @method _on_endElement
|
|
115
|
-
* @protected
|
|
116
|
-
*/
|
|
117
|
-
_on_endElement(level, elementName) {
|
|
118
|
-
// console.log("wxxxx _on_endElement#" + this.name, elementName, level, this.currentLevel);
|
|
119
|
-
(0, node_opcua_assert_1.assert)(this.attrs);
|
|
120
|
-
this.chunks = this.chunks || [];
|
|
121
|
-
if (this.level > level) {
|
|
122
|
-
// we end a child element of this node
|
|
123
|
-
this._on_endElement2(level, elementName);
|
|
124
|
-
}
|
|
125
|
-
else if (this.level === level) {
|
|
126
|
-
// we received the end event of this node
|
|
127
|
-
// we need to finish
|
|
128
|
-
this.text = this.chunks.join("");
|
|
129
|
-
this.chunks = [];
|
|
130
|
-
// this is the end
|
|
131
|
-
this._on_finish();
|
|
132
|
-
if (this.parent &&
|
|
133
|
-
this.parent.parser &&
|
|
134
|
-
Object.prototype.hasOwnProperty.call(this.parent.parser, elementName)) {
|
|
135
|
-
// console.log("xxx demoting#" + this.name, elementName, this.level);
|
|
136
|
-
this.engine._demote(this, level, elementName);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
/**
|
|
141
|
-
* @method _on_text
|
|
142
|
-
* @param text {String} the text found inside the element
|
|
143
|
-
* @protected
|
|
144
|
-
*/
|
|
145
|
-
_on_text(text) {
|
|
146
|
-
this.chunks = this.chunks || [];
|
|
147
|
-
text = text.trim();
|
|
148
|
-
if (text.length === 0) {
|
|
149
|
-
return;
|
|
150
|
-
}
|
|
151
|
-
this.chunks.push(text);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
exports.ReaderState = ReaderState;
|
|
155
|
-
const regexp = /(([^:]+):)?(.*)/;
|
|
156
|
-
function resolve_namespace(name) {
|
|
157
|
-
const m = name.match(regexp);
|
|
158
|
-
if (!m) {
|
|
159
|
-
throw new Error("Invalid match");
|
|
160
|
-
}
|
|
161
|
-
return {
|
|
162
|
-
ns: m[2],
|
|
163
|
-
tag: m[3]
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* @class Xml2Json
|
|
168
|
-
* @param options - the state machine as a ReaderState node.
|
|
169
|
-
* @param [options.parser=null] {ReaderState}
|
|
170
|
-
* @param [options.init|null]
|
|
171
|
-
* @param [options.finish]
|
|
172
|
-
* @param [options.startElement]
|
|
173
|
-
* @param [options.endElement]
|
|
174
|
-
* @constructor
|
|
175
|
-
*
|
|
176
|
-
* @example
|
|
177
|
-
* var parser = new Xml2Json({
|
|
178
|
-
* parser: {
|
|
179
|
-
* 'person': {
|
|
180
|
-
* init: function(name,attrs) {
|
|
181
|
-
* this.parent.root.obj = {};
|
|
182
|
-
* this.obj = this.parent.root.obj;
|
|
183
|
-
* this.obj['name'] = attrs['name'];
|
|
184
|
-
* },
|
|
185
|
-
* parser: {
|
|
186
|
-
* 'address': {
|
|
187
|
-
* finish: function(){
|
|
188
|
-
* this.parent.obj['address'] = this.text;
|
|
189
|
-
* }
|
|
190
|
-
* }
|
|
191
|
-
* }
|
|
192
|
-
* }
|
|
193
|
-
* }
|
|
194
|
-
* });
|
|
195
|
-
*
|
|
196
|
-
* var xml_string = "<employees>" +
|
|
197
|
-
* " <person name='John'>" +
|
|
198
|
-
* " <address>Paris</address>" +
|
|
199
|
-
* " </person>" +
|
|
200
|
-
* "</employees>";
|
|
201
|
-
*
|
|
202
|
-
* parser.parseString(xml_string, function() {
|
|
203
|
-
* parser.obj.should.eql({name: 'John',address: 'Paris'});
|
|
204
|
-
* done();
|
|
205
|
-
* });
|
|
206
|
-
*/
|
|
207
|
-
class Xml2Json {
|
|
208
|
-
constructor(options) {
|
|
209
|
-
this.currentLevel = 0;
|
|
210
|
-
this.state_stack = [];
|
|
211
|
-
this.current_state = null;
|
|
212
|
-
const state = options instanceof ReaderStateBase ? options : new ReaderState(options);
|
|
213
|
-
state.root = this;
|
|
214
|
-
this.state_stack = [];
|
|
215
|
-
this.current_state = null;
|
|
216
|
-
this._promote(state, 0);
|
|
217
|
-
}
|
|
218
|
-
parseString(xml_text, callback) {
|
|
219
|
-
const parser = this._prepareParser(callback);
|
|
220
|
-
parser.write(xml_text);
|
|
221
|
-
parser.end();
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* @param new_state
|
|
225
|
-
* @param name
|
|
226
|
-
* @param attr
|
|
227
|
-
* @private
|
|
228
|
-
* @internal
|
|
229
|
-
*/
|
|
230
|
-
_promote(new_state, level, name, attr) {
|
|
231
|
-
attr = attr || {};
|
|
232
|
-
this.state_stack.push({
|
|
233
|
-
backup: {},
|
|
234
|
-
state: this.current_state
|
|
235
|
-
});
|
|
236
|
-
const parent = this.current_state;
|
|
237
|
-
this.current_state = new_state;
|
|
238
|
-
this.current_state._on_init(name || "???", attr, parent, level, this);
|
|
239
|
-
}
|
|
240
|
-
/**
|
|
241
|
-
*
|
|
242
|
-
* @private
|
|
243
|
-
* @internal
|
|
244
|
-
*/
|
|
245
|
-
_demote(cur_state, level, elementName) {
|
|
246
|
-
/// assert(this.current_state === cur_state);
|
|
247
|
-
const { state, backup } = this.state_stack.pop();
|
|
248
|
-
this.current_state = state;
|
|
249
|
-
if (this.current_state) {
|
|
250
|
-
this.current_state._on_endElement2(level, elementName);
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
_prepareParser(callback) {
|
|
254
|
-
(0, node_opcua_assert_1.assert)(callback instanceof Function);
|
|
255
|
-
const parser = new LtxParser();
|
|
256
|
-
this.currentLevel = 0;
|
|
257
|
-
parser.on("startElement", (name, attrs) => {
|
|
258
|
-
const tag_ns = resolve_namespace(name);
|
|
259
|
-
this.currentLevel += 1;
|
|
260
|
-
if (this.current_state) {
|
|
261
|
-
this.current_state._on_startElement(this.currentLevel, tag_ns.tag, attrs);
|
|
262
|
-
}
|
|
263
|
-
});
|
|
264
|
-
parser.on("endElement", (name) => {
|
|
265
|
-
const tag_ns = resolve_namespace(name);
|
|
266
|
-
if (this.current_state) {
|
|
267
|
-
this.current_state._on_endElement(this.currentLevel, tag_ns.tag);
|
|
268
|
-
}
|
|
269
|
-
this.currentLevel -= 1;
|
|
270
|
-
if (this.currentLevel === 0) {
|
|
271
|
-
parser.emit("close");
|
|
272
|
-
}
|
|
273
|
-
});
|
|
274
|
-
parser.on("text", (text) => {
|
|
275
|
-
text = text.trim();
|
|
276
|
-
if (text.length === 0) {
|
|
277
|
-
return;
|
|
278
|
-
}
|
|
279
|
-
if (this.current_state) {
|
|
280
|
-
this.current_state._on_text(text);
|
|
281
|
-
}
|
|
282
|
-
});
|
|
283
|
-
parser.once("close", () => {
|
|
284
|
-
if (callback) {
|
|
285
|
-
callback(null, this.current_state._pojo);
|
|
286
|
-
}
|
|
287
|
-
});
|
|
288
|
-
return parser;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
exports.Xml2Json = Xml2Json;
|
|
292
|
-
// tslint:disable:no-var-requires
|
|
293
|
-
const thenify = require("thenify");
|
|
294
|
-
const opts = { multiArgs: false };
|
|
295
|
-
Xml2Json.prototype.parseString = thenify.withCallback(Xml2Json.prototype.parseString, opts);
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module node-opcua-xml2json
|
|
4
|
+
* node -> see if https://github.com/isaacs/sax-js could be used instead
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.Xml2Json = exports.ReaderState = exports.ReaderStateBase = void 0;
|
|
8
|
+
// tslint:disable:max-classes-per-file
|
|
9
|
+
// tslint:disable:no-var-requires
|
|
10
|
+
// tslint:disable:unified-signatures
|
|
11
|
+
const node_opcua_assert_1 = require("node-opcua-assert");
|
|
12
|
+
const LtxParser = require("ltx/lib/parsers/ltx.js");
|
|
13
|
+
/**
|
|
14
|
+
* @static
|
|
15
|
+
* @private
|
|
16
|
+
* @method _coerceParser
|
|
17
|
+
* @param parser {map<ReaderState|options>}
|
|
18
|
+
* @return {map}
|
|
19
|
+
*/
|
|
20
|
+
function _coerceParser(parser) {
|
|
21
|
+
for (const name of Object.keys(parser)) {
|
|
22
|
+
if (parser[name] && !(parser[name] instanceof ReaderState)) {
|
|
23
|
+
// this is to prevent recursion
|
|
24
|
+
const tmp = parser[name];
|
|
25
|
+
delete parser[name];
|
|
26
|
+
parser[name] = new ReaderState(tmp);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return parser;
|
|
30
|
+
}
|
|
31
|
+
class ReaderStateBase {
|
|
32
|
+
}
|
|
33
|
+
exports.ReaderStateBase = ReaderStateBase;
|
|
34
|
+
/**
|
|
35
|
+
* @class ReaderState
|
|
36
|
+
* @private
|
|
37
|
+
* @param options
|
|
38
|
+
* @param [options.parser=null] {map<ReaderState|options}}
|
|
39
|
+
* @param [options.init|null]
|
|
40
|
+
* @param [options.finish]
|
|
41
|
+
* @param [options.startElement]
|
|
42
|
+
* @param [options.endElement]
|
|
43
|
+
*/
|
|
44
|
+
class ReaderState extends ReaderStateBase {
|
|
45
|
+
constructor(options) {
|
|
46
|
+
super();
|
|
47
|
+
this.chunks = [];
|
|
48
|
+
this.text = "";
|
|
49
|
+
this.name = "";
|
|
50
|
+
this.level = -1;
|
|
51
|
+
this.currentLevel = -1;
|
|
52
|
+
// ensure options object has only expected properties
|
|
53
|
+
options.parser = options.parser || {};
|
|
54
|
+
if (!(options instanceof ReaderStateBase)) {
|
|
55
|
+
this._init = options.init;
|
|
56
|
+
this._finish = options.finish;
|
|
57
|
+
this._startElement = options.startElement;
|
|
58
|
+
this._endElement = options.endElement;
|
|
59
|
+
}
|
|
60
|
+
this.parser = _coerceParser(options.parser);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* @method _on_init
|
|
64
|
+
* @param elementName - the name of the element
|
|
65
|
+
* @param attrs
|
|
66
|
+
* @param parent
|
|
67
|
+
* @param level
|
|
68
|
+
* @param engine
|
|
69
|
+
* @protected
|
|
70
|
+
*/
|
|
71
|
+
_on_init(elementName, attrs, parent, level, engine) {
|
|
72
|
+
this.name = elementName;
|
|
73
|
+
this.parent = parent;
|
|
74
|
+
this.engine = engine;
|
|
75
|
+
this.data = {};
|
|
76
|
+
this.level = level;
|
|
77
|
+
this.currentLevel = this.level;
|
|
78
|
+
this.attrs = attrs;
|
|
79
|
+
(0, node_opcua_assert_1.assert)(this.attrs);
|
|
80
|
+
if (this._init) {
|
|
81
|
+
this._init(elementName, attrs, parent, engine);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
_on_finish() {
|
|
85
|
+
if (this._finish) {
|
|
86
|
+
this._finish();
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* @param level
|
|
91
|
+
* @param elementName - the name of the element
|
|
92
|
+
* @param attrs
|
|
93
|
+
* @protected
|
|
94
|
+
*/
|
|
95
|
+
_on_startElement(level, elementName, attrs) {
|
|
96
|
+
this.currentLevel = level;
|
|
97
|
+
// console.log("wxxxx _on_startElement#" + this.name, elementName, this.currentLevel);
|
|
98
|
+
this.chunks = [];
|
|
99
|
+
this.text = "";
|
|
100
|
+
if (this._startElement) {
|
|
101
|
+
this._startElement(elementName, attrs);
|
|
102
|
+
}
|
|
103
|
+
if (this.engine && Object.prototype.hasOwnProperty.call(this.parser, elementName)) {
|
|
104
|
+
// console.log("promoting ", elementName, this.level);
|
|
105
|
+
this.engine._promote(this.parser[elementName], level, elementName, attrs);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
_on_endElement2(level, elementName) {
|
|
109
|
+
if (this._endElement) {
|
|
110
|
+
this._endElement(elementName);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* @method _on_endElement
|
|
115
|
+
* @protected
|
|
116
|
+
*/
|
|
117
|
+
_on_endElement(level, elementName) {
|
|
118
|
+
// console.log("wxxxx _on_endElement#" + this.name, elementName, level, this.currentLevel);
|
|
119
|
+
(0, node_opcua_assert_1.assert)(this.attrs);
|
|
120
|
+
this.chunks = this.chunks || [];
|
|
121
|
+
if (this.level > level) {
|
|
122
|
+
// we end a child element of this node
|
|
123
|
+
this._on_endElement2(level, elementName);
|
|
124
|
+
}
|
|
125
|
+
else if (this.level === level) {
|
|
126
|
+
// we received the end event of this node
|
|
127
|
+
// we need to finish
|
|
128
|
+
this.text = this.chunks.join("");
|
|
129
|
+
this.chunks = [];
|
|
130
|
+
// this is the end
|
|
131
|
+
this._on_finish();
|
|
132
|
+
if (this.parent &&
|
|
133
|
+
this.parent.parser &&
|
|
134
|
+
Object.prototype.hasOwnProperty.call(this.parent.parser, elementName)) {
|
|
135
|
+
// console.log("xxx demoting#" + this.name, elementName, this.level);
|
|
136
|
+
this.engine._demote(this, level, elementName);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* @method _on_text
|
|
142
|
+
* @param text {String} the text found inside the element
|
|
143
|
+
* @protected
|
|
144
|
+
*/
|
|
145
|
+
_on_text(text) {
|
|
146
|
+
this.chunks = this.chunks || [];
|
|
147
|
+
text = text.trim();
|
|
148
|
+
if (text.length === 0) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
this.chunks.push(text);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
exports.ReaderState = ReaderState;
|
|
155
|
+
const regexp = /(([^:]+):)?(.*)/;
|
|
156
|
+
function resolve_namespace(name) {
|
|
157
|
+
const m = name.match(regexp);
|
|
158
|
+
if (!m) {
|
|
159
|
+
throw new Error("Invalid match");
|
|
160
|
+
}
|
|
161
|
+
return {
|
|
162
|
+
ns: m[2],
|
|
163
|
+
tag: m[3]
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* @class Xml2Json
|
|
168
|
+
* @param options - the state machine as a ReaderState node.
|
|
169
|
+
* @param [options.parser=null] {ReaderState}
|
|
170
|
+
* @param [options.init|null]
|
|
171
|
+
* @param [options.finish]
|
|
172
|
+
* @param [options.startElement]
|
|
173
|
+
* @param [options.endElement]
|
|
174
|
+
* @constructor
|
|
175
|
+
*
|
|
176
|
+
* @example
|
|
177
|
+
* var parser = new Xml2Json({
|
|
178
|
+
* parser: {
|
|
179
|
+
* 'person': {
|
|
180
|
+
* init: function(name,attrs) {
|
|
181
|
+
* this.parent.root.obj = {};
|
|
182
|
+
* this.obj = this.parent.root.obj;
|
|
183
|
+
* this.obj['name'] = attrs['name'];
|
|
184
|
+
* },
|
|
185
|
+
* parser: {
|
|
186
|
+
* 'address': {
|
|
187
|
+
* finish: function(){
|
|
188
|
+
* this.parent.obj['address'] = this.text;
|
|
189
|
+
* }
|
|
190
|
+
* }
|
|
191
|
+
* }
|
|
192
|
+
* }
|
|
193
|
+
* }
|
|
194
|
+
* });
|
|
195
|
+
*
|
|
196
|
+
* var xml_string = "<employees>" +
|
|
197
|
+
* " <person name='John'>" +
|
|
198
|
+
* " <address>Paris</address>" +
|
|
199
|
+
* " </person>" +
|
|
200
|
+
* "</employees>";
|
|
201
|
+
*
|
|
202
|
+
* parser.parseString(xml_string, function() {
|
|
203
|
+
* parser.obj.should.eql({name: 'John',address: 'Paris'});
|
|
204
|
+
* done();
|
|
205
|
+
* });
|
|
206
|
+
*/
|
|
207
|
+
class Xml2Json {
|
|
208
|
+
constructor(options) {
|
|
209
|
+
this.currentLevel = 0;
|
|
210
|
+
this.state_stack = [];
|
|
211
|
+
this.current_state = null;
|
|
212
|
+
const state = options instanceof ReaderStateBase ? options : new ReaderState(options);
|
|
213
|
+
state.root = this;
|
|
214
|
+
this.state_stack = [];
|
|
215
|
+
this.current_state = null;
|
|
216
|
+
this._promote(state, 0);
|
|
217
|
+
}
|
|
218
|
+
parseString(xml_text, callback) {
|
|
219
|
+
const parser = this._prepareParser(callback);
|
|
220
|
+
parser.write(xml_text);
|
|
221
|
+
parser.end();
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* @param new_state
|
|
225
|
+
* @param name
|
|
226
|
+
* @param attr
|
|
227
|
+
* @private
|
|
228
|
+
* @internal
|
|
229
|
+
*/
|
|
230
|
+
_promote(new_state, level, name, attr) {
|
|
231
|
+
attr = attr || {};
|
|
232
|
+
this.state_stack.push({
|
|
233
|
+
backup: {},
|
|
234
|
+
state: this.current_state
|
|
235
|
+
});
|
|
236
|
+
const parent = this.current_state;
|
|
237
|
+
this.current_state = new_state;
|
|
238
|
+
this.current_state._on_init(name || "???", attr, parent, level, this);
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
*
|
|
242
|
+
* @private
|
|
243
|
+
* @internal
|
|
244
|
+
*/
|
|
245
|
+
_demote(cur_state, level, elementName) {
|
|
246
|
+
/// assert(this.current_state === cur_state);
|
|
247
|
+
const { state, backup } = this.state_stack.pop();
|
|
248
|
+
this.current_state = state;
|
|
249
|
+
if (this.current_state) {
|
|
250
|
+
this.current_state._on_endElement2(level, elementName);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
_prepareParser(callback) {
|
|
254
|
+
(0, node_opcua_assert_1.assert)(callback instanceof Function);
|
|
255
|
+
const parser = new LtxParser();
|
|
256
|
+
this.currentLevel = 0;
|
|
257
|
+
parser.on("startElement", (name, attrs) => {
|
|
258
|
+
const tag_ns = resolve_namespace(name);
|
|
259
|
+
this.currentLevel += 1;
|
|
260
|
+
if (this.current_state) {
|
|
261
|
+
this.current_state._on_startElement(this.currentLevel, tag_ns.tag, attrs);
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
parser.on("endElement", (name) => {
|
|
265
|
+
const tag_ns = resolve_namespace(name);
|
|
266
|
+
if (this.current_state) {
|
|
267
|
+
this.current_state._on_endElement(this.currentLevel, tag_ns.tag);
|
|
268
|
+
}
|
|
269
|
+
this.currentLevel -= 1;
|
|
270
|
+
if (this.currentLevel === 0) {
|
|
271
|
+
parser.emit("close");
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
parser.on("text", (text) => {
|
|
275
|
+
text = text.trim();
|
|
276
|
+
if (text.length === 0) {
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
if (this.current_state) {
|
|
280
|
+
this.current_state._on_text(text);
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
parser.once("close", () => {
|
|
284
|
+
if (callback) {
|
|
285
|
+
callback(null, this.current_state._pojo);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
return parser;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
exports.Xml2Json = Xml2Json;
|
|
292
|
+
// tslint:disable:no-var-requires
|
|
293
|
+
const thenify = require("thenify");
|
|
294
|
+
const opts = { multiArgs: false };
|
|
295
|
+
Xml2Json.prototype.parseString = thenify.withCallback(Xml2Json.prototype.parseString, opts);
|
|
296
296
|
//# sourceMappingURL=xml2json.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ReaderState, ReaderStateParser, Xml2Json, XmlAttributes } from "./xml2json";
|
|
2
|
-
import { withPojoLambda } from "./xml2Json_pojo_tools";
|
|
3
|
-
export declare const json_parser: ReaderStateParser;
|
|
4
|
-
export declare function startPojo(pThis: ReaderState, elementName: string, attrs: XmlAttributes, withPojo: withPojoLambda): void;
|
|
5
|
-
export declare class Xml2JsonPojo extends Xml2Json {
|
|
6
|
-
constructor();
|
|
7
|
-
}
|
|
1
|
+
import { ReaderState, ReaderStateParser, Xml2Json, XmlAttributes } from "./xml2json";
|
|
2
|
+
import { withPojoLambda } from "./xml2Json_pojo_tools";
|
|
3
|
+
export declare const json_parser: ReaderStateParser;
|
|
4
|
+
export declare function startPojo(pThis: ReaderState, elementName: string, attrs: XmlAttributes, withPojo: withPojoLambda): void;
|
|
5
|
+
export declare class Xml2JsonPojo extends Xml2Json {
|
|
6
|
+
constructor();
|
|
7
|
+
}
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Xml2JsonPojo = exports.startPojo = exports.json_parser = void 0;
|
|
4
|
-
const xml2json_1 = require("./xml2json");
|
|
5
|
-
const xml2Json_pojo_tools_1 = require("./xml2Json_pojo_tools");
|
|
6
|
-
const json_extractor = new xml2Json_pojo_tools_1.ReaderState2();
|
|
7
|
-
exports.json_parser = {
|
|
8
|
-
init(elementName, attrs, parent, engine) {
|
|
9
|
-
json_extractor._on_init(elementName, attrs, parent, 0, engine);
|
|
10
|
-
},
|
|
11
|
-
finish() {
|
|
12
|
-
this.parent._pojo = json_extractor._pojo;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
function startPojo(pThis, elementName, attrs, withPojo) {
|
|
16
|
-
pThis.engine._promote(json_extractor, pThis.engine.currentLevel, elementName, attrs);
|
|
17
|
-
json_extractor._withPojo = (name, pojo) => {
|
|
18
|
-
withPojo(name, pojo);
|
|
19
|
-
pThis.engine._demote(json_extractor, pThis.engine.currentLevel, elementName);
|
|
20
|
-
};
|
|
21
|
-
}
|
|
22
|
-
exports.startPojo = startPojo;
|
|
23
|
-
class Xml2JsonPojo extends xml2json_1.Xml2Json {
|
|
24
|
-
constructor() {
|
|
25
|
-
super(json_extractor);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
exports.Xml2JsonPojo = Xml2JsonPojo;
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Xml2JsonPojo = exports.startPojo = exports.json_parser = void 0;
|
|
4
|
+
const xml2json_1 = require("./xml2json");
|
|
5
|
+
const xml2Json_pojo_tools_1 = require("./xml2Json_pojo_tools");
|
|
6
|
+
const json_extractor = new xml2Json_pojo_tools_1.ReaderState2();
|
|
7
|
+
exports.json_parser = {
|
|
8
|
+
init(elementName, attrs, parent, engine) {
|
|
9
|
+
json_extractor._on_init(elementName, attrs, parent, 0, engine);
|
|
10
|
+
},
|
|
11
|
+
finish() {
|
|
12
|
+
this.parent._pojo = json_extractor._pojo;
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
function startPojo(pThis, elementName, attrs, withPojo) {
|
|
16
|
+
pThis.engine._promote(json_extractor, pThis.engine.currentLevel, elementName, attrs);
|
|
17
|
+
json_extractor._withPojo = (name, pojo) => {
|
|
18
|
+
withPojo(name, pojo);
|
|
19
|
+
pThis.engine._demote(json_extractor, pThis.engine.currentLevel, elementName);
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
exports.startPojo = startPojo;
|
|
23
|
+
class Xml2JsonPojo extends xml2json_1.Xml2Json {
|
|
24
|
+
constructor() {
|
|
25
|
+
super(json_extractor);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
exports.Xml2JsonPojo = Xml2JsonPojo;
|
|
29
29
|
//# sourceMappingURL=xml2json_pojo.js.map
|