xmlrpc-helper.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. export class XmlRpcHelper {
  2. _hasActiveX: boolean;
  3. constructor() {
  4. try {
  5. new ActiveXObject('MSXML2.DOMDocument');
  6. this._hasActiveX = true;
  7. } catch (e) {
  8. this._hasActiveX = false;
  9. }
  10. }
  11. /**
  12. *
  13. */
  14. get hasActiveX(): boolean {
  15. return this._hasActiveX;
  16. }
  17. /**
  18. *
  19. */
  20. cloneArray(object: any): Array<any> {
  21. var length = object.length;
  22. if (length > 0) {
  23. let result = new Array(length);
  24. for (let i = 0; i < length; i++) {
  25. result[i] = object[i];
  26. }
  27. return result;
  28. }
  29. return [];
  30. }
  31. /**
  32. *
  33. */
  34. createMsXmlDocument(): any {
  35. let doc: any = new ActiveXObject('MSXML2.DOMDocument');
  36. if (doc) {
  37. doc.resolveExternals = false;
  38. doc.validateOnParse = false;
  39. try {
  40. doc.setProperty('ProhibitDTD', true);
  41. doc.setProperty('MaxXMLSize', 2 * 1024);
  42. doc.setProperty('MaxElementDepth', 256);
  43. } catch (e) {
  44. console.log(e);
  45. }
  46. }
  47. return doc;
  48. }
  49. /**
  50. *
  51. */
  52. createDocument(rootTag: string, uriNamespace?: string): any {
  53. if (!rootTag && uriNamespace) {
  54. throw Error("Can't create document with namespace and no root tag");
  55. }
  56. if (this.hasActiveX) {
  57. var doc = this.createMsXmlDocument();
  58. if (doc) {
  59. if (rootTag) {
  60. doc.appendChild(doc.createNode(1, rootTag, uriNamespace || ''));
  61. }
  62. return doc;
  63. }
  64. } else if (document.implementation && document.implementation.createDocument) {
  65. return document.implementation.createDocument(uriNamespace || '', rootTag || '', null);
  66. }
  67. throw Error('Your browser does not support creating new documents');
  68. }
  69. /**
  70. *
  71. */
  72. type(object): string {
  73. return Object.prototype.toString.call(object).slice(8, -1).toLowerCase();
  74. }
  75. /**
  76. *
  77. */
  78. createNode(doc: any, nodeName: any, ...children: any[]): any {
  79. let result = doc.createElement(nodeName);
  80. return result;
  81. }
  82. /**
  83. *
  84. */
  85. private appendChild(child: any): void {
  86. if (this.type(child) == 'object' && child.nodeType != 1) {
  87. for (let item in child) {
  88. }
  89. }
  90. }
  91. /**
  92. *
  93. */
  94. generateId(): string {
  95. return 'xmlrpc-' + (new Date().getTime()) + ' - ' + Math.floor(Math.random() * 1000);
  96. }
  97. /**
  98. *
  99. */
  100. loadXml(xml: any): Document {
  101. if (this.hasActiveX) {
  102. let doc = this.createMsXmlDocument();
  103. doc.loadXML(xml);
  104. return doc;
  105. } else if (typeof DOMParser != 'undefined') {
  106. return new DOMParser().parseFromString(xml, 'application/xml');
  107. }
  108. throw Error('Your browser does not support loading xml documents');
  109. }
  110. /**
  111. *
  112. */
  113. getOwnerDocument(node: any): any {
  114. return (node.nodeType == 9 ? node : node.ownerDocument || node.document);
  115. }
  116. /**
  117. *
  118. */
  119. selectSingleNode(node: any, path: any): any {
  120. let doc = this.getOwnerDocument(node);
  121. if (typeof node.selectSingleNode != 'undefined') {
  122. if (typeof doc.setProperty != 'undefined') {
  123. doc.setProperty('SelectionLanguage', 'XPath');
  124. }
  125. return node.selectSingleNode(path);
  126. } else if (document.implementation.hasFeature('XPath', '3.0')) {
  127. let resolver = doc.createNSResolver(doc.documentElement);
  128. let result = doc.evaluate(path, node, resolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  129. return result.singleNodeValue;
  130. }
  131. return null;
  132. }
  133. /**
  134. *
  135. */
  136. getTextContent(node: any, buffer: Array<any>, normalizeWhitespace?: boolean): string {
  137. const PREDEFINED_TAG_VALUES = { 'IMG': '', 'BR': '\n' };
  138. if (node.nodeName in ['SCRIPT', 'STYLE', 'HEAD', 'IFRAME', 'OBJECT']) {
  139. // Ignored
  140. } else if (node.nodeType == 3) {
  141. if (normalizeWhitespace) {
  142. buffer.push(String(node.nodeValue).replace(/(\r\n|\r|\n)/g, ''));
  143. } else {
  144. buffer.push(node.nodeValue);
  145. }
  146. } else if (node.nodeName in PREDEFINED_TAG_VALUES) {
  147. buffer.push(PREDEFINED_TAG_VALUES[node.nodeName]);
  148. } else {
  149. let child = node.firstChild;
  150. while (child) {
  151. this.getTextContent(child, buffer, normalizeWhitespace);
  152. child = child.nextSibling;
  153. }
  154. }
  155. return buffer.join('');
  156. }
  157. /**
  158. *
  159. */
  160. selectNodes(node: any, path: any): any {
  161. let doc = this.getOwnerDocument(node);
  162. if (typeof node.selectNodes != 'undefined') {
  163. if (typeof doc.setProperty != 'undefined') {
  164. doc.setProperty('SelectionLanguage', 'XPath');
  165. }
  166. return node.selectNodes(path);
  167. } else if (document.implementation.hasFeature('XPath', '3.0')) {
  168. let resolver = doc.createNSResolver(doc.documentElement);
  169. let nodes = doc.evaluate(path, node, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  170. let results = [];
  171. let count = nodes.snapshotLength;
  172. for (let i = 0; i < count; i++) {
  173. results.push(nodes.snapshotItem(i));
  174. }
  175. return results;
  176. } else {
  177. return [];
  178. }
  179. }
  180. }