-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathjson2xml.js
259 lines (239 loc) · 8.39 KB
/
json2xml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
'use strict';
//parse Empty Node as self closing node
const buildFromOrderedJs = require('./orderedJs2Xml');
const defaultOptions = {
attributeNamePrefix: '@_',
attributesGroupName: false,
textNodeName: '#text',
ignoreAttributes: true,
cdataPropName: false,
format: false,
indentBy: ' ',
suppressEmptyNode: false,
suppressUnpairedNode: true,
suppressBooleanAttributes: true,
tagValueProcessor: function(key, a) {
return a;
},
attributeValueProcessor: function(attrName, a) {
return a;
},
preserveOrder: false,
commentPropName: false,
unpairedTags: [],
entities: [
{ regex: new RegExp("&", "g"), val: "&" },//it must be on top
{ regex: new RegExp(">", "g"), val: ">" },
{ regex: new RegExp("<", "g"), val: "<" },
{ regex: new RegExp("\'", "g"), val: "'" },
{ regex: new RegExp("\"", "g"), val: """ }
],
processEntities: true,
stopNodes: [],
// transformTagName: false,
// transformAttributeName: false,
oneListGroup: false
};
function Builder(options) {
this.options = Object.assign({}, defaultOptions, options);
if (this.options.ignoreAttributes || this.options.attributesGroupName) {
this.isAttribute = function(/*a*/) {
return false;
};
} else {
this.attrPrefixLen = this.options.attributeNamePrefix.length;
this.isAttribute = isAttribute;
}
this.processTextOrObjNode = processTextOrObjNode
if (this.options.format) {
this.indentate = indentate;
this.tagEndChar = '>\n';
this.newLine = '\n';
} else {
this.indentate = function() {
return '';
};
this.tagEndChar = '>';
this.newLine = '';
}
}
Builder.prototype.build = function(jObj) {
if(this.options.preserveOrder){
return buildFromOrderedJs(jObj, this.options);
}else {
if(Array.isArray(jObj) && this.options.arrayNodeName && this.options.arrayNodeName.length > 1){
jObj = {
[this.options.arrayNodeName] : jObj
}
}
return this.j2x(jObj, 0).val;
}
};
Builder.prototype.j2x = function(jObj, level) {
let attrStr = '';
let val = '';
for (let key in jObj) {
if (typeof jObj[key] === 'undefined') {
// supress undefined node
} else if (jObj[key] === null) {
if(key[0] === "?") val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
else val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
// val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
} else if (jObj[key] instanceof Date) {
val += this.buildTextValNode(jObj[key], key, '', level);
} else if (typeof jObj[key] !== 'object') {
//premitive type
const attr = this.isAttribute(key);
if (attr) {
attrStr += this.buildAttrPairStr(attr, '' + jObj[key]);
}else {
//tag value
if (key === this.options.textNodeName) {
let newval = this.options.tagValueProcessor(key, '' + jObj[key]);
val += this.replaceEntitiesValue(newval);
} else {
val += this.buildTextValNode(jObj[key], key, '', level);
}
}
} else if (Array.isArray(jObj[key])) {
//repeated nodes
const arrLen = jObj[key].length;
let listTagVal = "";
for (let j = 0; j < arrLen; j++) {
const item = jObj[key][j];
if (typeof item === 'undefined') {
// supress undefined node
} else if (item === null) {
if(key[0] === "?") val += this.indentate(level) + '<' + key + '?' + this.tagEndChar;
else val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
// val += this.indentate(level) + '<' + key + '/' + this.tagEndChar;
} else if (typeof item === 'object') {
if(this.options.oneListGroup ){
listTagVal += this.j2x(item, level + 1).val;
}else{
listTagVal += this.processTextOrObjNode(item, key, level)
}
} else {
listTagVal += this.buildTextValNode(item, key, '', level);
}
}
if(this.options.oneListGroup){
listTagVal = this.buildObjectNode(listTagVal, key, '', level);
}
val += listTagVal;
} else {
//nested node
if (this.options.attributesGroupName && key === this.options.attributesGroupName) {
const Ks = Object.keys(jObj[key]);
const L = Ks.length;
for (let j = 0; j < L; j++) {
attrStr += this.buildAttrPairStr(Ks[j], '' + jObj[key][Ks[j]]);
}
} else {
val += this.processTextOrObjNode(jObj[key], key, level)
}
}
}
return {attrStr: attrStr, val: val};
};
Builder.prototype.buildAttrPairStr = function(attrName, val){
val = this.options.attributeValueProcessor(attrName, '' + val);
val = this.replaceEntitiesValue(val);
if (this.options.suppressBooleanAttributes && val === "true") {
return ' ' + attrName;
} else return ' ' + attrName + '="' + val + '"';
}
function processTextOrObjNode (object, key, level) {
const result = this.j2x(object, level + 1);
if (object[this.options.textNodeName] !== undefined && Object.keys(object).length === 1) {
return this.buildTextValNode(object[this.options.textNodeName], key, result.attrStr, level);
} else {
return this.buildObjectNode(result.val, key, result.attrStr, level);
}
}
Builder.prototype.buildObjectNode = function(val, key, attrStr, level) {
if(val === ""){
if(key[0] === "?") return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar;
else {
return this.indentate(level) + '<' + key + attrStr + this.closeTag(key) + this.tagEndChar;
}
}else{
let tagEndExp = '</' + key + this.tagEndChar;
let piClosingChar = "";
if(key[0] === "?") {
piClosingChar = "?";
tagEndExp = "";
}
if (attrStr && val.indexOf('<') === -1) {
return ( this.indentate(level) + '<' + key + attrStr + piClosingChar + '>' + val + tagEndExp );
} else if (this.options.commentPropName !== false && key === this.options.commentPropName && piClosingChar.length === 0) {
return this.indentate(level) + `<!--${val}-->` + this.newLine;
}else {
return (
this.indentate(level) + '<' + key + attrStr + piClosingChar + this.tagEndChar +
val +
this.indentate(level) + tagEndExp );
}
}
}
Builder.prototype.closeTag = function(key){
let closeTag = "";
if(this.options.unpairedTags.indexOf(key) !== -1){ //unpaired
if(!this.options.suppressUnpairedNode) closeTag = "/"
}else if(this.options.suppressEmptyNode){ //empty
closeTag = "/";
}else{
closeTag = `></${key}`
}
return closeTag;
}
function buildEmptyObjNode(val, key, attrStr, level) {
if (val !== '') {
return this.buildObjectNode(val, key, attrStr, level);
} else {
if(key[0] === "?") return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar;
else {
return this.indentate(level) + '<' + key + attrStr + '/' + this.tagEndChar;
// return this.buildTagStr(level,key, attrStr);
}
}
}
Builder.prototype.buildTextValNode = function(val, key, attrStr, level) {
if (this.options.cdataPropName !== false && key === this.options.cdataPropName) {
return this.indentate(level) + `<![CDATA[${val}]]>` + this.newLine;
}else if (this.options.commentPropName !== false && key === this.options.commentPropName) {
return this.indentate(level) + `<!--${val}-->` + this.newLine;
}else if(key[0] === "?") {//PI tag
return this.indentate(level) + '<' + key + attrStr+ '?' + this.tagEndChar;
}else{
let textValue = this.options.tagValueProcessor(key, val);
textValue = this.replaceEntitiesValue(textValue);
if( textValue === ''){
return this.indentate(level) + '<' + key + attrStr + this.closeTag(key) + this.tagEndChar;
}else{
return this.indentate(level) + '<' + key + attrStr + '>' +
textValue +
'</' + key + this.tagEndChar;
}
}
}
Builder.prototype.replaceEntitiesValue = function(textValue){
if(textValue && textValue.length > 0 && this.options.processEntities){
for (let i=0; i<this.options.entities.length; i++) {
const entity = this.options.entities[i];
textValue = textValue.replace(entity.regex, entity.val);
}
}
return textValue;
}
function indentate(level) {
return this.options.indentBy.repeat(level);
}
function isAttribute(name /*, options*/) {
if (name.startsWith(this.options.attributeNamePrefix)) {
return name.substr(this.attrPrefixLen);
} else {
return false;
}
}
module.exports = Builder;