xmlrpc-helper.js 6.0 KB

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