/*
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="";for(var attr in obj){if(typeof obj[attr]!="function"){s+=""+attr+""+mod.marshall(obj[attr])+"";}}
s+="";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';if(args.length>0){data+="";for(var i=0;i'+mod.marshall(args[i])+'';}
data+='';}
data+='';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;i0){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"+this.replace(/&/g,"&").replace(/";}
Number.prototype.toXmlRpc=function(){if(this==parseInt(this)){return""+this+"";}else if(this==parseFloat(this)){return""+this+"";}else{return false.toXmlRpc();}}
Boolean.prototype.toXmlRpc=function(){if(this==true){return"1";}else{return"0";}}
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""+isodate+"";}
Array.prototype.toXmlRpc=function(){var retstr="";for(var i=0;i"+mod.marshall(this[i])+"";}
return retstr+"";}
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));}})