builder.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Generated by CoffeeScript 1.12.7
  2. (function() {
  3. "use strict";
  4. var builder, defaults, escapeCDATA, requiresCDATA, wrapCDATA,
  5. hasProp = {}.hasOwnProperty;
  6. builder = require('xmlbuilder');
  7. defaults = require('./defaults').defaults;
  8. requiresCDATA = function(entry) {
  9. return typeof entry === "string" && (entry.indexOf('&') >= 0 || entry.indexOf('>') >= 0 || entry.indexOf('<') >= 0);
  10. };
  11. wrapCDATA = function(entry) {
  12. return "<![CDATA[" + (escapeCDATA(entry)) + "]]>";
  13. };
  14. escapeCDATA = function(entry) {
  15. return entry.replace(']]>', ']]]]><![CDATA[>');
  16. };
  17. exports.Builder = (function() {
  18. function Builder(opts) {
  19. var key, ref, value;
  20. this.options = {};
  21. ref = defaults["0.2"];
  22. for (key in ref) {
  23. if (!hasProp.call(ref, key)) continue;
  24. value = ref[key];
  25. this.options[key] = value;
  26. }
  27. for (key in opts) {
  28. if (!hasProp.call(opts, key)) continue;
  29. value = opts[key];
  30. this.options[key] = value;
  31. }
  32. }
  33. Builder.prototype.buildObject = function(rootObj) {
  34. var attrkey, charkey, render, rootElement, rootName;
  35. attrkey = this.options.attrkey;
  36. charkey = this.options.charkey;
  37. if ((Object.keys(rootObj).length === 1) && (this.options.rootName === defaults['0.2'].rootName)) {
  38. rootName = Object.keys(rootObj)[0];
  39. rootObj = rootObj[rootName];
  40. } else {
  41. rootName = this.options.rootName;
  42. }
  43. render = (function(_this) {
  44. return function(element, obj) {
  45. var attr, child, entry, index, key, value;
  46. if (typeof obj !== 'object') {
  47. if (_this.options.cdata && requiresCDATA(obj)) {
  48. element.raw(wrapCDATA(obj));
  49. } else {
  50. element.txt(obj);
  51. }
  52. } else if (Array.isArray(obj)) {
  53. for (index in obj) {
  54. if (!hasProp.call(obj, index)) continue;
  55. child = obj[index];
  56. for (key in child) {
  57. entry = child[key];
  58. element = render(element.ele(key), entry).up();
  59. }
  60. }
  61. } else {
  62. for (key in obj) {
  63. if (!hasProp.call(obj, key)) continue;
  64. child = obj[key];
  65. if (key === attrkey) {
  66. if (typeof child === "object") {
  67. for (attr in child) {
  68. value = child[attr];
  69. element = element.att(attr, value);
  70. }
  71. }
  72. } else if (key === charkey) {
  73. if (_this.options.cdata && requiresCDATA(child)) {
  74. element = element.raw(wrapCDATA(child));
  75. } else {
  76. element = element.txt(child);
  77. }
  78. } else if (Array.isArray(child)) {
  79. for (index in child) {
  80. if (!hasProp.call(child, index)) continue;
  81. entry = child[index];
  82. if (typeof entry === 'string') {
  83. if (_this.options.cdata && requiresCDATA(entry)) {
  84. element = element.ele(key).raw(wrapCDATA(entry)).up();
  85. } else {
  86. element = element.ele(key, entry).up();
  87. }
  88. } else {
  89. element = render(element.ele(key), entry).up();
  90. }
  91. }
  92. } else if (typeof child === "object") {
  93. element = render(element.ele(key), child).up();
  94. } else {
  95. if (typeof child === 'string' && _this.options.cdata && requiresCDATA(child)) {
  96. element = element.ele(key).raw(wrapCDATA(child)).up();
  97. } else {
  98. if (child == null) {
  99. child = '';
  100. }
  101. element = element.ele(key, child.toString()).up();
  102. }
  103. }
  104. }
  105. }
  106. return element;
  107. };
  108. })(this);
  109. rootElement = builder.create(rootName, this.options.xmldec, this.options.doctype, {
  110. headless: this.options.headless,
  111. allowSurrogateChars: this.options.allowSurrogateChars
  112. });
  113. return render(rootElement, rootObj).end(this.options.renderOpts);
  114. };
  115. return Builder;
  116. })();
  117. }).call(this);