js-to-xml.ts 3.3 KB

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