xmlrpc.js
Summary
No overview generated for 'xmlrpc.js'
"use strict";
var XMLRPCMessage = exports.XMLRPCMessage = function XMLRPCMessage(methodname) {
this.method = methodname || "system.listMethods";
this.params = [];
return this;
};
XMLRPCMessage.prototype.setMethod = function(methodName) {
if (!methodName) return;
this.method = methodName;
};
XMLRPCMessage.prototype.addParameter = function(data) {
if (arguments.length == 0) return;
this.params[this.params.length] = data;
};
XMLRPCMessage.prototype.xml = function() {
var method = this.method;
var xml = "";
xml += "<?xml version=\"1.0\"?>\n";
xml += "<methodCall>\n";
xml += "<methodName>" + method + "</methodName>\n";
xml += "<params>\n";
for ( var i = 0; i < this.params.length; i++) {
var data = this.params[i];
xml += "<param>\n";
xml += "<value>"
+ this.getParamXML(this.dataTypeOf(data),
data) + "</value>\n";
xml += "</param>\n";
}
xml += "</params>\n";
xml += "</methodCall>";
return xml;
};
XMLRPCMessage.prototype.dataTypeOf = function(o) {
var type = typeof (o);
type = type.toLowerCase();
switch (type) {
case "number":
if (Math.round(o) == o)
type = "i4";
else
type = "double";
break;
case "object":
var con = o.constructor;
if (con == Date)
type = "date";
else if (con == Array)
type = "array";
else
type = "struct";
break;
}
return type;
};
XMLRPCMessage.prototype.doValueXML = function(type, data) {
var xml = "<" + type + ">" + data + "</" + type + ">";
return xml;
};
XMLRPCMessage.prototype.doBooleanXML = function(data) {
var value = (data == true) ? 1 : 0;
var xml = "<boolean>" + value + "</boolean>";
return xml;
};
XMLRPCMessage.prototype.doDateXML = function(data) {
var leadingZero = function (n) {
if (n.length == 1)
n = "0" + n;
return n;
};
var dateToISO8601 = function(date) {
var year = new String(date.getYear());
var month = this.leadingZero(new String(date.getMonth()));
var day = this.leadingZero(new String(date.getDate()));
var time = this.leadingZero(new String(date.getHours())) + ":"
+ this.leadingZero(new String(date.getMinutes())) + ":"
+ this.leadingZero(new String(date.getSeconds()));
var converted = year + month + day + "T" + time;
return converted;
};
var xml = "<dateTime.iso8601>";
xml += dateToISO8601(data);
xml += "</dateTime.iso8601>";
return xml;
};
XMLRPCMessage.prototype.doArrayXML = function(data) {
var xml = "<array><data>\n";
for ( var i = 0; i < data.length; i++) {
xml += "<value>"
+ this.getParamXML(this.dataTypeOf(data[i]),
data[i]) + "</value>\n";
}
xml += "</data></array>\n";
return xml;
};
XMLRPCMessage.prototype.doStructXML = function(data) {
var xml = "<struct>\n";
for ( var i in data) {
xml += "<member>\n";
xml += "<name>" + i + "</name>\n";
xml += "<value>"
+ this.getParamXML(this.dataTypeOf(data[i]),
data[i]) + "</value>\n";
xml += "</member>\n";
}
xml += "</struct>\n";
return xml;
};
XMLRPCMessage.prototype.getParamXML = function(type, data) {
var xml;
switch (type) {
case "date":
xml = this.doDateXML(data);
break;
case "array":
xml = this.doArrayXML(data);
break;
case "struct":
xml = this.doStructXML(data);
break;
case "boolean":
xml = this.doBooleanXML(data);
break;
default:
xml = this.doValueXML(type, data);
break;
}
return xml;
};
Documentation generated by
JSDoc on Wed Jun 23 09:33:14 2010