XMLStreamWriter.js 10 KB

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