artdaq_node_server  v1_00_08
 All Classes Namespaces Files Variables Pages
XMLNode.js
1 // Generated by CoffeeScript 1.10.0
2 (function() {
3  var XMLCData, XMLComment, XMLDeclaration, XMLDocType, XMLElement, XMLNode, XMLProcessingInstruction, XMLRaw, XMLText, isEmpty, isFunction, isObject, ref,
4  hasProp = {}.hasOwnProperty;
5 
6  ref = require('./Utility'), isObject = ref.isObject, isFunction = ref.isFunction, isEmpty = ref.isEmpty;
7 
8  XMLElement = null;
9 
10  XMLCData = null;
11 
12  XMLComment = null;
13 
14  XMLDeclaration = null;
15 
16  XMLDocType = null;
17 
18  XMLRaw = null;
19 
20  XMLText = null;
21 
22  XMLProcessingInstruction = null;
23 
24  module.exports = XMLNode = (function() {
25  function XMLNode(parent) {
26  this.parent = parent;
27  if (this.parent) {
28  this.options = this.parent.options;
29  this.stringify = this.parent.stringify;
30  }
31  this.children = [];
32  if (!XMLElement) {
33  XMLElement = require('./XMLElement');
34  XMLCData = require('./XMLCData');
35  XMLComment = require('./XMLComment');
36  XMLDeclaration = require('./XMLDeclaration');
37  XMLDocType = require('./XMLDocType');
38  XMLRaw = require('./XMLRaw');
39  XMLText = require('./XMLText');
40  XMLProcessingInstruction = require('./XMLProcessingInstruction');
41  }
42  }
43 
44  XMLNode.prototype.element = function(name, attributes, text) {
45  var childNode, item, j, k, key, lastChild, len, len1, ref1, val;
46  lastChild = null;
47  if (attributes == null) {
48  attributes = {};
49  }
50  attributes = attributes.valueOf();
51  if (!isObject(attributes)) {
52  ref1 = [attributes, text], text = ref1[0], attributes = ref1[1];
53  }
54  if (name != null) {
55  name = name.valueOf();
56  }
57  if (Array.isArray(name)) {
58  for (j = 0, len = name.length; j < len; j++) {
59  item = name[j];
60  lastChild = this.element(item);
61  }
62  } else if (isFunction(name)) {
63  lastChild = this.element(name.apply());
64  } else if (isObject(name)) {
65  for (key in name) {
66  if (!hasProp.call(name, key)) continue;
67  val = name[key];
68  if (isFunction(val)) {
69  val = val.apply();
70  }
71  if ((isObject(val)) && (isEmpty(val))) {
72  val = null;
73  }
74  if (!this.options.ignoreDecorators && this.stringify.convertAttKey && key.indexOf(this.stringify.convertAttKey) === 0) {
75  lastChild = this.attribute(key.substr(this.stringify.convertAttKey.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 if (!this.options.ignoreDecorators && this.stringify.convertPIKey && name.indexOf(this.stringify.convertPIKey) === 0) {
100  lastChild = this.instruction(name.substr(this.stringify.convertPIKey.length), text);
101  } else {
102  lastChild = this.node(name, attributes, text);
103  }
104  }
105  if (lastChild == null) {
106  throw new Error("Could not create any elements with: " + name);
107  }
108  return lastChild;
109  };
110 
111  XMLNode.prototype.insertBefore = function(name, attributes, text) {
112  var child, i, removed;
113  if (this.isRoot) {
114  throw new Error("Cannot insert elements at root level");
115  }
116  i = this.parent.children.indexOf(this);
117  removed = this.parent.children.splice(i);
118  child = this.parent.element(name, attributes, text);
119  Array.prototype.push.apply(this.parent.children, removed);
120  return child;
121  };
122 
123  XMLNode.prototype.insertAfter = function(name, attributes, text) {
124  var child, i, removed;
125  if (this.isRoot) {
126  throw new Error("Cannot insert elements at root level");
127  }
128  i = this.parent.children.indexOf(this);
129  removed = this.parent.children.splice(i + 1);
130  child = this.parent.element(name, attributes, text);
131  Array.prototype.push.apply(this.parent.children, removed);
132  return child;
133  };
134 
135  XMLNode.prototype.remove = function() {
136  var i, ref1;
137  if (this.isRoot) {
138  throw new Error("Cannot remove the root element");
139  }
140  i = this.parent.children.indexOf(this);
141  [].splice.apply(this.parent.children, [i, i - i + 1].concat(ref1 = [])), ref1;
142  return this.parent;
143  };
144 
145  XMLNode.prototype.node = function(name, attributes, text) {
146  var child, ref1;
147  if (name != null) {
148  name = name.valueOf();
149  }
150  attributes || (attributes = {});
151  attributes = attributes.valueOf();
152  if (!isObject(attributes)) {
153  ref1 = [attributes, text], text = ref1[0], attributes = ref1[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.commentBefore = function(value) {
185  var child, i, removed;
186  i = this.parent.children.indexOf(this);
187  removed = this.parent.children.splice(i);
188  child = this.parent.comment(value);
189  Array.prototype.push.apply(this.parent.children, removed);
190  return this;
191  };
192 
193  XMLNode.prototype.commentAfter = function(value) {
194  var child, i, removed;
195  i = this.parent.children.indexOf(this);
196  removed = this.parent.children.splice(i + 1);
197  child = this.parent.comment(value);
198  Array.prototype.push.apply(this.parent.children, removed);
199  return this;
200  };
201 
202  XMLNode.prototype.raw = function(value) {
203  var child;
204  child = new XMLRaw(this, value);
205  this.children.push(child);
206  return this;
207  };
208 
209  XMLNode.prototype.instruction = function(target, value) {
210  var insTarget, insValue, instruction, j, len;
211  if (target != null) {
212  target = target.valueOf();
213  }
214  if (value != null) {
215  value = value.valueOf();
216  }
217  if (Array.isArray(target)) {
218  for (j = 0, len = target.length; j < len; j++) {
219  insTarget = target[j];
220  this.instruction(insTarget);
221  }
222  } else if (isObject(target)) {
223  for (insTarget in target) {
224  if (!hasProp.call(target, insTarget)) continue;
225  insValue = target[insTarget];
226  this.instruction(insTarget, insValue);
227  }
228  } else {
229  if (isFunction(value)) {
230  value = value.apply();
231  }
232  instruction = new XMLProcessingInstruction(this, target, value);
233  this.children.push(instruction);
234  }
235  return this;
236  };
237 
238  XMLNode.prototype.instructionBefore = function(target, value) {
239  var child, i, removed;
240  i = this.parent.children.indexOf(this);
241  removed = this.parent.children.splice(i);
242  child = this.parent.instruction(target, value);
243  Array.prototype.push.apply(this.parent.children, removed);
244  return this;
245  };
246 
247  XMLNode.prototype.instructionAfter = function(target, value) {
248  var child, i, removed;
249  i = this.parent.children.indexOf(this);
250  removed = this.parent.children.splice(i + 1);
251  child = this.parent.instruction(target, value);
252  Array.prototype.push.apply(this.parent.children, removed);
253  return this;
254  };
255 
256  XMLNode.prototype.declaration = function(version, encoding, standalone) {
257  var doc, xmldec;
258  doc = this.document();
259  xmldec = new XMLDeclaration(doc, version, encoding, standalone);
260  if (doc.children[0] instanceof XMLDeclaration) {
261  doc.children[0] = xmldec;
262  } else {
263  doc.children.unshift(xmldec);
264  }
265  return doc.root() || doc;
266  };
267 
268  XMLNode.prototype.doctype = function(pubID, sysID) {
269  var child, doc, doctype, i, j, k, len, len1, ref1, ref2;
270  doc = this.document();
271  doctype = new XMLDocType(doc, pubID, sysID);
272  ref1 = doc.children;
273  for (i = j = 0, len = ref1.length; j < len; i = ++j) {
274  child = ref1[i];
275  if (child instanceof XMLDocType) {
276  doc.children[i] = doctype;
277  return doctype;
278  }
279  }
280  ref2 = doc.children;
281  for (i = k = 0, len1 = ref2.length; k < len1; i = ++k) {
282  child = ref2[i];
283  if (child.isRoot) {
284  doc.children.splice(i, 0, doctype);
285  return doctype;
286  }
287  }
288  doc.children.push(doctype);
289  return doctype;
290  };
291 
292  XMLNode.prototype.up = function() {
293  if (this.isRoot) {
294  throw new Error("The root node has no parent. Use doc() if you need to get the document object.");
295  }
296  return this.parent;
297  };
298 
299  XMLNode.prototype.root = function() {
300  var node;
301  node = this;
302  while (node) {
303  if (node.isDocument) {
304  return node.rootObject;
305  } else if (node.isRoot) {
306  return node;
307  } else {
308  node = node.parent;
309  }
310  }
311  };
312 
313  XMLNode.prototype.document = function() {
314  var node;
315  node = this;
316  while (node) {
317  if (node.isDocument) {
318  return node;
319  } else {
320  node = node.parent;
321  }
322  }
323  };
324 
325  XMLNode.prototype.end = function(options) {
326  return this.document().end(options);
327  };
328 
329  XMLNode.prototype.prev = function() {
330  var i;
331  i = this.parent.children.indexOf(this);
332  if (i < 1) {
333  throw new Error("Already at the first node");
334  }
335  return this.parent.children[i - 1];
336  };
337 
338  XMLNode.prototype.next = function() {
339  var i;
340  i = this.parent.children.indexOf(this);
341  if (i === -1 || i === this.parent.children.length - 1) {
342  throw new Error("Already at the last node");
343  }
344  return this.parent.children[i + 1];
345  };
346 
347  XMLNode.prototype.importDocument = function(doc) {
348  var clonedRoot;
349  clonedRoot = doc.root().clone();
350  clonedRoot.parent = this;
351  clonedRoot.isRoot = false;
352  this.children.push(clonedRoot);
353  return this;
354  };
355 
356  XMLNode.prototype.ele = function(name, attributes, text) {
357  return this.element(name, attributes, text);
358  };
359 
360  XMLNode.prototype.nod = function(name, attributes, text) {
361  return this.node(name, attributes, text);
362  };
363 
364  XMLNode.prototype.txt = function(value) {
365  return this.text(value);
366  };
367 
368  XMLNode.prototype.dat = function(value) {
369  return this.cdata(value);
370  };
371 
372  XMLNode.prototype.com = function(value) {
373  return this.comment(value);
374  };
375 
376  XMLNode.prototype.ins = function(target, value) {
377  return this.instruction(target, value);
378  };
379 
380  XMLNode.prototype.doc = function() {
381  return this.document();
382  };
383 
384  XMLNode.prototype.dec = function(version, encoding, standalone) {
385  return this.declaration(version, encoding, standalone);
386  };
387 
388  XMLNode.prototype.dtd = function(pubID, sysID) {
389  return this.doctype(pubID, sysID);
390  };
391 
392  XMLNode.prototype.e = function(name, attributes, text) {
393  return this.element(name, attributes, text);
394  };
395 
396  XMLNode.prototype.n = function(name, attributes, text) {
397  return this.node(name, attributes, text);
398  };
399 
400  XMLNode.prototype.t = function(value) {
401  return this.text(value);
402  };
403 
404  XMLNode.prototype.d = function(value) {
405  return this.cdata(value);
406  };
407 
408  XMLNode.prototype.c = function(value) {
409  return this.comment(value);
410  };
411 
412  XMLNode.prototype.r = function(value) {
413  return this.raw(value);
414  };
415 
416  XMLNode.prototype.i = function(target, value) {
417  return this.instruction(target, value);
418  };
419 
420  XMLNode.prototype.u = function() {
421  return this.up();
422  };
423 
424  XMLNode.prototype.importXMLBuilder = function(doc) {
425  return this.importDocument(doc);
426  };
427 
428  return XMLNode;
429 
430  })();
431 
432 }).call(this);