artdaq_node_server  v1_00_09
 All Classes Namespaces Files Variables Pages
XMLNode.js
1 // Generated by CoffeeScript 1.9.1
2 (function() {
3  var XMLCData, XMLComment, XMLDeclaration, XMLDocType, XMLElement, XMLNode, XMLRaw, XMLText, isEmpty, isFunction, isObject,
4  hasProp = {}.hasOwnProperty;
5 
6  isObject = require('lodash/isObject');
7 
8  isFunction = require('lodash/isFunction');
9 
10  isEmpty = require('lodash/isEmpty');
11 
12  XMLElement = null;
13 
14  XMLCData = null;
15 
16  XMLComment = null;
17 
18  XMLDeclaration = null;
19 
20  XMLDocType = null;
21 
22  XMLRaw = null;
23 
24  XMLText = null;
25 
26  module.exports = XMLNode = (function() {
27  function XMLNode(parent) {
28  this.parent = parent;
29  this.options = this.parent.options;
30  this.stringify = this.parent.stringify;
31  if (XMLElement === null) {
32  XMLElement = require('./XMLElement');
33  XMLCData = require('./XMLCData');
34  XMLComment = require('./XMLComment');
35  XMLDeclaration = require('./XMLDeclaration');
36  XMLDocType = require('./XMLDocType');
37  XMLRaw = require('./XMLRaw');
38  XMLText = require('./XMLText');
39  }
40  }
41 
42  XMLNode.prototype.element = function(name, attributes, text) {
43  var childNode, item, j, k, key, lastChild, len, len1, ref, val;
44  lastChild = null;
45  if (attributes == null) {
46  attributes = {};
47  }
48  attributes = attributes.valueOf();
49  if (!isObject(attributes)) {
50  ref = [attributes, text], text = ref[0], attributes = ref[1];
51  }
52  if (name != null) {
53  name = name.valueOf();
54  }
55  if (Array.isArray(name)) {
56  for (j = 0, len = name.length; j < len; j++) {
57  item = name[j];
58  lastChild = this.element(item);
59  }
60  } else if (isFunction(name)) {
61  lastChild = this.element(name.apply());
62  } else if (isObject(name)) {
63  for (key in name) {
64  if (!hasProp.call(name, key)) continue;
65  val = name[key];
66  if (isFunction(val)) {
67  val = val.apply();
68  }
69  if ((isObject(val)) && (isEmpty(val))) {
70  val = null;
71  }
72  if (!this.options.ignoreDecorators && this.stringify.convertAttKey && key.indexOf(this.stringify.convertAttKey) === 0) {
73  lastChild = this.attribute(key.substr(this.stringify.convertAttKey.length), val);
74  } else if (!this.options.ignoreDecorators && this.stringify.convertPIKey && key.indexOf(this.stringify.convertPIKey) === 0) {
75  lastChild = this.instruction(key.substr(this.stringify.convertPIKey.length), val);
76  } else if (!this.options.separateArrayItems && Array.isArray(val)) {
77  for (k = 0, len1 = val.length; k < len1; k++) {
78  item = val[k];
79  childNode = {};
80  childNode[key] = item;
81  lastChild = this.element(childNode);
82  }
83  } else if (isObject(val)) {
84  lastChild = this.element(key);
85  lastChild.element(val);
86  } else {
87  lastChild = this.element(key, val);
88  }
89  }
90  } else {
91  if (!this.options.ignoreDecorators && this.stringify.convertTextKey && name.indexOf(this.stringify.convertTextKey) === 0) {
92  lastChild = this.text(text);
93  } else if (!this.options.ignoreDecorators && this.stringify.convertCDataKey && name.indexOf(this.stringify.convertCDataKey) === 0) {
94  lastChild = this.cdata(text);
95  } else if (!this.options.ignoreDecorators && this.stringify.convertCommentKey && name.indexOf(this.stringify.convertCommentKey) === 0) {
96  lastChild = this.comment(text);
97  } else if (!this.options.ignoreDecorators && this.stringify.convertRawKey && name.indexOf(this.stringify.convertRawKey) === 0) {
98  lastChild = this.raw(text);
99  } else {
100  lastChild = this.node(name, attributes, text);
101  }
102  }
103  if (lastChild == null) {
104  throw new Error("Could not create any elements with: " + name);
105  }
106  return lastChild;
107  };
108 
109  XMLNode.prototype.insertBefore = function(name, attributes, text) {
110  var child, i, removed;
111  if (this.isRoot) {
112  throw new Error("Cannot insert elements at root level");
113  }
114  i = this.parent.children.indexOf(this);
115  removed = this.parent.children.splice(i);
116  child = this.parent.element(name, attributes, text);
117  Array.prototype.push.apply(this.parent.children, removed);
118  return child;
119  };
120 
121  XMLNode.prototype.insertAfter = function(name, attributes, text) {
122  var child, i, removed;
123  if (this.isRoot) {
124  throw new Error("Cannot insert elements at root level");
125  }
126  i = this.parent.children.indexOf(this);
127  removed = this.parent.children.splice(i + 1);
128  child = this.parent.element(name, attributes, text);
129  Array.prototype.push.apply(this.parent.children, removed);
130  return child;
131  };
132 
133  XMLNode.prototype.remove = function() {
134  var i, ref;
135  if (this.isRoot) {
136  throw new Error("Cannot remove the root element");
137  }
138  i = this.parent.children.indexOf(this);
139  [].splice.apply(this.parent.children, [i, i - i + 1].concat(ref = [])), ref;
140  return this.parent;
141  };
142 
143  XMLNode.prototype.node = function(name, attributes, text) {
144  var child, ref;
145  if (name != null) {
146  name = name.valueOf();
147  }
148  if (attributes == null) {
149  attributes = {};
150  }
151  attributes = attributes.valueOf();
152  if (!isObject(attributes)) {
153  ref = [attributes, text], text = ref[0], attributes = ref[1];
154  }
155  child = new XMLElement(this, name, attributes);
156  if (text != null) {
157  child.text(text);
158  }
159  this.children.push(child);
160  return child;
161  };
162 
163  XMLNode.prototype.text = function(value) {
164  var child;
165  child = new XMLText(this, value);
166  this.children.push(child);
167  return this;
168  };
169 
170  XMLNode.prototype.cdata = function(value) {
171  var child;
172  child = new XMLCData(this, value);
173  this.children.push(child);
174  return this;
175  };
176 
177  XMLNode.prototype.comment = function(value) {
178  var child;
179  child = new XMLComment(this, value);
180  this.children.push(child);
181  return this;
182  };
183 
184  XMLNode.prototype.raw = function(value) {
185  var child;
186  child = new XMLRaw(this, value);
187  this.children.push(child);
188  return this;
189  };
190 
191  XMLNode.prototype.declaration = function(version, encoding, standalone) {
192  var doc, xmldec;
193  doc = this.document();
194  xmldec = new XMLDeclaration(doc, version, encoding, standalone);
195  doc.xmldec = xmldec;
196  return doc.root();
197  };
198 
199  XMLNode.prototype.doctype = function(pubID, sysID) {
200  var doc, doctype;
201  doc = this.document();
202  doctype = new XMLDocType(doc, pubID, sysID);
203  doc.doctype = doctype;
204  return doctype;
205  };
206 
207  XMLNode.prototype.up = function() {
208  if (this.isRoot) {
209  throw new Error("The root node has no parent. Use doc() if you need to get the document object.");
210  }
211  return this.parent;
212  };
213 
214  XMLNode.prototype.root = function() {
215  var child;
216  if (this.isRoot) {
217  return this;
218  }
219  child = this.parent;
220  while (!child.isRoot) {
221  child = child.parent;
222  }
223  return child;
224  };
225 
226  XMLNode.prototype.document = function() {
227  return this.root().documentObject;
228  };
229 
230  XMLNode.prototype.end = function(options) {
231  return this.document().toString(options);
232  };
233 
234  XMLNode.prototype.prev = function() {
235  var i;
236  if (this.isRoot) {
237  throw new Error("Root node has no siblings");
238  }
239  i = this.parent.children.indexOf(this);
240  if (i < 1) {
241  throw new Error("Already at the first node");
242  }
243  return this.parent.children[i - 1];
244  };
245 
246  XMLNode.prototype.next = function() {
247  var i;
248  if (this.isRoot) {
249  throw new Error("Root node has no siblings");
250  }
251  i = this.parent.children.indexOf(this);
252  if (i === -1 || i === this.parent.children.length - 1) {
253  throw new Error("Already at the last node");
254  }
255  return this.parent.children[i + 1];
256  };
257 
258  XMLNode.prototype.importXMLBuilder = function(xmlbuilder) {
259  var clonedRoot;
260  clonedRoot = xmlbuilder.root().clone();
261  clonedRoot.parent = this;
262  clonedRoot.isRoot = false;
263  this.children.push(clonedRoot);
264  return this;
265  };
266 
267  XMLNode.prototype.ele = function(name, attributes, text) {
268  return this.element(name, attributes, text);
269  };
270 
271  XMLNode.prototype.nod = function(name, attributes, text) {
272  return this.node(name, attributes, text);
273  };
274 
275  XMLNode.prototype.txt = function(value) {
276  return this.text(value);
277  };
278 
279  XMLNode.prototype.dat = function(value) {
280  return this.cdata(value);
281  };
282 
283  XMLNode.prototype.com = function(value) {
284  return this.comment(value);
285  };
286 
287  XMLNode.prototype.doc = function() {
288  return this.document();
289  };
290 
291  XMLNode.prototype.dec = function(version, encoding, standalone) {
292  return this.declaration(version, encoding, standalone);
293  };
294 
295  XMLNode.prototype.dtd = function(pubID, sysID) {
296  return this.doctype(pubID, sysID);
297  };
298 
299  XMLNode.prototype.e = function(name, attributes, text) {
300  return this.element(name, attributes, text);
301  };
302 
303  XMLNode.prototype.n = function(name, attributes, text) {
304  return this.node(name, attributes, text);
305  };
306 
307  XMLNode.prototype.t = function(value) {
308  return this.text(value);
309  };
310 
311  XMLNode.prototype.d = function(value) {
312  return this.cdata(value);
313  };
314 
315  XMLNode.prototype.c = function(value) {
316  return this.comment(value);
317  };
318 
319  XMLNode.prototype.r = function(value) {
320  return this.raw(value);
321  };
322 
323  XMLNode.prototype.u = function() {
324  return this.up();
325  };
326 
327  return XMLNode;
328 
329  })();
330 
331 }).call(this);