data2xml.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // --------------------------------------------------------------------------------------------------------------------
  2. //
  3. // data2xml.js - A data to XML converter with a nice interface (for NodeJS).
  4. //
  5. // Copyright (c) 2011 Andrew Chilton - http://chilts.org/
  6. // Written by Andrew Chilton <andychilton@gmail.com>
  7. //
  8. // License: http://opensource.org/licenses/MIT
  9. //
  10. // --------------------------------------------------------------------------------------------------------------------
  11. var valid = {
  12. 'omit' : true, // no element is output : ''
  13. 'empty' : true, // an empty element is output : '<element></element>'
  14. 'closed' : true // a closed element is output : '<element/>'
  15. };
  16. var defaults = {
  17. 'attrProp' : '_attr',
  18. 'valProp' : '_value',
  19. 'undefined' : 'omit',
  20. 'null' : 'omit',
  21. 'xmlDecl' : true,
  22. 'cdataProp' : '_cdata'
  23. };
  24. var xmlHeader = '<?xml version="1.0" encoding="utf-8"?>\n';
  25. module.exports = function(opts) {
  26. opts = opts || {};
  27. opts.attrProp = opts.attrProp || defaults.attrProp;
  28. opts.valProp = opts.valProp || defaults.valProp;
  29. opts.cdataProp = opts.cdataProp || defaults.cdataProp;
  30. opts.xmlHeader = opts.xmlHeader || xmlHeader;
  31. if (typeof opts.xmlDecl === 'undefined') {
  32. opts.xmlDecl = defaults.xmlDecl;
  33. }
  34. if ( opts['undefined'] && valid[opts['undefined']] ) {
  35. // nothing, this is fine
  36. }
  37. else {
  38. opts['undefined'] = defaults['undefined'];
  39. }
  40. if ( opts['null'] && valid[opts['null']] ) {
  41. // nothing, this is fine
  42. }
  43. else {
  44. opts['null'] = defaults['null'];
  45. }
  46. return function(name, data) {
  47. var xml = opts.xmlDecl ? opts.xmlHeader : '';
  48. xml += makeElement(name, data, opts);
  49. return xml;
  50. };
  51. };
  52. function entitify(str) {
  53. str = '' + str;
  54. str = str
  55. .replace(/&/g, '&amp;')
  56. .replace(/</g,'&lt;')
  57. .replace(/>/g,'&gt;')
  58. .replace(/'/g, '&apos;')
  59. .replace(/"/g, '&quot;');
  60. return str;
  61. }
  62. function makeElementAttrs(attr) {
  63. var attributes = '';
  64. for(var a in attr) {
  65. attributes += ' ' + a + '="' + entitify(attr[a]) + '"';
  66. }
  67. return attributes;
  68. }
  69. function makeStartTag(name, attr) {
  70. attr = attr || {};
  71. var tag = '<' + name;
  72. tag += makeElementAttrs(attr);
  73. tag += '>';
  74. return tag;
  75. }
  76. function makeClosedElement(name, attr) {
  77. attr = attr || {};
  78. var tag = '<' + name;
  79. tag += makeElementAttrs(attr);
  80. tag += '/>';
  81. return tag;
  82. }
  83. function undefinedElement(name, attr, opts) {
  84. if ( opts['undefined'] === 'omit' ) {
  85. return '';
  86. }
  87. if ( opts['undefined'] === 'empty' ) {
  88. return makeStartTag(name, attr) + makeEndTag(name);
  89. }
  90. else if ( opts['undefined'] === 'closed' ) {
  91. return makeClosedElement(name, attr);
  92. }
  93. }
  94. function nullElement(name, attr, opts) {
  95. if ( opts['null'] === 'omit' ) {
  96. return '';
  97. }
  98. if ( opts['null'] === 'empty' ) {
  99. return makeStartTag(name, attr) + makeEndTag(name);
  100. }
  101. else if ( opts['null'] === 'closed' ) {
  102. return makeClosedElement(name, attr);
  103. }
  104. }
  105. function makeEndTag(name) {
  106. return '</' + name + '>';
  107. }
  108. function makeElement(name, data, opts) {
  109. var cdataRegExp = /]]\>/g;
  110. var element = '';
  111. if ( Array.isArray(data) ) {
  112. data.forEach(function(v) {
  113. element += makeElement(name, v, opts);
  114. });
  115. return element;
  116. }
  117. else if ( typeof data === 'undefined' ) {
  118. return undefinedElement(name, null, opts);
  119. }
  120. else if ( data === null ) {
  121. return nullElement(name, null, opts);
  122. }
  123. else if ( typeof data === 'object' ) {
  124. var valElement;
  125. if (data.hasOwnProperty(opts.valProp)) {
  126. valElement = data[opts.valProp];
  127. if (typeof valElement === 'undefined') {
  128. return undefinedElement(name, data[opts.attrProp], opts);
  129. }
  130. if (valElement === null) {
  131. return nullElement(name, data[opts.attrProp], opts);
  132. }
  133. }
  134. element += makeStartTag(name, data[opts.attrProp]);
  135. if (valElement) {
  136. element += entitify(valElement);
  137. }
  138. else if ( data[opts.cdataProp] ) {
  139. element += '<![CDATA[' + data[opts.cdataProp].replace(cdataRegExp, ']]]]><![CDATA[>') + ']]>';
  140. }
  141. for (var el in data) {
  142. if ( el === opts.attrProp || el === opts.valProp || el === opts.cdataProp) {
  143. continue;
  144. }
  145. element += makeElement(el, data[el], opts);
  146. }
  147. element += makeEndTag(name);
  148. return element;
  149. }
  150. else {
  151. // a piece of data on it's own can't have attributes
  152. return makeStartTag(name) + entitify(data) + makeEndTag(name);
  153. }
  154. throw 'Unknown data ' + data;
  155. }
  156. // --------------------------------------------------------------------------------------------------------------------
  157. module.exports.makeStartTag = makeStartTag;
  158. module.exports.makeEndTag = makeEndTag;
  159. module.exports.makeElement = makeElement;
  160. module.exports.entitify = entitify;
  161. // --------------------------------------------------------------------------------------------------------------------