-
Notifications
You must be signed in to change notification settings - Fork 1
/
xmlFunctions.js
231 lines (194 loc) · 6.89 KB
/
xmlFunctions.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
function containString(nameObjectXML, nodeName) {
for (var i = 0; i < nameObjectXML.length; i++) {
if (nameObjectXML[i] == nodeName) {
return true;
}
}
return false;
}
function parse_xml(text, opts) {
// turn text into XML tree quickly
if (!opts) opts = {};
opts.text = text;
var parser = new XML(opts);
return parser.error() ? parser.getLastError() : parser.getTree();
};
function trim(text) {
// strip whitespace from beginning and end of string
if (text == null) return '';
if (text && text.replace) {
text = text.replace(/^\s+/, "");
text = text.replace(/\s+$/, "");
}
return text;
};
function encode_entities(text) {
// Simple entitize exports.for = function for composing XML
if (text == null) return '';
if (text && text.replace) {
text = text.replace(/\&/g, "&"); // MUST BE FIRST
text = text.replace(/</g, "<");
text = text.replace(/>/g, ">");
}
return text;
};
function encode_attrib_entities(text) {
// Simple entitize exports.for = function for composing XML attributes
if (text == null) return '';
if (text && text.replace) {
text = text.replace(/\&/g, "&"); // MUST BE FIRST
text = text.replace(/</g, "<");
text = text.replace(/>/g, ">");
text = text.replace(/\"/g, """);
text = text.replace(/\'/g, "'");
}
return text;
};
function decode_entities(text) {
// Decode XML entities into raw ASCII
if (text == null) return '';
if (text && text.replace && text.match(/\&/)) {
text = text.replace(/\<\;/g, "<");
text = text.replace(/\>\;/g, ">");
text = text.replace(/\"\;/g, '"');
text = text.replace(/\&apos\;/g, "'");
text = text.replace(/\&\;/g, "&"); // MUST BE LAST
}
return text;
};
function compose_xml(node, name, indent, indent_string, eol, sort) {
// Compose node into XML including attributes
// Recurse for child nodes
if (typeof (indent_string) == 'undefined') indent_string = "\t";
if (typeof (eol) == 'undefined') eol = "\n";
if (typeof (sort) == 'undefined') sort = true;
var xml = "";
// If this is the root node, set the indent to 0
// and setup the XML header (PI node)
if (!indent) {
indent = 0;
xml = xml_header + eol;
if (!name) {
// no name provided, assume content is wrapped in it
name = first_key(node);
node = node[name];
}
}
// Setup the indent text
var indent_text = "";
for (var k = 0; k < indent; k++) indent_text += indent_string;
if ((typeof (node) == 'object') && (node != null)) {
// node is object -- now see if it is an array or hash
if (!node.length) { // what about zero-length array?
// node is hash
xml += indent_text + "<" + name;
var num_keys = 0;
var has_attribs = 0;
for (var key in node) num_keys++; // there must be a better way...
if (node["_Attribs"]) {
has_attribs = 1;
var sorted_keys = sort ? hash_keys_to_array(node["_Attribs"]).sort() : hash_keys_to_array(node["_Attribs"]);
for (var idx = 0, len = sorted_keys.length; idx < len; idx++) {
var key = sorted_keys[idx];
xml += " " + key + "=\"" + encode_attrib_entities(node["_Attribs"][key]) + "\"";
}
} // has attribs
if (num_keys > has_attribs) {
// has child elements
xml += ">";
if (node["_Data"]) {
// simple text child node
xml += encode_entities(node["_Data"]) + "</" + name + ">" + eol;
} // just text
else {
xml += eol;
var sorted_keys = sort ? hash_keys_to_array(node).sort() : hash_keys_to_array(node);
for (var idx = 0, len = sorted_keys.length; idx < len; idx++) {
var key = sorted_keys[idx];
if ((key != "_Attribs") && key.match(re_valid_tag_name)) {
// recurse for node, with incremented indent value
xml += compose_xml(node[key], key, indent + 1, indent_string, eol, sort);
} // not _Attribs key
} // foreach key
xml += indent_text + "</" + name + ">" + eol;
} // real children
}
else {
// no child elements, so self-close
xml += "/>" + eol;
}
} // standard node
else {
// node is array
for (var idx = 0; idx < node.length; idx++) {
// recurse for node in array with same indent
xml += compose_xml(node[idx], name, indent, indent_string, eol, sort);
}
} // array of nodes
} // complex node
else {
// node is simple string
xml += indent_text + "<" + name + ">" + encode_entities(node) + "</" + name + ">" + eol;
} // simple text node
return xml;
};
function always_array(obj, key) {
// if object is not array, return array containing object
// if key is passed, work like XMLalwaysarray() instead
if (key) {
if ((typeof (obj[key]) != 'object') || (typeof (obj[key].length) == 'undefined')) {
var temp = obj[key];
delete obj[key];
obj[key] = new Array();
obj[key][0] = temp;
}
return null;
}
else {
if ((typeof (obj) != 'object') || (typeof (obj.length) == 'undefined')) { return [obj]; }
else return obj;
}
};
function hash_keys_to_array(hash) {
// convert hash keys to array (discard values)
var array = [];
for (var key in hash) array.push(key);
return array;
};
function isa_array(arg) {
// determine if arg is an array or is array-like
return Array.isArray(arg);
};
function isa_hash(arg) {
// determine if arg is a hash
return (!!arg && (typeof (arg) == 'object') && !isa_array(arg));
};
function first_key(hash) {
// return first key from hash (unordered)
for (var key in hash) return key;
return null; // no keys in hash
};
function num_keys(hash) {
// count the number of keys in a hash
var count = 0;
for (var a in hash) count++;
return count;
};
function getAttribute(tag, attribute) {
for (var i = 0; i < tag.attributes.length; i++) {
if (tag.attributes[i].nodeName == attribute) {
return tag.attributes[i].value;
}
}
return '0';
}
module.exports = {
isa_hash: isa_hash,
isa_array: isa_array,
containString: containString,
decode_entities: decode_entities,
num_keys: num_keys,
first_key: first_key,
getAttribute: getAttribute,
trim: trim,
}