node-opcua-xml2json 2.70.0 → 2.71.0
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 +24 -24
- package/dist/source/extension_object_parser.js +249 -249
- 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 +22 -22
- package/dist/source/nodejs/xml2json_fs.d.ts +10 -10
- package/dist/source/nodejs/xml2json_fs.js +38 -38
- 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 +179 -179
- package/dist/source/xml2json.js +302 -302
- package/dist/source/xml2json_pojo.d.ts +7 -7
- package/dist/source/xml2json_pojo.js +28 -28
- package/package.json +5 -5
package/dist/source/xml2json.js
CHANGED
|
@@ -1,303 +1,303 @@
|
|
|
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
|
-
parseStringSync(xml_text) {
|
|
219
|
-
let retValue = {};
|
|
220
|
-
const parser = this._prepareParser((err, r) => retValue = r);
|
|
221
|
-
parser.write(xml_text);
|
|
222
|
-
parser.end();
|
|
223
|
-
return retValue;
|
|
224
|
-
}
|
|
225
|
-
parseString(xml_text, callback) {
|
|
226
|
-
const parser = this._prepareParser(callback);
|
|
227
|
-
parser.write(xml_text);
|
|
228
|
-
parser.end();
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* @param new_state
|
|
232
|
-
* @param name
|
|
233
|
-
* @param attr
|
|
234
|
-
* @private
|
|
235
|
-
* @internal
|
|
236
|
-
*/
|
|
237
|
-
_promote(new_state, level, name, attr) {
|
|
238
|
-
attr = attr || {};
|
|
239
|
-
this.state_stack.push({
|
|
240
|
-
backup: {},
|
|
241
|
-
state: this.current_state
|
|
242
|
-
});
|
|
243
|
-
const parent = this.current_state;
|
|
244
|
-
this.current_state = new_state;
|
|
245
|
-
this.current_state._on_init(name || "???", attr, parent, level, this);
|
|
246
|
-
}
|
|
247
|
-
/**
|
|
248
|
-
*
|
|
249
|
-
* @private
|
|
250
|
-
* @internal
|
|
251
|
-
*/
|
|
252
|
-
_demote(cur_state, level, elementName) {
|
|
253
|
-
/// assert(this.current_state === cur_state);
|
|
254
|
-
const { state, backup } = this.state_stack.pop();
|
|
255
|
-
this.current_state = state;
|
|
256
|
-
if (this.current_state) {
|
|
257
|
-
this.current_state._on_endElement2(level, elementName);
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
_prepareParser(callback) {
|
|
261
|
-
(0, node_opcua_assert_1.assert)(typeof callback === 'function');
|
|
262
|
-
const parser = new LtxParser();
|
|
263
|
-
this.currentLevel = 0;
|
|
264
|
-
parser.on("startElement", (name, attrs) => {
|
|
265
|
-
const tag_ns = resolve_namespace(name);
|
|
266
|
-
this.currentLevel += 1;
|
|
267
|
-
if (this.current_state) {
|
|
268
|
-
this.current_state._on_startElement(this.currentLevel, tag_ns.tag, attrs);
|
|
269
|
-
}
|
|
270
|
-
});
|
|
271
|
-
parser.on("endElement", (name) => {
|
|
272
|
-
const tag_ns = resolve_namespace(name);
|
|
273
|
-
if (this.current_state) {
|
|
274
|
-
this.current_state._on_endElement(this.currentLevel, tag_ns.tag);
|
|
275
|
-
}
|
|
276
|
-
this.currentLevel -= 1;
|
|
277
|
-
if (this.currentLevel === 0) {
|
|
278
|
-
parser.emit("close");
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
parser.on("text", (text) => {
|
|
282
|
-
text = text.trim();
|
|
283
|
-
if (text.length === 0) {
|
|
284
|
-
return;
|
|
285
|
-
}
|
|
286
|
-
if (this.current_state) {
|
|
287
|
-
this.current_state._on_text(text);
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
parser.once("close", () => {
|
|
291
|
-
if (callback) {
|
|
292
|
-
callback(null, this.current_state._pojo);
|
|
293
|
-
}
|
|
294
|
-
});
|
|
295
|
-
return parser;
|
|
296
|
-
}
|
|
297
|
-
}
|
|
298
|
-
exports.Xml2Json = Xml2Json;
|
|
299
|
-
// tslint:disable:no-var-requires
|
|
300
|
-
const thenify = require("thenify");
|
|
301
|
-
const opts = { multiArgs: false };
|
|
302
|
-
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
|
+
parseStringSync(xml_text) {
|
|
219
|
+
let retValue = {};
|
|
220
|
+
const parser = this._prepareParser((err, r) => retValue = r);
|
|
221
|
+
parser.write(xml_text);
|
|
222
|
+
parser.end();
|
|
223
|
+
return retValue;
|
|
224
|
+
}
|
|
225
|
+
parseString(xml_text, callback) {
|
|
226
|
+
const parser = this._prepareParser(callback);
|
|
227
|
+
parser.write(xml_text);
|
|
228
|
+
parser.end();
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* @param new_state
|
|
232
|
+
* @param name
|
|
233
|
+
* @param attr
|
|
234
|
+
* @private
|
|
235
|
+
* @internal
|
|
236
|
+
*/
|
|
237
|
+
_promote(new_state, level, name, attr) {
|
|
238
|
+
attr = attr || {};
|
|
239
|
+
this.state_stack.push({
|
|
240
|
+
backup: {},
|
|
241
|
+
state: this.current_state
|
|
242
|
+
});
|
|
243
|
+
const parent = this.current_state;
|
|
244
|
+
this.current_state = new_state;
|
|
245
|
+
this.current_state._on_init(name || "???", attr, parent, level, this);
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
*
|
|
249
|
+
* @private
|
|
250
|
+
* @internal
|
|
251
|
+
*/
|
|
252
|
+
_demote(cur_state, level, elementName) {
|
|
253
|
+
/// assert(this.current_state === cur_state);
|
|
254
|
+
const { state, backup } = this.state_stack.pop();
|
|
255
|
+
this.current_state = state;
|
|
256
|
+
if (this.current_state) {
|
|
257
|
+
this.current_state._on_endElement2(level, elementName);
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
_prepareParser(callback) {
|
|
261
|
+
(0, node_opcua_assert_1.assert)(typeof callback === 'function');
|
|
262
|
+
const parser = new LtxParser();
|
|
263
|
+
this.currentLevel = 0;
|
|
264
|
+
parser.on("startElement", (name, attrs) => {
|
|
265
|
+
const tag_ns = resolve_namespace(name);
|
|
266
|
+
this.currentLevel += 1;
|
|
267
|
+
if (this.current_state) {
|
|
268
|
+
this.current_state._on_startElement(this.currentLevel, tag_ns.tag, attrs);
|
|
269
|
+
}
|
|
270
|
+
});
|
|
271
|
+
parser.on("endElement", (name) => {
|
|
272
|
+
const tag_ns = resolve_namespace(name);
|
|
273
|
+
if (this.current_state) {
|
|
274
|
+
this.current_state._on_endElement(this.currentLevel, tag_ns.tag);
|
|
275
|
+
}
|
|
276
|
+
this.currentLevel -= 1;
|
|
277
|
+
if (this.currentLevel === 0) {
|
|
278
|
+
parser.emit("close");
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
parser.on("text", (text) => {
|
|
282
|
+
text = text.trim();
|
|
283
|
+
if (text.length === 0) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
if (this.current_state) {
|
|
287
|
+
this.current_state._on_text(text);
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
parser.once("close", () => {
|
|
291
|
+
if (callback) {
|
|
292
|
+
callback(null, this.current_state._pojo);
|
|
293
|
+
}
|
|
294
|
+
});
|
|
295
|
+
return parser;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
exports.Xml2Json = Xml2Json;
|
|
299
|
+
// tslint:disable:no-var-requires
|
|
300
|
+
const thenify = require("thenify");
|
|
301
|
+
const opts = { multiArgs: false };
|
|
302
|
+
Xml2Json.prototype.parseString = thenify.withCallback(Xml2Json.prototype.parseString, opts);
|
|
303
303
|
//# 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
|
+
}
|