xmlrpc-helper.ts 6.4 KB

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