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