artdaq_node_server  v1_00_08
 All Classes Namespaces Files Variables Pages
XMLElement.js
1 // Generated by CoffeeScript 1.9.1
2 (function() {
3  var XMLAttribute, XMLElement, XMLNode, XMLProcessingInstruction, create, every, isFunction, isObject,
4  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; },
5  hasProp = {}.hasOwnProperty;
6 
7  create = require('lodash/create');
8 
9  isObject = require('lodash/isObject');
10 
11  isFunction = require('lodash/isFunction');
12 
13  every = require('lodash/every');
14 
15  XMLNode = require('./XMLNode');
16 
17  XMLAttribute = require('./XMLAttribute');
18 
19  XMLProcessingInstruction = require('./XMLProcessingInstruction');
20 
21  module.exports = XMLElement = (function(superClass) {
22  extend(XMLElement, superClass);
23 
24  function XMLElement(parent, name, attributes) {
25  XMLElement.__super__.constructor.call(this, parent);
26  if (name == null) {
27  throw new Error("Missing element name");
28  }
29  this.name = this.stringify.eleName(name);
30  this.children = [];
31  this.instructions = [];
32  this.attributes = {};
33  if (attributes != null) {
34  this.attribute(attributes);
35  }
36  }
37 
38  XMLElement.prototype.clone = function() {
39  var att, attName, clonedSelf, i, len, pi, ref, ref1;
40  clonedSelf = create(XMLElement.prototype, this);
41  if (clonedSelf.isRoot) {
42  clonedSelf.documentObject = null;
43  }
44  clonedSelf.attributes = {};
45  ref = this.attributes;
46  for (attName in ref) {
47  if (!hasProp.call(ref, attName)) continue;
48  att = ref[attName];
49  clonedSelf.attributes[attName] = att.clone();
50  }
51  clonedSelf.instructions = [];
52  ref1 = this.instructions;
53  for (i = 0, len = ref1.length; i < len; i++) {
54  pi = ref1[i];
55  clonedSelf.instructions.push(pi.clone());
56  }
57  clonedSelf.children = [];
58  this.children.forEach(function(child) {
59  var clonedChild;
60  clonedChild = child.clone();
61  clonedChild.parent = clonedSelf;
62  return clonedSelf.children.push(clonedChild);
63  });
64  return clonedSelf;
65  };
66 
67  XMLElement.prototype.attribute = function(name, value) {
68  var attName, attValue;
69  if (name != null) {
70  name = name.valueOf();
71  }
72  if (isObject(name)) {
73  for (attName in name) {
74  if (!hasProp.call(name, attName)) continue;
75  attValue = name[attName];
76  this.attribute(attName, attValue);
77  }
78  } else {
79  if (isFunction(value)) {
80  value = value.apply();
81  }
82  if (!this.options.skipNullAttributes || (value != null)) {
83  this.attributes[name] = new XMLAttribute(this, name, value);
84  }
85  }
86  return this;
87  };
88 
89  XMLElement.prototype.removeAttribute = function(name) {
90  var attName, i, len;
91  if (name == null) {
92  throw new Error("Missing attribute name");
93  }
94  name = name.valueOf();
95  if (Array.isArray(name)) {
96  for (i = 0, len = name.length; i < len; i++) {
97  attName = name[i];
98  delete this.attributes[attName];
99  }
100  } else {
101  delete this.attributes[name];
102  }
103  return this;
104  };
105 
106  XMLElement.prototype.instruction = function(target, value) {
107  var i, insTarget, insValue, instruction, len;
108  if (target != null) {
109  target = target.valueOf();
110  }
111  if (value != null) {
112  value = value.valueOf();
113  }
114  if (Array.isArray(target)) {
115  for (i = 0, len = target.length; i < len; i++) {
116  insTarget = target[i];
117  this.instruction(insTarget);
118  }
119  } else if (isObject(target)) {
120  for (insTarget in target) {
121  if (!hasProp.call(target, insTarget)) continue;
122  insValue = target[insTarget];
123  this.instruction(insTarget, insValue);
124  }
125  } else {
126  if (isFunction(value)) {
127  value = value.apply();
128  }
129  instruction = new XMLProcessingInstruction(this, target, value);
130  this.instructions.push(instruction);
131  }
132  return this;
133  };
134 
135  XMLElement.prototype.toString = function(options, level) {
136  var att, child, i, indent, instruction, j, len, len1, name, newline, offset, pretty, r, ref, ref1, ref2, ref3, ref4, ref5, space;
137  pretty = (options != null ? options.pretty : void 0) || false;
138  indent = (ref = options != null ? options.indent : void 0) != null ? ref : ' ';
139  offset = (ref1 = options != null ? options.offset : void 0) != null ? ref1 : 0;
140  newline = (ref2 = options != null ? options.newline : void 0) != null ? ref2 : '\n';
141  level || (level = 0);
142  space = new Array(level + offset + 1).join(indent);
143  r = '';
144  ref3 = this.instructions;
145  for (i = 0, len = ref3.length; i < len; i++) {
146  instruction = ref3[i];
147  r += instruction.toString(options, level);
148  }
149  if (pretty) {
150  r += space;
151  }
152  r += '<' + this.name;
153  ref4 = this.attributes;
154  for (name in ref4) {
155  if (!hasProp.call(ref4, name)) continue;
156  att = ref4[name];
157  r += att.toString(options);
158  }
159  if (this.children.length === 0 || every(this.children, function(e) {
160  return e.value === '';
161  })) {
162  r += '/>';
163  if (pretty) {
164  r += newline;
165  }
166  } else if (pretty && this.children.length === 1 && (this.children[0].value != null)) {
167  r += '>';
168  r += this.children[0].value;
169  r += '</' + this.name + '>';
170  r += newline;
171  } else {
172  r += '>';
173  if (pretty) {
174  r += newline;
175  }
176  ref5 = this.children;
177  for (j = 0, len1 = ref5.length; j < len1; j++) {
178  child = ref5[j];
179  r += child.toString(options, level + 1);
180  }
181  if (pretty) {
182  r += space;
183  }
184  r += '</' + this.name + '>';
185  if (pretty) {
186  r += newline;
187  }
188  }
189  return r;
190  };
191 
192  XMLElement.prototype.att = function(name, value) {
193  return this.attribute(name, value);
194  };
195 
196  XMLElement.prototype.ins = function(target, value) {
197  return this.instruction(target, value);
198  };
199 
200  XMLElement.prototype.a = function(name, value) {
201  return this.attribute(name, value);
202  };
203 
204  XMLElement.prototype.i = function(target, value) {
205  return this.instruction(target, value);
206  };
207 
208  return XMLElement;
209 
210  })(XMLNode);
211 
212 }).call(this);