No Description

XMLStreamWriter.js 10KB

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