parser.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. "use strict";
  4. var bom, defaults, events, isEmpty, processItem, processors, sax, setImmediate,
  5. bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
  6. 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; },
  7. hasProp = {}.hasOwnProperty;
  8. sax = require('sax');
  9. events = require('events');
  10. bom = require('./bom');
  11. processors = require('./processors');
  12. setImmediate = require('timers').setImmediate;
  13. defaults = require('./defaults').defaults;
  14. isEmpty = function(thing) {
  15. return typeof thing === "object" && (thing != null) && Object.keys(thing).length === 0;
  16. };
  17. processItem = function(processors, item, key) {
  18. var i, len, process;
  19. for (i = 0, len = processors.length; i < len; i++) {
  20. process = processors[i];
  21. item = process(item, key);
  22. }
  23. return item;
  24. };
  25. exports.Parser = (function(superClass) {
  26. extend(Parser, superClass);
  27. function Parser(opts) {
  28. this.parseStringPromise = bind(this.parseStringPromise, this);
  29. this.parseString = bind(this.parseString, this);
  30. this.reset = bind(this.reset, this);
  31. this.assignOrPush = bind(this.assignOrPush, this);
  32. this.processAsync = bind(this.processAsync, this);
  33. var key, ref, value;
  34. if (!(this instanceof exports.Parser)) {
  35. return new exports.Parser(opts);
  36. }
  37. this.options = {};
  38. ref = defaults["0.2"];
  39. for (key in ref) {
  40. if (!hasProp.call(ref, key)) continue;
  41. value = ref[key];
  42. this.options[key] = value;
  43. }
  44. for (key in opts) {
  45. if (!hasProp.call(opts, key)) continue;
  46. value = opts[key];
  47. this.options[key] = value;
  48. }
  49. if (this.options.xmlns) {
  50. this.options.xmlnskey = this.options.attrkey + "ns";
  51. }
  52. if (this.options.normalizeTags) {
  53. if (!this.options.tagNameProcessors) {
  54. this.options.tagNameProcessors = [];
  55. }
  56. this.options.tagNameProcessors.unshift(processors.normalize);
  57. }
  58. this.reset();
  59. }
  60. Parser.prototype.processAsync = function() {
  61. var chunk, err;
  62. try {
  63. if (this.remaining.length <= this.options.chunkSize) {
  64. chunk = this.remaining;
  65. this.remaining = '';
  66. this.saxParser = this.saxParser.write(chunk);
  67. return this.saxParser.close();
  68. } else {
  69. chunk = this.remaining.substr(0, this.options.chunkSize);
  70. this.remaining = this.remaining.substr(this.options.chunkSize, this.remaining.length);
  71. this.saxParser = this.saxParser.write(chunk);
  72. return setImmediate(this.processAsync);
  73. }
  74. } catch (error1) {
  75. err = error1;
  76. if (!this.saxParser.errThrown) {
  77. this.saxParser.errThrown = true;
  78. return this.emit(err);
  79. }
  80. }
  81. };
  82. Parser.prototype.assignOrPush = function(obj, key, newValue) {
  83. if (!(key in obj)) {
  84. if (!this.options.explicitArray) {
  85. return obj[key] = newValue;
  86. } else {
  87. return obj[key] = [newValue];
  88. }
  89. } else {
  90. if (!(obj[key] instanceof Array)) {
  91. obj[key] = [obj[key]];
  92. }
  93. return obj[key].push(newValue);
  94. }
  95. };
  96. Parser.prototype.reset = function() {
  97. var attrkey, charkey, ontext, stack;
  98. this.removeAllListeners();
  99. this.saxParser = sax.parser(this.options.strict, {
  100. trim: false,
  101. normalize: false,
  102. xmlns: this.options.xmlns
  103. });
  104. this.saxParser.errThrown = false;
  105. this.saxParser.onerror = (function(_this) {
  106. return function(error) {
  107. _this.saxParser.resume();
  108. if (!_this.saxParser.errThrown) {
  109. _this.saxParser.errThrown = true;
  110. return _this.emit("error", error);
  111. }
  112. };
  113. })(this);
  114. this.saxParser.onend = (function(_this) {
  115. return function() {
  116. if (!_this.saxParser.ended) {
  117. _this.saxParser.ended = true;
  118. return _this.emit("end", _this.resultObject);
  119. }
  120. };
  121. })(this);
  122. this.saxParser.ended = false;
  123. this.EXPLICIT_CHARKEY = this.options.explicitCharkey;
  124. this.resultObject = null;
  125. stack = [];
  126. attrkey = this.options.attrkey;
  127. charkey = this.options.charkey;
  128. this.saxParser.onopentag = (function(_this) {
  129. return function(node) {
  130. var key, newValue, obj, processedKey, ref;
  131. obj = {};
  132. obj[charkey] = "";
  133. if (!_this.options.ignoreAttrs) {
  134. ref = node.attributes;
  135. for (key in ref) {
  136. if (!hasProp.call(ref, key)) continue;
  137. if (!(attrkey in obj) && !_this.options.mergeAttrs) {
  138. obj[attrkey] = {};
  139. }
  140. newValue = _this.options.attrValueProcessors ? processItem(_this.options.attrValueProcessors, node.attributes[key], key) : node.attributes[key];
  141. processedKey = _this.options.attrNameProcessors ? processItem(_this.options.attrNameProcessors, key) : key;
  142. if (_this.options.mergeAttrs) {
  143. _this.assignOrPush(obj, processedKey, newValue);
  144. } else {
  145. obj[attrkey][processedKey] = newValue;
  146. }
  147. }
  148. }
  149. obj["#name"] = _this.options.tagNameProcessors ? processItem(_this.options.tagNameProcessors, node.name) : node.name;
  150. if (_this.options.xmlns) {
  151. obj[_this.options.xmlnskey] = {
  152. uri: node.uri,
  153. local: node.local
  154. };
  155. }
  156. return stack.push(obj);
  157. };
  158. })(this);
  159. this.saxParser.onclosetag = (function(_this) {
  160. return function() {
  161. var cdata, emptyStr, key, node, nodeName, obj, objClone, old, s, xpath;
  162. obj = stack.pop();
  163. nodeName = obj["#name"];
  164. if (!_this.options.explicitChildren || !_this.options.preserveChildrenOrder) {
  165. delete obj["#name"];
  166. }
  167. if (obj.cdata === true) {
  168. cdata = obj.cdata;
  169. delete obj.cdata;
  170. }
  171. s = stack[stack.length - 1];
  172. if (obj[charkey].match(/^\s*$/) && !cdata) {
  173. emptyStr = obj[charkey];
  174. delete obj[charkey];
  175. } else {
  176. if (_this.options.trim) {
  177. obj[charkey] = obj[charkey].trim();
  178. }
  179. if (_this.options.normalize) {
  180. obj[charkey] = obj[charkey].replace(/\s{2,}/g, " ").trim();
  181. }
  182. obj[charkey] = _this.options.valueProcessors ? processItem(_this.options.valueProcessors, obj[charkey], nodeName) : obj[charkey];
  183. if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) {
  184. obj = obj[charkey];
  185. }
  186. }
  187. if (isEmpty(obj)) {
  188. obj = _this.options.emptyTag !== '' ? _this.options.emptyTag : emptyStr;
  189. }
  190. if (_this.options.validator != null) {
  191. xpath = "/" + ((function() {
  192. var i, len, results;
  193. results = [];
  194. for (i = 0, len = stack.length; i < len; i++) {
  195. node = stack[i];
  196. results.push(node["#name"]);
  197. }
  198. return results;
  199. })()).concat(nodeName).join("/");
  200. (function() {
  201. var err;
  202. try {
  203. return obj = _this.options.validator(xpath, s && s[nodeName], obj);
  204. } catch (error1) {
  205. err = error1;
  206. return _this.emit("error", err);
  207. }
  208. })();
  209. }
  210. if (_this.options.explicitChildren && !_this.options.mergeAttrs && typeof obj === 'object') {
  211. if (!_this.options.preserveChildrenOrder) {
  212. node = {};
  213. if (_this.options.attrkey in obj) {
  214. node[_this.options.attrkey] = obj[_this.options.attrkey];
  215. delete obj[_this.options.attrkey];
  216. }
  217. if (!_this.options.charsAsChildren && _this.options.charkey in obj) {
  218. node[_this.options.charkey] = obj[_this.options.charkey];
  219. delete obj[_this.options.charkey];
  220. }
  221. if (Object.getOwnPropertyNames(obj).length > 0) {
  222. node[_this.options.childkey] = obj;
  223. }
  224. obj = node;
  225. } else if (s) {
  226. s[_this.options.childkey] = s[_this.options.childkey] || [];
  227. objClone = {};
  228. for (key in obj) {
  229. if (!hasProp.call(obj, key)) continue;
  230. objClone[key] = obj[key];
  231. }
  232. s[_this.options.childkey].push(objClone);
  233. delete obj["#name"];
  234. if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) {
  235. obj = obj[charkey];
  236. }
  237. }
  238. }
  239. if (stack.length > 0) {
  240. return _this.assignOrPush(s, nodeName, obj);
  241. } else {
  242. if (_this.options.explicitRoot) {
  243. old = obj;
  244. obj = {};
  245. obj[nodeName] = old;
  246. }
  247. _this.resultObject = obj;
  248. _this.saxParser.ended = true;
  249. return _this.emit("end", _this.resultObject);
  250. }
  251. };
  252. })(this);
  253. ontext = (function(_this) {
  254. return function(text) {
  255. var charChild, s;
  256. s = stack[stack.length - 1];
  257. if (s) {
  258. s[charkey] += text;
  259. if (_this.options.explicitChildren && _this.options.preserveChildrenOrder && _this.options.charsAsChildren && (_this.options.includeWhiteChars || text.replace(/\\n/g, '').trim() !== '')) {
  260. s[_this.options.childkey] = s[_this.options.childkey] || [];
  261. charChild = {
  262. '#name': '__text__'
  263. };
  264. charChild[charkey] = text;
  265. if (_this.options.normalize) {
  266. charChild[charkey] = charChild[charkey].replace(/\s{2,}/g, " ").trim();
  267. }
  268. s[_this.options.childkey].push(charChild);
  269. }
  270. return s;
  271. }
  272. };
  273. })(this);
  274. this.saxParser.ontext = ontext;
  275. return this.saxParser.oncdata = (function(_this) {
  276. return function(text) {
  277. var s;
  278. s = ontext(text);
  279. if (s) {
  280. return s.cdata = true;
  281. }
  282. };
  283. })(this);
  284. };
  285. Parser.prototype.parseString = function(str, cb) {
  286. var err;
  287. if ((cb != null) && typeof cb === "function") {
  288. this.on("end", function(result) {
  289. this.reset();
  290. return cb(null, result);
  291. });
  292. this.on("error", function(err) {
  293. this.reset();
  294. return cb(err);
  295. });
  296. }
  297. try {
  298. str = str.toString();
  299. if (str.trim() === '') {
  300. this.emit("end", null);
  301. return true;
  302. }
  303. str = bom.stripBOM(str);
  304. if (this.options.async) {
  305. this.remaining = str;
  306. setImmediate(this.processAsync);
  307. return this.saxParser;
  308. }
  309. return this.saxParser.write(str).close();
  310. } catch (error1) {
  311. err = error1;
  312. if (!(this.saxParser.errThrown || this.saxParser.ended)) {
  313. this.emit('error', err);
  314. return this.saxParser.errThrown = true;
  315. } else if (this.saxParser.ended) {
  316. throw err;
  317. }
  318. }
  319. };
  320. Parser.prototype.parseStringPromise = function(str) {
  321. return new Promise((function(_this) {
  322. return function(resolve, reject) {
  323. return _this.parseString(str, function(err, value) {
  324. if (err) {
  325. return reject(err);
  326. } else {
  327. return resolve(value);
  328. }
  329. });
  330. };
  331. })(this));
  332. };
  333. return Parser;
  334. })(events);
  335. exports.parseString = function(str, a, b) {
  336. var cb, options, parser;
  337. if (b != null) {
  338. if (typeof b === 'function') {
  339. cb = b;
  340. }
  341. if (typeof a === 'object') {
  342. options = a;
  343. }
  344. } else {
  345. if (typeof a === 'function') {
  346. cb = a;
  347. }
  348. options = {};
  349. }
  350. parser = new exports.Parser(options);
  351. return parser.parseString(str, cb);
  352. };
  353. exports.parseStringPromise = function(str, a) {
  354. var options, parser;
  355. if (typeof a === 'object') {
  356. options = a;
  357. }
  358. parser = new exports.Parser(options);
  359. return parser.parseStringPromise(str);
  360. };
  361. }).call(this);