artdaq_node_server  v1_00_07
 All Classes Namespaces Files Variables Pages
XMLStringWriter.js
1 // Generated by CoffeeScript 1.10.0
2 (function() {
3  var XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLElement, XMLProcessingInstruction, XMLRaw, XMLStringWriter, XMLText, XMLWriterBase,
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  XMLDeclaration = require('./XMLDeclaration');
8 
9  XMLDocType = require('./XMLDocType');
10 
11  XMLCData = require('./XMLCData');
12 
13  XMLComment = require('./XMLComment');
14 
15  XMLElement = require('./XMLElement');
16 
17  XMLRaw = require('./XMLRaw');
18 
19  XMLText = require('./XMLText');
20 
21  XMLProcessingInstruction = require('./XMLProcessingInstruction');
22 
23  XMLDTDAttList = require('./XMLDTDAttList');
24 
25  XMLDTDElement = require('./XMLDTDElement');
26 
27  XMLDTDEntity = require('./XMLDTDEntity');
28 
29  XMLDTDNotation = require('./XMLDTDNotation');
30 
31  XMLWriterBase = require('./XMLWriterBase');
32 
33  module.exports = XMLStringWriter = (function(superClass) {
34  extend(XMLStringWriter, superClass);
35 
36  function XMLStringWriter(options) {
37  XMLStringWriter.__super__.constructor.call(this, options);
38  }
39 
40  XMLStringWriter.prototype.document = function(doc) {
41  var child, i, len, r, ref;
42  r = '';
43  ref = doc.children;
44  for (i = 0, len = ref.length; i < len; i++) {
45  child = ref[i];
46  r += (function() {
47  switch (false) {
48  case !(child instanceof XMLDeclaration):
49  return this.declaration(child);
50  case !(child instanceof XMLDocType):
51  return this.docType(child);
52  case !(child instanceof XMLComment):
53  return this.comment(child);
54  case !(child instanceof XMLProcessingInstruction):
55  return this.processingInstruction(child);
56  default:
57  return this.element(child, 0);
58  }
59  }).call(this);
60  }
61  if (this.pretty && r.slice(-this.newline.length) === this.newline) {
62  r = r.slice(0, -this.newline.length);
63  }
64  return r;
65  };
66 
67  XMLStringWriter.prototype.attribute = function(att) {
68  return ' ' + att.name + '="' + att.value + '"';
69  };
70 
71  XMLStringWriter.prototype.cdata = function(node, level) {
72  return this.space(level) + '<![CDATA[' + node.text + ']]>' + this.newline;
73  };
74 
75  XMLStringWriter.prototype.comment = function(node, level) {
76  return this.space(level) + '<!-- ' + node.text + ' -->' + this.newline;
77  };
78 
79  XMLStringWriter.prototype.declaration = function(node, level) {
80  var r;
81  r = this.space(level);
82  r += '<?xml version="' + node.version + '"';
83  if (node.encoding != null) {
84  r += ' encoding="' + node.encoding + '"';
85  }
86  if (node.standalone != null) {
87  r += ' standalone="' + node.standalone + '"';
88  }
89  r += '?>';
90  r += this.newline;
91  return r;
92  };
93 
94  XMLStringWriter.prototype.docType = function(node, level) {
95  var child, i, len, r, ref;
96  level || (level = 0);
97  r = this.space(level);
98  r += '<!DOCTYPE ' + node.root().name;
99  if (node.pubID && node.sysID) {
100  r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
101  } else if (node.sysID) {
102  r += ' SYSTEM "' + node.sysID + '"';
103  }
104  if (node.children.length > 0) {
105  r += ' [';
106  r += this.newline;
107  ref = node.children;
108  for (i = 0, len = ref.length; i < len; i++) {
109  child = ref[i];
110  r += (function() {
111  switch (false) {
112  case !(child instanceof XMLDTDAttList):
113  return this.dtdAttList(child, level + 1);
114  case !(child instanceof XMLDTDElement):
115  return this.dtdElement(child, level + 1);
116  case !(child instanceof XMLDTDEntity):
117  return this.dtdEntity(child, level + 1);
118  case !(child instanceof XMLDTDNotation):
119  return this.dtdNotation(child, level + 1);
120  case !(child instanceof XMLCData):
121  return this.cdata(child, level + 1);
122  case !(child instanceof XMLComment):
123  return this.comment(child, level + 1);
124  case !(child instanceof XMLProcessingInstruction):
125  return this.processingInstruction(child, level + 1);
126  default:
127  throw new Error("Unknown DTD node type: " + child.constructor.name);
128  }
129  }).call(this);
130  }
131  r += ']';
132  }
133  r += '>';
134  r += this.newline;
135  return r;
136  };
137 
138  XMLStringWriter.prototype.element = function(node, level) {
139  var att, child, i, len, name, r, ref, ref1, space;
140  level || (level = 0);
141  space = this.space(level);
142  r = '';
143  r += space + '<' + node.name;
144  ref = node.attributes;
145  for (name in ref) {
146  if (!hasProp.call(ref, name)) continue;
147  att = ref[name];
148  r += this.attribute(att);
149  }
150  if (node.children.length === 0 || node.children.every(function(e) {
151  return e.value === '';
152  })) {
153  if (this.allowEmpty) {
154  r += '></' + node.name + '>' + this.newline;
155  } else {
156  r += '/>' + this.newline;
157  }
158  } else if (this.pretty && node.children.length === 1 && (node.children[0].value != null)) {
159  r += '>';
160  r += node.children[0].value;
161  r += '</' + node.name + '>' + this.newline;
162  } else {
163  r += '>' + this.newline;
164  ref1 = node.children;
165  for (i = 0, len = ref1.length; i < len; i++) {
166  child = ref1[i];
167  r += (function() {
168  switch (false) {
169  case !(child instanceof XMLCData):
170  return this.cdata(child, level + 1);
171  case !(child instanceof XMLComment):
172  return this.comment(child, level + 1);
173  case !(child instanceof XMLElement):
174  return this.element(child, level + 1);
175  case !(child instanceof XMLRaw):
176  return this.raw(child, level + 1);
177  case !(child instanceof XMLText):
178  return this.text(child, level + 1);
179  case !(child instanceof XMLProcessingInstruction):
180  return this.processingInstruction(child, level + 1);
181  default:
182  throw new Error("Unknown XML node type: " + child.constructor.name);
183  }
184  }).call(this);
185  }
186  r += space + '</' + node.name + '>' + this.newline;
187  }
188  return r;
189  };
190 
191  XMLStringWriter.prototype.processingInstruction = function(node, level) {
192  var r;
193  r = this.space(level) + '<?' + node.target;
194  if (node.value) {
195  r += ' ' + node.value;
196  }
197  r += '?>' + this.newline;
198  return r;
199  };
200 
201  XMLStringWriter.prototype.raw = function(node, level) {
202  return this.space(level) + node.value + this.newline;
203  };
204 
205  XMLStringWriter.prototype.text = function(node, level) {
206  return this.space(level) + node.value + this.newline;
207  };
208 
209  XMLStringWriter.prototype.dtdAttList = function(node, level) {
210  var r;
211  r = this.space(level) + '<!ATTLIST ' + node.elementName + ' ' + node.attributeName + ' ' + node.attributeType;
212  if (node.defaultValueType !== '#DEFAULT') {
213  r += ' ' + node.defaultValueType;
214  }
215  if (node.defaultValue) {
216  r += ' "' + node.defaultValue + '"';
217  }
218  r += '>' + this.newline;
219  return r;
220  };
221 
222  XMLStringWriter.prototype.dtdElement = function(node, level) {
223  return this.space(level) + '<!ELEMENT ' + node.name + ' ' + node.value + '>' + this.newline;
224  };
225 
226  XMLStringWriter.prototype.dtdEntity = function(node, level) {
227  var r;
228  r = this.space(level) + '<!ENTITY';
229  if (node.pe) {
230  r += ' %';
231  }
232  r += ' ' + node.name;
233  if (node.value) {
234  r += ' "' + node.value + '"';
235  } else {
236  if (node.pubID && node.sysID) {
237  r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
238  } else if (node.sysID) {
239  r += ' SYSTEM "' + node.sysID + '"';
240  }
241  if (node.nData) {
242  r += ' NDATA ' + node.nData;
243  }
244  }
245  r += '>' + this.newline;
246  return r;
247  };
248 
249  XMLStringWriter.prototype.dtdNotation = function(node, level) {
250  var r;
251  r = this.space(level) + '<!NOTATION ' + node.name;
252  if (node.pubID && node.sysID) {
253  r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
254  } else if (node.pubID) {
255  r += ' PUBLIC "' + node.pubID + '"';
256  } else if (node.sysID) {
257  r += ' SYSTEM "' + node.sysID + '"';
258  }
259  r += '>' + this.newline;
260  return r;
261  };
262 
263  XMLStringWriter.prototype.openNode = function(node, level) {
264  var att, name, r, ref;
265  level || (level = 0);
266  if (node instanceof XMLElement) {
267  r = this.space(level) + '<' + node.name;
268  ref = node.attributes;
269  for (name in ref) {
270  if (!hasProp.call(ref, name)) continue;
271  att = ref[name];
272  r += this.attribute(att);
273  }
274  r += (node.children ? '>' : '/>') + this.newline;
275  return r;
276  } else {
277  r = this.space(level) + '<!DOCTYPE ' + node.rootNodeName;
278  if (node.pubID && node.sysID) {
279  r += ' PUBLIC "' + node.pubID + '" "' + node.sysID + '"';
280  } else if (node.sysID) {
281  r += ' SYSTEM "' + node.sysID + '"';
282  }
283  r += (node.children ? ' [' : '>') + this.newline;
284  return r;
285  }
286  };
287 
288  XMLStringWriter.prototype.closeNode = function(node, level) {
289  level || (level = 0);
290  switch (false) {
291  case !(node instanceof XMLElement):
292  return this.space(level) + '</' + node.name + '>' + this.newline;
293  case !(node instanceof XMLDocType):
294  return this.space(level) + ']>' + this.newline;
295  }
296  };
297 
298  return XMLStringWriter;
299 
300  })(XMLWriterBase);
301 
302 }).call(this);