xmlrpc-helper.js 6.1 KB

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