node-opcua-xml2json 2.54.0 → 2.60.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/.mocharc.yml +13 -13
- package/LICENSE +20 -20
- package/dist/source/definition_parser.js +9 -9
- package/dist/source/extension_object_parser.d.ts +2 -2
- package/dist/source/extension_object_parser.js +11 -5
- package/dist/source/extension_object_parser.js.map +1 -1
- package/dist/source/fragment_cloner.js +6 -2
- package/dist/source/fragment_cloner.js.map +1 -1
- package/dist/source/fragment_cloner_parser.d.ts +2 -2
- package/dist/source/fragment_cloner_parser.js +0 -1
- package/dist/source/fragment_cloner_parser.js.map +1 -1
- package/dist/source/xml2Json_pojo_tools.js +1 -1
- package/dist/source/xml2Json_pojo_tools.js.map +1 -1
- package/dist/source/xml2json_pojo.js.map +1 -1
- package/package.json +9 -7
- package/pnpm-lock.yaml +4 -4
- package/source/definition_parser.ts +84 -84
- package/source/extension_object_parser.ts +290 -276
- package/source/fragment_cloner.ts +58 -51
- package/source/fragment_cloner_parser.ts +20 -17
- package/source/index.ts +6 -6
- package/source/nodejs/xml2json_fs.ts +43 -43
- package/source/xml2Json_pojo_tools.ts +98 -98
- package/source/xml2json_pojo.ts +26 -27
|
@@ -1,276 +1,290 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
function BasicType_parser(dataType: string, parseFunc: (this: any, text: string) => any): ParserLike {
|
|
5
|
-
const r: ReaderStateParserLike = {
|
|
6
|
-
init(this: any, elementName: string, attrs: XmlAttributes) {
|
|
7
|
-
this.value = undefined;
|
|
8
|
-
},
|
|
9
|
-
finish(this: any) {
|
|
10
|
-
this.value = parseFunc.call(this, this.text);
|
|
11
|
-
}
|
|
12
|
-
};
|
|
13
|
-
const _parser: ParserLike = {};
|
|
14
|
-
_parser[dataType] = r;
|
|
15
|
-
return _parser;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function ListOf(dataType: string, parseFunc: any) {
|
|
19
|
-
return {
|
|
20
|
-
init(this: any) {
|
|
21
|
-
this.value = [];
|
|
22
|
-
},
|
|
23
|
-
|
|
24
|
-
parser: BasicType_parser(dataType, parseFunc),
|
|
25
|
-
|
|
26
|
-
finish(this: any) {
|
|
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
|
-
|
|
1
|
+
import { lowerFirstLetter } from "node-opcua-utils";
|
|
2
|
+
import { ReaderState, ReaderStateParserLike, ParserLike, XmlAttributes } from "./xml2json";
|
|
3
|
+
|
|
4
|
+
function BasicType_parser(dataType: string, parseFunc: (this: any, text: string) => any): ParserLike {
|
|
5
|
+
const r: ReaderStateParserLike = {
|
|
6
|
+
init(this: any, elementName: string, attrs: XmlAttributes) {
|
|
7
|
+
this.value = undefined;
|
|
8
|
+
},
|
|
9
|
+
finish(this: any) {
|
|
10
|
+
this.value = parseFunc.call(this, this.text);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const _parser: ParserLike = {};
|
|
14
|
+
_parser[dataType] = r;
|
|
15
|
+
return _parser;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function ListOf(dataType: string, parseFunc: any) {
|
|
19
|
+
return {
|
|
20
|
+
init(this: any) {
|
|
21
|
+
this.value = [];
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
parser: BasicType_parser(dataType, parseFunc),
|
|
25
|
+
|
|
26
|
+
finish(this: any) {
|
|
27
|
+
/** empty */
|
|
28
|
+
},
|
|
29
|
+
endElement(this: any, elementName: string) {
|
|
30
|
+
this.value.push(this.parser[elementName].value);
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const localizedTextReader: ReaderStateParserLike = {
|
|
36
|
+
init(this: any) {
|
|
37
|
+
this.localizedText = {};
|
|
38
|
+
},
|
|
39
|
+
parser: {
|
|
40
|
+
Locale: {
|
|
41
|
+
finish(this: any) {
|
|
42
|
+
this.parent.localizedText = this.parent.localizedText || {};
|
|
43
|
+
this.parent.localizedText.locale = this.text.trim();
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
Text: {
|
|
47
|
+
finish(this: any) {
|
|
48
|
+
this.parent.localizedText = this.parent.localizedText || {};
|
|
49
|
+
this.parent.localizedText.text = this.text.trim();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
finish(this: any) {
|
|
54
|
+
this.value = this.localizedText;
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const partials: { [key: string]: ReaderStateParserLike } = {
|
|
59
|
+
LocalizedText: localizedTextReader,
|
|
60
|
+
String: {
|
|
61
|
+
finish(this: any) {
|
|
62
|
+
this.value = this.text;
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
Boolean: {
|
|
67
|
+
finish(this: any) {
|
|
68
|
+
this.value = this.text.toLowerCase() === "true";
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
ByteString: {
|
|
73
|
+
init(this: any) {
|
|
74
|
+
this.value = null;
|
|
75
|
+
},
|
|
76
|
+
finish(this: any) {
|
|
77
|
+
const base64text = this.text;
|
|
78
|
+
const byteString = Buffer.from(base64text, "base64");
|
|
79
|
+
this.value = byteString;
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
|
|
83
|
+
Float: {
|
|
84
|
+
finish(this: any) {
|
|
85
|
+
this.value = parseFloat(this.text);
|
|
86
|
+
}
|
|
87
|
+
},
|
|
88
|
+
|
|
89
|
+
Double: {
|
|
90
|
+
finish(this: any) {
|
|
91
|
+
this.value = parseFloat(this.text);
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
Int8: {
|
|
96
|
+
finish(this: any) {
|
|
97
|
+
this.value = parseInt(this.text, 10);
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
|
|
101
|
+
Int16: {
|
|
102
|
+
finish(this: any) {
|
|
103
|
+
this.value = parseInt(this.text, 10);
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
Int32: {
|
|
107
|
+
finish(this: any) {
|
|
108
|
+
this.value = parseInt(this.text, 10);
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
Int64: {
|
|
112
|
+
finish(this: any) {
|
|
113
|
+
this.value = parseInt(this.text, 10);
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
UInt8: {
|
|
118
|
+
finish(this: any) {
|
|
119
|
+
this.value = parseInt(this.text, 10);
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
|
|
123
|
+
UInt16: {
|
|
124
|
+
finish(this: any) {
|
|
125
|
+
this.value = parseInt(this.text, 10);
|
|
126
|
+
}
|
|
127
|
+
},
|
|
128
|
+
UInt32: {
|
|
129
|
+
finish(this: any) {
|
|
130
|
+
this.value = parseInt(this.text, 10);
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
UInt64: {
|
|
134
|
+
finish(this: any) {
|
|
135
|
+
this.value = parseInt(this.text, 10);
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
|
|
139
|
+
ListOfLocalizedText: {
|
|
140
|
+
init(this: any) {
|
|
141
|
+
this.value = [];
|
|
142
|
+
},
|
|
143
|
+
parser: { LocalizedText: localizedTextReader },
|
|
144
|
+
finish(this: any) {
|
|
145
|
+
/** empty */
|
|
146
|
+
},
|
|
147
|
+
endElement(this: any /*element*/) {
|
|
148
|
+
this.value.push(this.parser.LocalizedText.value);
|
|
149
|
+
}
|
|
150
|
+
},
|
|
151
|
+
|
|
152
|
+
ListOfDouble: ListOf("Double", parseFloat),
|
|
153
|
+
|
|
154
|
+
ListOfFloat: ListOf("Float", parseFloat),
|
|
155
|
+
|
|
156
|
+
ListOfInt32: ListOf("Int32", parseInt),
|
|
157
|
+
|
|
158
|
+
ListOfInt16: ListOf("Int16", parseInt),
|
|
159
|
+
|
|
160
|
+
ListOfInt8: ListOf("Int8", parseInt),
|
|
161
|
+
|
|
162
|
+
ListOfUint32: ListOf("Uint32", parseInt),
|
|
163
|
+
|
|
164
|
+
ListOfUint16: ListOf("Uint16", parseInt),
|
|
165
|
+
|
|
166
|
+
ListOfUint8: ListOf("Uint8", parseInt)
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
interface Field {
|
|
170
|
+
dataType: any;
|
|
171
|
+
description?: string;
|
|
172
|
+
name: string;
|
|
173
|
+
value?: any;
|
|
174
|
+
valueRank?: number; // default is -1 => scalar
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export interface Definition {
|
|
178
|
+
name: string;
|
|
179
|
+
fields: Field[];
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export interface DefinitionMap {
|
|
183
|
+
findDefinition(name: string): Definition;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function _clone(a: any): any {
|
|
187
|
+
if (typeof a === "string" || typeof a === "number" || typeof a === "boolean") {
|
|
188
|
+
return a;
|
|
189
|
+
}
|
|
190
|
+
if (a instanceof Array) {
|
|
191
|
+
return a.map((x) => _clone(x));
|
|
192
|
+
}
|
|
193
|
+
return { ...a };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
function _makeExtensionObjectReader(
|
|
197
|
+
definitionName: string,
|
|
198
|
+
definitionMap: DefinitionMap,
|
|
199
|
+
readerMap: Record<string, ReaderStateParserLike>
|
|
200
|
+
): ReaderStateParserLike {
|
|
201
|
+
// is it a basic type ?
|
|
202
|
+
if (Object.prototype.hasOwnProperty.call(partials, definitionName)) {
|
|
203
|
+
return partials[definitionName];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
let reader: ReaderStateParserLike = readerMap[definitionName]!;
|
|
207
|
+
|
|
208
|
+
if (reader) {
|
|
209
|
+
return reader;
|
|
210
|
+
}
|
|
211
|
+
const definition = definitionMap.findDefinition(definitionName);
|
|
212
|
+
if (!definition) {
|
|
213
|
+
throw new Error("cannot find definition for " + definitionName);
|
|
214
|
+
}
|
|
215
|
+
reader = {
|
|
216
|
+
finish(this: any) {
|
|
217
|
+
/** empty */
|
|
218
|
+
},
|
|
219
|
+
parser: {}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
for (const field of definition.fields) {
|
|
223
|
+
const fieldReader = _makeExtensionObjectReader(field.dataType, definitionMap, readerMap);
|
|
224
|
+
if (!fieldReader) {
|
|
225
|
+
throw new Error(" Cannot find reader for dataType " + field.dataType);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
if (field.valueRank === undefined || field.valueRank === -1) {
|
|
229
|
+
const parser = fieldReader;
|
|
230
|
+
if (!parser) {
|
|
231
|
+
throw new Error("??? " + field.dataType + " " + field.name);
|
|
232
|
+
}
|
|
233
|
+
reader.parser![field.name] = {
|
|
234
|
+
parser: fieldReader.parser,
|
|
235
|
+
// endElement: fieldReader.endElement,
|
|
236
|
+
finish(this: any) {
|
|
237
|
+
const elName = lowerFirstLetter(field.name);
|
|
238
|
+
fieldReader.finish!.call(this);
|
|
239
|
+
this.parent.value = this.parent.value || {};
|
|
240
|
+
this.parent.value[elName] = _clone(this.value);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
} else if (field.valueRank === 1) {
|
|
244
|
+
const listReader: ReaderStateParserLike = {
|
|
245
|
+
init(this: any) {
|
|
246
|
+
this.value = [];
|
|
247
|
+
},
|
|
248
|
+
parser: {},
|
|
249
|
+
finish(this: any) {
|
|
250
|
+
const elName = lowerFirstLetter(this.name);
|
|
251
|
+
this.parent.value = this.parent.value || {};
|
|
252
|
+
this.parent.value[elName] = this.value;
|
|
253
|
+
this.value = undefined;
|
|
254
|
+
},
|
|
255
|
+
startElement(name: string, attrs: XmlAttributes) {
|
|
256
|
+
// empty
|
|
257
|
+
},
|
|
258
|
+
endElement(element: string) {
|
|
259
|
+
this.value.push(_clone(this.parser[element].value));
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
listReader.parser![field.dataType] = fieldReader;
|
|
263
|
+
reader.parser![field.name] = listReader;
|
|
264
|
+
} else {
|
|
265
|
+
throw new Error("Unsupported ValueRank !");
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
// xx const parser: ParserLike = {};
|
|
269
|
+
// xx parser[definition.name] = reader;
|
|
270
|
+
readerMap[definitionName] = reader;
|
|
271
|
+
return reader;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
export function makeExtensionObjectReader(
|
|
275
|
+
definitionName: string,
|
|
276
|
+
definitionMap: DefinitionMap,
|
|
277
|
+
readerMap: Record<string, ReaderStateParserLike>
|
|
278
|
+
): ReaderState {
|
|
279
|
+
const reader1: ReaderStateParserLike = {
|
|
280
|
+
parser: {},
|
|
281
|
+
endElement(this: any) {
|
|
282
|
+
// console.log(this.parser[definitionName].value);
|
|
283
|
+
this._pojo = this.parser[definitionName].value;
|
|
284
|
+
}
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
reader1.parser![definitionName] = _makeExtensionObjectReader(definitionName, definitionMap, readerMap);
|
|
288
|
+
|
|
289
|
+
return new ReaderState(reader1);
|
|
290
|
+
}
|