parser.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 = Object.create(null);
  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] = Object.create(null);
  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. if (typeof _this.options.emptyTag === 'function') {
  189. obj = _this.options.emptyTag();
  190. } else {
  191. obj = _this.options.emptyTag !== '' ? _this.options.emptyTag : emptyStr;
  192. }
  193. }
  194. if (_this.options.validator != null) {
  195. xpath = "/" + ((function() {
  196. var i, len, results;
  197. results = [];
  198. for (i = 0, len = stack.length; i < len; i++) {
  199. node = stack[i];
  200. results.push(node["#name"]);
  201. }
  202. return results;
  203. })()).concat(nodeName).join("/");
  204. (function() {
  205. var err;
  206. try {
  207. return obj = _this.options.validator(xpath, s && s[nodeName], obj);
  208. } catch (error1) {
  209. err = error1;
  210. return _this.emit("error", err);
  211. }
  212. })();
  213. }
  214. if (_this.options.explicitChildren && !_this.options.mergeAttrs && typeof obj === 'object') {
  215. if (!_this.options.preserveChildrenOrder) {
  216. node = Object.create(null);
  217. if (_this.options.attrkey in obj) {
  218. node[_this.options.attrkey] = obj[_this.options.attrkey];
  219. delete obj[_this.options.attrkey];
  220. }
  221. if (!_this.options.charsAsChildren && _this.options.charkey in obj) {
  222. node[_this.options.charkey] = obj[_this.options.charkey];
  223. delete obj[_this.options.charkey];
  224. }
  225. if (Object.getOwnPropertyNames(obj).length > 0) {
  226. node[_this.options.childkey] = obj;
  227. }
  228. obj = node;
  229. } else if (s) {
  230. s[_this.options.childkey] = s[_this.options.childkey] || [];
  231. objClone = Object.create(null);
  232. for (key in obj) {
  233. if (!hasProp.call(obj, key)) continue;
  234. objClone[key] = obj[key];
  235. }
  236. s[_this.options.childkey].push(objClone);
  237. delete obj["#name"];
  238. if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) {
  239. obj = obj[charkey];
  240. }
  241. }
  242. }
  243. if (stack.length > 0) {
  244. return _this.assignOrPush(s, nodeName, obj);
  245. } else {
  246. if (_this.options.explicitRoot) {
  247. old = obj;
  248. obj = Object.create(null);
  249. obj[nodeName] = old;
  250. }
  251. _this.resultObject = obj;
  252. _this.saxParser.ended = true;
  253. return _this.emit("end", _this.resultObject);
  254. }
  255. };
  256. })(this);
  257. ontext = (function(_this) {
  258. return function(text) {
  259. var charChild, s;
  260. s = stack[stack.length - 1];
  261. if (s) {
  262. s[charkey] += text;
  263. if (_this.options.explicitChildren && _this.options.preserveChildrenOrder && _this.options.charsAsChildren && (_this.options.includeWhiteChars || text.replace(/\\n/g, '').trim() !== '')) {
  264. s[_this.options.childkey] = s[_this.options.childkey] || [];
  265. charChild = {
  266. '#name': '__text__'
  267. };
  268. charChild[charkey] = text;
  269. if (_this.options.normalize) {
  270. charChild[charkey] = charChild[charkey].replace(/\s{2,}/g, " ").trim();
  271. }
  272. s[_this.options.childkey].push(charChild);
  273. }
  274. return s;
  275. }
  276. };
  277. })(this);
  278. this.saxParser.ontext = ontext;
  279. return this.saxParser.oncdata = (function(_this) {
  280. return function(text) {
  281. var s;
  282. s = ontext(text);
  283. if (s) {
  284. return s.cdata = true;
  285. }
  286. };
  287. })(this);
  288. };
  289. Parser.prototype.parseString = function(str, cb) {
  290. var err;
  291. if ((cb != null) && typeof cb === "function") {
  292. this.on("end", function(result) {
  293. this.reset();
  294. return cb(null, result);
  295. });
  296. this.on("error", function(err) {
  297. this.reset();
  298. return cb(err);
  299. });
  300. }
  301. try {
  302. str = str.toString();
  303. if (str.trim() === '') {
  304. this.emit("end", null);
  305. return true;
  306. }
  307. str = bom.stripBOM(str);
  308. if (this.options.async) {
  309. this.remaining = str;
  310. setImmediate(this.processAsync);
  311. return this.saxParser;
  312. }
  313. return this.saxParser.write(str).close();
  314. } catch (error1) {
  315. err = error1;
  316. if (!(this.saxParser.errThrown || this.saxParser.ended)) {
  317. this.emit('error', err);
  318. return this.saxParser.errThrown = true;
  319. } else if (this.saxParser.ended) {
  320. throw err;
  321. }
  322. }
  323. };
  324. Parser.prototype.parseStringPromise = function(str) {
  325. return new Promise((function(_this) {
  326. return function(resolve, reject) {
  327. return _this.parseString(str, function(err, value) {
  328. if (err) {
  329. return reject(err);
  330. } else {
  331. return resolve(value);
  332. }
  333. });
  334. };
  335. })(this));
  336. };
  337. return Parser;
  338. })(events);
  339. exports.parseString = function(str, a, b) {
  340. var cb, options, parser;
  341. if (b != null) {
  342. if (typeof b === 'function') {
  343. cb = b;
  344. }
  345. if (typeof a === 'object') {
  346. options = a;
  347. }
  348. } else {
  349. if (typeof a === 'function') {
  350. cb = a;
  351. }
  352. options = {};
  353. }
  354. parser = new exports.Parser(options);
  355. return parser.parseString(str, cb);
  356. };
  357. exports.parseStringPromise = function(str, a) {
  358. var options, parser;
  359. if (typeof a === 'object') {
  360. options = a;
  361. }
  362. parser = new exports.Parser(options);
  363. return parser.parseStringPromise(str);
  364. };
  365. }).call(this);