buffalo.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. function BuffaloCall(methodname){
  2. this.method = methodname;
  3. this.params = [];
  4. return this;
  5. }
  6. BuffaloCall.prototype.setMethod = function(methodName){
  7. if (!methodName) return;
  8. this.method = methodName;
  9. }
  10. BuffaloCall.prototype.addParameter = function(data){
  11. if (arguments.length==0) return;
  12. this.params[this.params.length] = data;
  13. }
  14. BuffaloCall.prototype.xml = function(){
  15. var method = this.method;
  16. var xml = "";
  17. xml += "<burlap:call>\n";
  18. xml += "<method>" + method+ "</method>\n";
  19. for (var i = 0; i < this.params.length; i++){
  20. var data = this.params[i];
  21. xml += BuffaloCall.getParamXML(BuffaloCall.dataTypeOf(data),data) + "\n";
  22. }
  23. xml += "</burlap:call>";
  24. return xml;
  25. }
  26. BuffaloCall.dataTypeOf = function (o){
  27. var type = typeof(o);
  28. type = type.toLowerCase();
  29. switch(type){
  30. case "number":
  31. if (Math.round(o) == o) type = "int";
  32. else type = "double";
  33. break;
  34. case "object":
  35. var con = o.constructor;
  36. if (con == Date) type = "date";
  37. else if (con == Array) type = "list";
  38. else type = "map";
  39. break;
  40. }
  41. return type;
  42. }
  43. BuffaloCall.doValueXML = function(type,data){
  44. var xml = "<" + type + ">" + xmlEncode(data) + "</" + type + ">";
  45. return xml;
  46. }
  47. function xmlEncode(data) {
  48. var str = "";
  49. if (typeof(data) == "string") {
  50. str = data;
  51. } else {
  52. return data;
  53. }
  54. str = str.replace("&","&amp;");
  55. str = str.replace("<","&lt;");
  56. str = str.replace(">","&gt;");
  57. return str;
  58. }
  59. BuffaloCall.doBooleanXML = function(data){
  60. var value = (data==true)?1:0;
  61. var xml = "<boolean>" + value + "</boolean>";
  62. return xml;
  63. }
  64. BuffaloCall.doDateXML = function(data){
  65. var xml = "<date>";
  66. xml += dateToISO8609(data);
  67. xml += "</date>";
  68. return xml;
  69. }
  70. BuffaloCall.doArrayXML = function(data){
  71. var xml = "<list>\n";
  72. xml += "<type>" +""+ "</type>\n";
  73. xml += "<length>" +data.length+ "</length>\n";
  74. for (var i = 0; i < data.length; i++){
  75. xml += BuffaloCall.getParamXML(BuffaloCall.dataTypeOf(data[i]),data[i]) + "\n";
  76. }
  77. xml += "</list>\n";
  78. return xml;
  79. }
  80. BuffaloCall.doStructXML = function(data){
  81. var boClass = data[Buffalo.BOCLASS];
  82. var boType = "java.util.HashMap";
  83. if (typeof(boClass) != 'undefined' || boClass != null) {
  84. boType = boClass;
  85. }
  86. var xml = "<map>\n";
  87. xml += "<type>" +boType+ "</type>\n";
  88. for (var i in data){
  89. if (data[i] != boType) {
  90. xml += BuffaloCall.getParamXML(BuffaloCall.dataTypeOf(i),i)+"\n";
  91. xml += BuffaloCall.getParamXML(BuffaloCall.dataTypeOf(data[i]),data[i]) + "\n";
  92. }
  93. }
  94. xml += "</map>\n";
  95. return xml;
  96. }
  97. BuffaloCall.getParamXML = function(type,data){
  98. var xml;
  99. switch (type){
  100. case "date":
  101. xml = BuffaloCall.doDateXML(data);
  102. break;
  103. case "list":
  104. xml = BuffaloCall.doArrayXML(data);
  105. break;
  106. case "map":
  107. xml = BuffaloCall.doStructXML(data);
  108. break;
  109. case "boolean":
  110. xml = BuffaloCall.doBooleanXML(data);
  111. break;
  112. default:
  113. xml = BuffaloCall.doValueXML(type,data);
  114. break;
  115. }
  116. return xml;
  117. }
  118. function dateToISO8609(date){
  119. var year = new String(date.getYear());
  120. var month = leadingZero(new String(date.getMonth()+1));
  121. var day = leadingZero(new String(date.getDate()));
  122. var time = leadingZero(new String(date.getHours())) + leadingZero(new String(date.getMinutes())) + leadingZero(new String(date.getSeconds()));
  123. var converted = year+month+day+"T"+time+"Z";
  124. return converted;
  125. }
  126. function leadingZero(n){
  127. if (n.length==1) n = "0" + n;
  128. return n;
  129. }
  130. function BuffaloReply(sourceXML) {
  131. this._source = sourceXML;
  132. this._isFault = false;
  133. this._type = "null";
  134. this._objects = [];
  135. this._objectNodes = [];
  136. var xmldoc = XmlDocument.create();
  137. xmldoc.async=false;
  138. xmldoc.loadXML(sourceXML);
  139. var root = xmldoc.documentElement;
  140. this._root = root;
  141. var dataNode = root.firstChild;
  142. this._type = BuffaloReply._getType(dataNode);
  143. this.getType = function() {
  144. return this._type;
  145. }
  146. this.getResult = function() {
  147. return this.deserialize(dataNode);
  148. }
  149. this.isFault = function() {
  150. return (this._type == "fault");
  151. }
  152. this.isNull = function() {
  153. return (this._type == "null");
  154. }
  155. this.getSource = function() {
  156. return this._source;
  157. }
  158. }
  159. BuffaloReply.prototype.deserialize = function(dataNode) {
  160. var ret;
  161. type = BuffaloReply._getType(dataNode);
  162. switch (type) {
  163. case "boolean":
  164. ret = this.doBoolean(dataNode);
  165. break;
  166. case "date":
  167. ret = this.doDate(dataNode);
  168. break;
  169. case "double":
  170. ret = this.doDouble(dataNode);
  171. break;
  172. case "int":
  173. case "long":
  174. ret = this.doInt(dataNode);
  175. break;
  176. case "list":
  177. ret = this.doList(dataNode);
  178. break;
  179. case "map":
  180. ret = this.doMap(dataNode);
  181. break;
  182. case "null":
  183. ret = this.doNull(dataNode);
  184. break;
  185. case "ref":
  186. ret = this.doRef(dataNode);
  187. break;
  188. case "string":
  189. ret = this.doString(dataNode);
  190. break;
  191. case "xml":
  192. ret = this.doXML(dataNode);
  193. break;
  194. case "fault":
  195. ret = this.doFault(dataNode);
  196. break;
  197. default:
  198. ;
  199. }
  200. return ret;
  201. }
  202. BuffaloReply._getType = function(dataNode) {
  203. return dataNode.tagName;
  204. }
  205. BuffaloReply.getNodeText = function(dataNode) {
  206. if (dataNode.childNodes.length == 0) {
  207. return null;
  208. } else
  209. return dataNode.firstChild.nodeValue;
  210. }
  211. BuffaloReply.prototype.doBoolean = function (dataNode) {
  212. var value = BuffaloReply.getNodeText(dataNode);
  213. return (value == "1");
  214. }
  215. BuffaloReply.prototype.doDate = function (dataNode) {
  216. var dateStr = BuffaloReply.getNodeText(dataNode);
  217. var year = parseInt(dateStr.substring(0,4),"10");
  218. var month = parseInt(dateStr.substring(4,6),"10") - 1;
  219. var day = parseInt(dateStr.substring(6,8),"10");
  220. var hour = parseInt(dateStr.substring(9,11),"10");
  221. var minute = parseInt(dateStr.substring(11,13),"10");
  222. var second = parseInt(dateStr.substring(13,15),"10");
  223. var d = new Date(year, month, day, hour, minute, second);
  224. return d;
  225. }
  226. BuffaloReply.prototype.doDouble = function (dataNode) {
  227. var value = BuffaloReply.getNodeText(dataNode);
  228. return parseFloat(value);
  229. }
  230. BuffaloReply.prototype.doInt = function (dataNode) {
  231. var value = BuffaloReply.getNodeText(dataNode);
  232. return parseInt(value);
  233. }
  234. BuffaloReply.prototype.doList = function (dataNode) {
  235. var arr = new Array();
  236. this._objects[this._objects.length] = arr;
  237. var children = dataNode.childNodes;
  238. for (var i=2; i < children.length; i++) {
  239. arr[arr.length] = this.deserialize(children[i]);
  240. }
  241. return arr;
  242. }
  243. BuffaloReply.prototype.doMap = function (dataNode) {
  244. var obj = new Object();
  245. this._objects[this._objects.length] = obj;
  246. var attrs = dataNode.childNodes;
  247. for (var i = 1; i < attrs.length; i+=2) {
  248. if (attrs[i+1].hasChildNodes() ) {
  249. obj[BuffaloReply.getNodeText(attrs[i])] = this.deserialize(attrs[i+1]);
  250. } else {
  251. obj[BuffaloReply.getNodeText(attrs[i])] = attrs[i+1].text;
  252. }
  253. }
  254. return obj;
  255. }
  256. BuffaloReply.prototype.doNull = function (dataNode) {
  257. return null;
  258. }
  259. BuffaloReply.prototype.doRef = function (dataNode) {
  260. var value = BuffaloReply.getNodeText(dataNode);
  261. var idx = parseInt(value);
  262. return this._objects[idx];
  263. }
  264. BuffaloReply.prototype.doString = function (dataNode) {
  265. var value = BuffaloReply.getNodeText(dataNode);
  266. if (value == null) {
  267. return "";
  268. }
  269. return (value);
  270. }
  271. BuffaloReply.prototype.doXML = function (dataNode) {
  272. var value = BuffaloReply.getNodeText(dataNode);
  273. return unescape(value);
  274. }
  275. BuffaloReply.prototype.doFault = function (dataNode) {
  276. var code = BuffaloReply.getNodeText(dataNode.childNodes[1]);
  277. var msg = BuffaloReply.getNodeText(dataNode.childNodes[3]);
  278. var detail = this.deserialize(dataNode.childNodes[5]);
  279. return new BuffaloFault(code, msg, detail);
  280. }
  281. function BuffaloFault(code, message, detail) {
  282. this.code = code;
  283. this.message = message;
  284. this.detail = detail;
  285. this.toString = function() {
  286. return "code:" + this.code + ", message" + this.message + ", detail: " + this.detail;
  287. }
  288. }
  289. function Buffalo(gateway, async) {
  290. this.gateway = gateway;
  291. this.transport = null;
  292. this.async = (async != null) ? async : true;
  293. this.onLoading = Buffalo.showLoading;
  294. this.onFinish = new Function();
  295. this.onException = new Function();
  296. this.onError = Buffalo.showError;
  297. }
  298. Buffalo.prototype.setGateway = function(gateway) {
  299. this.gateway = gateway;
  300. }
  301. Buffalo.prototype.getGateway = function() {
  302. return this.gateway;
  303. }
  304. Buffalo.BOCLASS = "_BUFFALO_OBJECT_CLASS_";
  305. Buffalo.loadingPane = null;
  306. Buffalo.exceptionPane = null;
  307. Buffalo.errorPane = null;
  308. Buffalo.showLoading = function(state) {
  309. Buffalo.loadingPane = document.getElementById("buffalo_loading");
  310. if (Buffalo.loadingPane == null) {
  311. var el = document.createElement('DIV');
  312. el.setAttribute("id","buffalo_loading");
  313. el.style.cssText="display:none;font-family:Verdana;font-size:11px;border:1px solid #00CC00;background-color:#A4FFA4;padding:1px;position:absolute; right:1px; top:1px; width:110px; height:14px; z-index:10000";
  314. el.innerHTML="Buffalo loading... ";
  315. document.body.appendChild(el);
  316. Buffalo.loadingPane = el;
  317. }
  318. if (state) {
  319. Buffalo.loadingPane.style.display="block";
  320. Buffalo.loadingPane.style.top = document.body.scrollTop+1;
  321. } else {
  322. Buffalo.loadingPane.style.display="none";
  323. }
  324. }
  325. Buffalo.showError = function(errorStr) {
  326. Buffalo.errorPane = document.getElementById("buffalo_error");
  327. if (Buffalo.errorPane == null) {
  328. var el = document.createElement('DIV');
  329. el.setAttribute("id","buffalo_error");
  330. el.style.cssText="font-family:Verdana;font-size:11px;border:1px solid #00CC00;background-color:#ffdb9c;padding:1px;position:absolute;overflow:auto; right:1px; top:1px; width:500px; height:300px; z-index:1";
  331. el.innerHTML=errorStr;
  332. document.body.appendChild(el);
  333. Buffalo.errorPane = el;
  334. }
  335. }
  336. Buffalo.showException = function(ex) {
  337. }
  338. Buffalo.prototype.onStateChange = function(){
  339. if (this.transport.readyState == 4) {
  340. if (this.transport.status == '200') {
  341. var data = this.transport.responseText;
  342. if (data.indexOf("xmlns:burlap") == -1) {
  343. data.replace("<burlap:reply>", "<burlap:reply xmlns:burlap=\"http://www.amowa.net/buffalo/\">")
  344. }
  345. this.onLoading(false);
  346. this.onFinish(new BuffaloReply(data));
  347. } else {
  348. //this.onError(this.transport.responseText);
  349. }
  350. }
  351. }
  352. Function.prototype.bind = function(object) {
  353. var method = this;
  354. return function() {
  355. method.apply(object, arguments);
  356. }
  357. }
  358. Buffalo.prototype._remoteCall = function(url, burlapCall, callback) {
  359. this.transport = XmlHttp.create();
  360. this.transport.open("POST", url, this.async);
  361. this.transport.send(burlapCall.xml());
  362. this.onFinish = callback;
  363. if (this.async) {
  364. this.transport.onreadystatechange = this.onStateChange.bind(this);
  365. this.onLoading(true);
  366. } else {
  367. if (this.transport.status == '200') {
  368. var data = this.transport.responseText;
  369. if (data.indexOf("xmlns:burlap") == -1) {
  370. data.replace("<burlap:reply>", "<burlap:reply xmlns:burlap=\"http://www.amowa.net/buffalo/\">")
  371. }
  372. this.onFinish(new BuffaloReply(data));
  373. } else {
  374. //this.onError(this.transport.responseText);
  375. }
  376. }
  377. }
  378. Buffalo.prototype.remoteCall = function(service, params, callback) {
  379. try
  380. {
  381. var idx = service.indexOf(".");
  382. var serviceId = service.substring(0,idx);
  383. var method = service.substring(idx+1,service.length);
  384. var newUrl = this.gateway+"?sid="+serviceId;
  385. var call = new BuffaloCall(method);
  386. for (var i = 0; i < params.length; i++) {
  387. call.addParameter(params[i]);
  388. }
  389. this._remoteCall(newUrl, call, callback);
  390. }
  391. catch(E)
  392. {
  393. alert(E.message);
  394. }
  395. }
  396. Buffalo.getElementById = function(elementId) {
  397. return document.getElementById(elementId);
  398. }