forked from i-e-b/node-easyxml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyxml-ts.js
227 lines (208 loc) · 7.03 KB
/
easyxml-ts.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
var et = require('elementtree'),
inflect = require('inflect'),
ElementTree = et.ElementTree,
element = et.Element,
subElement = et.SubElement;
/**
* This function merges two objects. Pretty simple stuff.
*/
function merge(obj1, obj2)
{
var obj3 = {};
for (var attr1 in obj1)
{
obj3[attr1] = obj1[attr1];
}
for (var attr2 in obj2)
{
obj3[attr2] = obj2[attr2];
}
return obj3;
}
/**
* This function pads a number so that it is two digits
*/
function zeroPadTen(val)
{
if (val < 10)
{
return "0" + val;
}
return val;
}
/**
* This is our main EasyXml object which gets exported as a module
*/
var EasyXml = function ()
{
var self = this;
/**
* Default configuration object
*/
self.config = {
singularizeChildren: true,
underscoreAttributes: true,
underscoreChar: '_',
rootElement: 'response',
dateFormat: 'ISO', // ISO = ISO8601, SQL = MySQL Timestamp, JS = (new Date).toString()
manifest: false,
unwrappedArrays: false,
schema: null,
indent: 4
};
/**
* Public
* Merges in the provided config object with the defaults
*/
self.configure = function (config)
{
// should be merge, otherwise we lose defaults
self.config = merge(self.config, config);
};
/**
* Public
* Takes an object and returns an XML string
*/
self.render = function (object, objectType, rootElementOverride)
{
var xml = element(rootElementOverride || self.config.rootElement);
parseChildElement(xml, object, objectType);
return new ElementTree(xml).write({
xml_declaration: self.config.manifest,
indent: self.config.indent
});
};
/**
* Recursive, Private
* Takes an object and calls appropriate function to generate xml node in document
*/
function parseChildElement(parentXmlNode, parentObjectNode, objectType)
{
if (self.config.schema && objectType)
{
var schema = self.config.schema[objectType];
var elementNumber = 0;
var elementInfo = schema[elementNumber.toString()];
while (elementInfo)
{
processChildElement(elementInfo.ElementName, parentXmlNode, parentObjectNode, elementInfo.Type);
elementNumber++;
elementInfo = schema[elementNumber.toString()];
}
}
else
{
for (var key in parentObjectNode)
{
processChildElement(key, parentXmlNode, parentObjectNode, objectType);
}
}
}
/**
* Recursive, Private
* Takes an object and attaches it to the XML doc
*/
function processChildElement(key, parentXmlNode, parentObjectNode, objectType)
{
if (!parentObjectNode.hasOwnProperty(key)){
return;
}
var child = parentObjectNode[key];
var isAttribute = function (slf)
{
return (slf.config.underscoreAttributes && key.charAt(0) === self.config.underscoreChar);
};
var el = null;
if (!isAttribute(self))
el = subElement(parentXmlNode, key);
if ((!self.config.singularizeChildren && !self.config.unwrappedArrays) && typeof parentXmlNode === 'object' && typeof child === 'object')
{
for (var innerKey in child)
{
if (typeof child[innerKey] === 'object')
{
parseChildElement(el, child[innerKey], objectType);
} else
{
el = subElement(el, innerKey);
el.text = child[innerKey].toString();
}
}
} else if (isAttribute(self))
{
if (typeof child === 'string' || typeof child === 'number')
{
if (key === self.config.underscoreChar)
parentXmlNode.text = child;
else
parentXmlNode.set(key.substring(1), child);
} else
{
throw new Error(key + "contained non_string_attribute");
}
} else if (child === null)
{
el.text = ""
} else if (typeof child === 'object' && child.constructor && child.constructor.name && child.constructor.name === 'Date')
{
// Date
if (self.config.dateFormat === 'ISO')
{
// ISO: YYYY-MM-DDTHH:MM:SS.mmmZ
el.text = child.toISOString();
} else if (self.config.dateFormat === 'SQL')
{
// SQL: YYYY-MM-DD HH:MM:SS
var yyyy = child.getFullYear();
var mm = zeroPadTen(child.getMonth() + 1);
var dd = zeroPadTen(child.getDate());
var hh = zeroPadTen(child.getHours());
var min = zeroPadTen(child.getMinutes());
var ss = zeroPadTen(child.getSeconds());
el.text = [yyyy, '-', mm, '-', dd, ' ', hh, ':', min, ':', ss].join("");
} else if (self.config.dateFormat === 'JS')
{
// JavaScript date format
el.text = child.toString();
} else
{
throw new Error(key + "contained unknown_date_format");
}
} else if (typeof child === 'object' && child.constructor && child.constructor.name && child.constructor.name === 'Array')
{
// Array
var subElementName = inflect.singularize(key);
for (var key2 in child)
{
// if unwrapped arrays, make new subelements on the parent.
var el2 = (self.config.unwrappedArrays === true) ? (subElement(parentXmlNode, subElementName)) : (subElement(el, subElementName));
// Check type of child element
if (child.hasOwnProperty(key2) && typeof child[key2] === 'object')
{
parseChildElement(el2, child[key2], objectType);
} else
{
// Just add element directly without parsing
el2.text = child[key2].toString();
}
}
// if unwrapped arrays, the initial child element has been consumed:
if (self.config.unwrappedArrays === true)
{
parentXmlNode.delItem(parentXmlNode._children.indexOf(el));
el = undefined;
}
} else if (typeof child === 'object')
{
// Object, go deeper
parseChildElement(el, child, objectType);
} else if (typeof child === 'number' || typeof child === 'boolean')
{
el.text = child.toString();
} else if (typeof child === 'string')
{
el.text = child;
}
}
};
module.exports = new EasyXml();