xmlrpc-helper.ts 5.6 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: Array<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: string, child: any): void {
  79. let result = doc.createElement(nodeName);
  80. }
  81. /**
  82. *
  83. */
  84. private appendChild(child: any): void {
  85. if (this.type(child) == 'object' && child.nodeType != 1) {
  86. for (let item in child) {
  87. }
  88. }
  89. }
  90. /**
  91. *
  92. */
  93. generateId(): string {
  94. return 'xmlrpc-' + (new Date().getTime()) + ' - ' + Math.floor(Math.random() * 1000);
  95. }
  96. /**
  97. *
  98. */
  99. loadXml(xml: any): Document {
  100. if (this.hasActiveX) {
  101. let doc = this.createMsXmlDocument();
  102. doc.loadXML(xml);
  103. return doc;
  104. } else if (typeof DOMParser != 'undefined') {
  105. return new DOMParser().parseFromString(xml, 'application/xml');
  106. }
  107. throw Error('Your browser does not support loading xml documents');
  108. }
  109. /**
  110. *
  111. */
  112. getOwnerDocument(node: any): any {
  113. return (node.nodeType == 9 ? node : node.ownerDocument || node.document);
  114. }
  115. /**
  116. *
  117. */
  118. selectSingleNode(node: any, path: any): any {
  119. let doc = this.getOwnerDocument(node);
  120. if (typeof node.selectSingleNode != 'undefined') {
  121. if (typeof doc.setProperty != 'undefined') {
  122. doc.setProperty('SelectionLanguage', 'XPath');
  123. }
  124. return node.selectSingleNode(path);
  125. } else if (document.implementation.hasFeature('XPath', '3.0')) {
  126. let resolver = doc.createNSResolver(doc.documentElement);
  127. let result = doc.evaluate(path, node, resolver, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
  128. return result.singleNodeValue;
  129. }
  130. return null;
  131. }
  132. /**
  133. *
  134. */
  135. getTextContent(node: any, buffer: Array<any>, normalizeWhitespace?: boolean): string {
  136. const PREDEFINED_TAG_VALUES = { 'IMG': '', 'BR': '\n' };
  137. if (node.nodeName in ['SCRIPT', 'STYLE', 'HEAD', 'IFRAME', 'OBJECT']) {
  138. // Ignored
  139. } else if (node.nodeType == 3) {
  140. if (normalizeWhitespace) {
  141. buffer.push(String(node.nodeValue).replace(/(\r\n|\r|\n)/g, ''));
  142. } else {
  143. buffer.push(node.nodeValue);
  144. }
  145. } else if (node.nodeName in PREDEFINED_TAG_VALUES) {
  146. buffer.push(PREDEFINED_TAG_VALUES[node.nodeName]);
  147. } else {
  148. let child = node.firstChild;
  149. while (child) {
  150. this.getTextContent(child, buffer, normalizeWhitespace);
  151. child = child.nextSibling;
  152. }
  153. }
  154. return buffer.join('');
  155. }
  156. /**
  157. *
  158. */
  159. selectNodes(node: any, path: any): any {
  160. let doc = this.getOwnerDocument(node);
  161. if (typeof node.selectNodes != 'undefined') {
  162. if (typeof doc.setProperty != 'undefined') {
  163. doc.setProperty('SelectionLanguage', 'XPath');
  164. }
  165. return node.selectNodes(path);
  166. } else if (document.implementation.hasFeature('XPath', '3.0')) {
  167. let resolver = doc.createNSResolver(doc.documentElement);
  168. let nodes = doc.evaluate(path, node, resolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  169. let results = [];
  170. let count = nodes.snapshotLength;
  171. for (let i = 0; i < count; i++) {
  172. results.push(nodes.snapshotItem(i));
  173. }
  174. return results;
  175. } else {
  176. return [];
  177. }
  178. }
  179. }