diff options
Diffstat (limited to 'node_modules/xml2js/lib/xml2js.js')
-rw-r--r-- | node_modules/xml2js/lib/xml2js.js | 333 |
1 files changed, 220 insertions, 113 deletions
diff --git a/node_modules/xml2js/lib/xml2js.js b/node_modules/xml2js/lib/xml2js.js index 7c1cad319..26e34e76b 100644 --- a/node_modules/xml2js/lib/xml2js.js +++ b/node_modules/xml2js/lib/xml2js.js @@ -1,9 +1,10 @@ -// Generated by CoffeeScript 1.7.1 +// Generated by CoffeeScript 1.10.0 (function() { - var bom, builder, events, isEmpty, processName, processors, sax, - __hasProp = {}.hasOwnProperty, - __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, - __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; + "use strict"; + var bom, builder, escapeCDATA, events, isEmpty, processName, processors, requiresCDATA, sax, setImmediate, wrapCDATA, + extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + hasProp = {}.hasOwnProperty, + bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; sax = require('sax'); @@ -15,19 +16,33 @@ processors = require('./processors'); + setImmediate = require('timers').setImmediate; + isEmpty = function(thing) { return typeof thing === "object" && (thing != null) && Object.keys(thing).length === 0; }; processName = function(processors, processedName) { - var process, _i, _len; - for (_i = 0, _len = processors.length; _i < _len; _i++) { - process = processors[_i]; + var i, len, process; + for (i = 0, len = processors.length; i < len; i++) { + process = processors[i]; processedName = process(processedName); } return processedName; }; + requiresCDATA = function(entry) { + return entry.indexOf('&') >= 0 || entry.indexOf('>') >= 0 || entry.indexOf('<') >= 0; + }; + + wrapCDATA = function(entry) { + return "<![CDATA[" + (escapeCDATA(entry)) + "]]>"; + }; + + escapeCDATA = function(entry) { + return entry.replace(']]>', ']]]]><![CDATA[>'); + }; + exports.processors = processors; exports.defaults = { @@ -47,10 +62,14 @@ explicitChildren: false, childkey: '@@', charsAsChildren: false, + includeWhiteChars: false, async: false, strict: true, attrNameProcessors: null, - tagNameProcessors: null + attrValueProcessors: null, + tagNameProcessors: null, + valueProcessors: null, + emptyTag: '' }, "0.2": { explicitCharkey: false, @@ -66,12 +85,16 @@ validator: null, xmlns: false, explicitChildren: false, + preserveChildrenOrder: false, childkey: '$$', charsAsChildren: false, + includeWhiteChars: false, async: false, strict: true, attrNameProcessors: null, + attrValueProcessors: null, tagNameProcessors: null, + valueProcessors: null, rootName: 'root', xmldec: { 'version': '1.0', @@ -84,12 +107,15 @@ 'indent': ' ', 'newline': '\n' }, - headless: false + headless: false, + chunkSize: 10000, + emptyTag: '', + cdata: false } }; - exports.ValidationError = (function(_super) { - __extends(ValidationError, _super); + exports.ValidationError = (function(superClass) { + extend(ValidationError, superClass); function ValidationError(message) { this.message = message; @@ -101,16 +127,16 @@ exports.Builder = (function() { function Builder(opts) { - var key, value, _ref; + var key, ref, value; this.options = {}; - _ref = exports.defaults["0.2"]; - for (key in _ref) { - if (!__hasProp.call(_ref, key)) continue; - value = _ref[key]; + ref = exports.defaults["0.2"]; + for (key in ref) { + if (!hasProp.call(ref, key)) continue; + value = ref[key]; this.options[key] = value; } for (key in opts) { - if (!__hasProp.call(opts, key)) continue; + if (!hasProp.call(opts, key)) continue; value = opts[key]; this.options[key] = value; } @@ -126,44 +152,66 @@ } else { rootName = this.options.rootName; } - render = function(element, obj) { - var attr, child, entry, index, key, value, _ref, _ref1; - if (typeof obj !== 'object') { - element.txt(obj); - } else { - for (key in obj) { - if (!__hasProp.call(obj, key)) continue; - child = obj[key]; - if (key === attrkey) { - if (typeof child === "object") { - for (attr in child) { - value = child[attr]; - element = element.att(attr, value); + render = (function(_this) { + return function(element, obj) { + var attr, child, entry, index, key, value; + if (typeof obj !== 'object') { + if (_this.options.cdata && requiresCDATA(obj)) { + element.raw(wrapCDATA(obj)); + } else { + element.txt(obj); + } + } else { + for (key in obj) { + if (!hasProp.call(obj, key)) continue; + child = obj[key]; + if (key === attrkey) { + if (typeof child === "object") { + for (attr in child) { + value = child[attr]; + element = element.att(attr, value); + } } - } - } else if (key === charkey) { - element = element.txt(child); - } else if (typeof child === 'object' && ((child != null ? child.constructor : void 0) != null) && ((child != null ? (_ref = child.constructor) != null ? _ref.name : void 0 : void 0) != null) && (child != null ? (_ref1 = child.constructor) != null ? _ref1.name : void 0 : void 0) === 'Array') { - for (index in child) { - if (!__hasProp.call(child, index)) continue; - entry = child[index]; - if (typeof entry === 'string') { - element = element.ele(key, entry).up(); + } else if (key === charkey) { + if (_this.options.cdata && requiresCDATA(child)) { + element = element.raw(wrapCDATA(child)); } else { - element = arguments.callee(element.ele(key), entry).up(); + element = element.txt(child); + } + } else if (Array.isArray(child)) { + for (index in child) { + if (!hasProp.call(child, index)) continue; + entry = child[index]; + if (typeof entry === 'string') { + if (_this.options.cdata && requiresCDATA(entry)) { + element = element.ele(key).raw(wrapCDATA(entry)).up(); + } else { + element = element.ele(key, entry).up(); + } + } else { + element = render(element.ele(key), entry).up(); + } + } + } else if (typeof child === "object") { + element = render(element.ele(key), child).up(); + } else { + if (typeof child === 'string' && _this.options.cdata && requiresCDATA(child)) { + element = element.ele(key).raw(wrapCDATA(child)).up(); + } else { + if (child == null) { + child = ''; + } + element = element.ele(key, child.toString()).up(); } } - } else if (typeof child === "object") { - element = arguments.callee(element.ele(key), child).up(); - } else { - element = element.ele(key, child.toString()).up(); } } - } - return element; - }; + return element; + }; + })(this); rootElement = builder.create(rootName, this.options.xmldec, this.options.doctype, { - headless: this.options.headless + headless: this.options.headless, + allowSurrogateChars: this.options.allowSurrogateChars }); return render(rootElement, rootObj).end(this.options.renderOpts); }; @@ -172,26 +220,27 @@ })(); - exports.Parser = (function(_super) { - __extends(Parser, _super); + exports.Parser = (function(superClass) { + extend(Parser, superClass); function Parser(opts) { - this.parseString = __bind(this.parseString, this); - this.reset = __bind(this.reset, this); - this.assignOrPush = __bind(this.assignOrPush, this); - var key, value, _ref; + this.parseString = bind(this.parseString, this); + this.reset = bind(this.reset, this); + this.assignOrPush = bind(this.assignOrPush, this); + this.processAsync = bind(this.processAsync, this); + var key, ref, value; if (!(this instanceof exports.Parser)) { return new exports.Parser(opts); } this.options = {}; - _ref = exports.defaults["0.2"]; - for (key in _ref) { - if (!__hasProp.call(_ref, key)) continue; - value = _ref[key]; + ref = exports.defaults["0.2"]; + for (key in ref) { + if (!hasProp.call(ref, key)) continue; + value = ref[key]; this.options[key] = value; } for (key in opts) { - if (!__hasProp.call(opts, key)) continue; + if (!hasProp.call(opts, key)) continue; value = opts[key]; this.options[key] = value; } @@ -207,6 +256,29 @@ this.reset(); } + Parser.prototype.processAsync = function() { + var chunk, err, error1; + try { + if (this.remaining.length <= this.options.chunkSize) { + chunk = this.remaining; + this.remaining = ''; + this.saxParser = this.saxParser.write(chunk); + return this.saxParser.close(); + } else { + chunk = this.remaining.substr(0, this.options.chunkSize); + this.remaining = this.remaining.substr(this.options.chunkSize, this.remaining.length); + this.saxParser = this.saxParser.write(chunk); + return setImmediate(this.processAsync); + } + } catch (error1) { + err = error1; + if (!this.saxParser.errThrown) { + this.saxParser.errThrown = true; + return this.emit(err); + } + } + }; + Parser.prototype.assignOrPush = function(obj, key, newValue) { if (!(key in obj)) { if (!this.options.explicitArray) { @@ -240,6 +312,14 @@ } }; })(this); + this.saxParser.onend = (function(_this) { + return function() { + if (!_this.saxParser.ended) { + _this.saxParser.ended = true; + return _this.emit("end", _this.resultObject); + } + }; + })(this); this.saxParser.ended = false; this.EXPLICIT_CHARKEY = this.options.explicitCharkey; this.resultObject = null; @@ -248,17 +328,17 @@ charkey = this.options.charkey; this.saxParser.onopentag = (function(_this) { return function(node) { - var key, newValue, obj, processedKey, _ref; + var key, newValue, obj, processedKey, ref; obj = {}; obj[charkey] = ""; if (!_this.options.ignoreAttrs) { - _ref = node.attributes; - for (key in _ref) { - if (!__hasProp.call(_ref, key)) continue; + ref = node.attributes; + for (key in ref) { + if (!hasProp.call(ref, key)) continue; if (!(attrkey in obj) && !_this.options.mergeAttrs) { obj[attrkey] = {}; } - newValue = node.attributes[key]; + newValue = _this.options.attrValueProcessors ? processName(_this.options.attrValueProcessors, node.attributes[key]) : node.attributes[key]; processedKey = _this.options.attrNameProcessors ? processName(_this.options.attrNameProcessors, key) : key; if (_this.options.mergeAttrs) { _this.assignOrPush(obj, processedKey, newValue); @@ -279,12 +359,16 @@ })(this); this.saxParser.onclosetag = (function(_this) { return function() { - var cdata, emptyStr, err, node, nodeName, obj, old, s, xpath; + var cdata, emptyStr, err, error1, key, node, nodeName, obj, objClone, old, s, xpath; obj = stack.pop(); nodeName = obj["#name"]; - delete obj["#name"]; - cdata = obj.cdata; - delete obj.cdata; + if (!_this.options.explicitChildren || !_this.options.preserveChildrenOrder) { + delete obj["#name"]; + } + if (obj.cdata === true) { + cdata = obj.cdata; + delete obj.cdata; + } s = stack[stack.length - 1]; if (obj[charkey].match(/^\s*$/) && !cdata) { emptyStr = obj[charkey]; @@ -296,44 +380,59 @@ if (_this.options.normalize) { obj[charkey] = obj[charkey].replace(/\s{2,}/g, " ").trim(); } + obj[charkey] = _this.options.valueProcessors ? processName(_this.options.valueProcessors, obj[charkey]) : obj[charkey]; if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) { obj = obj[charkey]; } } if (isEmpty(obj)) { - obj = _this.options.emptyTag !== void 0 ? _this.options.emptyTag : emptyStr; + obj = _this.options.emptyTag !== '' ? _this.options.emptyTag : emptyStr; } if (_this.options.validator != null) { xpath = "/" + ((function() { - var _i, _len, _results; - _results = []; - for (_i = 0, _len = stack.length; _i < _len; _i++) { - node = stack[_i]; - _results.push(node["#name"]); + var i, len, results; + results = []; + for (i = 0, len = stack.length; i < len; i++) { + node = stack[i]; + results.push(node["#name"]); } - return _results; + return results; })()).concat(nodeName).join("/"); try { obj = _this.options.validator(xpath, s && s[nodeName], obj); - } catch (_error) { - err = _error; + } catch (error1) { + err = error1; _this.emit("error", err); } } if (_this.options.explicitChildren && !_this.options.mergeAttrs && typeof obj === 'object') { - node = {}; - if (_this.options.attrkey in obj) { - node[_this.options.attrkey] = obj[_this.options.attrkey]; - delete obj[_this.options.attrkey]; - } - if (!_this.options.charsAsChildren && _this.options.charkey in obj) { - node[_this.options.charkey] = obj[_this.options.charkey]; - delete obj[_this.options.charkey]; - } - if (Object.getOwnPropertyNames(obj).length > 0) { - node[_this.options.childkey] = obj; + if (!_this.options.preserveChildrenOrder) { + node = {}; + if (_this.options.attrkey in obj) { + node[_this.options.attrkey] = obj[_this.options.attrkey]; + delete obj[_this.options.attrkey]; + } + if (!_this.options.charsAsChildren && _this.options.charkey in obj) { + node[_this.options.charkey] = obj[_this.options.charkey]; + delete obj[_this.options.charkey]; + } + if (Object.getOwnPropertyNames(obj).length > 0) { + node[_this.options.childkey] = obj; + } + obj = node; + } else if (s) { + s[_this.options.childkey] = s[_this.options.childkey] || []; + objClone = {}; + for (key in obj) { + if (!hasProp.call(obj, key)) continue; + objClone[key] = obj[key]; + } + s[_this.options.childkey].push(objClone); + delete obj["#name"]; + if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) { + obj = obj[charkey]; + } } - obj = node; } if (stack.length > 0) { return _this.assignOrPush(s, nodeName, obj); @@ -351,10 +450,21 @@ })(this); ontext = (function(_this) { return function(text) { - var s; + var charChild, s; s = stack[stack.length - 1]; if (s) { s[charkey] += text; + if (_this.options.explicitChildren && _this.options.preserveChildrenOrder && _this.options.charsAsChildren && (_this.options.includeWhiteChars || text.replace(/\\n/g, '').trim() !== '')) { + s[_this.options.childkey] = s[_this.options.childkey] || []; + charChild = { + '#name': '__text__' + }; + charChild[charkey] = text; + if (_this.options.normalize) { + charChild[charkey] = charChild[charkey].replace(/\s{2,}/g, " ").trim(); + } + s[_this.options.childkey].push(charChild); + } return s; } }; @@ -372,40 +482,37 @@ }; Parser.prototype.parseString = function(str, cb) { - var err; + var err, error1; if ((cb != null) && typeof cb === "function") { this.on("end", function(result) { this.reset(); - if (this.options.async) { - return process.nextTick(function() { - return cb(null, result); - }); - } else { - return cb(null, result); - } + return cb(null, result); }); this.on("error", function(err) { this.reset(); - if (this.options.async) { - return process.nextTick(function() { - return cb(err); - }); - } else { - return cb(err); - } + return cb(err); }); } - if (str.toString().trim() === '') { - this.emit("end", null); - return true; - } try { - return this.saxParser.write(bom.stripBOM(str.toString())).close(); - } catch (_error) { - err = _error; + str = str.toString(); + if (str.trim() === '') { + this.emit("end", null); + return true; + } + str = bom.stripBOM(str); + if (this.options.async) { + this.remaining = str; + setImmediate(this.processAsync); + return this.saxParser; + } + return this.saxParser.write(str).close(); + } catch (error1) { + err = error1; if (!(this.saxParser.errThrown || this.saxParser.ended)) { this.emit('error', err); return this.saxParser.errThrown = true; + } else if (this.saxParser.ended) { + throw err; } } }; |