diff options
author | Matěj Cepl <mcepl@redhat.com> | 2010-09-24 00:36:58 +0200 |
---|---|---|
committer | Matěj Cepl <mcepl@redhat.com> | 2010-09-24 00:36:58 +0200 |
commit | f31ed4f55d9a33bb39a5e409b36878b5ac4ae470 (patch) | |
tree | e1a22a4eee232d16768a79f7149e3afba1439a48 /lib/xmlrpc.js | |
parent | 38c0e794b2a46545ed2b31d5ddfd27cd533c1d3d (diff) | |
download | bugzilla-triage-f31ed4f55d9a33bb39a5e409b36878b5ac4ae470.tar.gz |
Fix arrays in xmlrpc module0.24
Diffstat (limited to 'lib/xmlrpc.js')
-rw-r--r-- | lib/xmlrpc.js | 114 |
1 files 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 += "<params>\n"; // do individual parameters - for ( var i = 0; i < this.params.length; i++) { - var data = this.params[i]; + this.params.forEach(function (data) { xml += "<param>\n"; - xml += "<value>" - + this.getParamXML(this.dataTypeOf(data), - data) + "</value>\n"; + xml += "<value>" + + this.getParamXML(this.dataTypeOf(data), + data) + "</value>\n"; xml += "</param>\n"; - } - + }, this); xml += "</params>\n"; xml += "</methodCall>"; 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 + "</" + type + ">"; 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 = "<boolean>" + value + "</boolean>"; 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 = "<dateTime.iso8601>"; 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 = "<array><data>\n"; - for ( var i = 0; i < data.length; i++) { - xml += "<value>" - + this.getParamXML(this.dataTypeOf(data[i]), - data[i]) + "</value>\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) { +XMLRPCMessage.prototype.doStructXML = function (data) { var xml = "<struct>\n"; - for ( var i in data) { + for (var i in data) { xml += "<member>\n"; xml += "<name>" + i + "</name>\n"; - xml += "<value>" - + this.getParamXML(this.dataTypeOf(data[i]), + xml += "<value>" + this.getParamXML(this.dataTypeOf(data[i]), data[i]) + "</value>\n"; xml += "</member>\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": |