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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
|
/*
JavaScript XML Library
Plus a bunch of object utility functions
Usage:
var XML = require('pixl-xml');
var myxmlstring = '<?xml version="1.0"?><Document>' +
'<Simple>Hello</Simple>' +
'<Node Key="Value">Content</Node>' +
'</Document>';
var tree = XML.parse( myxmlstring, { preserveAttributes: true });
console.log( tree );
tree.Simple = "Hello2";
tree.Node._Attribs.Key = "Value2";
tree.Node._Data = "Content2";
tree.New = "I added this";
console.log( XML.stringify( tree, 'Document' ) );
Copyright (c) 2004 - 2015 Joseph Huckaby
Released under the MIT License
This version is for Node.JS, converted in 2012.
*/
var fs = require('fs');
var indent_string = "\t";
var xml_header = '<?xml version="1.0"?>';
var sort_args = null;
var re_valid_tag_name = /^\w[\w\-\:]*$/;
var XML = exports.XML = function XML(args) {
// class constructor for XML parser class
// pass in args hash or text to parse
if (!args) args = '';
if (isa_hash(args)) {
for (var key in args) this[key] = args[key];
}
else this.text = args || '';
// stringify buffers
if (this.text instanceof Buffer) {
this.text = this.text.toString();
}
if (!this.text.match(/^\s*</)) {
// try as file path
var file = this.text;
this.text = fs.readFileSync(file, { encoding: 'utf8' });
if (!this.text) throw new Error("File not found: " + file);
}
this.tree = {};
this.errors = [];
this.piNodeList = [];
this.dtdNodeList = [];
this.documentNodeName = '';
if (this.lowerCase) {
this.attribsKey = this.attribsKey.toLowerCase();
this.dataKey = this.dataKey.toLowerCase();
}
this.patTag.lastIndex = 0;
if (this.text) this.parse();
}
XML.prototype.preserveAttributes = false;
XML.prototype.lowerCase = false;
XML.prototype.patTag = /([^<]*?)<([^>]+)>/g;
XML.prototype.patSpecialTag = /^\s*([\!\?])/;
XML.prototype.patPITag = /^\s*\?/;
XML.prototype.patCommentTag = /^\s*\!--/;
XML.prototype.patDTDTag = /^\s*\!DOCTYPE/;
XML.prototype.patCDATATag = /^\s*\!\s*\[\s*CDATA/;
XML.prototype.patStandardTag = /^\s*(\/?)([\w\-\:\.]+)\s*(.*)$/;
XML.prototype.patSelfClosing = /\/\s*$/;
XML.prototype.patAttrib = new RegExp("([\\w\\-\\:\\.]+)\\s*=\\s*([\\\"\\'])([^\\2]*?)\\2", "g");
XML.prototype.patPINode = /^\s*\?\s*([\w\-\:]+)\s*(.*)$/;
XML.prototype.patEndComment = /--$/;
XML.prototype.patNextClose = /([^>]*?)>/g;
XML.prototype.patExternalDTDNode = new RegExp("^\\s*\\!DOCTYPE\\s+([\\w\\-\\:]+)\\s+(SYSTEM|PUBLIC)\\s+\\\"([^\\\"]+)\\\"");
XML.prototype.patInlineDTDNode = /^\s*\!DOCTYPE\s+([\w\-\:]+)\s+\[/;
XML.prototype.patEndDTD = /\]$/;
XML.prototype.patDTDNode = /^\s*\!DOCTYPE\s+([\w\-\:]+)\s+\[(.*)\]/;
XML.prototype.patEndCDATA = /\]\]$/;
XML.prototype.patCDATANode = /^\s*\!\s*\[\s*CDATA\s*\[([^]*)\]\]/;
XML.prototype.attribsKey = '_Attribs';
XML.prototype.dataKey = '_Data';
XML.prototype.parse = function(branch, name) {
// parse text into XML tree, recurse for nested nodes
if (!branch) branch = this.tree;
if (!name) name = null;
var foundClosing = false;
var matches = null;
// match each tag, plus preceding text
while ( matches = this.patTag.exec(this.text) ) {
var before = matches[1];
var tag = matches[2];
// text leading up to tag = content of parent node
if (before.match(/\S/)) {
if (typeof(branch[this.dataKey]) != 'undefined') branch[this.dataKey] += ' '; else branch[this.dataKey] = '';
branch[this.dataKey] += trim(decode_entities(before));
}
// parse based on tag type
if (tag.match(this.patSpecialTag)) {
// special tag
if (tag.match(this.patPITag)) tag = this.parsePINode(tag);
else if (tag.match(this.patCommentTag)) tag = this.parseCommentNode(tag);
else if (tag.match(this.patDTDTag)) tag = this.parseDTDNode(tag);
else if (tag.match(this.patCDATATag)) {
tag = this.parseCDATANode(tag);
if (typeof(branch[this.dataKey]) != 'undefined') branch[this.dataKey] += ' '; else branch[this.dataKey] = '';
branch[this.dataKey] += trim(decode_entities(tag));
} // cdata
else {
this.throwParseError( "Malformed special tag", tag );
break;
} // error
if (tag == null) break;
continue;
} // special tag
else {
// Tag is standard, so parse name and attributes (if any)
var matches = tag.match(this.patStandardTag);
if (!matches) {
this.throwParseError( "Malformed tag", tag );
break;
}
var closing = matches[1];
var nodeName = this.lowerCase ? matches[2].toLowerCase() : matches[2];
var attribsRaw = matches[3];
// If this is a closing tag, make sure it matches its opening tag
if (closing) {
if (nodeName == (name || '')) {
foundClosing = 1;
break;
}
else {
this.throwParseError( "Mismatched closing tag (expected </" + name + ">)", tag );
break;
}
} // closing tag
else {
// Not a closing tag, so parse attributes into hash. If tag
// is self-closing, no recursive parsing is needed.
var selfClosing = !!attribsRaw.match(this.patSelfClosing);
var leaf = {};
var attribs = leaf;
// preserve attributes means they go into a sub-hash named "_Attribs"
// the XML composer honors this for restoring the tree back into XML
if (this.preserveAttributes) {
leaf[this.attribsKey] = {};
attribs = leaf[this.attribsKey];
}
// parse attributes
this.patAttrib.lastIndex = 0;
while ( matches = this.patAttrib.exec(attribsRaw) ) {
var key = this.lowerCase ? matches[1].toLowerCase() : matches[1];
attribs[ key ] = decode_entities( matches[3] );
} // foreach attrib
// if no attribs found, but we created the _Attribs subhash, clean it up now
if (this.preserveAttributes && !num_keys(attribs)) {
delete leaf[this.attribsKey];
}
// Recurse for nested nodes
if (!selfClosing) {
this.parse( leaf, nodeName );
if (this.error()) break;
}
// Compress into simple node if text only
var num_leaf_keys = num_keys(leaf);
if ((typeof(leaf[this.dataKey]) != 'undefined') && (num_leaf_keys == 1)) {
leaf = leaf[this.dataKey];
}
else if (!num_leaf_keys) {
leaf = '';
}
// Add leaf to parent branch
if (typeof(branch[nodeName]) != 'undefined') {
if (isa_array(branch[nodeName])) {
branch[nodeName].push( leaf );
}
else {
var temp = branch[nodeName];
branch[nodeName] = [ temp, leaf ];
}
}
else {
branch[nodeName] = leaf;
}
if (this.error() || (branch == this.tree)) break;
} // not closing
} // standard tag
} // main reg exp
// Make sure we found the closing tag
if (name && !foundClosing) {
this.throwParseError( "Missing closing tag (expected </" + name + ">)", name );
}
// If we are the master node, finish parsing and setup our doc node
if (branch == this.tree) {
if (typeof(this.tree[this.dataKey]) != 'undefined') delete this.tree[this.dataKey];
if (num_keys(this.tree) > 1) {
this.throwParseError( 'Only one top-level node is allowed in document', first_key(this.tree) );
return;
}
this.documentNodeName = first_key(this.tree);
if (this.documentNodeName) {
this.tree = this.tree[this.documentNodeName];
}
}
};
XML.prototype.throwParseError = function(key, tag) {
// log error and locate current line number in source XML document
var parsedSource = this.text.substring(0, this.patTag.lastIndex);
var eolMatch = parsedSource.match(/\n/g);
var lineNum = (eolMatch ? eolMatch.length : 0) + 1;
lineNum -= tag.match(/\n/) ? tag.match(/\n/g).length : 0;
this.errors.push({
type: 'Parse',
key: key,
text: '<' + tag + '>',
line: lineNum
});
// Throw actual error (must wrap parse in try/catch)
throw new Error( this.getLastError() );
};
XML.prototype.error = function() {
// return number of errors
return this.errors.length;
};
XML.prototype.getError = function(error) {
// get formatted error
var text = '';
if (!error) return '';
text = (error.type || 'General') + ' Error';
if (error.code) text += ' ' + error.code;
text += ': ' + error.key;
if (error.line) text += ' on line ' + error.line;
if (error.text) text += ': ' + error.text;
return text;
};
XML.prototype.getLastError = function() {
// Get most recently thrown error in plain text format
if (!this.error()) return '';
return this.getError( this.errors[this.errors.length - 1] );
};
XML.prototype.parsePINode = function(tag) {
// Parse Processor Instruction Node, e.g. <?xml version="1.0"?>
if (!tag.match(this.patPINode)) {
this.throwParseError( "Malformed processor instruction", tag );
return null;
}
this.piNodeList.push( tag );
return tag;
};
XML.prototype.parseCommentNode = function(tag) {
// Parse Comment Node, e.g. <!-- hello -->
var matches = null;
this.patNextClose.lastIndex = this.patTag.lastIndex;
while (!tag.match(this.patEndComment)) {
if (matches = this.patNextClose.exec(this.text)) {
tag += '>' + matches[1];
}
else {
this.throwParseError( "Unclosed comment tag", tag );
return null;
}
}
this.patTag.lastIndex = this.patNextClose.lastIndex;
return tag;
};
XML.prototype.parseDTDNode = function(tag) {
// Parse Document Type Descriptor Node, e.g. <!DOCTYPE ... >
var matches = null;
if (tag.match(this.patExternalDTDNode)) {
// tag is external, and thus self-closing
this.dtdNodeList.push( tag );
}
else if (tag.match(this.patInlineDTDNode)) {
// Tag is inline, so check for nested nodes.
this.patNextClose.lastIndex = this.patTag.lastIndex;
while (!tag.match(this.patEndDTD)) {
if (matches = this.patNextClose.exec(this.text)) {
tag += '>' + matches[1];
}
else {
this.throwParseError( "Unclosed DTD tag", tag );
return null;
}
}
this.patTag.lastIndex = this.patNextClose.lastIndex;
// Make sure complete tag is well-formed, and push onto DTD stack.
if (tag.match(this.patDTDNode)) {
this.dtdNodeList.push( tag );
}
else {
this.throwParseError( "Malformed DTD tag", tag );
return null;
}
}
else {
this.throwParseError( "Malformed DTD tag", tag );
return null;
}
return tag;
};
XML.prototype.parseCDATANode = function(tag) {
// Parse CDATA Node, e.g. <![CDATA[Brooks & Shields]]>
var matches = null;
this.patNextClose.lastIndex = this.patTag.lastIndex;
while (!tag.match(this.patEndCDATA)) {
if (matches = this.patNextClose.exec(this.text)) {
tag += '>' + matches[1];
}
else {
this.throwParseError( "Unclosed CDATA tag", tag );
return null;
}
}
this.patTag.lastIndex = this.patNextClose.lastIndex;
if (matches = tag.match(this.patCDATANode)) {
return matches[1];
}
else {
this.throwParseError( "Malformed CDATA tag", tag );
return null;
}
};
XML.prototype.getTree = function() {
// get reference to parsed XML tree
return this.tree;
};
XML.prototype.compose = function() {
// compose tree back into XML
var raw = compose_xml( this.tree, this.documentNodeName );
var body = raw.substring( raw.indexOf("\n") + 1, raw.length );
var xml = '';
if (this.piNodeList.length) {
for (var idx = 0, len = this.piNodeList.length; idx < len; idx++) {
xml += '<' + this.piNodeList[idx] + '>' + "\n";
}
}
else {
xml += xml_header + "\n";
}
if (this.dtdNodeList.length) {
for (var idx = 0, len = this.dtdNodeList.length; idx < len; idx++) {
xml += '<' + this.dtdNodeList[idx] + '>' + "\n";
}
}
xml += body;
return xml;
};
//
// Static Utility Functions:
//
var parse_xml = exports.parse = 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();
};
var trim = exports.trim = 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;
};
var encode_entities = exports.encodeEntities = 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;
};
var encode_attrib_entities = exports.encodeAttribEntities = 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;
};
var decode_entities = exports.decodeEntities = 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;
};
var compose_xml = exports.stringify = function compose_xml(node, name, indent) {
// Compose node into XML including attributes
// Recurse for child nodes
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 + "\n";
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 = hash_keys_to_array(node["_Attribs"]).sort();
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 + ">\n";
} // just text
else {
xml += "\n";
var sorted_keys = hash_keys_to_array(node).sort();
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 );
} // not _Attribs key
} // foreach key
xml += indent_text + "</" + name + ">\n";
} // real children
}
else {
// no child elements, so self-close
xml += "/>\n";
}
} // 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 );
}
} // array of nodes
} // complex node
else {
// node is simple string
xml += indent_text + "<" + name + ">" + encode_entities(node) + "</" + name + ">\n";
} // simple text node
return xml;
};
var always_array = exports.alwaysArray = 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;
}
};
var hash_keys_to_array = exports.hashKeysToArray = 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;
};
var isa_hash = exports.isaHash = function isa_hash(arg) {
// determine if arg is a hash
return( !!arg && (typeof(arg) == 'object') && (typeof(arg.length) == 'undefined') );
};
var isa_array = exports.isaArray = function isa_array(arg) {
// determine if arg is an array or is array-like
if (typeof(arg) == 'array') return true;
return( !!arg && (typeof(arg) == 'object') && (typeof(arg.length) != 'undefined') );
};
var first_key = exports.firstKey = function first_key(hash) {
// return first key from hash (unordered)
for (var key in hash) return key;
return null; // no keys in hash
};
var num_keys = exports.numKeys = function num_keys(hash) {
// count the number of keys in a hash
var count = 0;
for (var a in hash) count++;
return count;
};
|