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;
7 XMLDeclaration = require(
'./XMLDeclaration');
9 XMLDocType = require(
'./XMLDocType');
11 XMLCData = require(
'./XMLCData');
13 XMLComment = require(
'./XMLComment');
15 XMLElement = require(
'./XMLElement');
17 XMLRaw = require(
'./XMLRaw');
19 XMLText = require(
'./XMLText');
21 XMLProcessingInstruction = require(
'./XMLProcessingInstruction');
23 XMLDTDAttList = require(
'./XMLDTDAttList');
25 XMLDTDElement = require(
'./XMLDTDElement');
27 XMLDTDEntity = require(
'./XMLDTDEntity');
29 XMLDTDNotation = require(
'./XMLDTDNotation');
31 XMLWriterBase = require(
'./XMLWriterBase');
33 module.exports = XMLStreamWriter = (
function(superClass) {
34 extend(XMLStreamWriter, superClass);
36 function XMLStreamWriter(stream, options) {
38 XMLStreamWriter.__super__.constructor.call(
this, options);
41 XMLStreamWriter.prototype.document =
function(doc) {
42 var child, i, j, len, len1, ref, ref1, results;
44 for (i = 0, len = ref.length; i < len; i++) {
46 child.isLastRootNode =
false;
48 doc.children[doc.children.length - 1].isLastRootNode =
true;
51 for (j = 0, len1 = ref1.length; j < len1; j++) {
54 case !(child instanceof XMLDeclaration):
55 results.push(
this.declaration(child));
57 case !(child instanceof XMLDocType):
58 results.push(
this.docType(child));
60 case !(child instanceof XMLComment):
61 results.push(
this.comment(child));
63 case !(child instanceof XMLProcessingInstruction):
64 results.push(
this.processingInstruction(child));
67 results.push(this.element(child));
73 XMLStreamWriter.prototype.attribute =
function(att) {
74 return this.stream.write(
' ' + att.name +
'="' + att.value +
'"');
77 XMLStreamWriter.prototype.cdata =
function(node, level) {
78 return this.stream.write(this.space(level) +
'<![CDATA[' + node.text +
']]>' +
this.endline(node));
81 XMLStreamWriter.prototype.comment =
function(node, level) {
82 return this.stream.write(this.space(level) +
'<!-- ' + node.text +
' -->' +
this.endline(node));
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 +
'"');
91 if (node.standalone != null) {
92 this.stream.write(
' standalone="' + node.standalone +
'"');
94 this.stream.write(
'?>');
95 return this.stream.write(this.endline(node));
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 +
'"');
108 if (node.children.length > 0) {
109 this.stream.write(
' [');
110 this.stream.write(this.endline(node));
112 for (i = 0, len = ref.length; i < len; i++) {
115 case !(child instanceof XMLDTDAttList):
116 this.dtdAttList(child, level + 1);
118 case !(child instanceof XMLDTDElement):
119 this.dtdElement(child, level + 1);
121 case !(child instanceof XMLDTDEntity):
122 this.dtdEntity(child, level + 1);
124 case !(child instanceof XMLDTDNotation):
125 this.dtdNotation(child, level + 1);
127 case !(child instanceof XMLCData):
128 this.cdata(child, level + 1);
130 case !(child instanceof XMLComment):
131 this.comment(child, level + 1);
133 case !(child instanceof XMLProcessingInstruction):
134 this.processingInstruction(child, level + 1);
137 throw new Error(
"Unknown DTD node type: " + child.constructor.name);
140 this.stream.write(
']');
142 this.stream.write(
'>');
143 return this.stream.write(this.endline(node));
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;
153 if (!hasProp.call(ref, name))
continue;
157 if (node.children.length === 0 || node.children.every(
function(e) {
158 return e.value ===
'';
160 if (this.allowEmpty) {
161 this.stream.write(
'></' + node.name +
'>');
163 this.stream.write(
'/>');
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 +
'>');
170 this.stream.write(
'>' + this.newline);
171 ref1 = node.children;
172 for (i = 0, len = ref1.length; i < len; i++) {
175 case !(child instanceof XMLCData):
176 this.cdata(child, level + 1);
178 case !(child instanceof XMLComment):
179 this.comment(child, level + 1);
181 case !(child instanceof XMLElement):
182 this.element(child, level + 1);
184 case !(child instanceof XMLRaw):
185 this.raw(child, level + 1);
187 case !(child instanceof XMLText):
188 this.text(child, level + 1);
190 case !(child instanceof XMLProcessingInstruction):
191 this.processingInstruction(child, level + 1);
194 throw new Error(
"Unknown XML node type: " + child.constructor.name);
197 this.stream.write(space +
'</' + node.name +
'>');
199 return this.stream.write(this.endline(node));
202 XMLStreamWriter.prototype.processingInstruction =
function(node, level) {
203 this.stream.write(this.space(level) +
'<?' + node.target);
205 this.stream.write(
' ' + node.value);
207 return this.stream.write(
'?>' + this.endline(node));
210 XMLStreamWriter.prototype.raw =
function(node, level) {
211 return this.stream.write(this.space(level) + node.value +
this.endline(node));
214 XMLStreamWriter.prototype.text =
function(node, level) {
215 return this.stream.write(this.space(level) + node.value +
this.endline(node));
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);
223 if (node.defaultValue) {
224 this.stream.write(
' "' + node.defaultValue +
'"');
226 return this.stream.write(
'>' + this.endline(node));
229 XMLStreamWriter.prototype.dtdElement =
function(node, level) {
230 return this.stream.write(this.space(level) +
'<!ELEMENT ' + node.name +
' ' + node.value +
'>' +
this.endline(node));
233 XMLStreamWriter.prototype.dtdEntity =
function(node, level) {
234 this.stream.write(this.space(level) +
'<!ENTITY');
236 this.stream.write(
' %');
238 this.stream.write(
' ' + node.name);
240 this.stream.write(
' "' + node.value +
'"');
242 if (node.pubID && node.sysID) {
243 this.stream.write(
' PUBLIC "' + node.pubID +
'" "' + node.sysID +
'"');
244 }
else if (node.sysID) {
245 this.stream.write(
' SYSTEM "' + node.sysID +
'"');
248 this.stream.write(
' NDATA ' + node.nData);
251 return this.stream.write(
'>' + this.endline(node));
254 XMLStreamWriter.prototype.dtdNotation =
function(node, level) {
255 this.stream.write(this.space(level) +
'<!NOTATION ' + node.name);
256 if (node.pubID && node.sysID) {
257 this.stream.write(
' PUBLIC "' + node.pubID +
'" "' + node.sysID +
'"');
258 }
else if (node.pubID) {
259 this.stream.write(
' PUBLIC "' + node.pubID +
'"');
260 }
else if (node.sysID) {
261 this.stream.write(
' SYSTEM "' + node.sysID +
'"');
263 return this.stream.write(
'>' + this.endline(node));
266 XMLStreamWriter.prototype.endline =
function(node) {
267 if (!node.isLastRootNode) {
274 return XMLStreamWriter;