aboutsummaryrefslogtreecommitdiffstats
path: root/lib/xmlrpc.js
blob: 54dcd72ba0525a451066e061cc00dac313babace (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* global exports: false */
"use strict";
// Modification of Matěj Cepl released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
/*
 *
 * xmlrpc.js beta version 1 Tool for creating XML-RPC formatted requests in
 * JavaScript
 *
 * Copyright 2001 Scott Andrew LePera scott@scottandrew.com
 *
 *
 * 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
 * warranty of any kind is made or implied by the author.
 *
 */

/**
 * 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.
 */

/**
 * pads a single number with a leading zero. Heh.
 *
 * @param n
 *          String or Number
 * @return String with leading zero added if necessary
 *
 * If the real parameter is not numerical, it is just returned as it is.
 */
var leadingZero = exports.leadingZero = function leadingZero(n) {
  if (isNaN(Number(n))) {
    return n;
  }
  ;

  var s = "0" + n;
  if (s.length > 2) {
    s = s.slice(1);
  }
  return s;
};

var dateToISO8601 = exports.dateToISO8601 = function dateToISO8601(
    date) {
  // wow I hate working with the Date object
  var year = date.getYear();
  var month = leadingZero(date.getMonth());
  var day = leadingZero(date.getDate());
  var time = leadingZero(date.getHours()) + ":"
      + leadingZero(date.getMinutes()) + ":"
      + leadingZero(date.getSeconds());

  var converted = year + month + day + "T" + time;
  return converted;
};

var XMLRPCMessage = exports.XMLRPCMessage = function XMLRPCMessage(
    methodname) {
  this.method = methodname || "system.listMethods";
  this.params = [];
  return this;
};

XMLRPCMessage.prototype.myIsArray = function myIsArray(obj) {
  return (typeof obj.sort === 'function');
};

XMLRPCMessage.prototype.setMethod = function(methodName) {
  if (methodName !== undefined) {
    this.method = methodName;
  }
};

XMLRPCMessage.prototype.addParameter = function(data) {
  if (data !== undefined) {
    this.params.push(data);
  }
};

XMLRPCMessage.prototype.xml = function() {

  var method = this.method;

  // assemble the XML message header
  var xml = "";

  xml += "<?xml version=\"1.0\"?>\n";
  xml += "<methodCall>\n";
  xml += "<methodName>" + method + "</methodName>\n";
  xml += "<params>\n";

  // do individual parameters
  this.params.forEach(function(data) {
    xml += "<param>\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) {
  // identifies the data type
  var type = typeof (o);
  type = type.toLowerCase();
  switch (type) {
  case "number":
    if (Math.round(o) === o) {
      type = "i4";
    }
    else {
      type = "double";
    }
    break;
  case "object":
    if ((o instanceof Date)) {
      type = "date";
    }
    else if (this.myIsArray(o)) {
      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 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;
};