artdaq_node_server  v1_00_07
 All Classes Namespaces Files Variables Pages
xml2js.js
1 // Generated by CoffeeScript 1.10.0
2 (function() {
3  "use strict";
4  var bom, builder, escapeCDATA, events, isEmpty, processName, processors, requiresCDATA, sax, setImmediate, wrapCDATA,
5  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; },
6  hasProp = {}.hasOwnProperty,
7  bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
8 
9  sax = require('sax');
10 
11  events = require('events');
12 
13  builder = require('xmlbuilder');
14 
15  bom = require('./bom');
16 
17  processors = require('./processors');
18 
19  setImmediate = require('timers').setImmediate;
20 
21  isEmpty = function(thing) {
22  return typeof thing === "object" && (thing != null) && Object.keys(thing).length === 0;
23  };
24 
25  processName = function(processors, processedName) {
26  var i, len, process;
27  for (i = 0, len = processors.length; i < len; i++) {
28  process = processors[i];
29  processedName = process(processedName);
30  }
31  return processedName;
32  };
33 
34  requiresCDATA = function(entry) {
35  return entry.indexOf('&') >= 0 || entry.indexOf('>') >= 0 || entry.indexOf('<') >= 0;
36  };
37 
38  wrapCDATA = function(entry) {
39  return "<![CDATA[" + (escapeCDATA(entry)) + "]]>";
40  };
41 
42  escapeCDATA = function(entry) {
43  return entry.replace(']]>', ']]]]><![CDATA[>');
44  };
45 
46  exports.processors = processors;
47 
48  exports.defaults = {
49  "0.1": {
50  explicitCharkey: false,
51  trim: true,
52  normalize: true,
53  normalizeTags: false,
54  attrkey: "@",
55  charkey: "#",
56  explicitArray: false,
57  ignoreAttrs: false,
58  mergeAttrs: false,
59  explicitRoot: false,
60  validator: null,
61  xmlns: false,
62  explicitChildren: false,
63  childkey: '@@',
64  charsAsChildren: false,
65  includeWhiteChars: false,
66  async: false,
67  strict: true,
68  attrNameProcessors: null,
69  attrValueProcessors: null,
70  tagNameProcessors: null,
71  valueProcessors: null,
72  emptyTag: ''
73  },
74  "0.2": {
75  explicitCharkey: false,
76  trim: false,
77  normalize: false,
78  normalizeTags: false,
79  attrkey: "$",
80  charkey: "_",
81  explicitArray: true,
82  ignoreAttrs: false,
83  mergeAttrs: false,
84  explicitRoot: true,
85  validator: null,
86  xmlns: false,
87  explicitChildren: false,
88  preserveChildrenOrder: false,
89  childkey: '$$',
90  charsAsChildren: false,
91  includeWhiteChars: false,
92  async: false,
93  strict: true,
94  attrNameProcessors: null,
95  attrValueProcessors: null,
96  tagNameProcessors: null,
97  valueProcessors: null,
98  rootName: 'root',
99  xmldec: {
100  'version': '1.0',
101  'encoding': 'UTF-8',
102  'standalone': true
103  },
104  doctype: null,
105  renderOpts: {
106  'pretty': true,
107  'indent': ' ',
108  'newline': '\n'
109  },
110  headless: false,
111  chunkSize: 10000,
112  emptyTag: '',
113  cdata: false
114  }
115  };
116 
117  exports.ValidationError = (function(superClass) {
118  extend(ValidationError, superClass);
119 
120  function ValidationError(message) {
121  this.message = message;
122  }
123 
124  return ValidationError;
125 
126  })(Error);
127 
128  exports.Builder = (function() {
129  function Builder(opts) {
130  var key, ref, value;
131  this.options = {};
132  ref = exports.defaults["0.2"];
133  for (key in ref) {
134  if (!hasProp.call(ref, key)) continue;
135  value = ref[key];
136  this.options[key] = value;
137  }
138  for (key in opts) {
139  if (!hasProp.call(opts, key)) continue;
140  value = opts[key];
141  this.options[key] = value;
142  }
143  }
144 
145  Builder.prototype.buildObject = function(rootObj) {
146  var attrkey, charkey, render, rootElement, rootName;
147  attrkey = this.options.attrkey;
148  charkey = this.options.charkey;
149  if ((Object.keys(rootObj).length === 1) && (this.options.rootName === exports.defaults['0.2'].rootName)) {
150  rootName = Object.keys(rootObj)[0];
151  rootObj = rootObj[rootName];
152  } else {
153  rootName = this.options.rootName;
154  }
155  render = (function(_this) {
156  return function(element, obj) {
157  var attr, child, entry, index, key, value;
158  if (typeof obj !== 'object') {
159  if (_this.options.cdata && requiresCDATA(obj)) {
160  element.raw(wrapCDATA(obj));
161  } else {
162  element.txt(obj);
163  }
164  } else {
165  for (key in obj) {
166  if (!hasProp.call(obj, key)) continue;
167  child = obj[key];
168  if (key === attrkey) {
169  if (typeof child === "object") {
170  for (attr in child) {
171  value = child[attr];
172  element = element.att(attr, value);
173  }
174  }
175  } else if (key === charkey) {
176  if (_this.options.cdata && requiresCDATA(child)) {
177  element = element.raw(wrapCDATA(child));
178  } else {
179  element = element.txt(child);
180  }
181  } else if (Array.isArray(child)) {
182  for (index in child) {
183  if (!hasProp.call(child, index)) continue;
184  entry = child[index];
185  if (typeof entry === 'string') {
186  if (_this.options.cdata && requiresCDATA(entry)) {
187  element = element.ele(key).raw(wrapCDATA(entry)).up();
188  } else {
189  element = element.ele(key, entry).up();
190  }
191  } else {
192  element = render(element.ele(key), entry).up();
193  }
194  }
195  } else if (typeof child === "object") {
196  element = render(element.ele(key), child).up();
197  } else {
198  if (typeof child === 'string' && _this.options.cdata && requiresCDATA(child)) {
199  element = element.ele(key).raw(wrapCDATA(child)).up();
200  } else {
201  if (child == null) {
202  child = '';
203  }
204  element = element.ele(key, child.toString()).up();
205  }
206  }
207  }
208  }
209  return element;
210  };
211  })(this);
212  rootElement = builder.create(rootName, this.options.xmldec, this.options.doctype, {
213  headless: this.options.headless,
214  allowSurrogateChars: this.options.allowSurrogateChars
215  });
216  return render(rootElement, rootObj).end(this.options.renderOpts);
217  };
218 
219  return Builder;
220 
221  })();
222 
223  exports.Parser = (function(superClass) {
224  extend(Parser, superClass);
225 
226  function Parser(opts) {
227  this.parseString = bind(this.parseString, this);
228  this.reset = bind(this.reset, this);
229  this.assignOrPush = bind(this.assignOrPush, this);
230  this.processAsync = bind(this.processAsync, this);
231  var key, ref, value;
232  if (!(this instanceof exports.Parser)) {
233  return new exports.Parser(opts);
234  }
235  this.options = {};
236  ref = exports.defaults["0.2"];
237  for (key in ref) {
238  if (!hasProp.call(ref, key)) continue;
239  value = ref[key];
240  this.options[key] = value;
241  }
242  for (key in opts) {
243  if (!hasProp.call(opts, key)) continue;
244  value = opts[key];
245  this.options[key] = value;
246  }
247  if (this.options.xmlns) {
248  this.options.xmlnskey = this.options.attrkey + "ns";
249  }
250  if (this.options.normalizeTags) {
251  if (!this.options.tagNameProcessors) {
252  this.options.tagNameProcessors = [];
253  }
254  this.options.tagNameProcessors.unshift(processors.normalize);
255  }
256  this.reset();
257  }
258 
259  Parser.prototype.processAsync = function() {
260  var chunk, err, error1;
261  try {
262  if (this.remaining.length <= this.options.chunkSize) {
263  chunk = this.remaining;
264  this.remaining = '';
265  this.saxParser = this.saxParser.write(chunk);
266  return this.saxParser.close();
267  } else {
268  chunk = this.remaining.substr(0, this.options.chunkSize);
269  this.remaining = this.remaining.substr(this.options.chunkSize, this.remaining.length);
270  this.saxParser = this.saxParser.write(chunk);
271  return setImmediate(this.processAsync);
272  }
273  } catch (error1) {
274  err = error1;
275  if (!this.saxParser.errThrown) {
276  this.saxParser.errThrown = true;
277  return this.emit(err);
278  }
279  }
280  };
281 
282  Parser.prototype.assignOrPush = function(obj, key, newValue) {
283  if (!(key in obj)) {
284  if (!this.options.explicitArray) {
285  return obj[key] = newValue;
286  } else {
287  return obj[key] = [newValue];
288  }
289  } else {
290  if (!(obj[key] instanceof Array)) {
291  obj[key] = [obj[key]];
292  }
293  return obj[key].push(newValue);
294  }
295  };
296 
297  Parser.prototype.reset = function() {
298  var attrkey, charkey, ontext, stack;
299  this.removeAllListeners();
300  this.saxParser = sax.parser(this.options.strict, {
301  trim: false,
302  normalize: false,
303  xmlns: this.options.xmlns
304  });
305  this.saxParser.errThrown = false;
306  this.saxParser.onerror = (function(_this) {
307  return function(error) {
308  _this.saxParser.resume();
309  if (!_this.saxParser.errThrown) {
310  _this.saxParser.errThrown = true;
311  return _this.emit("error", error);
312  }
313  };
314  })(this);
315  this.saxParser.onend = (function(_this) {
316  return function() {
317  if (!_this.saxParser.ended) {
318  _this.saxParser.ended = true;
319  return _this.emit("end", _this.resultObject);
320  }
321  };
322  })(this);
323  this.saxParser.ended = false;
324  this.EXPLICIT_CHARKEY = this.options.explicitCharkey;
325  this.resultObject = null;
326  stack = [];
327  attrkey = this.options.attrkey;
328  charkey = this.options.charkey;
329  this.saxParser.onopentag = (function(_this) {
330  return function(node) {
331  var key, newValue, obj, processedKey, ref;
332  obj = {};
333  obj[charkey] = "";
334  if (!_this.options.ignoreAttrs) {
335  ref = node.attributes;
336  for (key in ref) {
337  if (!hasProp.call(ref, key)) continue;
338  if (!(attrkey in obj) && !_this.options.mergeAttrs) {
339  obj[attrkey] = {};
340  }
341  newValue = _this.options.attrValueProcessors ? processName(_this.options.attrValueProcessors, node.attributes[key]) : node.attributes[key];
342  processedKey = _this.options.attrNameProcessors ? processName(_this.options.attrNameProcessors, key) : key;
343  if (_this.options.mergeAttrs) {
344  _this.assignOrPush(obj, processedKey, newValue);
345  } else {
346  obj[attrkey][processedKey] = newValue;
347  }
348  }
349  }
350  obj["#name"] = _this.options.tagNameProcessors ? processName(_this.options.tagNameProcessors, node.name) : node.name;
351  if (_this.options.xmlns) {
352  obj[_this.options.xmlnskey] = {
353  uri: node.uri,
354  local: node.local
355  };
356  }
357  return stack.push(obj);
358  };
359  })(this);
360  this.saxParser.onclosetag = (function(_this) {
361  return function() {
362  var cdata, emptyStr, err, error1, key, node, nodeName, obj, objClone, old, s, xpath;
363  obj = stack.pop();
364  nodeName = obj["#name"];
365  if (!_this.options.explicitChildren || !_this.options.preserveChildrenOrder) {
366  delete obj["#name"];
367  }
368  if (obj.cdata === true) {
369  cdata = obj.cdata;
370  delete obj.cdata;
371  }
372  s = stack[stack.length - 1];
373  if (obj[charkey].match(/^\s*$/) && !cdata) {
374  emptyStr = obj[charkey];
375  delete obj[charkey];
376  } else {
377  if (_this.options.trim) {
378  obj[charkey] = obj[charkey].trim();
379  }
380  if (_this.options.normalize) {
381  obj[charkey] = obj[charkey].replace(/\s{2,}/g, " ").trim();
382  }
383  obj[charkey] = _this.options.valueProcessors ? processName(_this.options.valueProcessors, obj[charkey]) : obj[charkey];
384  if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) {
385  obj = obj[charkey];
386  }
387  }
388  if (isEmpty(obj)) {
389  obj = _this.options.emptyTag !== '' ? _this.options.emptyTag : emptyStr;
390  }
391  if (_this.options.validator != null) {
392  xpath = "/" + ((function() {
393  var i, len, results;
394  results = [];
395  for (i = 0, len = stack.length; i < len; i++) {
396  node = stack[i];
397  results.push(node["#name"]);
398  }
399  return results;
400  })()).concat(nodeName).join("/");
401  try {
402  obj = _this.options.validator(xpath, s && s[nodeName], obj);
403  } catch (error1) {
404  err = error1;
405  _this.emit("error", err);
406  }
407  }
408  if (_this.options.explicitChildren && !_this.options.mergeAttrs && typeof obj === 'object') {
409  if (!_this.options.preserveChildrenOrder) {
410  node = {};
411  if (_this.options.attrkey in obj) {
412  node[_this.options.attrkey] = obj[_this.options.attrkey];
413  delete obj[_this.options.attrkey];
414  }
415  if (!_this.options.charsAsChildren && _this.options.charkey in obj) {
416  node[_this.options.charkey] = obj[_this.options.charkey];
417  delete obj[_this.options.charkey];
418  }
419  if (Object.getOwnPropertyNames(obj).length > 0) {
420  node[_this.options.childkey] = obj;
421  }
422  obj = node;
423  } else if (s) {
424  s[_this.options.childkey] = s[_this.options.childkey] || [];
425  objClone = {};
426  for (key in obj) {
427  if (!hasProp.call(obj, key)) continue;
428  objClone[key] = obj[key];
429  }
430  s[_this.options.childkey].push(objClone);
431  delete obj["#name"];
432  if (Object.keys(obj).length === 1 && charkey in obj && !_this.EXPLICIT_CHARKEY) {
433  obj = obj[charkey];
434  }
435  }
436  }
437  if (stack.length > 0) {
438  return _this.assignOrPush(s, nodeName, obj);
439  } else {
440  if (_this.options.explicitRoot) {
441  old = obj;
442  obj = {};
443  obj[nodeName] = old;
444  }
445  _this.resultObject = obj;
446  _this.saxParser.ended = true;
447  return _this.emit("end", _this.resultObject);
448  }
449  };
450  })(this);
451  ontext = (function(_this) {
452  return function(text) {
453  var charChild, s;
454  s = stack[stack.length - 1];
455  if (s) {
456  s[charkey] += text;
457  if (_this.options.explicitChildren && _this.options.preserveChildrenOrder && _this.options.charsAsChildren && (_this.options.includeWhiteChars || text.replace(/\\n/g, '').trim() !== '')) {
458  s[_this.options.childkey] = s[_this.options.childkey] || [];
459  charChild = {
460  '#name': '__text__'
461  };
462  charChild[charkey] = text;
463  if (_this.options.normalize) {
464  charChild[charkey] = charChild[charkey].replace(/\s{2,}/g, " ").trim();
465  }
466  s[_this.options.childkey].push(charChild);
467  }
468  return s;
469  }
470  };
471  })(this);
472  this.saxParser.ontext = ontext;
473  return this.saxParser.oncdata = (function(_this) {
474  return function(text) {
475  var s;
476  s = ontext(text);
477  if (s) {
478  return s.cdata = true;
479  }
480  };
481  })(this);
482  };
483 
484  Parser.prototype.parseString = function(str, cb) {
485  var err, error1;
486  if ((cb != null) && typeof cb === "function") {
487  this.on("end", function(result) {
488  this.reset();
489  return cb(null, result);
490  });
491  this.on("error", function(err) {
492  this.reset();
493  return cb(err);
494  });
495  }
496  try {
497  str = str.toString();
498  if (str.trim() === '') {
499  this.emit("end", null);
500  return true;
501  }
502  str = bom.stripBOM(str);
503  if (this.options.async) {
504  this.remaining = str;
505  setImmediate(this.processAsync);
506  return this.saxParser;
507  }
508  return this.saxParser.write(str).close();
509  } catch (error1) {
510  err = error1;
511  if (!(this.saxParser.errThrown || this.saxParser.ended)) {
512  this.emit('error', err);
513  return this.saxParser.errThrown = true;
514  } else if (this.saxParser.ended) {
515  throw err;
516  }
517  }
518  };
519 
520  return Parser;
521 
522  })(events.EventEmitter);
523 
524  exports.parseString = function(str, a, b) {
525  var cb, options, parser;
526  if (b != null) {
527  if (typeof b === 'function') {
528  cb = b;
529  }
530  if (typeof a === 'object') {
531  options = a;
532  }
533  } else {
534  if (typeof a === 'function') {
535  cb = a;
536  }
537  options = {};
538  }
539  parser = new exports.Parser(options);
540  return parser.parseString(str, cb);
541  };
542 
543 }).call(this);