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,113 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
*
|
|
5
|
+
* @param {array} node
|
|
6
|
+
* @param {any} options
|
|
7
|
+
* @returns
|
|
8
|
+
*/
|
|
9
|
+
function prettify(node, options){
|
|
10
|
+
return compress( node, options);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param {array} arr
|
|
16
|
+
* @param {object} options
|
|
17
|
+
* @param {string} jPath
|
|
18
|
+
* @returns object
|
|
19
|
+
*/
|
|
20
|
+
function compress(arr, options, jPath){
|
|
21
|
+
let text;
|
|
22
|
+
const compressedObj = {};
|
|
23
|
+
for (let i = 0; i < arr.length; i++) {
|
|
24
|
+
const tagObj = arr[i];
|
|
25
|
+
const property = propName(tagObj);
|
|
26
|
+
let newJpath = "";
|
|
27
|
+
if(jPath === undefined) newJpath = property;
|
|
28
|
+
else newJpath = jPath + "." + property;
|
|
29
|
+
|
|
30
|
+
if(property === options.textNodeName){
|
|
31
|
+
if(text === undefined) text = tagObj[property];
|
|
32
|
+
else text += "" + tagObj[property];
|
|
33
|
+
}else if(property === undefined){
|
|
34
|
+
continue;
|
|
35
|
+
}else if(tagObj[property]){
|
|
36
|
+
|
|
37
|
+
let val = compress(tagObj[property], options, newJpath);
|
|
38
|
+
const isLeaf = isLeafTag(val, options);
|
|
39
|
+
|
|
40
|
+
if(tagObj[":@"]){
|
|
41
|
+
assignAttributes( val, tagObj[":@"], newJpath, options);
|
|
42
|
+
}else if(Object.keys(val).length === 1 && val[options.textNodeName] !== undefined && !options.alwaysCreateTextNode){
|
|
43
|
+
val = val[options.textNodeName];
|
|
44
|
+
}else if(Object.keys(val).length === 0){
|
|
45
|
+
if(options.alwaysCreateTextNode) val[options.textNodeName] = "";
|
|
46
|
+
else val = "";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if(compressedObj[property] !== undefined && compressedObj.hasOwnProperty(property)) {
|
|
50
|
+
if(!Array.isArray(compressedObj[property])) {
|
|
51
|
+
compressedObj[property] = [ compressedObj[property] ];
|
|
52
|
+
}
|
|
53
|
+
compressedObj[property].push(val);
|
|
54
|
+
}else{
|
|
55
|
+
//TODO: if a node is not an array, then check if it should be an array
|
|
56
|
+
//also determine if it is a leaf node
|
|
57
|
+
if (options.isArray(property, newJpath, isLeaf )) {
|
|
58
|
+
compressedObj[property] = [val];
|
|
59
|
+
}else{
|
|
60
|
+
compressedObj[property] = val;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
}
|
|
66
|
+
// if(text && text.length > 0) compressedObj[options.textNodeName] = text;
|
|
67
|
+
if(typeof text === "string"){
|
|
68
|
+
if(text.length > 0) compressedObj[options.textNodeName] = text;
|
|
69
|
+
}else if(text !== undefined) compressedObj[options.textNodeName] = text;
|
|
70
|
+
return compressedObj;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function propName(obj){
|
|
74
|
+
const keys = Object.keys(obj);
|
|
75
|
+
for (let i = 0; i < keys.length; i++) {
|
|
76
|
+
const key = keys[i];
|
|
77
|
+
if(key !== ":@") return key;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function assignAttributes(obj, attrMap, jpath, options){
|
|
82
|
+
if (attrMap) {
|
|
83
|
+
const keys = Object.keys(attrMap);
|
|
84
|
+
const len = keys.length; //don't make it inline
|
|
85
|
+
for (let i = 0; i < len; i++) {
|
|
86
|
+
const atrrName = keys[i];
|
|
87
|
+
if (options.isArray(atrrName, jpath + "." + atrrName, true, true)) {
|
|
88
|
+
obj[atrrName] = [ attrMap[atrrName] ];
|
|
89
|
+
} else {
|
|
90
|
+
obj[atrrName] = attrMap[atrrName];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function isLeafTag(obj, options){
|
|
97
|
+
const { textNodeName } = options;
|
|
98
|
+
const propCount = Object.keys(obj).length;
|
|
99
|
+
|
|
100
|
+
if (propCount === 0) {
|
|
101
|
+
return true;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (
|
|
105
|
+
propCount === 1 &&
|
|
106
|
+
(obj[textNodeName] || typeof obj[textNodeName] === "boolean" || obj[textNodeName] === 0)
|
|
107
|
+
) {
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
exports.prettify = prettify;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
class XmlNode{
|
|
4
|
+
constructor(tagname) {
|
|
5
|
+
this.tagname = tagname;
|
|
6
|
+
this.child = []; //nested tags, text, cdata, comments in order
|
|
7
|
+
this[":@"] = {}; //attributes map
|
|
8
|
+
}
|
|
9
|
+
add(key,val){
|
|
10
|
+
// this.child.push( {name : key, val: val, isCdata: isCdata });
|
|
11
|
+
if(key === "__proto__") key = "#__proto__";
|
|
12
|
+
this.child.push( {[key]: val });
|
|
13
|
+
}
|
|
14
|
+
addChild(node) {
|
|
15
|
+
if(node.tagname === "__proto__") node.tagname = "#__proto__";
|
|
16
|
+
if(node[":@"] && Object.keys(node[":@"]).length > 0){
|
|
17
|
+
this.child.push( { [node.tagname]: node.child, [":@"]: node[":@"] });
|
|
18
|
+
}else{
|
|
19
|
+
this.child.push( { [node.tagname]: node.child });
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
module.exports = XmlNode;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-red-contrib-tak-registration",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"description": "A Node-RED node to register to TAK and to help wrap files as datapackages to send to TAK",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@turf/turf": "6.5.0",
|
|
7
7
|
"adm-zip": "0.5.10",
|
|
8
8
|
"axios": "1.4.0",
|
|
9
|
-
"fast-xml-parser": "
|
|
9
|
+
"fast-xml-parser": "4.2.7",
|
|
10
10
|
"form-data": "4.0.0",
|
|
11
11
|
"long": "5.2.3",
|
|
12
12
|
"protobufjs": "7.2.4",
|
package/tak-ingest.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
const { XMLParser, XMLBuilder, XMLValidator} = require("fast-xml-parser");
|
|
2
2
|
var Long = require('long').Long;
|
|
3
3
|
var protobuf = require('protobufjs');
|
|
4
4
|
var path = require('path');
|
|
@@ -17,6 +17,7 @@ module.exports = function(RED) {
|
|
|
17
17
|
RED.nodes.createNode(this,n);
|
|
18
18
|
var node = this;
|
|
19
19
|
var global = this.context().global;
|
|
20
|
+
const parser = new XMLParser(fastXmlOptions);
|
|
20
21
|
|
|
21
22
|
node.on("input",function(msg) {
|
|
22
23
|
if (Buffer.isBuffer(msg.payload)) {
|
|
@@ -72,7 +73,7 @@ module.exports = function(RED) {
|
|
|
72
73
|
}
|
|
73
74
|
return;
|
|
74
75
|
}
|
|
75
|
-
msg.payload =
|
|
76
|
+
msg.payload = parser.parse(msg.payload);
|
|
76
77
|
// Add any unique ID/callsigns to some global variables just in case it's useful for later.
|
|
77
78
|
if (msg.payload?.event?.detail?.contact?.callsign && msg.payload?.event?.uid) {
|
|
78
79
|
var a = global.get("_takgatewaycs") || {};
|
|
@@ -1,280 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
//parse Empty Node as self closing node
|
|
3
|
-
const buildOptions = require('./util').buildOptions;
|
|
4
|
-
|
|
5
|
-
const defaultOptions = {
|
|
6
|
-
attributeNamePrefix: '@_',
|
|
7
|
-
attrNodeName: false,
|
|
8
|
-
textNodeName: '#text',
|
|
9
|
-
ignoreAttributes: true,
|
|
10
|
-
cdataTagName: false,
|
|
11
|
-
cdataPositionChar: '\\c',
|
|
12
|
-
format: false,
|
|
13
|
-
indentBy: ' ',
|
|
14
|
-
supressEmptyNode: false,
|
|
15
|
-
tagValueProcessor: function(a) {
|
|
16
|
-
return a;
|
|
17
|
-
},
|
|
18
|
-
attrValueProcessor: function(a) {
|
|
19
|
-
return a;
|
|
20
|
-
},
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
const props = [
|
|
24
|
-
'attributeNamePrefix',
|
|
25
|
-
'attrNodeName',
|
|
26
|
-
'textNodeName',
|
|
27
|
-
'ignoreAttributes',
|
|
28
|
-
'cdataTagName',
|
|
29
|
-
'cdataPositionChar',
|
|
30
|
-
'format',
|
|
31
|
-
'indentBy',
|
|
32
|
-
'supressEmptyNode',
|
|
33
|
-
'tagValueProcessor',
|
|
34
|
-
'attrValueProcessor',
|
|
35
|
-
'rootNodeName', //when array as root
|
|
36
|
-
];
|
|
37
|
-
|
|
38
|
-
function Parser(options) {
|
|
39
|
-
this.options = buildOptions(options, defaultOptions, props);
|
|
40
|
-
if (this.options.ignoreAttributes || this.options.attrNodeName) {
|
|
41
|
-
this.isAttribute = function(/*a*/) {
|
|
42
|
-
return false;
|
|
43
|
-
};
|
|
44
|
-
} else {
|
|
45
|
-
this.attrPrefixLen = this.options.attributeNamePrefix.length;
|
|
46
|
-
this.isAttribute = isAttribute;
|
|
47
|
-
}
|
|
48
|
-
if (this.options.cdataTagName) {
|
|
49
|
-
this.isCDATA = isCDATA;
|
|
50
|
-
} else {
|
|
51
|
-
this.isCDATA = function(/*a*/) {
|
|
52
|
-
return false;
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
this.replaceCDATAstr = replaceCDATAstr;
|
|
56
|
-
this.replaceCDATAarr = replaceCDATAarr;
|
|
57
|
-
|
|
58
|
-
this.processTextOrObjNode = processTextOrObjNode
|
|
59
|
-
|
|
60
|
-
if (this.options.format) {
|
|
61
|
-
this.indentate = indentate;
|
|
62
|
-
this.tagEndChar = '>\n';
|
|
63
|
-
this.newLine = '\n';
|
|
64
|
-
} else {
|
|
65
|
-
this.indentate = function() {
|
|
66
|
-
return '';
|
|
67
|
-
};
|
|
68
|
-
this.tagEndChar = '>';
|
|
69
|
-
this.newLine = '';
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (this.options.supressEmptyNode) {
|
|
73
|
-
this.buildTextNode = buildEmptyTextNode;
|
|
74
|
-
this.buildObjNode = buildEmptyObjNode;
|
|
75
|
-
} else {
|
|
76
|
-
this.buildTextNode = buildTextValNode;
|
|
77
|
-
this.buildObjNode = buildObjectNode;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
this.buildTextValNode = buildTextValNode;
|
|
81
|
-
this.buildObjectNode = buildObjectNode;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
Parser.prototype.parse = function(jObj) {
|
|
85
|
-
if(Array.isArray(jObj) && this.options.rootNodeName && this.options.rootNodeName.length > 1){
|
|
86
|
-
jObj = {
|
|
87
|
-
[this.options.rootNodeName] : jObj
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return this.j2x(jObj, 0).val;
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
Parser.prototype.j2x = function(jObj, level) {
|
|
94
|
-
let attrStr = '';
|
|
95
|
-
let val = '';
|
|
96
|
-
for (let key in jObj) {
|
|
97
|
-
if (typeof jObj[key] === 'undefined') {
|
|
98
|
-
// supress undefined node
|
|
99
|
-
} else if (jObj[key] === null) {
|
|
100
|
-
val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
101
|
-
} else if (jObj[key] instanceof Date) {
|
|
102
|
-
val += this.buildTextNode(jObj[key], key, '', level);
|
|
103
|
-
} else if (typeof jObj[key] !== 'object') {
|
|
104
|
-
//premitive type
|
|
105
|
-
const attr = this.isAttribute(key);
|
|
106
|
-
if (attr) {
|
|
107
|
-
attrStr += ' ' + attr + '="' + this.options.attrValueProcessor('' + jObj[key]) + '"';
|
|
108
|
-
} else if (this.isCDATA(key)) {
|
|
109
|
-
if (jObj[this.options.textNodeName]) {
|
|
110
|
-
val += this.replaceCDATAstr(jObj[this.options.textNodeName], jObj[key]);
|
|
111
|
-
} else {
|
|
112
|
-
val += this.replaceCDATAstr('', jObj[key]);
|
|
113
|
-
}
|
|
114
|
-
} else {
|
|
115
|
-
//tag value
|
|
116
|
-
if (key === this.options.textNodeName) {
|
|
117
|
-
if (jObj[this.options.cdataTagName]) {
|
|
118
|
-
//value will added while processing cdata
|
|
119
|
-
} else {
|
|
120
|
-
val += this.options.tagValueProcessor('' + jObj[key]);
|
|
121
|
-
}
|
|
122
|
-
} else {
|
|
123
|
-
val += this.buildTextNode(jObj[key], key, '', level);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
} else if (Array.isArray(jObj[key])) {
|
|
127
|
-
//repeated nodes
|
|
128
|
-
if (this.isCDATA(key)) {
|
|
129
|
-
val += this.indentate(level);
|
|
130
|
-
if (jObj[this.options.textNodeName]) {
|
|
131
|
-
val += this.replaceCDATAarr(jObj[this.options.textNodeName], jObj[key]);
|
|
132
|
-
} else {
|
|
133
|
-
val += this.replaceCDATAarr('', jObj[key]);
|
|
134
|
-
}
|
|
135
|
-
} else {
|
|
136
|
-
//nested nodes
|
|
137
|
-
const arrLen = jObj[key].length;
|
|
138
|
-
for (let j = 0; j < arrLen; j++) {
|
|
139
|
-
const item = jObj[key][j];
|
|
140
|
-
if (typeof item === 'undefined') {
|
|
141
|
-
// supress undefined node
|
|
142
|
-
} else if (item === null) {
|
|
143
|
-
val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
|
|
144
|
-
} else if (typeof item === 'object') {
|
|
145
|
-
val += this.processTextOrObjNode(item, key, level)
|
|
146
|
-
} else {
|
|
147
|
-
val += this.buildTextNode(item, key, '', level);
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
} else {
|
|
152
|
-
//nested node
|
|
153
|
-
if (this.options.attrNodeName && key === this.options.attrNodeName) {
|
|
154
|
-
const Ks = Object.keys(jObj[key]);
|
|
155
|
-
const L = Ks.length;
|
|
156
|
-
for (let j = 0; j < L; j++) {
|
|
157
|
-
attrStr += ' ' + Ks[j] + '="' + this.options.attrValueProcessor('' + jObj[key][Ks[j]]) + '"';
|
|
158
|
-
}
|
|
159
|
-
} else {
|
|
160
|
-
val += this.processTextOrObjNode(jObj[key], key, level)
|
|
161
|
-
}
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
return {attrStr: attrStr, val: val};
|
|
165
|
-
};
|
|
166
|
-
|
|
167
|
-
function processTextOrObjNode (object, key, level) {
|
|
168
|
-
const result = this.j2x(object, level + 1);
|
|
169
|
-
if (object[this.options.textNodeName] !== undefined && Object.keys(object).length === 1) {
|
|
170
|
-
return this.buildTextNode(result.val, key, result.attrStr, level);
|
|
171
|
-
} else {
|
|
172
|
-
return this.buildObjNode(result.val, key, result.attrStr, level);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function replaceCDATAstr(str, cdata) {
|
|
177
|
-
str = this.options.tagValueProcessor('' + str);
|
|
178
|
-
if (this.options.cdataPositionChar === '' || str === '') {
|
|
179
|
-
return str + '<![CDATA[' + cdata + ']]' + this.tagEndChar;
|
|
180
|
-
} else {
|
|
181
|
-
return str.replace(this.options.cdataPositionChar, '<![CDATA[' + cdata + ']]' + this.tagEndChar);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function replaceCDATAarr(str, cdata) {
|
|
186
|
-
str = this.options.tagValueProcessor('' + str);
|
|
187
|
-
if (this.options.cdataPositionChar === '' || str === '') {
|
|
188
|
-
return str + '<![CDATA[' + cdata.join(']]><![CDATA[') + ']]' + this.tagEndChar;
|
|
189
|
-
} else {
|
|
190
|
-
for (let v in cdata) {
|
|
191
|
-
str = str.replace(this.options.cdataPositionChar, '<![CDATA[' + cdata[v] + ']]>');
|
|
192
|
-
}
|
|
193
|
-
return str + this.newLine;
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function buildObjectNode(val, key, attrStr, level) {
|
|
198
|
-
if (attrStr && val.indexOf('<') === -1) {
|
|
199
|
-
return (
|
|
200
|
-
this.indentate(level) +
|
|
201
|
-
'<' +
|
|
202
|
-
key +
|
|
203
|
-
attrStr +
|
|
204
|
-
'>' +
|
|
205
|
-
val +
|
|
206
|
-
//+ this.newLine
|
|
207
|
-
// + this.indentate(level)
|
|
208
|
-
'</' +
|
|
209
|
-
key +
|
|
210
|
-
this.tagEndChar
|
|
211
|
-
);
|
|
212
|
-
} else {
|
|
213
|
-
return (
|
|
214
|
-
this.indentate(level) +
|
|
215
|
-
'<' +
|
|
216
|
-
key +
|
|
217
|
-
attrStr +
|
|
218
|
-
this.tagEndChar +
|
|
219
|
-
val +
|
|
220
|
-
//+ this.newLine
|
|
221
|
-
this.indentate(level) +
|
|
222
|
-
'</' +
|
|
223
|
-
key +
|
|
224
|
-
this.tagEndChar
|
|
225
|
-
);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function buildEmptyObjNode(val, key, attrStr, level) {
|
|
230
|
-
if (val !== '') {
|
|
231
|
-
return this.buildObjectNode(val, key, attrStr, level);
|
|
232
|
-
} else {
|
|
233
|
-
return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
|
|
234
|
-
//+ this.newLine
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
function buildTextValNode(val, key, attrStr, level) {
|
|
239
|
-
return (
|
|
240
|
-
this.indentate(level) +
|
|
241
|
-
'<' +
|
|
242
|
-
key +
|
|
243
|
-
attrStr +
|
|
244
|
-
'>' +
|
|
245
|
-
this.options.tagValueProcessor(val) +
|
|
246
|
-
'</' +
|
|
247
|
-
key +
|
|
248
|
-
this.tagEndChar
|
|
249
|
-
);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function buildEmptyTextNode(val, key, attrStr, level) {
|
|
253
|
-
if (val !== '') {
|
|
254
|
-
return this.buildTextValNode(val, key, attrStr, level);
|
|
255
|
-
} else {
|
|
256
|
-
return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
function indentate(level) {
|
|
261
|
-
return this.options.indentBy.repeat(level);
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
function isAttribute(name /*, options*/) {
|
|
265
|
-
if (name.startsWith(this.options.attributeNamePrefix)) {
|
|
266
|
-
return name.substr(this.attrPrefixLen);
|
|
267
|
-
} else {
|
|
268
|
-
return false;
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
function isCDATA(name) {
|
|
273
|
-
return name === this.options.cdataTagName;
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
//formatting
|
|
277
|
-
//indentation
|
|
278
|
-
//\n after each closing or self closing tag
|
|
279
|
-
|
|
280
|
-
module.exports = Parser;
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const char = function(a) {
|
|
3
|
-
return String.fromCharCode(a);
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
const chars = {
|
|
7
|
-
nilChar: char(176),
|
|
8
|
-
missingChar: char(201),
|
|
9
|
-
nilPremitive: char(175),
|
|
10
|
-
missingPremitive: char(200),
|
|
11
|
-
|
|
12
|
-
emptyChar: char(178),
|
|
13
|
-
emptyValue: char(177), //empty Premitive
|
|
14
|
-
|
|
15
|
-
boundryChar: char(179),
|
|
16
|
-
|
|
17
|
-
objStart: char(198),
|
|
18
|
-
arrStart: char(204),
|
|
19
|
-
arrayEnd: char(185),
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
const charsArr = [
|
|
23
|
-
chars.nilChar,
|
|
24
|
-
chars.nilPremitive,
|
|
25
|
-
chars.missingChar,
|
|
26
|
-
chars.missingPremitive,
|
|
27
|
-
chars.boundryChar,
|
|
28
|
-
chars.emptyChar,
|
|
29
|
-
chars.emptyValue,
|
|
30
|
-
chars.arrayEnd,
|
|
31
|
-
chars.objStart,
|
|
32
|
-
chars.arrStart,
|
|
33
|
-
];
|
|
34
|
-
|
|
35
|
-
const _e = function(node, e_schema, options) {
|
|
36
|
-
if (typeof e_schema === 'string') {
|
|
37
|
-
//premitive
|
|
38
|
-
if (node && node[0] && node[0].val !== undefined) {
|
|
39
|
-
return getValue(node[0].val, e_schema);
|
|
40
|
-
} else {
|
|
41
|
-
return getValue(node, e_schema);
|
|
42
|
-
}
|
|
43
|
-
} else {
|
|
44
|
-
const hasValidData = hasData(node);
|
|
45
|
-
if (hasValidData === true) {
|
|
46
|
-
let str = '';
|
|
47
|
-
if (Array.isArray(e_schema)) {
|
|
48
|
-
//attributes can't be repeated. hence check in children tags only
|
|
49
|
-
str += chars.arrStart;
|
|
50
|
-
const itemSchema = e_schema[0];
|
|
51
|
-
//const itemSchemaType = itemSchema;
|
|
52
|
-
const arr_len = node.length;
|
|
53
|
-
|
|
54
|
-
if (typeof itemSchema === 'string') {
|
|
55
|
-
for (let arr_i = 0; arr_i < arr_len; arr_i++) {
|
|
56
|
-
const r = getValue(node[arr_i].val, itemSchema);
|
|
57
|
-
str = processValue(str, r);
|
|
58
|
-
}
|
|
59
|
-
} else {
|
|
60
|
-
for (let arr_i = 0; arr_i < arr_len; arr_i++) {
|
|
61
|
-
const r = _e(node[arr_i], itemSchema, options);
|
|
62
|
-
str = processValue(str, r);
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
str += chars.arrayEnd; //indicates that next item is not array item
|
|
66
|
-
} else {
|
|
67
|
-
//object
|
|
68
|
-
str += chars.objStart;
|
|
69
|
-
const keys = Object.keys(e_schema);
|
|
70
|
-
if (Array.isArray(node)) {
|
|
71
|
-
node = node[0];
|
|
72
|
-
}
|
|
73
|
-
for (let i in keys) {
|
|
74
|
-
const key = keys[i];
|
|
75
|
-
//a property defined in schema can be present either in attrsMap or children tags
|
|
76
|
-
//options.textNodeName will not present in both maps, take it's value from val
|
|
77
|
-
//options.attrNodeName will be present in attrsMap
|
|
78
|
-
let r;
|
|
79
|
-
if (!options.ignoreAttributes && node.attrsMap && node.attrsMap[key]) {
|
|
80
|
-
r = _e(node.attrsMap[key], e_schema[key], options);
|
|
81
|
-
} else if (key === options.textNodeName) {
|
|
82
|
-
r = _e(node.val, e_schema[key], options);
|
|
83
|
-
} else {
|
|
84
|
-
r = _e(node.child[key], e_schema[key], options);
|
|
85
|
-
}
|
|
86
|
-
str = processValue(str, r);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return str;
|
|
90
|
-
} else {
|
|
91
|
-
return hasValidData;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
};
|
|
95
|
-
|
|
96
|
-
const getValue = function(a /*, type*/) {
|
|
97
|
-
switch (a) {
|
|
98
|
-
case undefined:
|
|
99
|
-
return chars.missingPremitive;
|
|
100
|
-
case null:
|
|
101
|
-
return chars.nilPremitive;
|
|
102
|
-
case '':
|
|
103
|
-
return chars.emptyValue;
|
|
104
|
-
default:
|
|
105
|
-
return a;
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
const processValue = function(str, r) {
|
|
110
|
-
if (!isAppChar(r[0]) && !isAppChar(str[str.length - 1])) {
|
|
111
|
-
str += chars.boundryChar;
|
|
112
|
-
}
|
|
113
|
-
return str + r;
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const isAppChar = function(ch) {
|
|
117
|
-
return charsArr.indexOf(ch) !== -1;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
function hasData(jObj) {
|
|
121
|
-
if (jObj === undefined) {
|
|
122
|
-
return chars.missingChar;
|
|
123
|
-
} else if (jObj === null) {
|
|
124
|
-
return chars.nilChar;
|
|
125
|
-
} else if (
|
|
126
|
-
jObj.child &&
|
|
127
|
-
Object.keys(jObj.child).length === 0 &&
|
|
128
|
-
(!jObj.attrsMap || Object.keys(jObj.attrsMap).length === 0)
|
|
129
|
-
) {
|
|
130
|
-
return chars.emptyChar;
|
|
131
|
-
} else {
|
|
132
|
-
return true;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
const x2j = require('./xmlstr2xmlnode');
|
|
137
|
-
const buildOptions = require('./util').buildOptions;
|
|
138
|
-
|
|
139
|
-
const convert2nimn = function(node, e_schema, options) {
|
|
140
|
-
options = buildOptions(options, x2j.defaultOptions, x2j.props);
|
|
141
|
-
return _e(node, e_schema, options);
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
exports.convert2nimn = convert2nimn;
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const util = require('./util');
|
|
4
|
-
|
|
5
|
-
const convertToJson = function(node, options, parentTagName) {
|
|
6
|
-
const jObj = {};
|
|
7
|
-
|
|
8
|
-
// when no child node or attr is present
|
|
9
|
-
if (!options.alwaysCreateTextNode && (!node.child || util.isEmptyObject(node.child)) && (!node.attrsMap || util.isEmptyObject(node.attrsMap))) {
|
|
10
|
-
return util.isExist(node.val) ? node.val : '';
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
// otherwise create a textnode if node has some text
|
|
14
|
-
if (util.isExist(node.val) && !(typeof node.val === 'string' && (node.val === '' || node.val === options.cdataPositionChar))) {
|
|
15
|
-
const asArray = util.isTagNameInArrayMode(node.tagname, options.arrayMode, parentTagName)
|
|
16
|
-
jObj[options.textNodeName] = asArray ? [node.val] : node.val;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
util.merge(jObj, node.attrsMap, options.arrayMode);
|
|
20
|
-
|
|
21
|
-
const keys = Object.keys(node.child);
|
|
22
|
-
for (let index = 0; index < keys.length; index++) {
|
|
23
|
-
const tagName = keys[index];
|
|
24
|
-
if (node.child[tagName] && node.child[tagName].length > 1) {
|
|
25
|
-
jObj[tagName] = [];
|
|
26
|
-
for (let tag in node.child[tagName]) {
|
|
27
|
-
if (node.child[tagName].hasOwnProperty(tag)) {
|
|
28
|
-
jObj[tagName].push(convertToJson(node.child[tagName][tag], options, tagName));
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
} else {
|
|
32
|
-
const result = convertToJson(node.child[tagName][0], options, tagName);
|
|
33
|
-
const asArray = (options.arrayMode === true && typeof result === 'object') || util.isTagNameInArrayMode(tagName, options.arrayMode, parentTagName);
|
|
34
|
-
jObj[tagName] = asArray ? [result] : result;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
//add value
|
|
39
|
-
return jObj;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
exports.convertToJson = convertToJson;
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const util = require('./util');
|
|
4
|
-
const buildOptions = require('./util').buildOptions;
|
|
5
|
-
const x2j = require('./xmlstr2xmlnode');
|
|
6
|
-
|
|
7
|
-
//TODO: do it later
|
|
8
|
-
const convertToJsonString = function(node, options) {
|
|
9
|
-
options = buildOptions(options, x2j.defaultOptions, x2j.props);
|
|
10
|
-
|
|
11
|
-
options.indentBy = options.indentBy || '';
|
|
12
|
-
return _cToJsonStr(node, options, 0);
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
const _cToJsonStr = function(node, options, level) {
|
|
16
|
-
let jObj = '{';
|
|
17
|
-
|
|
18
|
-
//traver through all the children
|
|
19
|
-
const keys = Object.keys(node.child);
|
|
20
|
-
|
|
21
|
-
for (let index = 0; index < keys.length; index++) {
|
|
22
|
-
const tagname = keys[index];
|
|
23
|
-
if (node.child[tagname] && node.child[tagname].length > 1) {
|
|
24
|
-
jObj += '"' + tagname + '" : [ ';
|
|
25
|
-
for (let tag in node.child[tagname]) {
|
|
26
|
-
jObj += _cToJsonStr(node.child[tagname][tag], options) + ' , ';
|
|
27
|
-
}
|
|
28
|
-
jObj = jObj.substr(0, jObj.length - 1) + ' ] '; //remove extra comma in last
|
|
29
|
-
} else {
|
|
30
|
-
jObj += '"' + tagname + '" : ' + _cToJsonStr(node.child[tagname][0], options) + ' ,';
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
util.merge(jObj, node.attrsMap);
|
|
34
|
-
//add attrsMap as new children
|
|
35
|
-
if (util.isEmptyObject(jObj)) {
|
|
36
|
-
return util.isExist(node.val) ? node.val : '';
|
|
37
|
-
} else {
|
|
38
|
-
if (util.isExist(node.val)) {
|
|
39
|
-
if (!(typeof node.val === 'string' && (node.val === '' || node.val === options.cdataPositionChar))) {
|
|
40
|
-
jObj += '"' + options.textNodeName + '" : ' + stringval(node.val);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
//add value
|
|
45
|
-
if (jObj[jObj.length - 1] === ',') {
|
|
46
|
-
jObj = jObj.substr(0, jObj.length - 2);
|
|
47
|
-
}
|
|
48
|
-
return jObj + '}';
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
function stringval(v) {
|
|
52
|
-
if (v === true || v === false || !isNaN(v)) {
|
|
53
|
-
return v;
|
|
54
|
-
} else {
|
|
55
|
-
return '"' + v + '"';
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function indentate(options, level) {
|
|
60
|
-
return options.indentBy.repeat(level);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
exports.convertToJsonString = convertToJsonString;
|