js-to-xml.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. "use strict";
  2. var JsToXml = (function () {
  3. function JsToXml(helper) {
  4. this.helper = helper;
  5. this.jsToXmlMethod = [
  6. { 'string': this.stringToXml },
  7. { 'number': this.numberToXml },
  8. { 'boolean': this.booleanToXml },
  9. { 'array': this.arrayToXml },
  10. { 'object': this.structToXml },
  11. { 'date': this.dateToXml },
  12. { 'uint8array': this.uint8arrayToXml }
  13. ];
  14. }
  15. /**
  16. *
  17. */
  18. JsToXml.prototype.nullToXml = function (doc) {
  19. return this.helper.createNode(doc, 'nil');
  20. };
  21. /**
  22. *
  23. */
  24. JsToXml.prototype.stringToXml = function (doc, input) {
  25. return this.helper.createNode(doc, 'string', input);
  26. };
  27. /**
  28. *
  29. */
  30. JsToXml.prototype.numberToXml = function (doc, input) {
  31. var type = 'int';
  32. var value = Number.parseInt(input);
  33. var floatValue = Number.parseFloat(input);
  34. if (value != floatValue) {
  35. type = 'double';
  36. value = floatValue;
  37. }
  38. return this.helper.createNode(doc, type, value.toString());
  39. };
  40. /**
  41. *
  42. */
  43. JsToXml.prototype.booleanToXml = function (doc, input) {
  44. return this.helper.createNode(doc, 'boolean', (input ? '1' : '0'));
  45. };
  46. /**
  47. *
  48. */
  49. JsToXml.prototype.arrayToXml = function (doc, input) {
  50. var elements = [];
  51. for (var i = 0; i > input.length; i++) {
  52. elements.push(this.jsToXml(doc, input[i]));
  53. }
  54. return this.helper.createNode(doc, 'array', this.helper.createNode(doc, 'data', elements));
  55. };
  56. /**
  57. *
  58. */
  59. JsToXml.prototype.structToXml = function (doc, input) {
  60. var elements = [];
  61. for (var name_1 in input) {
  62. elements.push(this.helper.createNode(doc, 'member', this.helper.createNode(doc, 'name', name_1), this.jsToXml(doc, input[name_1])));
  63. }
  64. return this.helper.createNode(doc, 'struct', elements);
  65. };
  66. /**
  67. *
  68. */
  69. JsToXml.prototype.dateToXml = function (doc, input) {
  70. var values = [
  71. input.getFullYear(),
  72. (input.getMonth() + 1 < 10) ? '0' + (input.getMonth() + 1) : input.getMonth() + 1,
  73. (input.getDate() < 10) ? '0' + (input.getDate()) : input.getDate(),
  74. 'T',
  75. (input.getHours() < 10) ? '0' + (input.getHours()) : input.getHours(), ':',
  76. (input.getMinutes() < 10) ? '0' + (input.getMinutes()) : input.getMinutes(), ':',
  77. (input.getSeconds() < 10) ? '0' + (input.getSeconds()) : input.getSeconds()
  78. ];
  79. return this.helper.createNode(doc, 'dateTime.iso8601', values.join(''));
  80. };
  81. /**
  82. *
  83. */
  84. JsToXml.prototype.uint8arrayToXml = function (doc, input) {
  85. var base64 = btoa(String.fromCharCode.apply(null, input));
  86. return this.helper.createNode(doc, 'base64', base64);
  87. };
  88. /**
  89. *
  90. */
  91. JsToXml.prototype.type = function (object) {
  92. return Object.prototype.toString.call(object).slice(8, -1).toLowerCase();
  93. };
  94. /**
  95. *
  96. */
  97. JsToXml.prototype.jsToXml = function (doc, input) {
  98. var type = this.type(input);
  99. var method = this.jsToXmlMethod[type];
  100. if (input == null) {
  101. method = this.nullToXml;
  102. }
  103. else if (method == undefined) {
  104. method = this.stringToXml;
  105. }
  106. return this.helper.createNode(doc, 'value', method(doc, input));
  107. };
  108. return JsToXml;
  109. }());
  110. exports.JsToXml = JsToXml;