xmlCommunication.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: pps
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2017
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. //this JavaScript file provides a layer of abstraction over the browser specific
  9. //xml comunication controls.
  10. function dataItem(name, value) {
  11. this.name = name;
  12. this.value = value;
  13. }
  14. function dataCollection() {
  15. var dataArray = new Array();
  16. this.addItem = function(name, value) {
  17. dataArray[dataArray.length] = new dataItem(name,value);
  18. }
  19. this.getDataArray = function() {
  20. return dataArray;
  21. }
  22. }
  23. //Static public variables
  24. xmlCommunicator.prototype.STATUS_SUCCESS = 1;
  25. xmlCommunicator.prototype.STATUS_FAILED = 0;
  26. xmlCommunicator.prototype.STATUS_ASYNC_SENT = 2;
  27. xmlCommunicator.prototype.MODE_POST = 0;
  28. xmlCommunicator.prototype.MODE_GET = 1;
  29. //Class xmlCommunicator
  30. function xmlCommunicator (admCommandHandler) {
  31. //Constructor
  32. var mode = this.MODE_POST;
  33. var ACH = admCommandHandler;
  34. var xmlResponse = null;
  35. var textResponse = null;
  36. var requestSuccess = false;
  37. var requestInProgress = false;
  38. function returnSuccess(transport) {
  39. if (requestInProgress) {
  40. returnSuccessBlocking(transport);
  41. requestInProgress = false;
  42. ACH.requestCompleted(); //Inform the adminCommands layer that a request has completed
  43. }
  44. }
  45. function returnFailure(transport) {
  46. if (requestInProgress) {
  47. returnFailureBlocking(transport);
  48. requestInProgress = false;
  49. ACH.requestCompleted(); //Inform the adminCommands layer that a request has completed
  50. }
  51. }
  52. function returnException(e) {
  53. if (requestInProgress) {
  54. returnExceptionBlocking(e);
  55. requestInProgress = false;
  56. ACH.requestCompleted(); //Inform the adminCommands layer that a request has completed
  57. }
  58. }
  59. function returnSuccessBlocking(transport) {
  60. requestSuccess = true;
  61. xmlResponse = transport.responseXml;
  62. textResponse = transport.responseText;
  63. //In Firefoxm it's possible to have gotten a valid XML document, but not have it parsed.
  64. //So here is a workaround
  65. if (textResponse && !xmlResponse) {
  66. if (typeof (DOMParser) != "undefined") {
  67. xmlResponse = (new DOMParser()).parseFromString(textResponse,"text/xml");
  68. }
  69. }
  70. }
  71. function returnFailureBlocking(transport) {
  72. requestSuccess = false;
  73. xmlResponse = null;
  74. textResponse = null;
  75. }
  76. function returnExceptionBlocking(e) {
  77. requestSuccess = false;
  78. xmlResponse = null;
  79. textResponse = null;
  80. }
  81. //Public methods
  82. this.setMode = function (theMode) {
  83. mode = theMode;
  84. }
  85. this.GetXMLResponse = function() {
  86. return xmlResponse;
  87. }
  88. this.GetTextResponse = function() {
  89. return textResponse;
  90. }
  91. this.requestSuceeded = function() {
  92. return requestSuccess;
  93. }
  94. this.sendRequest = function(dataArray, isBlocking) {
  95. if (requestInProgress) {
  96. return this.STATUS_FAILED;
  97. } else {
  98. requestInProgress = true;
  99. var requestProps = {};
  100. requestProps.method = (mode == this.MODE_POST)? "post" : "get";
  101. //requestProps.parameters = {};
  102. var sPostBody = "";
  103. for (var i = 0; i < dataArray.length; i++) {
  104. if (i)
  105. sPostBody += "&";
  106. sPostBody += dataArray[i].name;
  107. sPostBody += "=";
  108. sPostBody += _F_Strings.urlEncode(dataArray[i].value);
  109. //requestProps.parameters[dataArray[i].name] = dataArray[i].value;
  110. }
  111. requestProps.postBody = sPostBody;
  112. requestProps.asynchronous = !(isBlocking);
  113. requestProps.onSuccess = (isBlocking)? returnSuccessBlocking : returnSuccess;
  114. requestProps.onFailure = (isBlocking)? returnFailureBlocking : returnFailure;
  115. requestProps.onException = (isBlocking)? returnExceptionBlocking : returnException;
  116. new _F_Ajax.Request("$CGI$", requestProps);
  117. if (isBlocking) {
  118. if (requestSuccess)
  119. return this.STATUS_SUCCESS;
  120. else
  121. return this.STATUS_FAILED;
  122. } else
  123. return this.STATUS_ASYNC_SENT;
  124. }
  125. }
  126. }