xml-stream.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. "use strict";
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  4. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  5. var _ = require('./under-dash');
  6. var utils = require('./utils'); // constants
  7. var OPEN_ANGLE = '<';
  8. var CLOSE_ANGLE = '>';
  9. var OPEN_ANGLE_SLASH = '</';
  10. var CLOSE_SLASH_ANGLE = '/>';
  11. var EQUALS_QUOTE = '="';
  12. var QUOTE = '"';
  13. var SPACE = ' ';
  14. function pushAttribute(xml, name, value) {
  15. xml.push(SPACE);
  16. xml.push(name);
  17. xml.push(EQUALS_QUOTE);
  18. xml.push(utils.xmlEncode(value.toString()));
  19. xml.push(QUOTE);
  20. }
  21. function pushAttributes(xml, attributes) {
  22. if (attributes) {
  23. _.each(attributes, function (value, name) {
  24. if (value !== undefined) {
  25. pushAttribute(xml, name, value);
  26. }
  27. });
  28. }
  29. }
  30. var XmlStream = /*#__PURE__*/function () {
  31. function XmlStream() {
  32. _classCallCheck(this, XmlStream);
  33. this._xml = [];
  34. this._stack = [];
  35. this._rollbacks = [];
  36. }
  37. _createClass(XmlStream, [{
  38. key: "openXml",
  39. value: function openXml(docAttributes) {
  40. var xml = this._xml; // <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  41. xml.push('<?xml');
  42. pushAttributes(xml, docAttributes);
  43. xml.push('?>\n');
  44. }
  45. }, {
  46. key: "openNode",
  47. value: function openNode(name, attributes) {
  48. var parent = this.tos;
  49. var xml = this._xml;
  50. if (parent && this.open) {
  51. xml.push(CLOSE_ANGLE);
  52. }
  53. this._stack.push(name); // start streaming node
  54. xml.push(OPEN_ANGLE);
  55. xml.push(name);
  56. pushAttributes(xml, attributes);
  57. this.leaf = true;
  58. this.open = true;
  59. }
  60. }, {
  61. key: "addAttribute",
  62. value: function addAttribute(name, value) {
  63. if (!this.open) {
  64. throw new Error('Cannot write attributes to node if it is not open');
  65. }
  66. if (value !== undefined) {
  67. pushAttribute(this._xml, name, value);
  68. }
  69. }
  70. }, {
  71. key: "addAttributes",
  72. value: function addAttributes(attrs) {
  73. if (!this.open) {
  74. throw new Error('Cannot write attributes to node if it is not open');
  75. }
  76. pushAttributes(this._xml, attrs);
  77. }
  78. }, {
  79. key: "writeText",
  80. value: function writeText(text) {
  81. var xml = this._xml;
  82. if (this.open) {
  83. xml.push(CLOSE_ANGLE);
  84. this.open = false;
  85. }
  86. this.leaf = false;
  87. xml.push(utils.xmlEncode(text.toString()));
  88. }
  89. }, {
  90. key: "writeXml",
  91. value: function writeXml(xml) {
  92. if (this.open) {
  93. this._xml.push(CLOSE_ANGLE);
  94. this.open = false;
  95. }
  96. this.leaf = false;
  97. this._xml.push(xml);
  98. }
  99. }, {
  100. key: "closeNode",
  101. value: function closeNode() {
  102. var node = this._stack.pop();
  103. var xml = this._xml;
  104. if (this.leaf) {
  105. xml.push(CLOSE_SLASH_ANGLE);
  106. } else {
  107. xml.push(OPEN_ANGLE_SLASH);
  108. xml.push(node);
  109. xml.push(CLOSE_ANGLE);
  110. }
  111. this.open = false;
  112. this.leaf = false;
  113. }
  114. }, {
  115. key: "leafNode",
  116. value: function leafNode(name, attributes, text) {
  117. this.openNode(name, attributes);
  118. if (text !== undefined) {
  119. // zeros need to be written
  120. this.writeText(text);
  121. }
  122. this.closeNode();
  123. }
  124. }, {
  125. key: "closeAll",
  126. value: function closeAll() {
  127. while (this._stack.length) {
  128. this.closeNode();
  129. }
  130. }
  131. }, {
  132. key: "addRollback",
  133. value: function addRollback() {
  134. this._rollbacks.push({
  135. xml: this._xml.length,
  136. stack: this._stack.length,
  137. leaf: this.leaf,
  138. open: this.open
  139. });
  140. return this.cursor;
  141. }
  142. }, {
  143. key: "commit",
  144. value: function commit() {
  145. this._rollbacks.pop();
  146. }
  147. }, {
  148. key: "rollback",
  149. value: function rollback() {
  150. var r = this._rollbacks.pop();
  151. if (this._xml.length > r.xml) {
  152. this._xml.splice(r.xml, this._xml.length - r.xml);
  153. }
  154. if (this._stack.length > r.stack) {
  155. this._stack.splice(r.stack, this._stack.length - r.stack);
  156. }
  157. this.leaf = r.leaf;
  158. this.open = r.open;
  159. }
  160. }, {
  161. key: "tos",
  162. get: function get() {
  163. return this._stack.length ? this._stack[this._stack.length - 1] : undefined;
  164. }
  165. }, {
  166. key: "cursor",
  167. get: function get() {
  168. // handy way to track whether anything has been added
  169. return this._xml.length;
  170. }
  171. }, {
  172. key: "xml",
  173. get: function get() {
  174. this.closeAll();
  175. return this._xml.join('');
  176. }
  177. }]);
  178. return XmlStream;
  179. }();
  180. XmlStream.StdDocAttributes = {
  181. version: '1.0',
  182. encoding: 'UTF-8',
  183. standalone: 'yes'
  184. };
  185. module.exports = XmlStream;
  186. //# sourceMappingURL=xml-stream.js.map