artdaq_node_server  v1_00_08
 All Classes Namespaces Files Variables Pages
XMLDocumentCB.js
1 // Generated by CoffeeScript 1.10.0
2 (function() {
3  var XMLAttribute, XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLDocumentCB, XMLElement, XMLProcessingInstruction, XMLRaw, XMLStringWriter, XMLStringifier, XMLText, isFunction, isObject, isPlainObject, ref,
4  hasProp = {}.hasOwnProperty;
5 
6  ref = require('./Utility'), isObject = ref.isObject, isFunction = ref.isFunction, isPlainObject = ref.isPlainObject;
7 
8  XMLElement = require('./XMLElement');
9 
10  XMLCData = require('./XMLCData');
11 
12  XMLComment = require('./XMLComment');
13 
14  XMLRaw = require('./XMLRaw');
15 
16  XMLText = require('./XMLText');
17 
18  XMLProcessingInstruction = require('./XMLProcessingInstruction');
19 
20  XMLDeclaration = require('./XMLDeclaration');
21 
22  XMLDocType = require('./XMLDocType');
23 
24  XMLDTDAttList = require('./XMLDTDAttList');
25 
26  XMLDTDEntity = require('./XMLDTDEntity');
27 
28  XMLDTDElement = require('./XMLDTDElement');
29 
30  XMLDTDNotation = require('./XMLDTDNotation');
31 
32  XMLAttribute = require('./XMLAttribute');
33 
34  XMLStringifier = require('./XMLStringifier');
35 
36  XMLStringWriter = require('./XMLStringWriter');
37 
38  module.exports = XMLDocumentCB = (function() {
39  function XMLDocumentCB(options, onData, onEnd) {
40  var writerOptions;
41  options || (options = {});
42  if (!options.writer) {
43  options.writer = new XMLStringWriter(options);
44  } else if (isPlainObject(options.writer)) {
45  writerOptions = options.writer;
46  options.writer = new XMLStringWriter(writerOptions);
47  }
48  this.options = options;
49  this.writer = options.writer;
50  this.stringify = new XMLStringifier(options);
51  this.onDataCallback = onData || function() {};
52  this.onEndCallback = onEnd || function() {};
53  this.currentNode = null;
54  this.currentLevel = -1;
55  this.openTags = {};
56  this.documentStarted = false;
57  this.documentCompleted = false;
58  this.root = null;
59  }
60 
61  XMLDocumentCB.prototype.node = function(name, attributes, text) {
62  var ref1;
63  if (name == null) {
64  throw new Error("Missing node name");
65  }
66  if (this.root && this.currentLevel === -1) {
67  throw new Error("Document can only have one root node");
68  }
69  this.openCurrent();
70  name = name.valueOf();
71  if (attributes == null) {
72  attributes = {};
73  }
74  attributes = attributes.valueOf();
75  if (!isObject(attributes)) {
76  ref1 = [attributes, text], text = ref1[0], attributes = ref1[1];
77  }
78  this.currentNode = new XMLElement(this, name, attributes);
79  this.currentNode.children = false;
80  this.currentLevel++;
81  this.openTags[this.currentLevel] = this.currentNode;
82  if (text != null) {
83  this.text(text);
84  }
85  return this;
86  };
87 
88  XMLDocumentCB.prototype.element = function(name, attributes, text) {
89  if (this.currentNode && this.currentNode instanceof XMLDocType) {
90  return this.dtdElement.apply(this, arguments);
91  } else {
92  return this.node(name, attributes, text);
93  }
94  };
95 
96  XMLDocumentCB.prototype.attribute = function(name, value) {
97  var attName, attValue;
98  if (!this.currentNode || this.currentNode.children) {
99  throw new Error("att() can only be used immediately after an ele() call in callback mode");
100  }
101  if (name != null) {
102  name = name.valueOf();
103  }
104  if (isObject(name)) {
105  for (attName in name) {
106  if (!hasProp.call(name, attName)) continue;
107  attValue = name[attName];
108  this.attribute(attName, attValue);
109  }
110  } else {
111  if (isFunction(value)) {
112  value = value.apply();
113  }
114  if (!this.options.skipNullAttributes || (value != null)) {
115  this.currentNode.attributes[name] = new XMLAttribute(this, name, value);
116  }
117  }
118  return this;
119  };
120 
121  XMLDocumentCB.prototype.text = function(value) {
122  var node;
123  this.openCurrent();
124  node = new XMLText(this, value);
125  this.onData(this.writer.text(node, this.currentLevel + 1));
126  return this;
127  };
128 
129  XMLDocumentCB.prototype.cdata = function(value) {
130  var node;
131  this.openCurrent();
132  node = new XMLCData(this, value);
133  this.onData(this.writer.cdata(node, this.currentLevel + 1));
134  return this;
135  };
136 
137  XMLDocumentCB.prototype.comment = function(value) {
138  var node;
139  this.openCurrent();
140  node = new XMLComment(this, value);
141  this.onData(this.writer.comment(node, this.currentLevel + 1));
142  return this;
143  };
144 
145  XMLDocumentCB.prototype.raw = function(value) {
146  var node;
147  this.openCurrent();
148  node = new XMLRaw(this, value);
149  this.onData(this.writer.raw(node, this.currentLevel + 1));
150  return this;
151  };
152 
153  XMLDocumentCB.prototype.instruction = function(target, value) {
154  var i, insTarget, insValue, len, node;
155  this.openCurrent();
156  if (target != null) {
157  target = target.valueOf();
158  }
159  if (value != null) {
160  value = value.valueOf();
161  }
162  if (Array.isArray(target)) {
163  for (i = 0, len = target.length; i < len; i++) {
164  insTarget = target[i];
165  this.instruction(insTarget);
166  }
167  } else if (isObject(target)) {
168  for (insTarget in target) {
169  if (!hasProp.call(target, insTarget)) continue;
170  insValue = target[insTarget];
171  this.instruction(insTarget, insValue);
172  }
173  } else {
174  if (isFunction(value)) {
175  value = value.apply();
176  }
177  node = new XMLProcessingInstruction(this, target, value);
178  this.onData(this.writer.processingInstruction(node, this.currentLevel + 1));
179  }
180  return this;
181  };
182 
183  XMLDocumentCB.prototype.declaration = function(version, encoding, standalone) {
184  var node;
185  this.openCurrent();
186  if (this.documentStarted) {
187  throw new Error("declaration() must be the first node");
188  }
189  node = new XMLDeclaration(this, version, encoding, standalone);
190  this.onData(this.writer.declaration(node, this.currentLevel + 1));
191  return this;
192  };
193 
194  XMLDocumentCB.prototype.doctype = function(root, pubID, sysID) {
195  this.openCurrent();
196  if (root == null) {
197  throw new Error("Missing root node name");
198  }
199  if (this.root) {
200  throw new Error("dtd() must come before the root node");
201  }
202  this.currentNode = new XMLDocType(this, pubID, sysID);
203  this.currentNode.rootNodeName = root;
204  this.currentNode.children = false;
205  this.currentLevel++;
206  this.openTags[this.currentLevel] = this.currentNode;
207  return this;
208  };
209 
210  XMLDocumentCB.prototype.dtdElement = function(name, value) {
211  var node;
212  this.openCurrent();
213  node = new XMLDTDElement(this, name, value);
214  this.onData(this.writer.dtdElement(node, this.currentLevel + 1));
215  return this;
216  };
217 
218  XMLDocumentCB.prototype.attList = function(elementName, attributeName, attributeType, defaultValueType, defaultValue) {
219  var node;
220  this.openCurrent();
221  node = new XMLDTDAttList(this, elementName, attributeName, attributeType, defaultValueType, defaultValue);
222  this.onData(this.writer.dtdAttList(node, this.currentLevel + 1));
223  return this;
224  };
225 
226  XMLDocumentCB.prototype.entity = function(name, value) {
227  var node;
228  this.openCurrent();
229  node = new XMLDTDEntity(this, false, name, value);
230  this.onData(this.writer.dtdEntity(node, this.currentLevel + 1));
231  return this;
232  };
233 
234  XMLDocumentCB.prototype.pEntity = function(name, value) {
235  var node;
236  this.openCurrent();
237  node = new XMLDTDEntity(this, true, name, value);
238  this.onData(this.writer.dtdEntity(node, this.currentLevel + 1));
239  return this;
240  };
241 
242  XMLDocumentCB.prototype.notation = function(name, value) {
243  var node;
244  this.openCurrent();
245  node = new XMLDTDNotation(this, name, value);
246  this.onData(this.writer.dtdNotation(node, this.currentLevel + 1));
247  return this;
248  };
249 
250  XMLDocumentCB.prototype.up = function() {
251  if (this.currentLevel < 0) {
252  throw new Error("The document node has no parent");
253  }
254  if (this.currentNode) {
255  if (this.currentNode.children) {
256  this.closeNode(this.currentNode);
257  } else {
258  this.openNode(this.currentNode);
259  }
260  this.currentNode = null;
261  } else {
262  this.closeNode(this.openTags[this.currentLevel]);
263  }
264  delete this.openTags[this.currentLevel];
265  this.currentLevel--;
266  return this;
267  };
268 
269  XMLDocumentCB.prototype.end = function() {
270  while (this.currentLevel >= 0) {
271  this.up();
272  }
273  return this.onEnd();
274  };
275 
276  XMLDocumentCB.prototype.openCurrent = function() {
277  if (this.currentNode) {
278  this.currentNode.children = true;
279  return this.openNode(this.currentNode);
280  }
281  };
282 
283  XMLDocumentCB.prototype.openNode = function(node) {
284  if (!node.isOpen) {
285  if (!this.root && this.currentLevel === 0 && node instanceof XMLElement) {
286  this.root = node;
287  }
288  this.onData(this.writer.openNode(node, this.currentLevel));
289  return node.isOpen = true;
290  }
291  };
292 
293  XMLDocumentCB.prototype.closeNode = function(node) {
294  if (!node.isClosed) {
295  this.onData(this.writer.closeNode(node, this.currentLevel));
296  return node.isClosed = true;
297  }
298  };
299 
300  XMLDocumentCB.prototype.onData = function(chunk) {
301  this.documentStarted = true;
302  return this.onDataCallback(chunk);
303  };
304 
305  XMLDocumentCB.prototype.onEnd = function() {
306  this.documentCompleted = true;
307  return this.onEndCallback();
308  };
309 
310  XMLDocumentCB.prototype.ele = function() {
311  return this.element.apply(this, arguments);
312  };
313 
314  XMLDocumentCB.prototype.nod = function(name, attributes, text) {
315  return this.node(name, attributes, text);
316  };
317 
318  XMLDocumentCB.prototype.txt = function(value) {
319  return this.text(value);
320  };
321 
322  XMLDocumentCB.prototype.dat = function(value) {
323  return this.cdata(value);
324  };
325 
326  XMLDocumentCB.prototype.com = function(value) {
327  return this.comment(value);
328  };
329 
330  XMLDocumentCB.prototype.ins = function(target, value) {
331  return this.instruction(target, value);
332  };
333 
334  XMLDocumentCB.prototype.dec = function(version, encoding, standalone) {
335  return this.declaration(version, encoding, standalone);
336  };
337 
338  XMLDocumentCB.prototype.dtd = function(root, pubID, sysID) {
339  return this.doctype(root, pubID, sysID);
340  };
341 
342  XMLDocumentCB.prototype.e = function(name, attributes, text) {
343  return this.element(name, attributes, text);
344  };
345 
346  XMLDocumentCB.prototype.n = function(name, attributes, text) {
347  return this.node(name, attributes, text);
348  };
349 
350  XMLDocumentCB.prototype.t = function(value) {
351  return this.text(value);
352  };
353 
354  XMLDocumentCB.prototype.d = function(value) {
355  return this.cdata(value);
356  };
357 
358  XMLDocumentCB.prototype.c = function(value) {
359  return this.comment(value);
360  };
361 
362  XMLDocumentCB.prototype.r = function(value) {
363  return this.raw(value);
364  };
365 
366  XMLDocumentCB.prototype.i = function(target, value) {
367  return this.instruction(target, value);
368  };
369 
370  XMLDocumentCB.prototype.att = function() {
371  if (this.currentNode && this.currentNode instanceof XMLDocType) {
372  return this.attList.apply(this, arguments);
373  } else {
374  return this.attribute.apply(this, arguments);
375  }
376  };
377 
378  XMLDocumentCB.prototype.a = function() {
379  if (this.currentNode && this.currentNode instanceof XMLDocType) {
380  return this.attList.apply(this, arguments);
381  } else {
382  return this.attribute.apply(this, arguments);
383  }
384  };
385 
386  XMLDocumentCB.prototype.ent = function(name, value) {
387  return this.entity(name, value);
388  };
389 
390  XMLDocumentCB.prototype.pent = function(name, value) {
391  return this.pEntity(name, value);
392  };
393 
394  XMLDocumentCB.prototype.not = function(name, value) {
395  return this.notation(name, value);
396  };
397 
398  return XMLDocumentCB;
399 
400  })();
401 
402 }).call(this);