node-red-contrib-tak-registration 0.7.2 → 0.8.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/README.md +1 -1
- package/node_modules/fast-xml-parser/CHANGELOG.md +561 -0
- package/node_modules/fast-xml-parser/README.md +134 -287
- package/node_modules/fast-xml-parser/package.json +17 -40
- package/node_modules/fast-xml-parser/{cli.js → src/cli/cli.js} +16 -24
- package/node_modules/fast-xml-parser/src/cli/man.js +12 -0
- package/node_modules/fast-xml-parser/src/fxp.d.ts +108 -0
- package/node_modules/fast-xml-parser/src/fxp.js +11 -0
- package/node_modules/fast-xml-parser/src/util.js +0 -36
- package/node_modules/fast-xml-parser/src/validator.js +15 -5
- package/node_modules/fast-xml-parser/src/xmlbuilder/json2xml.js +269 -0
- package/node_modules/fast-xml-parser/src/xmlbuilder/orderedJs2Xml.js +131 -0
- package/node_modules/fast-xml-parser/src/xmlbuilder/prettifyJs2Xml.js +0 -0
- package/node_modules/fast-xml-parser/src/xmlparser/DocTypeReader.js +152 -0
- package/node_modules/fast-xml-parser/src/xmlparser/OptionsBuilder.js +48 -0
- package/node_modules/fast-xml-parser/src/xmlparser/OrderedObjParser.js +589 -0
- package/node_modules/fast-xml-parser/src/xmlparser/XMLParser.js +58 -0
- package/node_modules/fast-xml-parser/src/xmlparser/node2json.js +113 -0
- package/node_modules/fast-xml-parser/src/xmlparser/xmlNode.js +25 -0
- package/package.json +2 -2
- package/tak-ingest.js +3 -2
- package/node_modules/fast-xml-parser/src/json2xml.js +0 -280
- package/node_modules/fast-xml-parser/src/nimndata.js +0 -144
- package/node_modules/fast-xml-parser/src/node2json.js +0 -42
- package/node_modules/fast-xml-parser/src/node2json_str.js +0 -63
- package/node_modules/fast-xml-parser/src/parser.d.ts +0 -79
- package/node_modules/fast-xml-parser/src/parser.js +0 -76
- package/node_modules/fast-xml-parser/src/xmlNode.js +0 -17
- package/node_modules/fast-xml-parser/src/xmlstr2xmlnode.js +0 -339
- /package/node_modules/fast-xml-parser/src/{read.js → cli/read.js} +0 -0
|
@@ -0,0 +1,589 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
///@ts-check
|
|
3
|
+
|
|
4
|
+
const util = require('../util');
|
|
5
|
+
const xmlNode = require('./xmlNode');
|
|
6
|
+
const readDocType = require("./DocTypeReader");
|
|
7
|
+
const toNumber = require("strnum");
|
|
8
|
+
|
|
9
|
+
const regx =
|
|
10
|
+
'<((!\\[CDATA\\[([\\s\\S]*?)(]]>))|((NAME:)?(NAME))([^>]*)>|((\\/)(NAME)\\s*>))([^<]*)'
|
|
11
|
+
.replace(/NAME/g, util.nameRegexp);
|
|
12
|
+
|
|
13
|
+
//const tagsRegx = new RegExp("<(\\/?[\\w:\\-\._]+)([^>]*)>(\\s*"+cdataRegx+")*([^<]+)?","g");
|
|
14
|
+
//const tagsRegx = new RegExp("<(\\/?)((\\w*:)?([\\w:\\-\._]+))([^>]*)>([^<]*)("+cdataRegx+"([^<]*))*([^<]+)?","g");
|
|
15
|
+
|
|
16
|
+
class OrderedObjParser{
|
|
17
|
+
constructor(options){
|
|
18
|
+
this.options = options;
|
|
19
|
+
this.currentNode = null;
|
|
20
|
+
this.tagsNodeStack = [];
|
|
21
|
+
this.docTypeEntities = {};
|
|
22
|
+
this.lastEntities = {
|
|
23
|
+
"apos" : { regex: /&(apos|#39|#x27);/g, val : "'"},
|
|
24
|
+
"gt" : { regex: /&(gt|#62|#x3E);/g, val : ">"},
|
|
25
|
+
"lt" : { regex: /&(lt|#60|#x3C);/g, val : "<"},
|
|
26
|
+
"quot" : { regex: /&(quot|#34|#x22);/g, val : "\""},
|
|
27
|
+
};
|
|
28
|
+
this.ampEntity = { regex: /&(amp|#38|#x26);/g, val : "&"};
|
|
29
|
+
this.htmlEntities = {
|
|
30
|
+
"space": { regex: /&(nbsp|#160);/g, val: " " },
|
|
31
|
+
// "lt" : { regex: /&(lt|#60);/g, val: "<" },
|
|
32
|
+
// "gt" : { regex: /&(gt|#62);/g, val: ">" },
|
|
33
|
+
// "amp" : { regex: /&(amp|#38);/g, val: "&" },
|
|
34
|
+
// "quot" : { regex: /&(quot|#34);/g, val: "\"" },
|
|
35
|
+
// "apos" : { regex: /&(apos|#39);/g, val: "'" },
|
|
36
|
+
"cent" : { regex: /&(cent|#162);/g, val: "¢" },
|
|
37
|
+
"pound" : { regex: /&(pound|#163);/g, val: "£" },
|
|
38
|
+
"yen" : { regex: /&(yen|#165);/g, val: "¥" },
|
|
39
|
+
"euro" : { regex: /&(euro|#8364);/g, val: "€" },
|
|
40
|
+
"copyright" : { regex: /&(copy|#169);/g, val: "©" },
|
|
41
|
+
"reg" : { regex: /&(reg|#174);/g, val: "®" },
|
|
42
|
+
"inr" : { regex: /&(inr|#8377);/g, val: "₹" },
|
|
43
|
+
};
|
|
44
|
+
this.addExternalEntities = addExternalEntities;
|
|
45
|
+
this.parseXml = parseXml;
|
|
46
|
+
this.parseTextData = parseTextData;
|
|
47
|
+
this.resolveNameSpace = resolveNameSpace;
|
|
48
|
+
this.buildAttributesMap = buildAttributesMap;
|
|
49
|
+
this.isItStopNode = isItStopNode;
|
|
50
|
+
this.replaceEntitiesValue = replaceEntitiesValue;
|
|
51
|
+
this.readStopNodeData = readStopNodeData;
|
|
52
|
+
this.saveTextToParentTag = saveTextToParentTag;
|
|
53
|
+
this.addChild = addChild;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function addExternalEntities(externalEntities){
|
|
59
|
+
const entKeys = Object.keys(externalEntities);
|
|
60
|
+
for (let i = 0; i < entKeys.length; i++) {
|
|
61
|
+
const ent = entKeys[i];
|
|
62
|
+
this.lastEntities[ent] = {
|
|
63
|
+
regex: new RegExp("&"+ent+";","g"),
|
|
64
|
+
val : externalEntities[ent]
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {string} val
|
|
71
|
+
* @param {string} tagName
|
|
72
|
+
* @param {string} jPath
|
|
73
|
+
* @param {boolean} dontTrim
|
|
74
|
+
* @param {boolean} hasAttributes
|
|
75
|
+
* @param {boolean} isLeafNode
|
|
76
|
+
* @param {boolean} escapeEntities
|
|
77
|
+
*/
|
|
78
|
+
function parseTextData(val, tagName, jPath, dontTrim, hasAttributes, isLeafNode, escapeEntities) {
|
|
79
|
+
if (val !== undefined) {
|
|
80
|
+
if (this.options.trimValues && !dontTrim) {
|
|
81
|
+
val = val.trim();
|
|
82
|
+
}
|
|
83
|
+
if(val.length > 0){
|
|
84
|
+
if(!escapeEntities) val = this.replaceEntitiesValue(val);
|
|
85
|
+
|
|
86
|
+
const newval = this.options.tagValueProcessor(tagName, val, jPath, hasAttributes, isLeafNode);
|
|
87
|
+
if(newval === null || newval === undefined){
|
|
88
|
+
//don't parse
|
|
89
|
+
return val;
|
|
90
|
+
}else if(typeof newval !== typeof val || newval !== val){
|
|
91
|
+
//overwrite
|
|
92
|
+
return newval;
|
|
93
|
+
}else if(this.options.trimValues){
|
|
94
|
+
return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
|
|
95
|
+
}else{
|
|
96
|
+
const trimmedVal = val.trim();
|
|
97
|
+
if(trimmedVal === val){
|
|
98
|
+
return parseValue(val, this.options.parseTagValue, this.options.numberParseOptions);
|
|
99
|
+
}else{
|
|
100
|
+
return val;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function resolveNameSpace(tagname) {
|
|
108
|
+
if (this.options.removeNSPrefix) {
|
|
109
|
+
const tags = tagname.split(':');
|
|
110
|
+
const prefix = tagname.charAt(0) === '/' ? '/' : '';
|
|
111
|
+
if (tags[0] === 'xmlns') {
|
|
112
|
+
return '';
|
|
113
|
+
}
|
|
114
|
+
if (tags.length === 2) {
|
|
115
|
+
tagname = prefix + tags[1];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return tagname;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
//TODO: change regex to capture NS
|
|
122
|
+
//const attrsRegx = new RegExp("([\\w\\-\\.\\:]+)\\s*=\\s*(['\"])((.|\n)*?)\\2","gm");
|
|
123
|
+
const attrsRegx = new RegExp('([^\\s=]+)\\s*(=\\s*([\'"])([\\s\\S]*?)\\3)?', 'gm');
|
|
124
|
+
|
|
125
|
+
function buildAttributesMap(attrStr, jPath, tagName) {
|
|
126
|
+
if (!this.options.ignoreAttributes && typeof attrStr === 'string') {
|
|
127
|
+
// attrStr = attrStr.replace(/\r?\n/g, ' ');
|
|
128
|
+
//attrStr = attrStr || attrStr.trim();
|
|
129
|
+
|
|
130
|
+
const matches = util.getAllMatches(attrStr, attrsRegx);
|
|
131
|
+
const len = matches.length; //don't make it inline
|
|
132
|
+
const attrs = {};
|
|
133
|
+
for (let i = 0; i < len; i++) {
|
|
134
|
+
const attrName = this.resolveNameSpace(matches[i][1]);
|
|
135
|
+
let oldVal = matches[i][4];
|
|
136
|
+
let aName = this.options.attributeNamePrefix + attrName;
|
|
137
|
+
if (attrName.length) {
|
|
138
|
+
if (this.options.transformAttributeName) {
|
|
139
|
+
aName = this.options.transformAttributeName(aName);
|
|
140
|
+
}
|
|
141
|
+
if(aName === "__proto__") aName = "#__proto__";
|
|
142
|
+
if (oldVal !== undefined) {
|
|
143
|
+
if (this.options.trimValues) {
|
|
144
|
+
oldVal = oldVal.trim();
|
|
145
|
+
}
|
|
146
|
+
oldVal = this.replaceEntitiesValue(oldVal);
|
|
147
|
+
const newVal = this.options.attributeValueProcessor(attrName, oldVal, jPath);
|
|
148
|
+
if(newVal === null || newVal === undefined){
|
|
149
|
+
//don't parse
|
|
150
|
+
attrs[aName] = oldVal;
|
|
151
|
+
}else if(typeof newVal !== typeof oldVal || newVal !== oldVal){
|
|
152
|
+
//overwrite
|
|
153
|
+
attrs[aName] = newVal;
|
|
154
|
+
}else{
|
|
155
|
+
//parse
|
|
156
|
+
attrs[aName] = parseValue(
|
|
157
|
+
oldVal,
|
|
158
|
+
this.options.parseAttributeValue,
|
|
159
|
+
this.options.numberParseOptions
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
} else if (this.options.allowBooleanAttributes) {
|
|
163
|
+
attrs[aName] = true;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (!Object.keys(attrs).length) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
if (this.options.attributesGroupName) {
|
|
171
|
+
const attrCollection = {};
|
|
172
|
+
attrCollection[this.options.attributesGroupName] = attrs;
|
|
173
|
+
return attrCollection;
|
|
174
|
+
}
|
|
175
|
+
return attrs
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const parseXml = function(xmlData) {
|
|
180
|
+
xmlData = xmlData.replace(/\r\n?/g, "\n"); //TODO: remove this line
|
|
181
|
+
const xmlObj = new xmlNode('!xml');
|
|
182
|
+
let currentNode = xmlObj;
|
|
183
|
+
let textData = "";
|
|
184
|
+
let jPath = "";
|
|
185
|
+
for(let i=0; i< xmlData.length; i++){//for each char in XML data
|
|
186
|
+
const ch = xmlData[i];
|
|
187
|
+
if(ch === '<'){
|
|
188
|
+
// const nextIndex = i+1;
|
|
189
|
+
// const _2ndChar = xmlData[nextIndex];
|
|
190
|
+
if( xmlData[i+1] === '/') {//Closing Tag
|
|
191
|
+
const closeIndex = findClosingIndex(xmlData, ">", i, "Closing Tag is not closed.")
|
|
192
|
+
let tagName = xmlData.substring(i+2,closeIndex).trim();
|
|
193
|
+
|
|
194
|
+
if(this.options.removeNSPrefix){
|
|
195
|
+
const colonIndex = tagName.indexOf(":");
|
|
196
|
+
if(colonIndex !== -1){
|
|
197
|
+
tagName = tagName.substr(colonIndex+1);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if(this.options.transformTagName) {
|
|
202
|
+
tagName = this.options.transformTagName(tagName);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if(currentNode){
|
|
206
|
+
textData = this.saveTextToParentTag(textData, currentNode, jPath);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
//check if last tag of nested tag was unpaired tag
|
|
210
|
+
const lastTagName = jPath.substring(jPath.lastIndexOf(".")+1);
|
|
211
|
+
if(tagName && this.options.unpairedTags.indexOf(tagName) !== -1 ){
|
|
212
|
+
throw new Error(`Unpaired tag can not be used as closing tag: </${tagName}>`);
|
|
213
|
+
}
|
|
214
|
+
let propIndex = 0
|
|
215
|
+
if(lastTagName && this.options.unpairedTags.indexOf(lastTagName) !== -1 ){
|
|
216
|
+
propIndex = jPath.lastIndexOf('.', jPath.lastIndexOf('.')-1)
|
|
217
|
+
this.tagsNodeStack.pop();
|
|
218
|
+
}else{
|
|
219
|
+
propIndex = jPath.lastIndexOf(".");
|
|
220
|
+
}
|
|
221
|
+
jPath = jPath.substring(0, propIndex);
|
|
222
|
+
|
|
223
|
+
currentNode = this.tagsNodeStack.pop();//avoid recursion, set the parent tag scope
|
|
224
|
+
textData = "";
|
|
225
|
+
i = closeIndex;
|
|
226
|
+
} else if( xmlData[i+1] === '?') {
|
|
227
|
+
|
|
228
|
+
let tagData = readTagExp(xmlData,i, false, "?>");
|
|
229
|
+
if(!tagData) throw new Error("Pi Tag is not closed.");
|
|
230
|
+
|
|
231
|
+
textData = this.saveTextToParentTag(textData, currentNode, jPath);
|
|
232
|
+
if( (this.options.ignoreDeclaration && tagData.tagName === "?xml") || this.options.ignorePiTags){
|
|
233
|
+
|
|
234
|
+
}else{
|
|
235
|
+
|
|
236
|
+
const childNode = new xmlNode(tagData.tagName);
|
|
237
|
+
childNode.add(this.options.textNodeName, "");
|
|
238
|
+
|
|
239
|
+
if(tagData.tagName !== tagData.tagExp && tagData.attrExpPresent){
|
|
240
|
+
childNode[":@"] = this.buildAttributesMap(tagData.tagExp, jPath, tagData.tagName);
|
|
241
|
+
}
|
|
242
|
+
this.addChild(currentNode, childNode, jPath)
|
|
243
|
+
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
i = tagData.closeIndex + 1;
|
|
248
|
+
} else if(xmlData.substr(i + 1, 3) === '!--') {
|
|
249
|
+
const endIndex = findClosingIndex(xmlData, "-->", i+4, "Comment is not closed.")
|
|
250
|
+
if(this.options.commentPropName){
|
|
251
|
+
const comment = xmlData.substring(i + 4, endIndex - 2);
|
|
252
|
+
|
|
253
|
+
textData = this.saveTextToParentTag(textData, currentNode, jPath);
|
|
254
|
+
|
|
255
|
+
currentNode.add(this.options.commentPropName, [ { [this.options.textNodeName] : comment } ]);
|
|
256
|
+
}
|
|
257
|
+
i = endIndex;
|
|
258
|
+
} else if( xmlData.substr(i + 1, 2) === '!D') {
|
|
259
|
+
const result = readDocType(xmlData, i);
|
|
260
|
+
this.docTypeEntities = result.entities;
|
|
261
|
+
i = result.i;
|
|
262
|
+
}else if(xmlData.substr(i + 1, 2) === '![') {
|
|
263
|
+
const closeIndex = findClosingIndex(xmlData, "]]>", i, "CDATA is not closed.") - 2;
|
|
264
|
+
const tagExp = xmlData.substring(i + 9,closeIndex);
|
|
265
|
+
|
|
266
|
+
textData = this.saveTextToParentTag(textData, currentNode, jPath);
|
|
267
|
+
|
|
268
|
+
//cdata should be set even if it is 0 length string
|
|
269
|
+
if(this.options.cdataPropName){
|
|
270
|
+
// let val = this.parseTextData(tagExp, this.options.cdataPropName, jPath + "." + this.options.cdataPropName, true, false, true);
|
|
271
|
+
// if(!val) val = "";
|
|
272
|
+
currentNode.add(this.options.cdataPropName, [ { [this.options.textNodeName] : tagExp } ]);
|
|
273
|
+
}else{
|
|
274
|
+
let val = this.parseTextData(tagExp, currentNode.tagname, jPath, true, false, true);
|
|
275
|
+
if(val == undefined) val = "";
|
|
276
|
+
currentNode.add(this.options.textNodeName, val);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
i = closeIndex + 2;
|
|
280
|
+
}else {//Opening tag
|
|
281
|
+
let result = readTagExp(xmlData,i, this.options.removeNSPrefix);
|
|
282
|
+
let tagName= result.tagName;
|
|
283
|
+
let tagExp = result.tagExp;
|
|
284
|
+
let attrExpPresent = result.attrExpPresent;
|
|
285
|
+
let closeIndex = result.closeIndex;
|
|
286
|
+
|
|
287
|
+
if (this.options.transformTagName) {
|
|
288
|
+
tagName = this.options.transformTagName(tagName);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
//save text as child node
|
|
292
|
+
if (currentNode && textData) {
|
|
293
|
+
if(currentNode.tagname !== '!xml'){
|
|
294
|
+
//when nested tag is found
|
|
295
|
+
textData = this.saveTextToParentTag(textData, currentNode, jPath, false);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
//check if last tag was unpaired tag
|
|
300
|
+
const lastTag = currentNode;
|
|
301
|
+
if(lastTag && this.options.unpairedTags.indexOf(lastTag.tagname) !== -1 ){
|
|
302
|
+
currentNode = this.tagsNodeStack.pop();
|
|
303
|
+
jPath = jPath.substring(0, jPath.lastIndexOf("."));
|
|
304
|
+
}
|
|
305
|
+
if(tagName !== xmlObj.tagname){
|
|
306
|
+
jPath += jPath ? "." + tagName : tagName;
|
|
307
|
+
}
|
|
308
|
+
if (this.isItStopNode(this.options.stopNodes, jPath, tagName)) { //TODO: namespace
|
|
309
|
+
let tagContent = "";
|
|
310
|
+
//self-closing tag
|
|
311
|
+
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
|
|
312
|
+
i = result.closeIndex;
|
|
313
|
+
}
|
|
314
|
+
//unpaired tag
|
|
315
|
+
else if(this.options.unpairedTags.indexOf(tagName) !== -1){
|
|
316
|
+
i = result.closeIndex;
|
|
317
|
+
}
|
|
318
|
+
//normal tag
|
|
319
|
+
else{
|
|
320
|
+
//read until closing tag is found
|
|
321
|
+
const result = this.readStopNodeData(xmlData, tagName, closeIndex + 1);
|
|
322
|
+
if(!result) throw new Error(`Unexpected end of ${tagName}`);
|
|
323
|
+
i = result.i;
|
|
324
|
+
tagContent = result.tagContent;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const childNode = new xmlNode(tagName);
|
|
328
|
+
if(tagName !== tagExp && attrExpPresent){
|
|
329
|
+
childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
|
|
330
|
+
}
|
|
331
|
+
if(tagContent) {
|
|
332
|
+
tagContent = this.parseTextData(tagContent, tagName, jPath, true, attrExpPresent, true, true);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
jPath = jPath.substr(0, jPath.lastIndexOf("."));
|
|
336
|
+
childNode.add(this.options.textNodeName, tagContent);
|
|
337
|
+
|
|
338
|
+
this.addChild(currentNode, childNode, jPath)
|
|
339
|
+
}else{
|
|
340
|
+
//selfClosing tag
|
|
341
|
+
if(tagExp.length > 0 && tagExp.lastIndexOf("/") === tagExp.length - 1){
|
|
342
|
+
if(tagName[tagName.length - 1] === "/"){ //remove trailing '/'
|
|
343
|
+
tagName = tagName.substr(0, tagName.length - 1);
|
|
344
|
+
jPath = jPath.substr(0, jPath.length - 1);
|
|
345
|
+
tagExp = tagName;
|
|
346
|
+
}else{
|
|
347
|
+
tagExp = tagExp.substr(0, tagExp.length - 1);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
if(this.options.transformTagName) {
|
|
351
|
+
tagName = this.options.transformTagName(tagName);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
const childNode = new xmlNode(tagName);
|
|
355
|
+
if(tagName !== tagExp && attrExpPresent){
|
|
356
|
+
childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
|
|
357
|
+
}
|
|
358
|
+
this.addChild(currentNode, childNode, jPath)
|
|
359
|
+
jPath = jPath.substr(0, jPath.lastIndexOf("."));
|
|
360
|
+
}
|
|
361
|
+
//opening tag
|
|
362
|
+
else{
|
|
363
|
+
const childNode = new xmlNode( tagName);
|
|
364
|
+
this.tagsNodeStack.push(currentNode);
|
|
365
|
+
|
|
366
|
+
if(tagName !== tagExp && attrExpPresent){
|
|
367
|
+
childNode[":@"] = this.buildAttributesMap(tagExp, jPath, tagName);
|
|
368
|
+
}
|
|
369
|
+
this.addChild(currentNode, childNode, jPath)
|
|
370
|
+
currentNode = childNode;
|
|
371
|
+
}
|
|
372
|
+
textData = "";
|
|
373
|
+
i = closeIndex;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}else{
|
|
377
|
+
textData += xmlData[i];
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
return xmlObj.child;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
function addChild(currentNode, childNode, jPath){
|
|
384
|
+
const result = this.options.updateTag(childNode.tagname, jPath, childNode[":@"])
|
|
385
|
+
if(result === false){
|
|
386
|
+
}else if(typeof result === "string"){
|
|
387
|
+
childNode.tagname = result
|
|
388
|
+
currentNode.addChild(childNode);
|
|
389
|
+
}else{
|
|
390
|
+
currentNode.addChild(childNode);
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const replaceEntitiesValue = function(val){
|
|
395
|
+
|
|
396
|
+
if(this.options.processEntities){
|
|
397
|
+
for(let entityName in this.docTypeEntities){
|
|
398
|
+
const entity = this.docTypeEntities[entityName];
|
|
399
|
+
val = val.replace( entity.regx, entity.val);
|
|
400
|
+
}
|
|
401
|
+
for(let entityName in this.lastEntities){
|
|
402
|
+
const entity = this.lastEntities[entityName];
|
|
403
|
+
val = val.replace( entity.regex, entity.val);
|
|
404
|
+
}
|
|
405
|
+
if(this.options.htmlEntities){
|
|
406
|
+
for(let entityName in this.htmlEntities){
|
|
407
|
+
const entity = this.htmlEntities[entityName];
|
|
408
|
+
val = val.replace( entity.regex, entity.val);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
val = val.replace( this.ampEntity.regex, this.ampEntity.val);
|
|
412
|
+
}
|
|
413
|
+
return val;
|
|
414
|
+
}
|
|
415
|
+
function saveTextToParentTag(textData, currentNode, jPath, isLeafNode) {
|
|
416
|
+
if (textData) { //store previously collected data as textNode
|
|
417
|
+
if(isLeafNode === undefined) isLeafNode = Object.keys(currentNode.child).length === 0
|
|
418
|
+
|
|
419
|
+
textData = this.parseTextData(textData,
|
|
420
|
+
currentNode.tagname,
|
|
421
|
+
jPath,
|
|
422
|
+
false,
|
|
423
|
+
currentNode[":@"] ? Object.keys(currentNode[":@"]).length !== 0 : false,
|
|
424
|
+
isLeafNode);
|
|
425
|
+
|
|
426
|
+
if (textData !== undefined && textData !== "")
|
|
427
|
+
currentNode.add(this.options.textNodeName, textData);
|
|
428
|
+
textData = "";
|
|
429
|
+
}
|
|
430
|
+
return textData;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
//TODO: use jPath to simplify the logic
|
|
434
|
+
/**
|
|
435
|
+
*
|
|
436
|
+
* @param {string[]} stopNodes
|
|
437
|
+
* @param {string} jPath
|
|
438
|
+
* @param {string} currentTagName
|
|
439
|
+
*/
|
|
440
|
+
function isItStopNode(stopNodes, jPath, currentTagName){
|
|
441
|
+
const allNodesExp = "*." + currentTagName;
|
|
442
|
+
for (const stopNodePath in stopNodes) {
|
|
443
|
+
const stopNodeExp = stopNodes[stopNodePath];
|
|
444
|
+
if( allNodesExp === stopNodeExp || jPath === stopNodeExp ) return true;
|
|
445
|
+
}
|
|
446
|
+
return false;
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Returns the tag Expression and where it is ending handling single-double quotes situation
|
|
451
|
+
* @param {string} xmlData
|
|
452
|
+
* @param {number} i starting index
|
|
453
|
+
* @returns
|
|
454
|
+
*/
|
|
455
|
+
function tagExpWithClosingIndex(xmlData, i, closingChar = ">"){
|
|
456
|
+
let attrBoundary;
|
|
457
|
+
let tagExp = "";
|
|
458
|
+
for (let index = i; index < xmlData.length; index++) {
|
|
459
|
+
let ch = xmlData[index];
|
|
460
|
+
if (attrBoundary) {
|
|
461
|
+
if (ch === attrBoundary) attrBoundary = "";//reset
|
|
462
|
+
} else if (ch === '"' || ch === "'") {
|
|
463
|
+
attrBoundary = ch;
|
|
464
|
+
} else if (ch === closingChar[0]) {
|
|
465
|
+
if(closingChar[1]){
|
|
466
|
+
if(xmlData[index + 1] === closingChar[1]){
|
|
467
|
+
return {
|
|
468
|
+
data: tagExp,
|
|
469
|
+
index: index
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
}else{
|
|
473
|
+
return {
|
|
474
|
+
data: tagExp,
|
|
475
|
+
index: index
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
} else if (ch === '\t') {
|
|
479
|
+
ch = " "
|
|
480
|
+
}
|
|
481
|
+
tagExp += ch;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
function findClosingIndex(xmlData, str, i, errMsg){
|
|
486
|
+
const closingIndex = xmlData.indexOf(str, i);
|
|
487
|
+
if(closingIndex === -1){
|
|
488
|
+
throw new Error(errMsg)
|
|
489
|
+
}else{
|
|
490
|
+
return closingIndex + str.length - 1;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
function readTagExp(xmlData,i, removeNSPrefix, closingChar = ">"){
|
|
495
|
+
const result = tagExpWithClosingIndex(xmlData, i+1, closingChar);
|
|
496
|
+
if(!result) return;
|
|
497
|
+
let tagExp = result.data;
|
|
498
|
+
const closeIndex = result.index;
|
|
499
|
+
const separatorIndex = tagExp.search(/\s/);
|
|
500
|
+
let tagName = tagExp;
|
|
501
|
+
let attrExpPresent = true;
|
|
502
|
+
if(separatorIndex !== -1){//separate tag name and attributes expression
|
|
503
|
+
tagName = tagExp.substr(0, separatorIndex).replace(/\s\s*$/, '');
|
|
504
|
+
tagExp = tagExp.substr(separatorIndex + 1);
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
if(removeNSPrefix){
|
|
508
|
+
const colonIndex = tagName.indexOf(":");
|
|
509
|
+
if(colonIndex !== -1){
|
|
510
|
+
tagName = tagName.substr(colonIndex+1);
|
|
511
|
+
attrExpPresent = tagName !== result.data.substr(colonIndex + 1);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
return {
|
|
516
|
+
tagName: tagName,
|
|
517
|
+
tagExp: tagExp,
|
|
518
|
+
closeIndex: closeIndex,
|
|
519
|
+
attrExpPresent: attrExpPresent,
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
/**
|
|
523
|
+
* find paired tag for a stop node
|
|
524
|
+
* @param {string} xmlData
|
|
525
|
+
* @param {string} tagName
|
|
526
|
+
* @param {number} i
|
|
527
|
+
*/
|
|
528
|
+
function readStopNodeData(xmlData, tagName, i){
|
|
529
|
+
const startIndex = i;
|
|
530
|
+
// Starting at 1 since we already have an open tag
|
|
531
|
+
let openTagCount = 1;
|
|
532
|
+
|
|
533
|
+
for (; i < xmlData.length; i++) {
|
|
534
|
+
if( xmlData[i] === "<"){
|
|
535
|
+
if (xmlData[i+1] === "/") {//close tag
|
|
536
|
+
const closeIndex = findClosingIndex(xmlData, ">", i, `${tagName} is not closed`);
|
|
537
|
+
let closeTagName = xmlData.substring(i+2,closeIndex).trim();
|
|
538
|
+
if(closeTagName === tagName){
|
|
539
|
+
openTagCount--;
|
|
540
|
+
if (openTagCount === 0) {
|
|
541
|
+
return {
|
|
542
|
+
tagContent: xmlData.substring(startIndex, i),
|
|
543
|
+
i : closeIndex
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
i=closeIndex;
|
|
548
|
+
} else if(xmlData[i+1] === '?') {
|
|
549
|
+
const closeIndex = findClosingIndex(xmlData, "?>", i+1, "StopNode is not closed.")
|
|
550
|
+
i=closeIndex;
|
|
551
|
+
} else if(xmlData.substr(i + 1, 3) === '!--') {
|
|
552
|
+
const closeIndex = findClosingIndex(xmlData, "-->", i+3, "StopNode is not closed.")
|
|
553
|
+
i=closeIndex;
|
|
554
|
+
} else if(xmlData.substr(i + 1, 2) === '![') {
|
|
555
|
+
const closeIndex = findClosingIndex(xmlData, "]]>", i, "StopNode is not closed.") - 2;
|
|
556
|
+
i=closeIndex;
|
|
557
|
+
} else {
|
|
558
|
+
const tagData = readTagExp(xmlData, i, '>')
|
|
559
|
+
|
|
560
|
+
if (tagData) {
|
|
561
|
+
const openTagName = tagData && tagData.tagName;
|
|
562
|
+
if (openTagName === tagName && tagData.tagExp[tagData.tagExp.length-1] !== "/") {
|
|
563
|
+
openTagCount++;
|
|
564
|
+
}
|
|
565
|
+
i=tagData.closeIndex;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}//end for loop
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
function parseValue(val, shouldParse, options) {
|
|
573
|
+
if (shouldParse && typeof val === 'string') {
|
|
574
|
+
//console.log(options)
|
|
575
|
+
const newval = val.trim();
|
|
576
|
+
if(newval === 'true' ) return true;
|
|
577
|
+
else if(newval === 'false' ) return false;
|
|
578
|
+
else return toNumber(val, options);
|
|
579
|
+
} else {
|
|
580
|
+
if (util.isExist(val)) {
|
|
581
|
+
return val;
|
|
582
|
+
} else {
|
|
583
|
+
return '';
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
|
|
589
|
+
module.exports = OrderedObjParser;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
const { buildOptions} = require("./OptionsBuilder");
|
|
2
|
+
const OrderedObjParser = require("./OrderedObjParser");
|
|
3
|
+
const { prettify} = require("./node2json");
|
|
4
|
+
const validator = require('../validator');
|
|
5
|
+
|
|
6
|
+
class XMLParser{
|
|
7
|
+
|
|
8
|
+
constructor(options){
|
|
9
|
+
this.externalEntities = {};
|
|
10
|
+
this.options = buildOptions(options);
|
|
11
|
+
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Parse XML dats to JS object
|
|
15
|
+
* @param {string|Buffer} xmlData
|
|
16
|
+
* @param {boolean|Object} validationOption
|
|
17
|
+
*/
|
|
18
|
+
parse(xmlData,validationOption){
|
|
19
|
+
if(typeof xmlData === "string"){
|
|
20
|
+
}else if( xmlData.toString){
|
|
21
|
+
xmlData = xmlData.toString();
|
|
22
|
+
}else{
|
|
23
|
+
throw new Error("XML data is accepted in String or Bytes[] form.")
|
|
24
|
+
}
|
|
25
|
+
if( validationOption){
|
|
26
|
+
if(validationOption === true) validationOption = {}; //validate with default options
|
|
27
|
+
|
|
28
|
+
const result = validator.validate(xmlData, validationOption);
|
|
29
|
+
if (result !== true) {
|
|
30
|
+
throw Error( `${result.err.msg}:${result.err.line}:${result.err.col}` )
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const orderedObjParser = new OrderedObjParser(this.options);
|
|
34
|
+
orderedObjParser.addExternalEntities(this.externalEntities);
|
|
35
|
+
const orderedResult = orderedObjParser.parseXml(xmlData);
|
|
36
|
+
if(this.options.preserveOrder || orderedResult === undefined) return orderedResult;
|
|
37
|
+
else return prettify(orderedResult, this.options);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Add Entity which is not by default supported by this library
|
|
42
|
+
* @param {string} key
|
|
43
|
+
* @param {string} value
|
|
44
|
+
*/
|
|
45
|
+
addEntity(key, value){
|
|
46
|
+
if(value.indexOf("&") !== -1){
|
|
47
|
+
throw new Error("Entity value can't have '&'")
|
|
48
|
+
}else if(key.indexOf("&") !== -1 || key.indexOf(";") !== -1){
|
|
49
|
+
throw new Error("An entity must be set without '&' and ';'. Eg. use '#xD' for '
'")
|
|
50
|
+
}else if(value === "&"){
|
|
51
|
+
throw new Error("An entity with value '&' is not permitted");
|
|
52
|
+
}else{
|
|
53
|
+
this.externalEntities[key] = value;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
module.exports = XMLParser;
|