XMLStringWriter.js 10 KB

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