From f31ed4f55d9a33bb39a5e409b36878b5ac4ae470 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Fri, 24 Sep 2010 00:36:58 +0200 Subject: Fix arrays in xmlrpc module --- lib/xmlrpc.js | 114 +++++++++++++++++++++++++++++++++------------------------- 1 file changed, 66 insertions(+), 48 deletions(-) diff --git a/lib/xmlrpc.js b/lib/xmlrpc.js index 9163089..e883d91 100644 --- a/lib/xmlrpc.js +++ b/lib/xmlrpc.js @@ -1,13 +1,15 @@ +/*jslint rhino: true, forin: true, onevar: false, browser: true */ +/*global exports: false */ +"use strict"; // Modification of Matěj Cepl released under the MIT/X11 license // http://www.opensource.org/licenses/mit-license.php -"use strict"; /* * * xmlrpc.js beta version 1 Tool for creating XML-RPC formatted requests in * JavaScript * * Copyright 2001 Scott Andrew LePera scott@scottandrew.com - * http://www.scottandrew.com/xml-rpc + * * * License: You are granted the right to use and/or redistribute this code only * if this license and the copyright notice are included and you accept that no @@ -15,23 +17,40 @@ * */ +/** + * checks whether parameter is an array + * + * @param obj Object + * @return Boolean true if obj is array + * + * The problem is that in different contexts, Array is not same, and so obj is + * not an instance of SAME Array. + */ + 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.myIsArray = function myIsArray(obj) { + return (typeof obj.sort === 'function'); }; -XMLRPCMessage.prototype.addParameter = function(data) { - if (arguments.length == 0) return; - this.params[this.params.length] = data; + +XMLRPCMessage.prototype.setMethod = function (methodName) { + if (methodName !== undefined) { + this.method = methodName; + } }; -XMLRPCMessage.prototype.xml = function() { +XMLRPCMessage.prototype.addParameter = function (data) { + if (data !== undefined) { + this.params.push(data); + } +}; + +XMLRPCMessage.prototype.xml = function () { var method = this.method; @@ -44,75 +63,75 @@ XMLRPCMessage.prototype.xml = function() { xml += "\n"; // do individual parameters - for ( var i = 0; i < this.params.length; i++) { - var data = this.params[i]; + this.params.forEach(function (data) { xml += "\n"; - xml += "" - + this.getParamXML(this.dataTypeOf(data), - data) + "\n"; + xml += "" + + this.getParamXML(this.dataTypeOf(data), + data) + "\n"; xml += "\n"; - } - + }, this); xml += "\n"; xml += ""; return xml; // for now }; -XMLRPCMessage.prototype.dataTypeOf = function(o) { +XMLRPCMessage.prototype.dataTypeOf = function (o) { // identifies the data type var type = typeof (o); type = type.toLowerCase(); switch (type) { case "number": - if (Math.round(o) == o) + if (Math.round(o) === o) { type = "i4"; - else + } else { type = "double"; + } break; case "object": - var con = o.constructor; - if (con == Date) + if ((o instanceof Date)) { type = "date"; - else if (con == Array) + } else if (this.myIsArray(o)) { type = "array"; - else + } else { type = "struct"; + } break; } return type; }; -XMLRPCMessage.prototype.doValueXML = function(type, data) { +XMLRPCMessage.prototype.doValueXML = function (type, data) { var xml = "<" + type + ">" + data + ""; return xml; }; -XMLRPCMessage.prototype.doBooleanXML = function(data) { - var value = (data == true) ? 1 : 0; +XMLRPCMessage.prototype.doBooleanXML = function (data) { + var value = (data === true) ? 1 : 0; var xml = "" + value + ""; return xml; }; -XMLRPCMessage.prototype.doDateXML = function(data) { - var leadingZero = function (n) { +XMLRPCMessage.prototype.doDateXML = function (data) { + function leadingZero(n) { // pads a single number with a leading zero. Heh. - if (n.length == 1) + if (n.length === 1) { n = "0" + n; + } return n; - }; - var dateToISO8601 = function(date) { + } + function dateToISO8601(date) { // wow I hate working with the Date object - 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 year = date.getYear(); + var month = this.leadingZero(date.getMonth()); + var day = this.leadingZero(date.getDate()); + var time = this.leadingZero(date.getHours()) + + ":" + this.leadingZero(date.getMinutes()) + + ":" + this.leadingZero(date.getSeconds()); var converted = year + month + day + "T" + time; return converted; - }; + } var xml = ""; xml += dateToISO8601(data); @@ -120,24 +139,23 @@ XMLRPCMessage.prototype.doDateXML = function(data) { return xml; }; -XMLRPCMessage.prototype.doArrayXML = function(data) { +XMLRPCMessage.prototype.doArrayXML = function (data) { var xml = "\n"; - for ( var i = 0; i < data.length; i++) { - xml += "" - + this.getParamXML(this.dataTypeOf(data[i]), - data[i]) + "\n"; + for (var i = 0; i < data.length; i++) { + xml += "" + + this.getParamXML(this.dataTypeOf(data[i]), + data[i]) + "\n"; } xml += "\n"; return xml; }; -XMLRPCMessage.prototype.doStructXML = function(data) { +XMLRPCMessage.prototype.doStructXML = function (data) { var xml = "\n"; - for ( var i in data) { + for (var i in data) { xml += "\n"; xml += "" + i + "\n"; - xml += "" - + this.getParamXML(this.dataTypeOf(data[i]), + xml += "" + this.getParamXML(this.dataTypeOf(data[i]), data[i]) + "\n"; xml += "\n"; } @@ -145,7 +163,7 @@ XMLRPCMessage.prototype.doStructXML = function(data) { return xml; }; -XMLRPCMessage.prototype.getParamXML = function(type, data) { +XMLRPCMessage.prototype.getParamXML = function (type, data) { var xml; switch (type) { case "date": -- cgit