forked from michaelkourlas/node-js2xmlparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjs2xmlparser.js
More file actions
382 lines (339 loc) · 15.1 KB
/
Copy pathjs2xmlparser.js
File metadata and controls
382 lines (339 loc) · 15.1 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/* jshint node:true */
/**
* js2xmlparser
* Copyright © 2012 Michael Kourlas and other contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
(function () {
"use strict";
var xmlDeclaration = true;
var xmlVersion = "1.0";
var xmlEncoding = "UTF-8";
var attributeString = "@";
var aliasString = "=";
var valueString = "#";
var prettyPrinting = true;
var indentString = "\t";
var convertMap = {};
var arrayMap = {};
var useCDATA = false;
module.exports = function (root, data, options) {
return toXML(init(root, data, options));
};
// Initialization
var init = function (root, data, options) {
// Set option defaults
setOptionDefaults();
// Error checking for root element
if (typeof root !== "string") {
throw new Error("root element must be a string");
}
else if (root === "") {
throw new Error("root element cannot be empty");
}
// Error checking and variable initialization for options
if (typeof options === "object" && options !== null) {
if ("declaration" in options) {
if ("include" in options.declaration) {
if (typeof options.declaration.include === "boolean") {
xmlDeclaration = options.declaration.include;
}
else {
throw new Error("declaration.include option must be a boolean");
}
}
if ("encoding" in options.declaration) {
if (typeof options.declaration.encoding === "string" || options.declaration.encoding === null) {
xmlEncoding = options.declaration.encoding;
}
else {
throw new Error("declaration.encoding option must be a string or null");
}
}
}
if ("attributeString" in options) {
if (typeof options.attributeString === "string") {
attributeString = options.attributeString;
}
else {
throw new Error("attributeString option must be a string");
}
}
if ("valueString" in options) {
if (typeof options.valueString === "string") {
valueString = options.valueString;
}
else {
throw new Error("valueString option must be a string");
}
}
if ("aliasString" in options) {
if (typeof options.aliasString === "string") {
aliasString = options.aliasString;
}
else {
throw new Error("aliasString option must be a string");
}
}
if ("prettyPrinting" in options) {
if ("enabled" in options.prettyPrinting) {
if (typeof options.prettyPrinting.enabled === "boolean") {
prettyPrinting = options.prettyPrinting.enabled;
}
else {
throw new Error("prettyPrinting.enabled option must be a boolean");
}
}
if ("indentString" in options.prettyPrinting) {
if (typeof options.prettyPrinting.indentString === "string") {
indentString = options.prettyPrinting.indentString;
}
else {
throw new Error("prettyPrinting.indentString option must be a string");
}
}
}
if ("convertMap" in options) {
if (Object.prototype.toString.call(options.convertMap) === "[object Object]") {
convertMap = options.convertMap;
}
else {
throw new Error("convertMap option must be an object");
}
}
if ("arrayMap" in options) {
if (Object.prototype.toString.call(options.arrayMap) === "[object Object]") {
arrayMap = options.arrayMap;
}
else {
throw new Error("arrayMap option must be an object");
}
}
if ("useCDATA" in options) {
if (typeof options.useCDATA === "boolean") {
useCDATA = options.useCDATA;
}
else {
throw new Error("useCDATA option must be a boolean");
}
}
}
// Error checking and variable initialization for data
if (typeof data !== "string" && typeof data !== "object" && typeof data !== "number" &&
typeof data !== "boolean" && data !== null) {
throw new Error("data must be an object (excluding arrays) or a JSON string");
}
if (data === null) {
throw new Error("data must be an object (excluding arrays) or a JSON string");
}
if (Object.prototype.toString.call(data) === "[object Array]" && !(arrayMap && arrayMap[root])) {
throw new Error("data must be an object (excluding arrays) or a JSON string, unless an arrayMap option exists for root");
}
if (typeof data === "string") {
data = JSON.parse(data);
}
var tempData = {};
tempData[root] = data; // Add root element to object
return tempData;
};
// Convert object to XML
var toXML = function (object) {
// Initialize arguments, if necessary
var xml = arguments[1] || "";
var level = arguments[2] || 0;
var i = null;
var tempObject = {};
for (var property in object) {
if (object.hasOwnProperty(property)) {
// Element name cannot start with a number
var elementName = property;
if (/^\d/.test(property)) {
elementName = "_" + property;
}
// Skip alias string property
if (elementName === aliasString) {
continue;
}
// When alias string property is present, use as alias for element name
if (Object.prototype.toString.call(object[property]) === "[object Object]" &&
aliasString in object[property]) {
elementName = object[property][aliasString];
}
// Arrays
if (Object.prototype.toString.call(object[property]) === "[object Array]") {
// Wrap array with outer tag if arrayMap is used
if (arrayMap[property]) {
xml += addIndent("<" + elementName, level);
xml += addBreak(">");
}
// Create separate XML elements for each array element
for (i = 0; i < object[property].length; i++) {
tempObject = {};
var newLevel = level;
// When arrayMap is used, use the arrayMap tag instead and increment level
if (arrayMap[property]) {
tempObject[arrayMap[property]] = object[property][i];
newLevel = level + 1;
} else {
tempObject[property] = object[property][i];
}
xml = toXML(tempObject, xml, newLevel);
}
// Wrap array with outer tag if arrayMap is used
if (arrayMap[property]) {
xml += addBreak(addIndent("</" + elementName + ">", level));
}
}
// JSON-type objects with properties
else if (Object.prototype.toString.call(object[property]) === "[object Object]") {
xml += addIndent("<" + elementName, level);
// Add attributes
var lengthExcludingAttributes = Object.keys(object[property]).length;
if (Object.prototype.toString.call(object[property][attributeString]) === "[object Object]") {
lengthExcludingAttributes -= 1;
for (var attribute in object[property][attributeString]) {
if (object[property][attributeString].hasOwnProperty(attribute)) {
xml += " " + attribute + "=\"" +
toString(object[property][attributeString][attribute], true) + "\"";
}
}
}
else if (typeof object[property][attributeString] !== "undefined") {
// Fix for the case where an object contains a single property with the attribute string as its
// name, but this property contains no attributes; in that case, lengthExcludingAttributes
// should be set to zero to ensure that the object is considered an empty object for the
// purposes of the following if statement.
lengthExcludingAttributes -= 1;
}
if (lengthExcludingAttributes === 0) { // Empty object
xml += addBreak("/>");
}
else if ((lengthExcludingAttributes === 1 ||
(lengthExcludingAttributes === 2 && aliasString in object[property])) &&
valueString in object[property]) { // Value string only
xml += addBreak(">" + toString(object[property][valueString], false) + "</" + elementName +
">");
}
else { // Object with properties
xml += addBreak(">");
// Create separate object for each property and pass to this function
for (var subProperty in object[property]) {
if (object[property].hasOwnProperty(subProperty) && subProperty !== attributeString &&
subProperty !== valueString) {
tempObject = {};
tempObject[subProperty] = object[property][subProperty];
xml = toXML(tempObject, xml, level + 1);
}
}
xml += addBreak(addIndent("</" + elementName + ">", level));
}
}
// Everything else
else {
xml += addBreak(addIndent("<" + elementName + ">" + toString(object[property], false) + "</" +
elementName + ">", level));
}
}
}
// Finalize XML at end of process
if (level === 0) {
// Strip trailing whitespace
xml = xml.replace(/\s+$/g, "");
// Add XML declaration
if (xmlDeclaration) {
if (xmlEncoding === null) {
xml = addBreak("<?xml version=\"" + xmlVersion + "\"?>") + xml;
}
else {
xml = addBreak("<?xml version=\"" + xmlVersion + "\" encoding=\"" + xmlEncoding + "\"?>") + xml;
}
}
}
return xml;
};
// Add indenting to data for pretty printing
var addIndent = function (data, level) {
if (prettyPrinting) {
var indent = "";
for (var i = 0; i < level; i++) {
indent += indentString;
}
data = indent + data;
}
return data;
};
// Add line break to data for pretty printing
var addBreak = function (data) {
return prettyPrinting ? data + "\n" : data;
};
// Convert anything into a valid XML string representation
var toString = function (data, isAttribute) {
// Recursive function used to handle nested functions
var functionHelper = function (data) {
if (Object.prototype.toString.call(data) === "[object Function]") {
return functionHelper(data());
}
else {
return data;
}
};
// Convert map
if (Object.prototype.toString.call(data) in convertMap) {
data = convertMap[Object.prototype.toString.call(data)](data);
}
else if ("*" in convertMap) {
data = convertMap["*"](data);
}
// Functions
else if (Object.prototype.toString.call(data) === "[object Function]") {
data = functionHelper(data());
}
// Empty objects
else if (Object.prototype.toString.call(data) === "[object Object]" && Object.keys(data).length === 0) {
data = "";
}
// Cast data to string
if (typeof data !== "string") {
data = (data === null || typeof data === "undefined") ? "" : data.toString();
}
// Output as CDATA instead of escaping if option set (and only if not an attribute value)
if (useCDATA && !isAttribute) {
data = "<![CDATA[" + data.replace(/]]>/gm, "]]]]><![CDATA[>") + "]]>";
}
else {
// Escape illegal XML characters
data = data.replace(/&/gm, "&")
.replace(/</gm, "<")
.replace(/>/gm, ">")
.replace(/"/gm, """)
.replace(/'/gm, "'");
}
return data;
};
// Revert options back to their default settings
var setOptionDefaults = function () {
useCDATA = false;
convertMap = {};
arrayMap = {};
xmlDeclaration = true;
xmlVersion = "1.0";
xmlEncoding = "UTF-8";
attributeString = "@";
aliasString = "=";
valueString = "#";
prettyPrinting = true;
indentString = "\t";
};
})();