1// Init base-level objects 2var Seam = new Object(); 3Seam.Remoting = new Object(); 4Seam.Component = new Object(); 5Seam.pageContext = new Object(); 6 7// Components registered here 8Seam.Component.components = new Array(); 9Seam.Component.instances = new Array(); 10 11Seam.Component.newInstance = function(name) 12{ 13 for (var i = 0; i < Seam.Component.components.length; i++) 14 { 15 if (Seam.Component.components[i].__name == name) 16 return new Seam.Component.components[i]; 17 } 18} 19 20Seam.Component.getInstance = function(name) 21{ 22 for (var i = 0; i < Seam.Component.components.length; i++) 23 { 24 if (Seam.Component.components[i].__name == name) 25 { 26 if (Seam.Component.components[i].__instance == null) 27 Seam.Component.components[i].__instance = new Seam.Component.components[i](); 28 return Seam.Component.components[i].__instance; 29 } 30 } 31 return null; 32} 33 34Seam.Component.getComponentType = function(obj) 35{ 36 for (var i = 0; i < Seam.Component.components.length; i++) 37 { 38 if (obj instanceof Seam.Component.components[i]) 39 return Seam.Component.components[i]; 40 } 41 return null; 42} 43 44Seam.Component.getComponentName = function(obj) 45{ 46 var componentType = Seam.Component.getComponentType(obj); 47 return componentType ? componentType.__name : null; 48} 49 50Seam.Component.register = function(component) 51{ 52 for (var i = 0; i < Seam.Component.components.length; i++) 53 { 54 if (Seam.Component.components[i].__name == component.__name) 55 { 56 // Replace the existing component with the new one 57 Seam.Component.components[i] = component; 58 return; 59 } 60 } 61 Seam.Component.components.push(component); 62 component.__instance = null; 63} 64 65Seam.Component.isRegistered = function(name) 66{ 67 for (var i = 0; i < Seam.Component.components.length; i++) 68 { 69 if (Seam.Component.components[i].__name == name) 70 return true; 71 } 72 return false; 73} 74 75Seam.Component.getMetadata = function(obj) 76{ 77 for (var i = 0; i < Seam.Component.components.length; i++) 78 { 79 if (obj instanceof Seam.Component.components[i]) 80 return Seam.Component.components[i].__metadata; 81 } 82 return null; 83} 84 85Seam.Remoting.extractEncodedSessionId = function(url) 86{ 87 var sessionId = null; 88 if (url.indexOf(';jsessionid=') >= 0) 89 { 90 var qpos = url.indexOf('?'); 91 sessionId = url.substring(url.indexOf(';jsessionid=') + 12, qpos >= 0 ? qpos : url.length); 92 } 93 return sessionId; 94} 95 96Seam.Remoting.PATH_EXECUTE = "/execute"; 97Seam.Remoting.PATH_SUBSCRIPTION = "/subscription"; 98Seam.Remoting.PATH_POLL = "/poll"; 99 100Seam.Remoting.encodedSessionId = Seam.Remoting.extractEncodedSessionId(window.location.href); 101 102// Type declarations will live in this namespace 103Seam.Remoting.type = new Object(); 104 105// Types are registered in an array 106Seam.Remoting.types = new Array(); 107 108Seam.Remoting.debug = false; 109Seam.Remoting.debugWindow = null; 110 111Seam.Remoting.setDebug = function(val) 112{ 113 Seam.Remoting.debug = val; 114} 115 116// Log a message to a popup debug window 117Seam.Remoting.log = function(msg) 118{ 119 if (!Seam.Remoting.debug) 120 return; 121 122 if (!Seam.Remoting.debugWindow || Seam.Remoting.debugWindow.document == null) 123 { 124 var attr = "left=400,top=400,resizable=yes,scrollbars=yes,width=400,height=400"; 125 Seam.Remoting.debugWindow = window.open("", "__seamDebugWindow", attr); 126 if (Seam.Remoting.debugWindow) 127 { 128 Seam.Remoting.debugWindow.document.write("<html><head><title>Seam Debug Window</title></head><body></body></html>"); 129 var bodyTag = Seam.Remoting.debugWindow.document.getElementsByTagName("body").item(0); 130 bodyTag.style.fontFamily = "arial"; 131 bodyTag.style.fontSize = "8pt"; 132 } 133 } 134 135 if (Seam.Remoting.debugWindow) 136 { 137 msg = msg.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">"); 138 Seam.Remoting.debugWindow.document.write("<pre>" + (new Date()) + ": " + msg + "</pre><br/>"); 139 } 140} 141 142Seam.Remoting.createNamespace = function(namespace) 143{ 144 var parts = namespace.split("."); 145 var base = Seam.Remoting.type; 146 147 for(var i = 0; i < parts.length; i++) 148 { 149 if (typeof base[parts[i]] == "undefined") 150 base[parts[i]] = new Object(); 151 base = base[parts[i]]; 152 } 153} 154 155Seam.Remoting.__Context = function() 156{ 157 this.conversationId = null; 158 159 Seam.Remoting.__Context.prototype.setConversationId = function(conversationId) 160 { 161 this.conversationId = conversationId; 162 } 163 164 Seam.Remoting.__Context.prototype.getConversationId = function() 165 { 166 return this.conversationId; 167 } 168} 169 170Seam.Remoting.Exception = function(msg) 171{ 172 this.message = msg; 173 174 Seam.Remoting.Exception.prototype.getMessage = function() 175 { 176 return this.message; 177 } 178} 179 180Seam.Remoting.context = new Seam.Remoting.__Context(); 181 182Seam.Remoting.getContext = function() 183{ 184 return Seam.Remoting.context; 185} 186 187Seam.Remoting.Map = function() 188{ 189 this.elements = new Array(); 190 191 Seam.Remoting.Map.prototype.size = function() 192 { 193 return this.elements.length; 194 } 195 196 Seam.Remoting.Map.prototype.isEmpty = function() 197 { 198 return this.elements.length == 0; 199 } 200 201 Seam.Remoting.Map.prototype.keySet = function() 202 { 203 var keySet = new Array(); 204 for (var i = 0; i < this.elements.length; i++) 205 keySet[keySet.length] = this.elements[i].key; 206 return keySet; 207 } 208 209 Seam.Remoting.Map.prototype.values = function() 210 { 211 var values = new Array(); 212 for (var i = 0; i < this.elements.length; i++) 213 values[values.length] = this.elements[i].value; 214 return values; 215 } 216 217 Seam.Remoting.Map.prototype.get = function(key) 218 { 219 for (var i = 0; i < this.elements.length; i++) 220 { 221 if (this.elements[i].key == key) 222 return this.elements[i].value; 223 } 224 return null; 225 } 226 227 Seam.Remoting.Map.prototype.put = function(key, value) 228 { 229 for (var i = 0; i < this.elements.length; i++) 230 { 231 if (this.elements[i].key == key) 232 { 233 this.elements[i].value = value; 234 return; 235 } 236 } 237 this.elements.push({key:key,value:value}); 238 } 239 240 Seam.Remoting.Map.prototype.remove = function(key) 241 { 242 for (var i = 0; i < this.elements.length; i++) 243 { 244 if (this.elements[i].key == key) 245 this.elements.splice(i, 1); 246 } 247 } 248 249 Seam.Remoting.Map.prototype.contains = function(key) 250 { 251 for (var i = 0; i < this.elements.length; i++) 252 { 253 if (this.elements[i].key == key) 254 return true; 255 } 256 return false;
257 } 258} 259 260Seam.Remoting.registerType = function(type) 261{ 262 for (var i = 0; i < Seam.Remoting.types.length; i++) 263 { 264 if (Seam.Remoting.types[i].__name == type.__name) 265 { 266 Seam.Remoting.types[i] = type; 267 return; 268 } 269 } 270 Seam.Remoting.types.push(type); 271} 272 273Seam.Remoting.createType = function(name) 274{ 275 for (var i = 0; i < Seam.Remoting.types.length; i++) 276 { 277 if (Seam.Remoting.types[i].__name == name) 278 return new Seam.Remoting.types[i]; 279 } 280} 281 282Seam.Remoting.getType = function(obj) 283{ 284 for (var i = 0; i < Seam.Remoting.types.length; i++) 285 { 286 if (obj instanceof Seam.Remoting.types[i]) 287 return Seam.Remoting.types[i]; 288 } 289 return null; 290} 291 292Seam.Remoting.getTypeName = function(obj) 293{ 294 var type = Seam.Remoting.getType(obj); 295 return type ? type.__name : null; 296} 297 298Seam.Remoting.getMetadata = function(obj) 299{ 300 for (var i = 0; i < Seam.Remoting.types.length; i++) 301 { 302 if (obj instanceof Seam.Remoting.types[i]) 303 return Seam.Remoting.types[i].__metadata; 304 } 305 return null; 306} 307 308Seam.Remoting.serializeValue = function(value, type, refs) 309{ 310 if (value == null) 311 return "<null/>"; 312 else if (type) 313 { 314 switch (type) { 315 // Boolean 316 case "bool": return "<bool>" + (value ? "true" : "false") + "</bool>"; 317 318 // Numerical types 319 case "number": return "<number>" + value + "</number>"; 320 321 // Date 322 case "date": return Seam.Remoting.serializeDate(value); 323 // Beans 324 case "bean": return Seam.Remoting.getTypeRef(value, refs); 325 326 // Collections 327 case "bag": return Seam.Remoting.serializeBag(value, refs); 328 case "map": return Seam.Remoting.serializeMap(value, refs); 329 330 default: return "<str>" + encodeURIComponent(value) + "</str>"; 331 } 332 } 333 else // We don't know the type.. try to guess 334 { 335 switch (typeof(value)) { 336 case "number": 337 return "<number>" + value + "</number>"; 338 case "boolean": 339 return "<bool>" + (value ? "true" : "false") + "</bool>"; 340 case "object": 341 if (value instanceof Array) 342 return Seam.Remoting.serializeBag(value, refs); 343 else if (value instanceof Date) 344 return Seam.Remoting.serializeDate(value); 345 else if (value instanceof Seam.Remoting.Map) 346 return Seam.Remoting.serializeMap(value, refs); 347 else 348 return Seam.Remoting.getTypeRef(value, refs); 349 default: 350 return "<str>" + encodeURIComponent(value) + "</str>"; // Default to String 351 } 352 } 353} 354 355Seam.Remoting.serializeBag = function(value, refs) 356{ 357 var data = "<bag>"; 358 359 for (var i = 0; i < value.length; i++) 360 { 361 data += "<element>"; 362 data += Seam.Remoting.serializeValue(value[i], null, refs); 363 data += "</element>"; 364 } 365 366 data += "</bag>"; 367 return data; 368} 369 370Seam.Remoting.serializeMap = function(value, refs) 371{ 372 var data = "<map>"; 373 374 var keyset = value.keySet(); 375 for (var i = 0; i < keyset.length; i++) 376 { 377 data += "<element><k>"; 378 data += Seam.Remoting.serializeValue(keyset[i], null, refs); 379 data += "</k><v>"; 380 data += Seam.Remoting.serializeValue(value.get(keyset[i]), null, refs); 381 data += "</v></element>"; 382 } 383 384 data += "</map>"; 385 return data; 386} 387 388Seam.Remoting.serializeDate = function(value) 389{ 390 var zeroPad = function(val, digits) { while (("" + val).length < digits) val = "0" + val; return val; }; 391 392 var data = "<date>"; 393 data += value.getFullYear(); 394 data += zeroPad(value.getMonth() + 1, 2); 395 data += zeroPad(value.getDate(), 2); 396 data += zeroPad(value.getHours(), 2); 397 data += zeroPad(value.getMinutes(), 2); 398 data += zeroPad(value.getSeconds(), 2); 399 data += zeroPad(value.getMilliseconds(), 3); 400 data += "</date>"; 401 return data; 402} 403 404Seam.Remoting.getTypeRef = function(obj, refs) 405{ 406 var refId = -1; 407 408 for (var i = 0; i < refs.length; i++) 409 { 410 if (refs[i] == obj) 411 { 412 refId = i; 413 break; 414 } 415 } 416 417 if (refId == -1) 418 { 419 refId = refs.length; 420 refs[refId] = obj; 421 } 422 423 return "<ref id=\"" + refId + "\"/>"; 424} 425 426Seam.Remoting.serializeType = function(obj, refs) 427{ 428 var data = "<bean type=\""; 429 430 var objType = Seam.Component.getComponentType(obj); 431 var isComponent = objType != null; 432 433 if (!isComponent) 434 objType = Seam.Remoting.getType(obj); 435 436 if (!objType) 437 { 438 alert("Unknown Type error."); 439 return null; 440 } 441 442 data += objType.__name; 443 data += "\">\n"; 444 445 var meta = isComponent ? Seam.Component.getMetadata(obj) : Seam.Remoting.getMetadata(obj); 446 for (var i = 0; i < meta.length; i++) 447 { 448 data += "<member name=\""; 449 data += meta[i].field; 450 data += "\">"; 451 data += Seam.Remoting.serializeValue(obj[meta[i].field], meta[i].type, refs); 452 data += "</member>\n"; 453 } 454 455 data += "</bean>"; 456 457 return data; 458} 459 460Seam.Remoting.__callId = 0; 461 462Seam.Remoting.eval = function(expression, callback) 463{ 464 var callId = "" + Seam.Remoting.__callId++; 465 var data = "<eval expr=\""; 466 data += expression; 467 data += "\" id=\""; 468 data += callId; 469 data += "\"/>"; 470 var call = {data: data, id: callId, callback: callback}; 471 472 var envelope = Seam.Remoting.createEnvelope(Seam.Remoting.createHeader(), data); 473 Seam.Remoting.pendingCalls.put(call.id, call); 474 475 call.asyncReq = Seam.Remoting.sendAjaxRequest(envelope, Seam.Remoting.PATH_EXECUTE, Seam.Remoting.processResponse, false); 476} 477 478Seam.Remoting.createCall = function(component, methodName, params, callback, exceptionHandler) 479{ 480 var callId = "" + Seam.Remoting.__callId++; 481 if (!callback) 482 callback = component.__callback[methodName]; 483 484 var data = "<call component=\""; 485 data += Seam.Component.getComponentType(component).__name; 486 data += "\" method=\""; 487 data += methodName; 488 data += "\" id=\""; 489 data += callId; 490 data += "\">\n"; 491 492 // Add parameters 493 data += "<params>"; 494 495 var refs = new Array(); 496 497 for (var i = 0; i < params.length; i++) 498 { 499 data += "<param>"; 500 data += Seam.Remoting.serializeValue(params[i], null, refs); 501 data += "</param>"; 502 } 503 504 data += "</params>"; 505 506 // Add refs 507 data += "<refs>"; 508 for (var i = 0; i < refs.length; i++) 509 { 510 data += "<ref id=\"" + i + "\">"; 511 data += Seam.Remoting.serializeType(refs[i], refs); 512 data += "</ref>"; 513 } 514 data += "</refs>"; 515 516 data += "</call>"; 517 518 return {data: data, id: callId, callback: callback, exceptionHandler: exceptionHandler}; 519} 520 521Seam.Remoting.createHeader = function() 522{ 523 var header = ""; 524 525 header += "<context>"; 526 if (Seam.Remoting.getContext().getConversationId()) 527 { 528 header += "<conversationId>"; 529 header += Seam.Remoting.getContext().getConversationId(); 530 header += "</conversationId>"; 531 } 532 header += "</context>"; 533 534 return header; 535} 536 537Seam.Remoting.createEnvelope = function(header, body) 538{ 539 var data = "<envelope>"; 540 541 if (header) 542 { 543 data += "<header>"; 544 data += header; 545 data += "</header>"; 546 } 547 548 if (body) 549 { 550 data += "<body>"; 551 data += body; 552 data += "</body>"; 553 } 554 555 data += "</envelope>"; 556 557 return data; 558} 559 560Seam.Remoting.pendingCalls = new Seam.Remoting.Map(); 561Seam.Remoting.inBatch = false;
562Seam.Remoting.batchedCalls = new Array(); 563 564Seam.Remoting.startBatch = function() 565{ 566 Seam.Remoting.inBatch = true; 567 Seam.Remoting.batchedCalls.length = 0; 568} 569 570Seam.Remoting.executeBatch = function() 571{ 572 if (!Seam.Remoting.inBatch) 573 return; 574 575 var data = ""; 576 for (var i = 0; i < Seam.Remoting.batchedCalls.length; i++) 577 { 578 Seam.Remoting.pendingCalls.put(Seam.Remoting.batchedCalls[i].id, Seam.Remoting.batchedCalls[i]); 579 data += Seam.Remoting.batchedCalls[i].data; 580 } 581 582 var envelope = Seam.Remoting.createEnvelope(Seam.Remoting.createHeader(), data); 583 Seam.Remoting.batchAsyncReq = Seam.Remoting.sendAjaxRequest(envelope, Seam.Remoting.PATH_EXECUTE, Seam.Remoting.processResponse, false); 584 Seam.Remoting.inBatch = false; 585} 586 587Seam.Remoting.cancelBatch = function() 588{ 589 Seam.Remoting.inBatch = false; 590 for (var i = 0; i < Seam.Remoting.batchedCalls.length; i++) 591 Seam.Remoting.pendingCalls.remove(Seam.Remoting.batchedCalls[i].id); 592} 593 594Seam.Remoting.cancelCall = function(callId) 595{ 596 var call = Seam.Remoting.pendingCalls.get(callId); 597 Seam.Remoting.pendingCalls.remove(callId); 598 if (call && call.asyncReq) 599 { 600 if (Seam.Remoting.pendingCalls.isEmpty()) 601 Seam.Remoting.hideLoadingMessage(); 602 call.asyncReq.onreadystatechange = function() {}; 603 call.asyncReq.abort(); 604 } 605} 606 607Seam.Remoting.execute = function(component, methodName, params, callback, exceptionHandler) 608{ 609 var call = Seam.Remoting.createCall(component, methodName, params, callback, exceptionHandler); 610 611 if (Seam.Remoting.inBatch) 612 { 613 Seam.Remoting.batchedCalls[Seam.Remoting.batchedCalls.length] = call; 614 } 615 else 616 { 617 // Marshal the request 618 var envelope = Seam.Remoting.createEnvelope(Seam.Remoting.createHeader(), call.data); 619 Seam.Remoting.pendingCalls.put(call.id, call); 620 call.asyncReq = Seam.Remoting.sendAjaxRequest(envelope, Seam.Remoting.PATH_EXECUTE, Seam.Remoting.processResponse, false); 621 } 622 623 return call; 624} 625 626Seam.Remoting.sendAjaxRequest = function(envelope, path, callback, silent) 627{ 628 Seam.Remoting.log("Request packet:\n" + envelope); 629 630 if (!silent) 631 Seam.Remoting.displayLoadingMessage(); 632 633 var asyncReq; 634 635 if (window.XMLHttpRequest) 636 { 637 asyncReq = new XMLHttpRequest(); 638 if (asyncReq.overrideMimeType) 639 asyncReq.overrideMimeType('text/xml'); 640 } 641 else 642 asyncReq = new ActiveXObject("Microsoft.XMLHTTP"); 643 644 var rcb = Seam.Remoting.requestCallback; 645 646 asyncReq.onreadystatechange = function() { 647 if (rcb) rcb(asyncReq, callback); 648 } 649 650 if (Seam.Remoting.encodedSessionId) 651 { 652 path += ';jsessionid=' + Seam.Remoting.encodedSessionId; 653 } 654 655 asyncReq.open("POST", Seam.Remoting.resourcePath + path, true); 656 asyncReq.send(envelope); 657 return asyncReq; 658} 659 660Seam.Remoting.setCallback = function(component, methodName, callback) 661{ 662 component.__callback[methodName] = callback; 663} 664 665Seam.Remoting.requestCallback = function(req, callback) 666{ 667 if (req.readyState == 4) 668 { 669 var inScope = typeof(Seam) == "undefined" ? false : true; 670 671 if (inScope) Seam.Remoting.hideLoadingMessage(); 672 req.onreadystatechange = function() {}; 673 674 if (req.status == 200) 675 { 676 if (inScope) Seam.Remoting.log("Response packet:\n" + req.responseText); 677 678 if (callback) 679 callback(req.responseXML); 680 } 681 else 682 alert("There was an error processing your request. Error code: " + req.status); 683 } 684} 685 686Seam.Remoting.processResponse = function(doc) 687{ 688 var headerNode; 689 var bodyNode; 690 691 var inScope = typeof(Seam) == "undefined" ? false : true; 692 if (!inScope) return; 693 694 var context = new Seam.Remoting.__Context; 695 696 if (doc.documentElement) 697 { 698 for (var i = 0; i < doc.documentElement.childNodes.length; i++) 699 { 700 var node = doc.documentElement.childNodes.item(i); 701 if (node.tagName == "header") 702 headerNode = node; 703 else if (node.tagName == "body") 704 bodyNode = node; 705 } 706 } 707 708 if (headerNode) 709 { 710 var contextNode; 711 for (var i = 0; i < headerNode.childNodes.length; i++) 712 { 713 var node = headerNode.childNodes.item(i); 714 if (node.tagName == "context") 715 { 716 contextNode = node; 717 break; 718 } 719 } 720 if (contextNode && context) 721 { 722 Seam.Remoting.unmarshalContext(contextNode, context); 723 if (context.getConversationId() && Seam.Remoting.getContext().getConversationId() == null) 724 Seam.Remoting.getContext().setConversationId(context.getConversationId()); 725 } 726 } 727 728 if (bodyNode) 729 { 730 for (var i = 0; i < bodyNode.childNodes.length; i++) 731 { 732 var node = bodyNode.childNodes.item(i); 733 if (node.tagName == "result") 734 Seam.Remoting.processResult(node, context); 735 } 736 } 737} 738 739Seam.Remoting.processResult = function(result, context) 740{ 741 var callId = result.getAttribute("id"); 742 var call = Seam.Remoting.pendingCalls.get(callId); 743 Seam.Remoting.pendingCalls.remove(callId); 744 745 if (call && (call.callback || call.exceptionHandler)) 746 { 747 var valueNode = null; 748 var refsNode = null; 749 var exceptionNode = null; 750 751 var children = result.childNodes; 752 for (var i = 0; i < children.length; i++) 753 { 754 var tag = children.item(i).tagName; 755 if (tag == "value") 756 valueNode = children.item(i); 757 else if (tag == "refs") 758 refsNode = children.item(i); 759 else if (tag == "exception") 760 exceptionNode = children.item(i); 761 } 762 763 if (exceptionNode != null) 764 { 765 var msgNode = null; 766 var children = exceptionNode.childNodes; 767 for (var i = 0; i < children.length; i++) 768 { 769 var tag = children.item(i).tagName; 770 if (tag == "message") 771 msgNode = children.item(i); 772 } 773 774 var msg = Seam.Remoting.unmarshalValue(msgNode.firstChild); 775 var ex = new Seam.Remoting.Exception(msg); 776 call.exceptionHandler(ex); 777 } 778 else 779 { 780 var refs = new Array(); 781 if (refsNode) 782 Seam.Remoting.unmarshalRefs(refsNode, refs); 783
784 var value = Seam.Remoting.unmarshalValue(valueNode.firstChild, refs); 785 786 call.callback(value, context, callId); 787 } 788 } 789} 790 791Seam.Remoting.unmarshalContext = function(ctxNode, context) 792{ 793 for (var i = 0; i < ctxNode.childNodes.length; i++) 794 { 795 var tag = ctxNode.childNodes.item(i).tagName; 796 if (tag == "conversationId") 797 context.setConversationId(ctxNode.childNodes.item(i).firstChild.nodeValue); 798 } 799} 800 801Seam.Remoting.unmarshalRefs = function(refsNode, refs) 802{ 803 var objs = new Array(); 804 805 // Pass 1 - create the reference objects 806 for (var i = 0; i < refsNode.childNodes.length; i++) 807 { 808 if (refsNode.childNodes.item(i).tagName == "ref") 809 { 810 var refNode = refsNode.childNodes.item(i); 811 var refId = parseInt(refNode.getAttribute("id")); 812 813 var valueNode = refNode.firstChild; 814 if (valueNode.tagName == "bean") 815 { 816 var obj = null; 817 var typeName = valueNode.getAttribute("type"); 818 if (Seam.Component.isRegistered(typeName)) 819 obj = Seam.Component.newInstance(typeName); 820 else 821 obj = Seam.Remoting.createType(typeName); 822 if (obj) 823 { 824 refs[refId] = obj; 825 objs[objs.length] = {obj: obj, node: valueNode}; 826 } 827 } 828 } 829 } 830 831 // Pass 2 - populate the object members 832 for (var i = 0; i < objs.length; i++) 833 { 834 for (var j = 0; j < objs[i].node.childNodes.length; j++) 835 { 836 var child = objs[i].node.childNodes.item(j); 837 if (child.tagName == "member") 838 { 839 var name = child.getAttribute("name"); 840 objs[i].obj[name] = Seam.Remoting.unmarshalValue(child.firstChild, refs); 841 } 842 } 843 } 844} 845 846Seam.Remoting.unmarshalValue = function(element, refs) 847{ 848 var tag = element.tagName; 849 850 switch (tag) 851 { 852 case "bool": return element.firstChild.nodeValue == "true"; 853 case "number": 854 if (element.firstChild.nodeValue.indexOf(".") == -1) 855 return parseInt(element.firstChild.nodeValue); 856 else 857 return parseFloat(element.firstChild.nodeValue); 858 case "str": 859 var data = ""; 860 for (var i = 0; i < element.childNodes.length; i++) 861 { 862 if (element.childNodes[i].nodeType == 3) // NODE_TEXT 863 data += element.childNodes[i].nodeValue; 864 } 865 return decodeURIComponent(data); 866 case "ref": return refs[parseInt(element.getAttribute("id"))]; 867 case "bag": 868 var value = new Array(); 869 for (var i = 0; i < element.childNodes.length; i++) 870 { 871 if (element.childNodes.item(i).tagName == "element") 872 value[value.length] = Seam.Remoting.unmarshalValue(element.childNodes.item(i).firstChild, refs); 873 } 874 return value; 875 case "map": 876 var map = new Seam.Remoting.Map(); 877 for (var i = 0; i < element.childNodes.length; i++) 878 { 879 var childNode = element.childNodes.item(i); 880 if (childNode.tagName == "element") 881 { 882 var key = null 883 var value = null; 884 885 for (var j = 0; j < childNode.childNodes.length; j++) 886 { 887 if (key == null && childNode.childNodes.item(j).tagName == "k") 888 key = Seam.Remoting.unmarshalValue(childNode.childNodes.item(j).firstChild, refs); 889 else if (value == null && childNode.childNodes.item(j).tagName == "v") 890 value = Seam.Remoting.unmarshalValue(childNode.childNodes.item(j).firstChild, refs); 891 } 892 893 if (key != null) 894 map.put(key, value); 895 } 896 } 897 return map; 898 case "date": return Seam.Remoting.deserializeDate(element.firstChild.nodeValue); 899 default: return null; 900 } 901} 902 903Seam.Remoting.deserializeDate = function(val) 904{ 905 var dte = new Date(); 906 dte.setFullYear(parseInt(val.substring(0,4), 10)); 907 dte.setMonth(parseInt(val.substring(4,6), 10) - 1); 908 dte.setDate(parseInt(val.substring(6,8), 10)); 909 dte.setHours(parseInt(val.substring(8,10), 10)); 910 dte.setMinutes(parseInt(val.substring(10,12), 10)); 911 dte.setSeconds(parseInt(val.substring(12,14), 10)); 912 dte.setMilliseconds(parseInt(val.substring(14,17), 10)); 913 return dte; 914} 915 916Seam.Remoting.loadingMsgDiv = null; 917Seam.Remoting.loadingMessage = "Please wait..."; 918Seam.Remoting.displayLoadingMessage = function() 919{ 920 if (!Seam.Remoting.loadingMsgDiv) 921 { 922 Seam.Remoting.loadingMsgDiv = document.createElement('div'); 923 var msgDiv = Seam.Remoting.loadingMsgDiv; 924 msgDiv.setAttribute('id', 'loadingMsg'); 925 926 msgDiv.style.position = "absolute"; 927 msgDiv.style.top = "0px"; 928 msgDiv.style.right = "0px"; 929 msgDiv.style.background = "red"; 930 msgDiv.style.color = "white"; 931 msgDiv.style.fontFamily = "Verdana,Helvetica,Arial"; 932 msgDiv.style.fontSize = "small"; 933 msgDiv.style.padding = "2px"; 934 msgDiv.style.border = "1px solid black"; 935 936 document.body.appendChild(msgDiv); 937 938 var text = document.createTextNode(Seam.Remoting.loadingMessage); 939 msgDiv.appendChild(text); 940 } 941 else 942 { 943 Seam.Remoting.loadingMsgDiv.innerHTML = Seam.Remoting.loadingMessage; 944 Seam.Remoting.loadingMsgDiv.style.visibility = 'visible'; 945 } 946} 947 948Seam.Remoting.hideLoadingMessage = function() 949{ 950 if (Seam.Remoting.loadingMsgDiv) 951 Seam.Remoting.loadingMsgDiv.style.visibility = 'hidden'; 952} 953
954/* Messaging API */ 955 956Seam.Remoting.pollInterval = 10; // Default poll interval of 10 seconds 957Seam.Remoting.pollTimeout = 0; // Default timeout of 0 seconds 958Seam.Remoting.polling = false; 959 960Seam.Remoting.setPollInterval = function(interval) 961{ 962 Seam.Remoting.pollInterval = interval; 963} 964 965Seam.Remoting.setPollTimeout = function(timeout) 966{ 967 Seam.Remoting.pollTimeout = timeout; 968} 969 970Seam.Remoting.subscriptionRegistry = new Array(); 971 972Seam.Remoting.subscribe = function(topicName, callback) 973{ 974 for (var i = 0; i < Seam.Remoting.subscriptionRegistry.length; i++) 975 { 976 if (Seam.Remoting.subscriptionRegistry[i].topic == topicName) 977 return; 978 } 979 980 var body = "<subscribe topic=\"" + topicName + "\"/>"; 981 var env = Seam.Remoting.createEnvelope(null, body); 982 Seam.Remoting.subscriptionRegistry.push({topic:topicName, callback:callback}); 983 Seam.Remoting.sendAjaxRequest(env, Seam.Remoting.PATH_SUBSCRIPTION, Seam.Remoting.subscriptionCallback, false); 984} 985 986Seam.Remoting.unsubscribe = function(topicName) 987{ 988 var token = null; 989 990 for (var i = 0; i < Seam.Remoting.subscriptionRegistry.length; i++) 991 { 992 if (Seam.Remoting.subscriptionRegistry[i].topic == topicName) 993 { 994 token = Seam.Remoting.subscriptionRegistry[i].token; 995 Seam.Remoting.subscriptionRegistry.splice(i, 1); 996 } 997 } 998 999 if (token) 1000 { 1001 var body = "<unsubscribe token=\"" + token + "\"/>"; 1002 var env = Seam.Remoting.createEnvelope(null, body); 1003 Seam.Remoting.sendAjaxRequest(env, Seam.Remoting.PATH_SUBSCRIPTION, null, false); 1004 } 1005} 1006 1007Seam.Remoting.subscriptionCallback = function(doc) 1008{ 1009 var body = doc.documentElement.firstChild; 1010 for (var i = 0; i < body.childNodes.length; i++) 1011 { 1012 var node = body.childNodes.item(i); 1013 if (node.tagName == "subscription") 1014 { 1015 var topic = node.getAttribute("topic"); 1016 var token = node.getAttribute("token"); 1017 for (var i = 0; i < Seam.Remoting.subscriptionRegistry.length; i++) 1018 { 1019 if (Seam.Remoting.subscriptionRegistry[i].topic == topic) 1020 { 1021 Seam.Remoting.subscriptionRegistry[i].token = token; 1022 Seam.Remoting.poll(); 1023 break; 1024 } 1025 } 1026 } 1027 } 1028} 1029 1030Seam.Remoting.pollTimeoutFunction = null; 1031 1032Seam.Remoting.poll = function() 1033{ 1034 if (Seam.Remoting.polling) 1035 return; 1036 1037 Seam.Remoting.polling = true; 1038 clearTimeout(Seam.Remoting.pollTimeoutFunction); 1039 1040 var body = ""; 1041 1042 if (Seam.Remoting.subscriptionRegistry.length == 0) 1043 { 1044 Seam.Remoting.polling = false; 1045 return; 1046 } 1047 1048 for (var i = 0; i < Seam.Remoting.subscriptionRegistry.length; i++) 1049 { 1050 body += "<poll token=\"" + Seam.Remoting.subscriptionRegistry[i].token + "\" "; 1051 body += "timeout=\"" + Seam.Remoting.pollTimeout + "\"/>"; 1052 } 1053 1054 var env = Seam.Remoting.createEnvelope(null, body); 1055 Seam.Remoting.sendAjaxRequest(env, Seam.Remoting.PATH_POLL, Seam.Remoting.pollCallback, true); 1056} 1057 1058Seam.Remoting.pollCallback = function(doc) 1059{ 1060 Seam.Remoting.polling = false; 1061 1062 var body = doc.documentElement.firstChild; 1063 for (var i = 0; i < body.childNodes.length; i++) 1064 { 1065 var node = body.childNodes.item(i); 1066 if (node.tagName == "messages") 1067 Seam.Remoting.processMessages(node); 1068 else if (node.tagName == "errors") 1069 Seam.Remoting.processPollErrors(node); 1070 } 1071 1072 Seam.Remoting.pollTimeoutFunction = setTimeout("Seam.Remoting.poll()", Math.max(Seam.Remoting.pollInterval * 1000, 1000)); 1073} 1074 1075Seam.Remoting.processMessages = function(messages) 1076{ 1077 var token = messages.getAttribute("token"); 1078 1079 var callback = null; 1080 for (var i = 0; i < Seam.Remoting.subscriptionRegistry.length; i++) 1081 { 1082 if (Seam.Remoting.subscriptionRegistry[i].token == token) 1083 { 1084 callback = Seam.Remoting.subscriptionRegistry[i].callback; 1085 break; 1086 } 1087 } 1088 1089 if (callback != null) 1090 { 1091 var messageNode = null; 1092 1093 var children = messages.childNodes; 1094 for (var i = 0; i < children.length; i++) 1095 { 1096 if (children.item(i).tagName == "message") 1097 { 1098 messageNode = children.item(i); 1099 var messageType = messageNode.getAttribute("type"); 1100 1101 var valueNode = null; 1102 var refsNode = null; 1103 for (var j = 0; j < messageNode.childNodes.length; j++) 1104 { 1105 var node = messageNode.childNodes.item(j); 1106 if (node.tagName == "value") 1107 valueNode = node; 1108 else if (node.tagName == "refs") 1109 refsNode = node; 1110 } 1111 1112 var refs = new Array(); 1113 if (refsNode) 1114 Seam.Remoting.unmarshalRefs(refsNode, refs); 1115
1116 var value = Seam.Remoting.unmarshalValue(valueNode.firstChild, refs); 1117 1118 callback(Seam.Remoting.createMessage(messageType, value)); 1119 } 1120 } 1121 } 1122} 1123 1124Seam.Remoting.processErrors = function(errors) 1125{ 1126 var token = errors.getAttribute("token"); 1127 1128 // Unsubscribe to the topic 1129 for (var i = 0; i < Seam.Remoting.subscriptionRegistry.length; i++) 1130 { 1131 if (Seam.Remoting.subscriptionRegistry[i].token == token) 1132 { 1133 Seam.Remoting.subscriptionRegistry.splice(i, 1); 1134 break; 1135 } 1136 } 1137 1138 for (var i = 0; i < errors.childNodes.length; i++) 1139 { 1140 if (errors.childNodes.item(i).tagName == "error") 1141 { 1142 var errorNode = errors.childNodes.item(i); 1143 var code = errorNode.getAttribute("code"); 1144 var message = errorNode.firstChild.nodeValue; 1145 1146 if (Seam.Remoting.onPollError) 1147 Seam.Remoting.onPollError(code, message); 1148 else 1149 alert("A polling error occurred: " + code + " " + message); 1150 } 1151 } 1152} 1153 1154Seam.Remoting.ObjectMessage = function() 1155{ 1156 this.value = null; 1157 1158 Seam.Remoting.ObjectMessage.prototype.getValue = function() 1159 { 1160 return this.value; 1161 } 1162 1163 Seam.Remoting.ObjectMessage.prototype.setValue = function(value) 1164 { 1165 this.value = value; 1166 } 1167} 1168 1169Seam.Remoting.TextMessage = function() 1170{ 1171 this.text = null; 1172 1173 Seam.Remoting.TextMessage.prototype.getText = function() 1174 { 1175 return this.text; 1176 } 1177 1178 Seam.Remoting.TextMessage.prototype.setText = function(text) 1179 { 1180 this.text = text; 1181 } 1182} 1183 1184Seam.Remoting.createMessage = function(messageType, value) 1185{ 1186 switch (messageType) 1187 { 1188 case "object": 1189 var msg = new Seam.Remoting.ObjectMessage(); 1190 msg.setValue(value); 1191 return msg; 1192 case "text": 1193 var msg = new Seam.Remoting.TextMessage(); 1194 msg.setText(value); 1195 return msg; 1196 } 1197 return null; 1198} 1199 1200Seam.Remoting.resourcePath = "/iwr/seam/resource/remoting"; 1201Seam.Remoting.debug = false; 1202Seam.Remoting.pollInterval = 1; 1203Seam.Remoting.pollTimeout = 10;
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.