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