home *** CD-ROM | disk | FTP | other *** search
/ PC go! 2014 June / PCgo_CD_2014_06.iso / node_modules / xml2js / lib / xml2js.js
Encoding:
JavaScript  |  2013-04-09  |  8.1 KB  |  284 lines

  1. // Generated by CoffeeScript 1.5.0
  2. (function() {
  3.   var events, isEmpty, sax,
  4.     __hasProp = {}.hasOwnProperty,
  5.     __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; },
  6.     __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
  7.  
  8.   sax = require('sax');
  9.  
  10.   events = require('events');
  11.  
  12.   isEmpty = function(thing) {
  13.     return typeof thing === "object" && (thing != null) && Object.keys(thing).length === 0;
  14.   };
  15.  
  16.   exports.defaults = {
  17.     "0.1": {
  18.       explicitCharkey: false,
  19.       trim: true,
  20.       normalize: true,
  21.       normalizeTags: false,
  22.       attrkey: "@",
  23.       charkey: "#",
  24.       explicitArray: false,
  25.       ignoreAttrs: false,
  26.       mergeAttrs: false,
  27.       explicitRoot: false,
  28.       validator: null,
  29.       xmlns: false,
  30.       explicitChildren: false,
  31.       childkey: '@@',
  32.       charsAsChildren: false,
  33.       async: false
  34.     },
  35.     "0.2": {
  36.       explicitCharkey: false,
  37.       trim: false,
  38.       normalize: false,
  39.       normalizeTags: false,
  40.       attrkey: "$",
  41.       charkey: "_",
  42.       explicitArray: true,
  43.       ignoreAttrs: false,
  44.       mergeAttrs: false,
  45.       explicitRoot: true,
  46.       validator: null,
  47.       xmlns: false,
  48.       explicitChildren: false,
  49.       childkey: '$$',
  50.       charsAsChildren: false,
  51.       async: false
  52.     }
  53.   };
  54.  
  55.   exports.ValidationError = (function(_super) {
  56.  
  57.     __extends(ValidationError, _super);
  58.  
  59.     function ValidationError(message) {
  60.       this.message = message;
  61.     }
  62.  
  63.     return ValidationError;
  64.  
  65.   })(Error);
  66.  
  67.   exports.Parser = (function(_super) {
  68.  
  69.     __extends(Parser, _super);
  70.  
  71.     function Parser(opts) {
  72.       this.parseString = __bind(this.parseString, this);
  73.       this.reset = __bind(this.reset, this);
  74.       var key, value, _ref;
  75.       this.options = {};
  76.       _ref = exports.defaults["0.2"];
  77.       for (key in _ref) {
  78.         if (!__hasProp.call(_ref, key)) continue;
  79.         value = _ref[key];
  80.         this.options[key] = value;
  81.       }
  82.       for (key in opts) {
  83.         if (!__hasProp.call(opts, key)) continue;
  84.         value = opts[key];
  85.         this.options[key] = value;
  86.       }
  87.       if (this.options.xmlns) {
  88.         this.options.xmlnskey = this.options.attrkey + "ns";
  89.       }
  90.       this.reset();
  91.     }
  92.  
  93.     Parser.prototype.reset = function() {
  94.       var attrkey, charkey, err, stack,
  95.         _this = this;
  96.       this.removeAllListeners();
  97.       this.saxParser = sax.parser(true, {
  98.         trim: false,
  99.         normalize: false,
  100.         xmlns: this.options.xmlns
  101.       });
  102.       err = false;
  103.       this.saxParser.onerror = function(error) {
  104.         if (!err) {
  105.           err = true;
  106.           return _this.emit("error", error);
  107.         }
  108.       };
  109.       this.EXPLICIT_CHARKEY = this.options.explicitCharkey;
  110.       this.resultObject = null;
  111.       stack = [];
  112.       attrkey = this.options.attrkey;
  113.       charkey = this.options.charkey;
  114.       this.saxParser.onopentag = function(node) {
  115.         var key, obj, _ref;
  116.         obj = {};
  117.         obj[charkey] = "";
  118.         if (!_this.options.ignoreAttrs) {
  119.           _ref = node.attributes;
  120.           for (key in _ref) {
  121.             if (!__hasProp.call(_ref, key)) continue;
  122.             if (!(attrkey in obj) && !_this.options.mergeAttrs) {
  123.               obj[attrkey] = {};
  124.             }
  125.             if (_this.options.mergeAttrs) {
  126.               obj[key] = node.attributes[key];
  127.             } else {
  128.               obj[attrkey][key] = node.attributes[key];
  129.             }
  130.           }
  131.         }
  132.         obj["#name"] = _this.options.normalizeTags ? node.name.toLowerCase() : node.name;
  133.         if (_this.options.xmlns) {
  134.           obj[_this.options.xmlnskey] = {
  135.             uri: node.uri,
  136.             local: node.local
  137.           };
  138.         }
  139.         return stack.push(obj);
  140.       };
  141.       this.saxParser.onclosetag = function() {
  142.         var node, nodeName, obj, old, s, xpath;
  143.         obj = stack.pop();
  144.         nodeName = obj["#name"];
  145.         delete obj["#name"];
  146.         s = stack[stack.length - 1];
  147.         if (obj[charkey].match(/^\s*$/)) {
  148.           delete obj[charkey];
  149.         } else {
  150.           if (_this.options.trim) {
  151.             obj[charkey] = obj[charkey].trim();
  152.           }
  153.           if (_this.options.normalize) {
  154.             obj[charkey] = obj[charkey].replace(/\s{2,}/g, " ").trim();
  155.           }
  156.           if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) {
  157.             obj = obj[charkey];
  158.           }
  159.         }
  160.         if (_this.options.emptyTag !== void 0 && isEmpty(obj)) {
  161.           obj = _this.options.emptyTag;
  162.         }
  163.         if (_this.options.validator != null) {
  164.           xpath = "/" + ((function() {
  165.             var _i, _len, _results;
  166.             _results = [];
  167.             for (_i = 0, _len = stack.length; _i < _len; _i++) {
  168.               node = stack[_i];
  169.               _results.push(node["#name"]);
  170.             }
  171.             return _results;
  172.           })()).concat(nodeName).join("/");
  173.           try {
  174.             obj = _this.options.validator(xpath, s && s[nodeName], obj);
  175.           } catch (err) {
  176.             _this.emit("error", err);
  177.           }
  178.         }
  179.         if (_this.options.explicitChildren && !_this.options.mergeAttrs && typeof obj === 'object') {
  180.           node = {};
  181.           if (_this.options.attrkey in obj) {
  182.             node[_this.options.attrkey] = obj[_this.options.attrkey];
  183.             delete obj[_this.options.attrkey];
  184.           }
  185.           if (!_this.options.charsAsChildren && _this.options.charkey in obj) {
  186.             node[_this.options.charkey] = obj[_this.options.charkey];
  187.             delete obj[_this.options.charkey];
  188.           }
  189.           if (Object.getOwnPropertyNames(obj).length > 0) {
  190.             node[_this.options.childkey] = obj;
  191.           }
  192.           obj = node;
  193.         }
  194.         if (stack.length > 0) {
  195.           if (!_this.options.explicitArray) {
  196.             if (!(nodeName in s)) {
  197.               return s[nodeName] = obj;
  198.             } else if (s[nodeName] instanceof Array) {
  199.               return s[nodeName].push(obj);
  200.             } else {
  201.               old = s[nodeName];
  202.               s[nodeName] = [old];
  203.               return s[nodeName].push(obj);
  204.             }
  205.           } else {
  206.             if (!(s[nodeName] instanceof Array)) {
  207.               s[nodeName] = [];
  208.             }
  209.             return s[nodeName].push(obj);
  210.           }
  211.         } else {
  212.           if (_this.options.explicitRoot) {
  213.             old = obj;
  214.             obj = {};
  215.             obj[nodeName] = old;
  216.           }
  217.           _this.resultObject = obj;
  218.           return _this.emit("end", _this.resultObject);
  219.         }
  220.       };
  221.       return this.saxParser.ontext = this.saxParser.oncdata = function(text) {
  222.         var s;
  223.         s = stack[stack.length - 1];
  224.         if (s) {
  225.           return s[charkey] += text;
  226.         }
  227.       };
  228.     };
  229.  
  230.     Parser.prototype.parseString = function(str, cb) {
  231.       if ((cb != null) && typeof cb === "function") {
  232.         this.on("end", function(result) {
  233.           this.reset();
  234.           if (this.options.async) {
  235.             return process.nextTick(function() {
  236.               return cb(null, result);
  237.             });
  238.           } else {
  239.             return cb(null, result);
  240.           }
  241.         });
  242.         this.on("error", function(err) {
  243.           this.reset();
  244.           if (this.options.async) {
  245.             return process.nextTick(function() {
  246.               return cb(err);
  247.             });
  248.           } else {
  249.             return cb(err);
  250.           }
  251.         });
  252.       }
  253.       if (str.toString().trim() === '') {
  254.         this.emit("end", null);
  255.         return true;
  256.       }
  257.       return this.saxParser.write(str.toString());
  258.     };
  259.  
  260.     return Parser;
  261.  
  262.   })(events.EventEmitter);
  263.  
  264.   exports.parseString = function(str, a, b) {
  265.     var cb, options, parser;
  266.     if (b != null) {
  267.       if (typeof b === 'function') {
  268.         cb = b;
  269.       }
  270.       if (typeof a === 'object') {
  271.         options = a;
  272.       }
  273.     } else {
  274.       if (typeof a === 'function') {
  275.         cb = a;
  276.       }
  277.       options = {};
  278.     }
  279.     parser = new exports.Parser(options);
  280.     return parser.parseString(str, cb);
  281.   };
  282.  
  283. }).call(this);
  284.