00001
00002 (function() {
00003 var XMLCData, XMLComment, XMLDTDAttList, XMLDTDElement, XMLDTDEntity, XMLDTDNotation, XMLDeclaration, XMLDocType, XMLElement, XMLProcessingInstruction, XMLRaw, XMLStreamWriter, XMLText, XMLWriterBase,
00004 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; },
00005 hasProp = {}.hasOwnProperty;
00006
00007 XMLDeclaration = require('./XMLDeclaration');
00008
00009 XMLDocType = require('./XMLDocType');
00010
00011 XMLCData = require('./XMLCData');
00012
00013 XMLComment = require('./XMLComment');
00014
00015 XMLElement = require('./XMLElement');
00016
00017 XMLRaw = require('./XMLRaw');
00018
00019 XMLText = require('./XMLText');
00020
00021 XMLProcessingInstruction = require('./XMLProcessingInstruction');
00022
00023 XMLDTDAttList = require('./XMLDTDAttList');
00024
00025 XMLDTDElement = require('./XMLDTDElement');
00026
00027 XMLDTDEntity = require('./XMLDTDEntity');
00028
00029 XMLDTDNotation = require('./XMLDTDNotation');
00030
00031 XMLWriterBase = require('./XMLWriterBase');
00032
00033 module.exports = XMLStreamWriter = (function(superClass) {
00034 extend(XMLStreamWriter, superClass);
00035
00036 function XMLStreamWriter(stream, options) {
00037 this.stream = stream;
00038 XMLStreamWriter.__super__.constructor.call(this, options);
00039 }
00040
00041 XMLStreamWriter.prototype.document = function(doc) {
00042 var child, i, j, len, len1, ref, ref1, results;
00043 ref = doc.children;
00044 for (i = 0, len = ref.length; i < len; i++) {
00045 child = ref[i];
00046 child.isLastRootNode = false;
00047 }
00048 doc.children[doc.children.length - 1].isLastRootNode = true;
00049 ref1 = doc.children;
00050 results = [];
00051 for (j = 0, len1 = ref1.length; j < len1; j++) {
00052 child = ref1[j];
00053 switch (false) {
00054 case !(child instanceof XMLDeclaration):
00055 results.push(this.declaration(child));
00056 break;
00057 case !(child instanceof XMLDocType):
00058 results.push(this.docType(child));
00059 break;
00060 case !(child instanceof XMLComment):
00061 results.push(this.comment(child));
00062 break;
00063 case !(child instanceof XMLProcessingInstruction):
00064 results.push(this.processingInstruction(child));
00065 break;
00066 default:
00067 results.push(this.element(child));
00068 }
00069 }
00070 return results;
00071 };
00072
00073 XMLStreamWriter.prototype.attribute = function(att) {
00074 return this.stream.write(' ' + att.name + '="' + att.value + '"');
00075 };
00076
00077 XMLStreamWriter.prototype.cdata = function(node, level) {
00078 return this.stream.write(this.space(level) + '<![CDATA[' + node.text + ']]>' + this.endline(node));
00079 };
00080
00081 XMLStreamWriter.prototype.comment = function(node, level) {
00082 return this.stream.write(this.space(level) + '<!-- ' + node.text + ' -->' + this.endline(node));
00083 };
00084
00085 XMLStreamWriter.prototype.declaration = function(node, level) {
00086 this.stream.write(this.space(level));
00087 this.stream.write('<?xml version="' + node.version + '"');
00088 if (node.encoding != null) {
00089 this.stream.write(' encoding="' + node.encoding + '"');
00090 }
00091 if (node.standalone != null) {
00092 this.stream.write(' standalone="' + node.standalone + '"');
00093 }
00094 this.stream.write('?>');
00095 return this.stream.write(this.endline(node));
00096 };
00097
00098 XMLStreamWriter.prototype.docType = function(node, level) {
00099 var child, i, len, ref;
00100 level || (level = 0);
00101 this.stream.write(this.space(level));
00102 this.stream.write('<!DOCTYPE ' + node.root().name);
00103 if (node.pubID && node.sysID) {
00104 this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
00105 } else if (node.sysID) {
00106 this.stream.write(' SYSTEM "' + node.sysID + '"');
00107 }
00108 if (node.children.length > 0) {
00109 this.stream.write(' [');
00110 this.stream.write(this.endline(node));
00111 ref = node.children;
00112 for (i = 0, len = ref.length; i < len; i++) {
00113 child = ref[i];
00114 switch (false) {
00115 case !(child instanceof XMLDTDAttList):
00116 this.dtdAttList(child, level + 1);
00117 break;
00118 case !(child instanceof XMLDTDElement):
00119 this.dtdElement(child, level + 1);
00120 break;
00121 case !(child instanceof XMLDTDEntity):
00122 this.dtdEntity(child, level + 1);
00123 break;
00124 case !(child instanceof XMLDTDNotation):
00125 this.dtdNotation(child, level + 1);
00126 break;
00127 case !(child instanceof XMLCData):
00128 this.cdata(child, level + 1);
00129 break;
00130 case !(child instanceof XMLComment):
00131 this.comment(child, level + 1);
00132 break;
00133 case !(child instanceof XMLProcessingInstruction):
00134 this.processingInstruction(child, level + 1);
00135 break;
00136 default:
00137 throw new Error("Unknown DTD node type: " + child.constructor.name);
00138 }
00139 }
00140 this.stream.write(']');
00141 }
00142 this.stream.write('>');
00143 return this.stream.write(this.endline(node));
00144 };
00145
00146 XMLStreamWriter.prototype.element = function(node, level) {
00147 var att, child, i, len, name, ref, ref1, space;
00148 level || (level = 0);
00149 space = this.space(level);
00150 this.stream.write(space + '<' + node.name);
00151 ref = node.attributes;
00152 for (name in ref) {
00153 if (!hasProp.call(ref, name)) continue;
00154 att = ref[name];
00155 this.attribute(att);
00156 }
00157 if (node.children.length === 0 || node.children.every(function(e) {
00158 return e.value === '';
00159 })) {
00160 if (this.allowEmpty) {
00161 this.stream.write('></' + node.name + '>');
00162 } else {
00163 this.stream.write('/>');
00164 }
00165 } else if (this.pretty && node.children.length === 1 && (node.children[0].value != null)) {
00166 this.stream.write('>');
00167 this.stream.write(node.children[0].value);
00168 this.stream.write('</' + node.name + '>');
00169 } else {
00170 this.stream.write('>' + this.newline);
00171 ref1 = node.children;
00172 for (i = 0, len = ref1.length; i < len; i++) {
00173 child = ref1[i];
00174 switch (false) {
00175 case !(child instanceof XMLCData):
00176 this.cdata(child, level + 1);
00177 break;
00178 case !(child instanceof XMLComment):
00179 this.comment(child, level + 1);
00180 break;
00181 case !(child instanceof XMLElement):
00182 this.element(child, level + 1);
00183 break;
00184 case !(child instanceof XMLRaw):
00185 this.raw(child, level + 1);
00186 break;
00187 case !(child instanceof XMLText):
00188 this.text(child, level + 1);
00189 break;
00190 case !(child instanceof XMLProcessingInstruction):
00191 this.processingInstruction(child, level + 1);
00192 break;
00193 default:
00194 throw new Error("Unknown XML node type: " + child.constructor.name);
00195 }
00196 }
00197 this.stream.write(space + '</' + node.name + '>');
00198 }
00199 return this.stream.write(this.endline(node));
00200 };
00201
00202 XMLStreamWriter.prototype.processingInstruction = function(node, level) {
00203 this.stream.write(this.space(level) + '<?' + node.target);
00204 if (node.value) {
00205 this.stream.write(' ' + node.value);
00206 }
00207 return this.stream.write('?>' + this.endline(node));
00208 };
00209
00210 XMLStreamWriter.prototype.raw = function(node, level) {
00211 return this.stream.write(this.space(level) + node.value + this.endline(node));
00212 };
00213
00214 XMLStreamWriter.prototype.text = function(node, level) {
00215 return this.stream.write(this.space(level) + node.value + this.endline(node));
00216 };
00217
00218 XMLStreamWriter.prototype.dtdAttList = function(node, level) {
00219 this.stream.write(this.space(level) + '<!ATTLIST ' + node.elementName + ' ' + node.attributeName + ' ' + node.attributeType);
00220 if (node.defaultValueType !== '#DEFAULT') {
00221 this.stream.write(' ' + node.defaultValueType);
00222 }
00223 if (node.defaultValue) {
00224 this.stream.write(' "' + node.defaultValue + '"');
00225 }
00226 return this.stream.write('>' + this.endline(node));
00227 };
00228
00229 XMLStreamWriter.prototype.dtdElement = function(node, level) {
00230 return this.stream.write(this.space(level) + '<!ELEMENT ' + node.name + ' ' + node.value + '>' + this.endline(node));
00231 };
00232
00233 XMLStreamWriter.prototype.dtdEntity = function(node, level) {
00234 this.stream.write(this.space(level) + '<!ENTITY');
00235 if (node.pe) {
00236 this.stream.write(' %');
00237 }
00238 this.stream.write(' ' + node.name);
00239 if (node.value) {
00240 this.stream.write(' "' + node.value + '"');
00241 } else {
00242 if (node.pubID && node.sysID) {
00243 this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
00244 } else if (node.sysID) {
00245 this.stream.write(' SYSTEM "' + node.sysID + '"');
00246 }
00247 if (node.nData) {
00248 this.stream.write(' NDATA ' + node.nData);
00249 }
00250 }
00251 return this.stream.write('>' + this.endline(node));
00252 };
00253
00254 XMLStreamWriter.prototype.dtdNotation = function(node, level) {
00255 this.stream.write(this.space(level) + '<!NOTATION ' + node.name);
00256 if (node.pubID && node.sysID) {
00257 this.stream.write(' PUBLIC "' + node.pubID + '" "' + node.sysID + '"');
00258 } else if (node.pubID) {
00259 this.stream.write(' PUBLIC "' + node.pubID + '"');
00260 } else if (node.sysID) {
00261 this.stream.write(' SYSTEM "' + node.sysID + '"');
00262 }
00263 return this.stream.write('>' + this.endline(node));
00264 };
00265
00266 XMLStreamWriter.prototype.endline = function(node) {
00267 if (!node.isLastRootNode) {
00268 return this.newline;
00269 } else {
00270 return '';
00271 }
00272 };
00273
00274 return XMLStreamWriter;
00275
00276 })(XMLWriterBase);
00277
00278 }).call(this);