This commit is contained in:
2024-04-27 09:23:34 +02:00
commit 11e713ca6f
11884 changed files with 3263371 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
/*
Copyright (c) 2004 Jan-Klaas Kollhof
This file is part of the JavaScript o lait library(jsolait).
jsolait is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
Module("codecs","0.1.2",function(mod){mod.listEncoders=function(){var c=[];for(var attr in String.prototype){if(attr.slice(0,7)=="encode_"){c.push(attr.slice(7));}}
return c;}
mod.listDecoders=function(){var c=[];for(var attr in String.prototype){if(attr.slice(0,7)=="decode_"){c.push(attr.slice(7));}}
return c;}
String.prototype.decode=function(codec){var n="decode_"+codec;if(String.prototype[n]){var args=[];for(var i=1;i<arguments.length;i++){args[i-1]=arguments[i];}
return String.prototype[n].apply(this,args);}else{throw new mod.Exception("Decoder '%s' not found.".format(codec));}}
String.prototype.encode=function(codec){var n="encode_"+codec;if(String.prototype[n]){var args=[];for(var i=1;i<arguments.length;i++){args[i-1]=arguments[i];}
return String.prototype[n].apply(this,args);}else{throw new mod.Exception("Ecnoder '%s' not found.".format(codec));}}
String.prototype.decode_base64=function(){if((this.length%4)==0){if(typeof(atob)!="undefined"){return atob(this);}else{var nBits;var sDecoded=new Array(this.length/4);var base64='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';for(var i=0;i<this.length;i+=4){nBits=(base64.indexOf(this.charAt(i))&0xff)<<18|(base64.indexOf(this.charAt(i+1))&0xff)<<12|(base64.indexOf(this.charAt(i+2))&0xff)<<6|base64.indexOf(this.charAt(i+3))&0xff;sDecoded[i]=String.fromCharCode((nBits&0xff0000)>>16,(nBits&0xff00)>>8,nBits&0xff);}
sDecoded[sDecoded.length-1]=sDecoded[sDecoded.length-1].substring(0,3-((this.charCodeAt(i-2)==61)?2:(this.charCodeAt(i-1)==61?1:0)));return sDecoded.join("");}}else{throw new mod.Exception("String length must be divisible by 4.");}}
String.prototype.encode_base64=function(){if(typeof(btoa)!="undefined"){return btoa(this);}else{var base64=['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'];var sbin;var pad=0;var s=""+this;if((s.length%3)==1){s+=String.fromCharCode(0);s+=String.fromCharCode(0);pad=2;}else if((s.length%3)==2){s+=String.fromCharCode(0);pad=1;}
var rslt=new Array(s.length/3);var ri=0;for(var i=0;i<s.length;i+=3){sbin=((s.charCodeAt(i)&0xff)<<16)|((s.charCodeAt(i+1)&0xff)<<8)|(s.charCodeAt(i+2)&0xff);rslt[ri]=(base64[(sbin>>18)&0x3f]+base64[(sbin>>12)&0x3f]+base64[(sbin>>6)&0x3f]+base64[sbin&0x3f]);ri++;}
if(pad>0){rslt[rslt.length-1]=rslt[rslt.length-1].substr(0,4-pad)+((pad==2)?"==":(pad==1)?"=":"");}
return rslt.join("");}}
String.prototype.decode_uri=function(){return decodeURI(this);}
String.prototype.encode_uri=function(){return encodeURI(this);}})

View File

@@ -0,0 +1,34 @@
/*
Copyright (c) 2003 Jan-Klaas Kollhof
This file is part of the JavaScript o lait library(jsolait).
jsolait is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
Module("crypto","0.1.2",function(mod){mod.listEncrypters=function(){var c=[];for(var attr in String.prototype){if(attr.slice(0,8)=="encrypt_"){c.push(attr.slice(8));}}
return c;}
mod.listDecrypters=function(){var c=[];for(var attr in String.prototype){if(attr.slice(0,8)=="decrypt_"){c.push(attr.slice(8));}}
return c;}
String.prototype.encrypt=function(crydec){var n="encrypt_"+crydec;if(String.prototype[n]){var args=[];for(var i=1;i<arguments.length;i++){args[i-1]=arguments[i];}
return String.prototype[n].apply(this,args);}else{throw new mod.Exception("Decrypter '%s' not found.".format(crydec));}}
String.prototype.decrypt=function(crydec){var n="decrypt_"+crydec;if(String.prototype[n]){var args=[];for(var i=1;i<arguments.length;i++){args[i-1]=arguments[i];}
return String.prototype[n].apply(this,args);}else{throw new mod.Exception("Encrypter '%s' not found.".format(crydec));}}
String.prototype.encrypt_xor=function(key){var e=new Array(this.length);var l=key.length;for(var i=0;i<this.length;i++){e[i]=String.fromCharCode(this.charCodeAt(i)^key.charCodeAt(i%l));}
return e.join("");}
String.prototype.decrypt_xor=String.prototype.encrypt_xor;String.prototype.encrypt_rc4=function(key){var sbox=new Array(256);for(var i=0;i<256;i++){sbox[i]=i;}
var j=0;for(var i=0;i<256;i++){j=(j+sbox[i]+key.charCodeAt(i%key.length))%256;var tmp=sbox[i];sbox[i]=sbox[j];sbox[j]=tmp;}
var i=256;var j=256;var rslt=new Array(this.length);for(var k=0;k<this.length;k++){i=(i+1)%256;j=(j+sbox[i])%256;var tmp=sbox[i];sbox[i]=sbox[j];sbox[j]=tmp;t=(sbox[i]+sbox[j])%256;rslt[k]=String.fromCharCode(this.charCodeAt(k)^sbox[t]);}
return rslt.join("");}
String.prototype.decrypt_rc4=String.prototype.encrypt_rc4;})

View File

@@ -0,0 +1,71 @@
/*
Copyright (c) 2003-2004 Jan-Klaas Kollhof
This file is part of the JavaScript o lait library(jsolait).
jsolait is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
Module("jsonrpc","0.3.2",function(mod){var lang=importModule("lang");var tokens=lang.tokens;var ObjectBuffer=Class("ObjectBuffer",function(publ,supr){publ.init=function(){this.data="";}
publ.getObjects=function(data){this.data+=data;var t=new lang.Tokenizer(this.data);var brCnt=0;var objects=[];var readCnt=0
while(!t.finished()){var n=t.next();if(n.type!=tokens.ERR){if(n.value=="{"){brCnt+=1;}else if(n.value=="}"){brCnt-=1;if(brCnt==0){var s=this.data.slice(readCnt,n.pos+1);readCnt+=s.length;objects.push(s);}}}else{break;}}
this.data=this.data.slice(readCnt);return objects;}})
var nameAllowed=function(name){return name.match(/^[a-zA-Z]\w*$/)!=null;}
var getMethodByName=function(obj,name){try{obj=obj._getMethodByName(name)}catch(e){var names=name.split(".");for(var i=0;i<names.length;i++){name=names[i];if(nameAllowed(name)){obj=obj[name];}}}
return obj;}
var Response=Class("Response",function(publ,supr){publ.init=function(id,result,error){this.id=id;this.result=result;this.error=error;}
publ._toJSON=function(){var p=[lang.objToJson(this.id),lang.objToJson(this.result),lang.objToJson(this.error)];return'{"id":'+p[0]+', "result":'+p[1]+', "error":'+p[2]+"}";}})
var Request=Class("Request",function(publ,supr){publ.init=function(id,method,params){this.id=id;this.method=method;this.params=params;}
publ._toJSON=function(){var p=[lang.objToJson(this.id),lang.objToJson(this.method),lang.objToJson(this.params)];return'{"id":'+p[0]+', "method":'+p[1]+', "params":'+p[2]+"}";}})
var Notification=Class("Notification",function(publ,supr){publ.init=function(method,params){this.method=method;this.params=params;}
publ._toJSON=function(){var p=[lang.objToJson(this.method),lang.objToJson(this.params)];return'{"method":'+p[0]+', "params":'+p[1]+"}";}})
var ResponseHandler=Class("ResponseHandler",function(publ,supr){publ.init=function(callback){this.callback=callback;}
publ.handleResponse=function(resp){this.callback(resp.result,resp.error);}})
var RPCLib=Class("RPCLib",function(publ,supr){})
var BaseConnectionHandler=Class("BaseConnectionHandler",function(publ,supr){publ.init=function(service){this.service=service;this.jsonParser=new lang.JSONParser();this.jsonParser.addLib(new RPCLib(),"rpc",[]);this.respHandlers=[];this.objBuffer=new ObjectBuffer();}
publ.addResponseHandler=function(cb){var id=1;while(this.respHandlers[""+id]){id+=1;}
id=""+id;this.respHandlers[id]=new ResponseHandler(cb);return id;}
publ.send=function(data){}
publ.sendNotify=function(name,args){var n=new Notification(name,args);n=this.jsonParser.objToJson(n);this.send(n)}
publ.sendRequest=function(name,args,callback){var id=this.addResponseHandler(callback);var r=new Request(id,name,args);r=this.jsonParser.objToJson(r);this.send(r);}
publ.sendResponse=function(id,result,error){var r=new Response(id,result,error);r=this.jsonParser.objToJson(r);this.send(r);}
publ.handleRequest=function(req){var name=req.method;var params=req.params;var id=req.id;if(this.service[name]){try{var rslt=this.service[name].apply(this.service,params);this.sendResponse(id,rslt,null)}catch(e){this.sendResponse(id,null,new ApplicationError(""+e))}}else{this.sendResponse(id,null,new MethodNotFound());}}
publ.handleNotification=function(notif){if(this.service[notif.method]){try{this.service[notif.method].apply(this.service,notif.params);}catch(e){}}}
publ.handleResponse=function(resp){var id=resp.id;var h=this.respHandlers[id];h.handleResponse(resp)
delete this.respHandlers[id]}
publ.handleData=function(data){var objs=this.objBuffer.getObjects(data);for(var i=0;i<objs.length;i++){try{var obj=this.jsonParser.jsonToObj(objs[i]);}catch(e){throw"Not well formed";}
if(obj.method!=null){if(obj.id!=null){this.handleRequest(new Request(obj.id,obj.method,obj.params));}else{this.handleNotification(new Notification(obj.method,obj.params));}}else if(obj.id!=null){this.handleResponse(new Response(obj.id,obj.result,obj.error));}else{throw"Unknown Data";}}}})
var SocketConnectionHandler=Class("SocketConnectionHandler",BaseConnectionHandler,function(publ,supr){publ.init=function(socket,localService){this.socket=socket;socket.addEventListener("connectionData",this,false);supr(this).init(localService);}
publ.handleEvent=function(evt){this.handleData(evt.data);}
publ.send=function(data){this.socket.send(data);}
publ.close=function(data){this.socket.close();}})
var HTTPConnectionHandler=Class("HTTPConnectionHandler",BaseConnectionHandler,function(publ,supr){var urllib;publ.request_id=1;publ.init=function(url,localService){urllib=importModule("urllib");this.url=url;supr(this).init(localService);}
publ.handleData=function(data){try{var obj=JSON.parse(data);}catch(e){;throw" Not well formed\n\n"+e+"\n\nResponse from server:\n\n "+data;}
if(obj.id!=null){return obj;}else{throw"Unknown Data (No id property found)";}}
publ.sendRequest=function(name,args,callback){var sync=false;if(typeof callback!="function"){args.push(callback);sync=true;}
var data=new Request(this.request_id++,name,args);data=JSON.stringify(data);if(sync){var rsp=urllib.postURL(this.url,data,[["Content-Type","text/plain"]]);rsp=this.handleData(rsp.responseText);if(rsp.error){throw rsp.error;}else{return rsp.result;}}else{var self=this;var request_id=this.request_id;urllib.postURL(this.url,data,[["Content-Type","text/plain"]],function(rsp){try{rsp=self.handleData(rsp.responseText);}catch(e){callback(request_id,null,e);return;}
callback(request_id,rsp.result,rsp.error);});}}
publ.sendNotify=function(name,args){var data=new Notification(name,args);data=this.jsonParser.objToJson(data);urllib.postURL(this.url,data,[["Content-Type","text/plain"]],function(rsp){});}})
var PeerObject=Class("PeerObject",function(publ,supr){publ.init=function(name,conn){var fn=function(){var args=[];for(var i=0;i<arguments.length;i++){args[i]=arguments[i];}
var cb=args.pop();return conn.sendRequest(name,args,cb);}
return fn;}})
var PeerNotifyObject=Class("PeerNotifyObject",function(publ,supr){publ.init=function(name,conn){var fn=function(){var args=[];for(var i=0;i<arguments.length;i++){args[i]=arguments[i];}
conn.sendNotify(name,args);}
return fn;}})
var BasePeer=Class("BasePeer",function(publ,supr){publ.init=function(conn,methodNames){this._conn=conn;this.notify=new PeerObject("notify",conn);this._add(methodNames);}
var setupPeerMethod=function(root,methodName,conn,MethClass){var names=methodName.split(".");var obj=root;for(var n=0;n<names.length-1;n++){var name=names[n];if(obj[name]){obj=obj[name];}else{obj[name]=new Object();obj=obj[name];}}
var name=names[names.length-1];if(obj[name]){}else{var mth=new MethClass(methodName,conn);obj[name]=mth;}}
publ._add=function(methodNames){for(var i=0;i<methodNames.length;i++){setupPeerMethod(this,methodNames[i],this._conn,PeerObject);setupPeerMethod(this.notify,methodNames[i],this._conn,PeerNotifyObject);}}})
mod.ServiceProxy=Class("ServiceProxy",BasePeer,function(publ,supr){publ.init=function(url,methodNames,localService){var n=url.match(/^jsonrpc:\/\/(.*:\d*)$/);if(n!=null){var hostaddr=n[1];try{var socket=createConnection();}catch(e){throw"Can't create a socket connection."}
socket.connect(hostaddr);supr(this).init(new SocketConnectionHandler(socket,localService),methodNames);}else{this.httpConn=new HTTPConnectionHandler(url,localService);supr(this).init(this.httpConn,methodNames);}}})})

View File

@@ -0,0 +1,71 @@
/*
Copyright (c) 2003-2004 Jan-Klaas Kollhof
This file is part of the JavaScript o lait library(jsolait).
jsolait is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
Module("jsonrpclite","0.3.2",function(mod){var lang=importModule("langlite");var tokens=lang.tokens;var ObjectBuffer=Class("ObjectBuffer",function(publ,supr){publ.init=function(){this.data="";}
publ.getObjects=function(data){this.data+=data;var t=new lang.Tokenizer(this.data);var brCnt=0;var objects=[];var readCnt=0
while(!t.finished()){var n=t.next();if(n.type!=tokens.ERR){if(n.value=="{"){brCnt+=1;}else if(n.value=="}"){brCnt-=1;if(brCnt==0){var s=this.data.slice(readCnt,n.pos+1);readCnt+=s.length;objects.push(s);}}}else{break;}}
this.data=this.data.slice(readCnt);return objects;}})
var nameAllowed=function(name){return name.match(/^[a-zA-Z]\w*$/)!=null;}
var getMethodByName=function(obj,name){try{obj=obj._getMethodByName(name)}catch(e){var names=name.split(".");for(var i=0;i<names.length;i++){name=names[i];if(nameAllowed(name)){obj=obj[name];}}}
return obj;}
var Response=Class("Response",function(publ,supr){publ.init=function(id,result,error){this.id=id;this.result=result;this.error=error;}
publ._toJSON=function(){var p=[lang.objToJson(this.id),lang.objToJson(this.result),lang.objToJson(this.error)];return'{"id":'+p[0]+', "result":'+p[1]+', "error":'+p[2]+"}";}})
var Request=Class("Request",function(publ,supr){publ.init=function(id,method,params){this.id=id;this.method=method;this.params=params;}
publ._toJSON=function(){var p=[lang.objToJson(this.id),lang.objToJson(this.method),lang.objToJson(this.params)];return'{"id":'+p[0]+', "method":'+p[1]+', "params":'+p[2]+"}";}})
var Notification=Class("Notification",function(publ,supr){publ.init=function(method,params){this.method=method;this.params=params;}
publ._toJSON=function(){var p=[lang.objToJson(this.method),lang.objToJson(this.params)];return'{"method":'+p[0]+', "params":'+p[1]+"}";}})
var ResponseHandler=Class("ResponseHandler",function(publ,supr){publ.init=function(callback){this.callback=callback;}
publ.handleResponse=function(resp){this.callback(resp.result,resp.error);}})
var RPCLib=Class("RPCLib",function(publ,supr){})
var BaseConnectionHandler=Class("BaseConnectionHandler",function(publ,supr){publ.init=function(service){this.service=service;this.jsonParser=new lang.JSONParser();this.jsonParser.addLib(new RPCLib(),"rpc",[]);this.respHandlers=[];this.objBuffer=new ObjectBuffer();}
publ.addResponseHandler=function(cb){var id=1;while(this.respHandlers[""+id]){id+=1;}
id=""+id;this.respHandlers[id]=new ResponseHandler(cb);return id;}
publ.send=function(data){}
publ.sendNotify=function(name,args){var n=new Notification(name,args);n=this.jsonParser.objToJson(n);this.send(n)}
publ.sendRequest=function(name,args,callback){var id=this.addResponseHandler(callback);var r=new Request(id,name,args);r=this.jsonParser.objToJson(r);this.send(r);}
publ.sendResponse=function(id,result,error){var r=new Response(id,result,error);r=this.jsonParser.objToJson(r);this.send(r);}
publ.handleRequest=function(req){var name=req.method;var params=req.params;var id=req.id;if(this.service[name]){try{var rslt=this.service[name].apply(this.service,params);this.sendResponse(id,rslt,null)}catch(e){this.sendResponse(id,null,new ApplicationError(""+e))}}else{this.sendResponse(id,null,new MethodNotFound());}}
publ.handleNotification=function(notif){if(this.service[notif.method]){try{this.service[notif.method].apply(this.service,notif.params);}catch(e){}}}
publ.handleResponse=function(resp){var id=resp.id;var h=this.respHandlers[id];h.handleResponse(resp)
delete this.respHandlers[id]}
publ.handleData=function(data){var objs=this.objBuffer.getObjects(data);for(var i=0;i<objs.length;i++){try{var obj=this.jsonParser.jsonToObj(objs[i]);}catch(e){throw"Not well formed";}
if(obj.method!=null){if(obj.id!=null){this.handleRequest(new Request(obj.id,obj.method,obj.params));}else{this.handleNotification(new Notification(obj.method,obj.params));}}else if(obj.id!=null){this.handleResponse(new Response(obj.id,obj.result,obj.error));}else{throw"Unknown Data";}}}})
var SocketConnectionHandler=Class("SocketConnectionHandler",BaseConnectionHandler,function(publ,supr){publ.init=function(socket,localService){this.socket=socket;socket.addEventListener("connectionData",this,false);supr(this).init(localService);}
publ.handleEvent=function(evt){this.handleData(evt.data);}
publ.send=function(data){this.socket.send(data);}
publ.close=function(data){this.socket.close();}})
var HTTPConnectionHandler=Class("HTTPConnectionHandler",BaseConnectionHandler,function(publ,supr){var urllib;publ.request_id=1;publ.init=function(url,localService){urllib=importModule("urllib");this.url=url;supr(this).init(localService);}
publ.handleData=function(data){try{var obj=JSON.parse(data);}catch(e){;throw" Not well formed\n\n"+e+"\n\nResponse from server:\n\n "+data;}
if(obj.id!=null){return obj;}else{throw"Unknown Data (No id property found)";}}
publ.sendRequest=function(name,args,callback){var sync=false;if(typeof callback!="function"){args.push(callback);sync=true;}
var data=new Request(this.request_id++,name,args);data=JSON.stringify(data);if(sync){var rsp=urllib.postURL(this.url,data,[["Content-Type","text/plain"]]);rsp=this.handleData(rsp.responseText);if(rsp.error){throw rsp.error;}else{return rsp.result;}}else{var self=this;var request_id=this.request_id;urllib.postURL(this.url,data,[["Content-Type","text/plain"]],function(rsp){try{rsp=self.handleData(rsp.responseText);}catch(e){callback(request_id,null,e);return;}
callback(request_id,rsp.result,rsp.error);});}}
publ.sendNotify=function(name,args){var data=new Notification(name,args);data=this.jsonParser.objToJson(data);urllib.postURL(this.url,data,[["Content-Type","text/plain"]],function(rsp){});}})
var PeerObject=Class("PeerObject",function(publ,supr){publ.init=function(name,conn){var fn=function(){var args=[];for(var i=0;i<arguments.length;i++){args[i]=arguments[i];}
var cb=args.pop();return conn.sendRequest(name,args,cb);}
return fn;}})
var PeerNotifyObject=Class("PeerNotifyObject",function(publ,supr){publ.init=function(name,conn){var fn=function(){var args=[];for(var i=0;i<arguments.length;i++){args[i]=arguments[i];}
conn.sendNotify(name,args);}
return fn;}})
var BasePeer=Class("BasePeer",function(publ,supr){publ.init=function(conn,methodNames){this._conn=conn;this.notify=new PeerObject("notify",conn);this._add(methodNames);}
var setupPeerMethod=function(root,methodName,conn,MethClass){var names=methodName.split(".");var obj=root;for(var n=0;n<names.length-1;n++){var name=names[n];if(obj[name]){obj=obj[name];}else{obj[name]=new Object();obj=obj[name];}}
var name=names[names.length-1];if(obj[name]){}else{var mth=new MethClass(methodName,conn);obj[name]=mth;}}
publ._add=function(methodNames){for(var i=0;i<methodNames.length;i++){setupPeerMethod(this,methodNames[i],this._conn,PeerObject);setupPeerMethod(this.notify,methodNames[i],this._conn,PeerNotifyObject);}}})
mod.ServiceProxy=Class("ServiceProxy",BasePeer,function(publ,supr){publ.init=function(url,methodNames,localService){var n=url.match(/^jsonrpc:\/\/(.*:\d*)$/);if(n!=null){var hostaddr=n[1];try{var socket=createConnection();}catch(e){throw"Can't create a socket connection."}
socket.connect(hostaddr);supr(this).init(new SocketConnectionHandler(socket,localService),methodNames);}else{this.httpConn=new HTTPConnectionHandler(url,localService);supr(this).init(this.httpConn,methodNames);}}})})

View File

@@ -0,0 +1,72 @@
/*
Copyright (c) 2004 Jan-Klaas Kollhof
This file is part of the JavaScript o lait library(jsolait).
jsolait is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
Module("lang","0.3.7",function(mod){var ISODate=function(d){if(/^(\d{4})(\d{2})(\d{2})T(\d{2}):(\d{2}):(\d{2})/.test(d)){return new Date(Date.UTC(RegExp.$1,RegExp.$2-1,RegExp.$3,RegExp.$4,RegExp.$5,RegExp.$6));}else{throw new mod.Exception("Not an ISO date: "+d);}}
mod.JSONParser=Class("JSONParser",function(publ,supr){publ.init=function(){this.libs={};var sys={"ISODate":ISODate};this.addLib(sys,"sys",["ISODate"]);}
publ.addLib=function(obj,name,exports){if(exports==null){this.libs[name]=obj;}else{for(var i=0;i<exports.length;i++){this.libs[name+"."+exports[i]]=obj[exports[i]];}}}
var EmptyValue={};var SeqSep={};var parseValue=function(tkns,libs){var tkn=tkns.nextNonWS();switch(tkn.type){case mod.tokens.STR:case mod.tokens.NUM:return eval(tkn.value);case mod.tokens.NAME:return parseName(tkn.value);case mod.tokens.OP:switch(tkn.value){case"[":return parseArray(tkns,libs);break;case"{":return parseObj(tkns,libs);break;case"}":case"]":return EmptyValue;case",":return SeqSep;default:throw new mod.Exception("expected '[' or '{' but found: '"+tkn.value+"'");}}
return EmptyValue;}
var parseArray=function(tkns,libs){var a=[];while(!tkns.finished()){var v=parseValue(tkns,libs);if(v==EmptyValue){return a;}else{a.push(v);v=parseValue(tkns,libs);if(v==EmptyValue){return a;}else if(v!=SeqSep){throw new mod.Exception("',' expected but found: '"+v+"'");}}}
throw new mod.Exception("']' expected");}
var parseObj=function(tkns,libs){var obj={};var nme=""
while(!tkns.finished()){var tkn=tkns.nextNonWS();if(tkn.type==mod.tokens.STR){var nme=eval(tkn.value);tkn=tkns.nextNonWS();if(tkn.value==":"){var v=parseValue(tkns,libs);if(v==SeqSep||v==EmptyValue){throw new mod.Exception("value expected");}else{obj[nme]=v;v=parseValue(tkns,libs);if(v==EmptyValue){return transformObj(obj,libs);}else if(v!=SeqSep){throw new mod.Exception("',' expected");}}}else{throw new mod.Exception("':' expected but found: '"+tkn.value+"'");}}else if(tkn.value=="}"){return transformObj(obj,libs);}else{throw new mod.Exception("String expected");}}
throw new mod.Exception("'}' expected.")}
var transformObj=function(obj,libs){var o2;if(obj.jsonclass!=null){var clsName=obj.jsonclass[0];var params=obj.jsonclass[1]
if(libs[clsName]){o2=libs[clsName].apply(this,params);for(var nme in obj){if(nme!="jsonclass"){if(typeof obj[nme]!="function"){o2[nme]=obj[nme];}}}}else{throw new mod.Exception("jsonclass not found: "+clsName);}}else{o2=obj;}
return o2;}
var parseName=function(name){switch(name){case"null":return null;case"true":return true;case"false":return false;default:throw new mod.Exception("'null', 'true', 'false' expected but found: '"+name+"'");}}
publ.jsonToObj=function(data){var t=new mod.Tokenizer(data);return parseValue(t,this.libs);}
publ.objToJson=function(obj){if(obj==null){return"null";}else{return obj.toJSON();}}})
mod.parser=new mod.JSONParser();mod.jsonToObj=function(src){return mod.parser.jsonToObj(src);}
mod.objToJson=function(obj){return mod.parser.objToJson(obj);}
mod.tokens={};mod.tokens.WSP=0;mod.tokens.OP=1;mod.tokens.STR=2;mod.tokens.NAME=3;mod.tokens.NUM=4;mod.tokens.ERR=5;mod.tokens.NL=6;mod.tokens.COMMENT=7;mod.tokens.DOCCOMMENT=8;mod.tokens.REGEXP=9;mod.Token=Class(function(publ,supr){publ.init=function(type,value,pos,err){this.type=type;this.value=value;this.pos=pos;this.err=err;}})
mod.Tokenizer=Class("Tokenizer",function(publ,supr){publ.init=function(s){this._working=s;this._pos=0;}
publ.finished=function(){return this._working.length==0;}
publ.nextNonWS=function(nlIsWS){var tkn=this.next();while((tkn.type==mod.tokens.WSP)||(nlIsWS&&(tkn.type==mod.tokens.NL))){tkn=this.next();}
return tkn;}
publ.next=function(){if(this._working==""){throw new mod.Exception("Empty");}
var s1=this._working.charAt(0);var s2=s1+this._working.charAt(1);var s3=s2+this._working.charAt(2);var rslt=[];switch(s1){case'"':case"'":try{s1=extractQString(this._working);rslt=new mod.Token(mod.tokens.STR,s1,this._pos);}catch(e){rslt=new mod.Token(mod.tokens.ERR,s1,this._pos,e);}
break;case"\n":case"\r":rslt=new mod.Token(mod.tokens.NL,s1,this._pos);break;case"-":s1=this._working.match(/-\d+\.\d+|-\d+/)[0];if(/^-\d|-\d\.\d/.test(s1)){rslt=new mod.Token(mod.tokens.NUM,s1,this._pos);break;}
case"{":case"}":case"[":case"]":case"(":case")":case":":case",":case".":case";":case"*":case"-":case"+":case"=":case"<":case">":case"!":case"|":case"&":switch(s2){case"==":case"!=":case"<>":case"<=":case">=":case"||":case"&&":rslt=new mod.Token(mod.tokens.OP,s2,this._pos);break;default:rslt=new mod.Token(mod.tokens.OP,s1,this._pos);}
break;case"/":if(s2=="//"||s3=="///"){s1=extractSLComment(this._working);rslt=new mod.Token(s1.charAt(2)!="/"?mod.tokens.COMMENT:mod.tokens.DOCCOMMENT,s1,this._pos);}else if(s2=="/*"||s3=="/**"){try{s1=extractMLComment(this._working);rslt=new mod.Token(s3!="/**"?mod.tokens.COMMENT:mod.tokens.DOCCOMMENT,s1,this._pos);}catch(e){rslt=new mod.Token(mod.tokens.ERR,s3!="/**"?s2:s3,this._pos,e);}}else{try{s1=extractRegExp(this._working);rslt=new mod.Token(mod.tokens.REGEXP,s1,this._pos);}catch(e){rslt=new mod.Token(mod.tokens.OP,s1,this._pos,e);}}
break;case" ":var i=0;var s="";while(this._working.charAt(i)==" "){s+=" ";i++;}
rslt=new mod.Token(mod.tokens.WSP,s,this._pos);break;default:s1=this._working.match(/\d+\.\d+|\d+|\w+/)[0];if(/^\d|\d\.\d/.test(s1)){rslt=new mod.Token(mod.tokens.NUM,s1,this._pos);}else{rslt=new mod.Token(mod.tokens.NAME,s1,this._pos);}}
this._working=this._working.slice(rslt.value.length);this._pos+=rslt.value.length;return rslt;}
var searchQoute=function(s,q){if(q=="'"){return s.search(/[\\']/);}else{return s.search(/[\\"]/);}}
var extractQString=function(s){if(s.charAt(0)=="'"){var q="'";}else{var q='"';}
s=s.slice(1);var rs="";var p=searchQoute(s,q);while(p>=0){if(p>=0){if(s.charAt(p)==q){rs+=s.slice(0,p+1);s=s.slice(p+1);return q+rs;}else{rs+=s.slice(0,p+2);s=s.slice(p+2);}}
p=searchQoute(s,q);}
throw new mod.Exception("End of String expected.");}
var extractSLComment=function(s){var p=s.search(/\n/);if(p>=0){return s.slice(0,p+1);}else{return s;}}
var extractMLComment=function(s){var p=s.search(/\*\//);if(p>=0){return s.slice(0,p+2);}else{throw new mod.Exception("End of comment expected.");}}
var extractRegExp=function(s){var p=0;for(var i=0;i<s.length;i++){if(s.charAt(i)=="/"){p=i;}
if(s.charAt(i)=="\n"){i=s.length;}}
return s.slice(0,p+1);}})
Object.prototype.toJSON=function(){var v=[];for(attr in this){if(typeof this[attr]!="function"){v.push('"'+attr+'": '+mod.objToJson(this[attr]));}}
return"{"+v.join(", ")+"}";}
String.prototype.toJSON=function(){var s='"'+this.replace(/(["\\])/g,'\\$1')+'"';s=s.replace(/(\n)/g,"\\n");return s;}
Number.prototype.toJSON=function(){return this.toString();}
Boolean.prototype.toJSON=function(){return this.toString();}
Date.prototype.toJSON=function(){var padd=function(s,p){s=p+s
return s.substring(s.length-p.length)}
var y=padd(this.getUTCFullYear(),"0000");var m=padd(this.getUTCMonth()+1,"00");var d=padd(this.getUTCDate(),"00");var h=padd(this.getUTCHours(),"00");var min=padd(this.getUTCMinutes(),"00");var s=padd(this.getUTCSeconds(),"00");var isodate=y+m+d+"T"+h+":"+min+":"+s
return'{"jsonclass":["sys.ISODate", ["'+isodate+'"]]}';}
Array.prototype.toJSON=function(){var v=[];for(var i=0;i<this.length;i++){v.push(mod.objToJson(this[i]));}
return"["+v.join(", ")+"]";}
mod.test=function(){try{print(mod.jsonToObj("['sds', -12377,-1212.1212, 12, '-2312']").toJSON());}catch(e){print(e.toTraceString());}}})

View File

@@ -0,0 +1,40 @@
/*
Copyright (c) 2004 Jan-Klaas Kollhof
This file is part of the JavaScript o lait library(jsolait).
jsolait is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
Module("langlite","0.3.7",function(mod){mod.JSONParser=Class("JSONParser",function(publ,supr){publ.init=function(){this.libs={};}
publ.addLib=function(obj,name,exports){if(exports==null){this.libs[name]=obj;}else{for(var i=0;i<exports.length;i++){this.libs[name+"."+exports[i]]=obj[exports[i]];}}}
publ.objToJson=function(obj){if(obj==null){return"null";}else{return mod.objToJson(obj);}}})
mod.parser=new mod.JSONParser();mod.jsonToObj=function(src){return mod.parser.jsonToObj(src);}
var json_types=new Object();json_types['object']=function(obj){var v=[];for(attr in obj){if(typeof obj[attr]!="function"){v.push('"'+attr+'": '+mod.objToJson(obj[attr]));}}
return"{"+v.join(", ")+"}";}
json_types['string']=function(obj){var s='"'+obj.replace(/(["\\])/g,'\\$1')+'"';s=s.replace(/(\n)/g,"\\n");return s;}
json_types['number']=function(obj){return obj.toString();}
json_types['boolean']=function(obj){return obj.toString();}
json_types['date']=function(obj){var padd=function(s,p){s=p+s
return s.substring(s.length-p.length)}
var y=padd(obj.getUTCFullYear(),"0000");var m=padd(obj.getUTCMonth()+1,"00");var d=padd(obj.getUTCDate(),"00");var h=padd(obj.getUTCHours(),"00");var min=padd(obj.getUTCMinutes(),"00");var s=padd(obj.getUTCSeconds(),"00");var isodate=y+m+d+"T"+h+":"+min+":"+s
return'{"jsonclass":["sys.ISODate", ["'+isodate+'"]]}';}
json_types['array']=function(obj){var v=[];for(var i=0;i<obj.length;i++){v.push(mod.objToJson(obj[i]));}
return"["+v.join(", ")+"]";}
mod.objToJson=function(obj){if(typeof(obj)=='undefined')
{return'';}
if(typeof(json_types[typeof(obj)])=='undefined')
{alert('class not defined for toJSON():'+typeof(obj));}
return json_types[typeof(obj)](obj);}
mod.test=function(){try{print(mod.objToJson(['sds',-12377,-1212.1212,12,'-2312']));}catch(e){print(e.toTraceString());}}})

View File

@@ -0,0 +1,42 @@
/*
Copyright (c) 2003 Jan-Klaas Kollhof
This file is part of the JavaScript o lait library(jsolait).
jsolait is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
Module("urllib","1.1.3",function(mod){mod.NoHTTPRequestObject=Class("NoHTTPRequestObject",mod.Exception,function(publ,supr){publ.init=function(trace){supr(this).init("Could not create an HTTP request object",trace);}})
mod.RequestOpenFailed=Class("RequestOpenFailed",mod.Exception,function(publ,supr){publ.init=function(trace){supr(this).init("Opening of HTTP request failed.",trace);}})
mod.SendFailed=Class("SendFailed",mod.Exception,function(publ,supr){publ.init=function(trace){supr(this).init("Sending of HTTP request failed.",trace);}})
var ASVRequest=Class("ASVRequest",function(publ){publ.init=function(){if((getURL==null)||(postURL==null)){throw"getURL and postURL are not available!";}else{this.readyState=0;this.responseText="";this.__contType="";this.status=200;}}
publ.open=function(type,url,async){if(async==false){throw"Can only open asynchronous connections!";}
this.__type=type;this.__url=url;this.readyState=0;}
publ.setRequestHeader=function(name,value){if(name=="Content-Type"){this.__contType=value;}}
publ.send=function(data){var self=this;var cbh=new Object();cbh.operationComplete=function(rsp){self.readyState=4;self.responseText=rsp.content;if(this.ignoreComplete==false){if(self.onreadystatechange){self.onreadystatechange();}}}
cbh.ignoreComplete=false;try{if(this.__type=="GET"){getURL(this.__url,cbh);}else if(this.__type=="POST"){postURL(this.__url,data,cbh,this.__contType);}}catch(e){cbh.ignoreComplete=true;throw e;}}})
var getHTTP=function(){var obj;try{obj=new XMLHttpRequest();}catch(e){try{obj=new ActiveXObject("Msxml2.XMLHTTP.4.0");}catch(e){try{obj=new ActiveXObject("Msxml2.XMLHTTP")}catch(e){try{obj=new ActiveXObject("microsoft.XMLHTTP");}catch(e){try{obj=new ASVRequest();}catch(e){throw new mod.NoHTTPRequestObject("Neither Mozilla, IE nor ASV found. Can't do HTTP request without them.");}}}}}
return obj;}
mod.sendRequest=function(type,url,user,pass,data,headers,callback){var async=false;if(arguments[arguments.length-1]instanceof Function){var async=true;callback=arguments[arguments.length-1];}
var headindex=arguments.length-((async||arguments[arguments.length-1]==null)?2:1);if(arguments[headindex]instanceof Array){headers=arguments[headindex];}else{headers=[];}
if(typeof user=="string"&&typeof pass=="string"){if(typeof data!="string"){data="";}}else if(typeof user=="string"){data=user;user=null;pass=null;}else{user=null;pass=null;}
var xmlhttp=getHTTP();try{if(user!=null){xmlhttp.open(type,url,async,user,pass);}else{xmlhttp.open(type,url,async);}}catch(e){throw new mod.RequestOpenFailed(e);}
for(var i=0;i<headers.length;i++){xmlhttp.setRequestHeader(headers[i][0],headers[i][1]);}
if(async){xmlhttp.onreadystatechange=function(){if(xmlhttp.readyState==4){callback(xmlhttp);xmlhttp=null;}else if(xmlhttp.readyState==2){try{var isNetscape=netscape;try{var s=xmlhttp.status;}catch(e){callback(xmlhttp);xmlhttp=null;}}catch(e){}}}}
try{xmlhttp.send(data);}catch(e){if(async){callback(xmlhttp,e);xmlhttp=null;}else{throw new mod.SendFailed(e);}}
return xmlhttp;}
mod.getURL=function(url,user,pass,headers,callback){var a=new Array("GET");for(var i=0;i<arguments.length;i++){a.push(arguments[i]);}
return mod.sendRequest.apply(this,a)}
mod.postURL=function(url,user,pass,data,headers,callback){var a=new Array("POST");for(var i=0;i<arguments.length;i++){a.push(arguments[i]);}
return mod.sendRequest.apply(this,a)}})

View File

@@ -0,0 +1,34 @@
/*
Copyright (c) 2003 Jan-Klaas Kollhof
This file is part of the JavaScript o lait library(jsolait).
jsolait is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
Module("xml","1.1.2",function(mod){mod.NoXMLParser=Class("NoXMLParser",mod.Exception,function(publ,supr){publ.init=function(trace){supr(this).init("Could not create an XML parser.",trace);}})
mod.ParsingFailed=Class("ParsingFailed",mod.Exception,function(publ,supr){publ.init=function(xml,trace){supr(this).init("Failed parsing XML document.",trace);this.xml=xml;}
publ.xml;})
mod.parseXML=function(xml){var obj=null;var isMoz=false;var isIE=false;var isASV=false;try{var p=window.parseXML;if(p==null){throw"No ASV paseXML";}
isASV=true;}catch(e){try{obj=new DOMParser();isMoz=true;}catch(e){try{obj=new ActiveXObject("Msxml2.DomDocument.4.0");isIE=true;}catch(e){try{obj=new ActiveXObject("Msxml2.DomDocument");isIE=true;}catch(e){try{obj=new ActiveXObject("microsoft.XMLDOM");isIE=true;}catch(e){throw new mod.NoXMLParser(e);}}}}}
try{if(isMoz){obj=obj.parseFromString(xml,"text/xml");return obj;}else if(isIE){obj.loadXML(xml);return obj;}else if(isASV){return window.parseXML(xml,null);}}catch(e){throw new mod.ParsingFailed(xml,e);}}
mod.importNode=function(importedNode,deep){deep=(deep==null)?true:deep;var ELEMENT_NODE=1;var ATTRIBUTE_NODE=2;var TEXT_NODE=3;var CDATA_SECTION_NODE=4;var ENTITY_REFERENCE_NODE=5;var ENTITY_NODE=6;var PROCESSING_INSTRUCTION_NODE=7;var COMMENT_NODE=8;var DOCUMENT_NODE=9;var DOCUMENT_TYPE_NODE=10;var DOCUMENT_FRAGMENT_NODE=11;var NOTATION_NODE=12;var importChildren=function(srcNode,parent){if(deep){for(var i=0;i<srcNode.childNodes.length;i++){var n=mod.importNode(srcNode.childNodes.item(i),true);parent.appendChild(n);}}}
var node=null;switch(importedNode.nodeType){case ATTRIBUTE_NODE:node=document.createAttributeNS(importedNode.namespaceURI,importedNode.nodeName);node.value=importedNode.value;break;case DOCUMENT_FRAGMENT_NODE:node=document.createDocumentFragment();importChildren(importedNode,node);break;case ELEMENT_NODE:node=document.createElementNS(importedNode.namespaceURI,importedNode.tagName);for(var i=0;i<importedNode.attributes.length;i++){var attr=this.importNode(importedNode.attributes.item(i),deep);node.setAttributeNodeNS(attr);}
importChildren(importedNode,node);break;case ENTITY_REFERENCE_NODE:node=importedNode;break;case PROCESSING_INSTRUCTION_NODE:node=document.createProcessingInstruction(importedNode.target,importedNode.data);break;case TEXT_NODE:case CDATA_SECTION_NODE:case COMMENT_NODE:node=document.createTextNode(importedNode.nodeValue);break;case DOCUMENT_NODE:case DOCUMENT_TYPE_NODE:case NOTATION_NODE:case ENTITY_NODE:throw"not supported in DOM2";break;}
return node;}
mod.node2XML=function(node){var ELEMENT_NODE=1;var ATTRIBUTE_NODE=2;var TEXT_NODE=3;var CDATA_SECTION_NODE=4;var ENTITY_REFERENCE_NODE=5;var ENTITY_NODE=6;var PROCESSING_INSTRUCTION_NODE=7;var COMMENT_NODE=8;var DOCUMENT_NODE=9;var DOCUMENT_TYPE_NODE=10;var DOCUMENT_FRAGMENT_NODE=11;var NOTATION_NODE=12;var s="";switch(node.nodeType){case ATTRIBUTE_NODE:s+=node.nodeName+'="'+node.value+'"';break;case DOCUMENT_NODE:s+=this.node2XML(node.documentElement);break;case ELEMENT_NODE:s+="<"+node.tagName;for(var i=0;i<node.attributes.length;i++){s+=" "+this.node2XML(node.attributes.item(i));}
if(node.childNodes.length==0){s+="/>\n";}else{s+=">";for(var i=0;i<node.childNodes.length;i++){s+=this.node2XML(node.childNodes.item(i));}
s+="</"+node.tagName+">\n";}
break;case PROCESSING_INSTRUCTION_NODE:s+="<?"+node.target+" "+node.data+" ?>";break;case TEXT_NODE:s+=node.nodeValue;break;case CDATA_SECTION_NODE:s+="<"+"![CDATA["+node.nodeValue+"]"+"]>";break;case COMMENT_NODE:s+="<!--"+node.nodeValue+"-->";break;case ENTITY_REFERENCE_NODE:case DOCUMENT_FRAGMENT_NODE:case DOCUMENT_TYPE_NODE:case NOTATION_NODE:case ENTITY_NODE:throw new mod.Exception("Nodetype(%s) not supported.".format(node.nodeType));break;}
return s;}})

View File

@@ -0,0 +1,95 @@
/*
Copyright (c) 2003-2004 Jan-Klaas Kollhof
This file is part of the JavaScript o lait library(jsolait).
jsolait is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
This software is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this software; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
Module("xmlrpc","1.3.3",function(mod){var xmlext=importModule("xml");var urllib=importModule("urllib");mod.InvalidServerResponse=Class("InvalidServerResponse",mod.Exception,function(publ,supr){publ.init=function(status){supr(this).init("The server did not respond with a status 200 (OK) but with: "+status);this.status=status;}
publ.status;})
mod.MalformedXmlRpc=Class("MalformedXmlRpc",mod.Exception,function(publ,supr){publ.init=function(msg,xml,trace){supr(this).init(msg,trace);this.xml=xml;}
publ.xml;})
mod.Fault=Class("Fault",mod.Exception,function(publ,supr){publ.init=function(faultCode,faultString){supr(this).init("XML-RPC Fault: "+faultCode+"\n\n"+faultString);this.faultCode=faultCode;this.faultString=faultString;}
publ.faultCode;publ.faultString;})
mod.marshall=function(obj){if(obj.toXmlRpc){return obj.toXmlRpc();}else{var s="<struct>";for(var attr in obj){if(typeof obj[attr]!="function"){s+="<member><name>"+attr+"</name><value>"+mod.marshall(obj[attr])+"</value></member>";}}
s+="</struct>";return s;}}
mod.unmarshall=function(xml){try{var doc=xmlext.parseXML(xml);}catch(e){throw new mod.MalformedXmlRpc("The server's response could not be parsed.",xml,e);}
var rslt=mod.unmarshallDoc(doc,xml);doc=null;return rslt;}
mod.unmarshallDoc=function(doc,xml){try{var node=doc.documentElement;if(node==null){throw new mod.MalformedXmlRpc("No documentElement found.",xml);}
switch(node.tagName){case"methodResponse":return parseMethodResponse(node);case"methodCall":return parseMethodCall(node);default:throw new mod.MalformedXmlRpc("'methodCall' or 'methodResponse' element expected.\nFound: '"+node.tagName+"'",xml);}}catch(e){if(e instanceof mod.Fault){throw e;}else{throw new mod.MalformedXmlRpc("Unmarshalling of XML failed.",xml,e);}}}
var parseMethodResponse=function(node){try{for(var i=0;i<node.childNodes.length;i++){var child=node.childNodes.item(i);if(child.nodeType==1){switch(child.tagName){case"fault":throw parseFault(child);case"params":var params=parseParams(child);if(params.length==1){return params[0];}else{throw new mod.MalformedXmlRpc("'params' element inside 'methodResponse' must have exactly ONE 'param' child element.\nFound: "+params.length);}
default:throw new mod.MalformedXmlRpc("'fault' or 'params' element expected.\nFound: '"+child.tagName+"'");}}}
throw new mod.MalformedXmlRpc("No child elements found.");}catch(e){if(e instanceof mod.Fault){throw e;}else{throw new mod.MalformedXmlRpc("'methodResponse' element could not be parsed.",null,e);}}}
var parseMethodCall=function(node){try{var methodName=null;var params=new Array();for(var i=0;i<node.childNodes.length;i++){var child=node.childNodes.item(i);if(child.nodeType==1){switch(child.tagName){case"methodName":methodName=new String(child.firstChild.nodeValue);break;case"params":params=parseParams(child);break;default:throw new mod.MalformedXmlRpc("'methodName' or 'params' element expected.\nFound: '"+child.tagName+"'");}}}
if(methodName==null){throw new mod.MalformedXmlRpc("'methodName' element expected.");}else{return new Array(methodName,params);}}catch(e){throw new mod.MalformedXmlRpc("'methodCall' element could not be parsed.",null,e);}}
var parseParams=function(node){try{var params=new Array();for(var i=0;i<node.childNodes.length;i++){var child=node.childNodes.item(i);if(child.nodeType==1){switch(child.tagName){case"param":params.push(parseParam(child));break;default:throw new mod.MalformedXmlRpc("'param' element expected.\nFound: '"+child.tagName+"'");}}}
return params;}catch(e){throw new mod.MalformedXmlRpc("'params' element could not be parsed.",null,e);}}
var parseParam=function(node){try{for(var i=0;i<node.childNodes.length;i++){var child=node.childNodes.item(i);if(child.nodeType==1){switch(child.tagName){case"value":return parseValue(child);default:throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '"+child.tagName+"'");}}}
throw new mod.MalformedXmlRpc("'value' element expected.But none found.");}catch(e){throw new mod.MalformedXmlRpc("'param' element could not be parsed.",null,e);}}
var parseValue=function(node){try{for(var i=0;i<node.childNodes.length;i++){var child=node.childNodes.item(i);if(child.nodeType==1){switch(child.tagName){case"string":var s=""
for(var j=0;j<child.childNodes.length;j++){s+=new String(child.childNodes.item(j).nodeValue);}
return s;case"int":case"i4":case"double":return(child.firstChild)?new Number(child.firstChild.nodeValue):0;case"boolean":return Boolean(isNaN(parseInt(child.firstChild.nodeValue))?(child.firstChild.nodeValue=="true"):parseInt(child.firstChild.nodeValue));case"base64":return parseBase64(child);case"dateTime.iso8601":return parseDateTime(child);case"array":return parseArray(child);case"struct":return parseStruct(child);case"nil":return null;default:throw new mod.MalformedXmlRpc("'string','int','i4','double','boolean','base64','dateTime.iso8601','array' or 'struct' element expected.\nFound: '"+child.tagName+"'");}}}
if(node.firstChild){var s=""
for(var j=0;j<node.childNodes.length;j++){s+=new String(node.childNodes.item(j).nodeValue);}
return s;}else{return"";}}catch(e){throw new mod.MalformedXmlRpc("'value' element could not be parsed.",null,e);}}
var parseBase64=function(node){try{var s=node.firstChild.nodeValue;return s.decode("base64");}catch(e){throw new mod.MalformedXmlRpc("'base64' element could not be parsed.",null,e);}}
var parseDateTime=function(node){try{if(/^(\d{4})-?(\d{2})-?(\d{2})T(\d{2}):?(\d{2}):?(\d{2})/.test(node.firstChild.nodeValue)){return new Date(Date.UTC(RegExp.$1,RegExp.$2-1,RegExp.$3,RegExp.$4,RegExp.$5,RegExp.$6));}else{throw new mod.MalformedXmlRpc("Could not convert the given date.");}}catch(e){throw new mod.MalformedXmlRpc("'dateTime.iso8601' element could not be parsed.",null,e);}}
var parseArray=function(node){try{for(var i=0;i<node.childNodes.length;i++){var child=node.childNodes.item(i);if(child.nodeType==1){switch(child.tagName){case"data":return parseData(child);default:throw new mod.MalformedXmlRpc("'data' element expected.\nFound: '"+child.tagName+"'");}}}
throw new mod.MalformedXmlRpc("'data' element expected. But not found.");}catch(e){throw new mod.MalformedXmlRpc("'array' element could not be parsed.",null,e);}}
var parseData=function(node){try{var rslt=new Array();for(var i=0;i<node.childNodes.length;i++){var child=node.childNodes.item(i);if(child.nodeType==1){switch(child.tagName){case"value":rslt.push(parseValue(child));break;default:throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '"+child.tagName+"'");}}}
return rslt;}catch(e){throw new mod.MalformedXmlRpc("'data' element could not be parsed.",null,e);}}
var parseStruct=function(node){try{var struct=new Object();for(var i=0;i<node.childNodes.length;i++){var child=node.childNodes.item(i);if(child.nodeType==1){switch(child.tagName){case"member":var member=parseMember(child);if(member[0]!=""){struct[member[0]]=member[1];}
break;default:throw new mod.MalformedXmlRpc("'data' element expected.\nFound: '"+child.tagName+"'");}}}
return struct;}catch(e){throw new mod.MalformedXmlRpc("'struct' element could not be parsed.",null,e);}}
var parseMember=function(node){try{var name="";var value=null;for(var i=0;i<node.childNodes.length;i++){var child=node.childNodes.item(i);if(child.nodeType==1){switch(child.tagName){case"value":value=parseValue(child);break;case"name":if(child.hasChildNodes()){name=new String(child.firstChild.nodeValue);}
break;default:throw new mod.MalformedXmlRpc("'value' or 'name' element expected.\nFound: '"+child.tagName+"'");}}}
return[name,value];}catch(e){throw new mod.MalformedXmlRpc("'member' element could not be parsed.",null,e);}}
var parseFault=function(node){try{for(var i=0;i<node.childNodes.length;i++){var child=node.childNodes.item(i);if(child.nodeType==1){switch(child.tagName){case"value":var flt=parseValue(child);return new mod.Fault(flt.faultCode,flt.faultString);default:throw new mod.MalformedXmlRpc("'value' element expected.\nFound: '"+child.tagName+"'");}}}
throw new mod.MalformedXmlRpc("'value' element expected. But not found.");}catch(e){throw new mod.MalformedXmlRpc("'fault' element could not be parsed.",null,e);}}
mod.XMLRPCMethod=Class("XMLRPCMethod",function(publ){var postData=function(url,user,pass,data,callback){if(callback==null){var rslt=urllib.postURL(url,user,pass,data,[["Content-Type","text/xml"]]);return rslt;}else{urllib.postURL(url,user,pass,data,[["Content-Type","text/xml"]],callback);}}
var handleResponse=function(resp){var status=null;try{status=resp.status;}catch(e){}
if(status==200){var respDoc=null;try{respDoc=resp.responseXML;}catch(e){}
var respTxt="";try{respTxt=resp.responseText;}catch(e){}
if(respDoc==null){if(respTxt==null||respTxt==""){throw new mod.MalformedXmlRpc("The server responded with an empty document.","");}else{return mod.unmarshall(respTxt);}}else{return mod.unmarshallDoc(respDoc,respTxt);}}else{throw new mod.InvalidServerResponse(status);}}
var getXML=function(methodName,args){var data='<?xml version="1.0"?><methodCall><methodName>'+methodName+'</methodName>';if(args.length>0){data+="<params>";for(var i=0;i<args.length;i++){data+='<param><value>'+mod.marshall(args[i])+'</value></param>';}
data+='</params>';}
data+='</methodCall>';return data;}
publ.init=function(url,methodName,user,pass){var fn=function(){if(typeof arguments[arguments.length-1]!="function"){var data=getXML(fn.methodName,arguments);var resp=postData(fn.url,fn.user,fn.password,data);return handleResponse(resp);}else{var args=new Array();for(var i=0;i<arguments.length;i++){args.push(arguments[i]);}
var cb=args.pop();var data=getXML(fn.methodName,args);postData(fn.url,fn.user,fn.password,data,function(resp){var rslt=null;var exc=null;try{rslt=handleResponse(resp);}catch(e){exc=e;}
try{cb(rslt,exc);}catch(e){}
args=null;resp=null;});}}
fn.methodName=methodName;fn.url=url;fn.user=user;fn.password=pass;fn.toMulticall=this.toMulticall;fn.toString=this.toString;fn.setAuthentication=this.setAuthentication;fn.constructor=this.constructor;return fn;}
publ.toMulticall=function(){var multiCallable=new Object();multiCallable.methodName=this.methodName;var params=[];for(var i=0;i<arguments.length;i++){params[i]=arguments[i];}
multiCallable.params=params;return multiCallable;}
publ.setAuthentication=function(user,pass){this.user=user;this.password=pass;}
publ.methodName;publ.url;publ.user;publ.password;})
mod.ServiceProxy=Class("ServiceProxy",function(publ){publ.init=function(url,methodNames,user,pass){if(methodNames instanceof Array){if(methodNames.length>0){var tryIntrospection=false;}else{var tryIntrospection=true;}}else{pass=user;user=methodNames;methodNames=[];var tryIntrospection=true;}
this._url=url;this._user=user;this._password=pass;this._addMethodNames(methodNames);if(tryIntrospection){try{this._introspect();}catch(e){}}}
publ._addMethodNames=function(methodNames){for(var i=0;i<methodNames.length;i++){var obj=this;var names=methodNames[i].split(".");for(var n=0;n<names.length-1;n++){var name=names[n];if(obj[name]){obj=obj[name];}else{obj[name]=new Object();obj=obj[name];}}
var name=names[names.length-1];if(obj[name]){}else{var mth=new mod.XMLRPCMethod(this._url,methodNames[i],this._user,this._password);obj[name]=mth;this._methods.push(mth);}}}
publ._setAuthentication=function(user,pass){this._user=user;this._password=pass;for(var i=0;i<this._methods.length;i++){this._methods[i].setAuthentication(user,pass);}}
publ._introspect=function(){this._addMethodNames(["system.listMethods","system.methodHelp","system.methodSignature"]);var m=this.system.listMethods();this._addMethodNames(m);}
publ._url;publ._user;publ._password;publ._methods=new Array();})
mod.ServerProxy=mod.ServiceProxy;String.prototype.toXmlRpc=function(){return"<string>"+this.replace(/&/g,"&amp;").replace(/</g,"&lt;")+"</string>";}
Number.prototype.toXmlRpc=function(){if(this==parseInt(this)){return"<int>"+this+"</int>";}else if(this==parseFloat(this)){return"<double>"+this+"</double>";}else{return false.toXmlRpc();}}
Boolean.prototype.toXmlRpc=function(){if(this==true){return"<boolean>1</boolean>";}else{return"<boolean>0</boolean>";}}
Date.prototype.toXmlRpc=function(){var padd=function(s,p){s=p+s
return s.substring(s.length-p.length)}
var y=padd(this.getUTCFullYear(),"0000");var m=padd(this.getUTCMonth()+1,"00");var d=padd(this.getUTCDate(),"00");var h=padd(this.getUTCHours(),"00");var min=padd(this.getUTCMinutes(),"00");var s=padd(this.getUTCSeconds(),"00");var isodate=y+m+d+"T"+h+":"+min+":"+s
return"<dateTime.iso8601>"+isodate+"</dateTime.iso8601>";}
Array.prototype.toXmlRpc=function(){var retstr="<array><data>";for(var i=0;i<this.length;i++){retstr+="<value>"+mod.marshall(this[i])+"</value>";}
return retstr+"</data></array>";}
mod.test=function(){print("creating ServiceProxy object using introspection for method construction...\n");var s=new mod.ServiceProxy("http://localhost/testx.py");print("%s created\n".format(s));print("creating and marshalling test data:\n");var o=[1.234,5,{a:"Hello & < ",b:new Date()}];print(mod.marshall(o));print("\ncalling echo() on remote service...\n");var r=s.echo(o);print("service returned data(marshalled again):\n")
print(mod.marshall(r));}})