forked from manoelcampos/xml2lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmlrpclib.lua
151 lines (140 loc) · 5.26 KB
/
xmlrpclib.lua
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
---XMLRPC serialiser/deserialiser<br/>
--
-- XXXX Incomplete/Deprecated - DO NOT USE<br/>
--
-- $Id: xmlrpclib.lua,v 1.1.1.1 2001/11/28 06:11:33 paulc Exp $<br/>
--
-- $Log: xmlrpclib.lua,v $<br/>
-- Revision 1.1.1.1 2001/11/28 06:11:33 paulc<br/>
-- Initial Import
--
---xmlrpclib
xmlrpclib = function()
obj = {}
obj._BOOL = type(true)
obj._FROMXML = { i4 = tonumber,
int = tonumber,
boolean = function(x) return x ~= '0' end,
string = function(x) return x end,
double = tonumber,
base64 = function(x) return x end,
['dateTime.iso8601'] = function(x) return x end,
['nil'] = function(x) return nil end,
}
obj._TOXML = { table = function(self,x)
local res = ""
if tag(x) == self._BOOL then
return "<value><boolean>"..x[1].."</boolean></value>"
elseif table.getn(x) > 0 then
res = res.."<value><array><data>"
for i=1,table.getn(x) do
res = res..self._TOXML[type(x[i])](self,x[i])
end
res = res.."</data></array></value>"
return res
else
res = res.."<value><struct>"
for k,v in x do
res = res.."<member><name>"..k.."</name>"
res = res..self._TOXML[type(v)](self,v).."</member>".."\n"
end
res = res.."</struct></value>"
end
return res
end,
number = function(self,x)
if x==ceil(x) then
return "<value><int>"..tostring(x).."</int></value>"
else
return "<value><double>"..tostring(x).."</double></value>"
end
end,
string = function(self,x)
return "<value><string>"..x.."</string></value>"
end,
['nil'] = function(self,x)
return "<value><nil/></value>"
end,
}
function obj:bool(x)
local res = {}
if x then
res[1] = 1
else
res[1] = 0
end
return res
end
function obj:serialise(t)
local res = ""
if type(t) ~= 'table' then
t = {t}
end
for i=1,table.getn(t) do
res = res.."<param>"..self._TOXML[type(t[i])](self,t[i]).."</param>".."\n"
end
return res
end
function obj:methodResponse(t)
local res = "<?xml version=\"1.0\"?>\n"
res = res.."<methodResponse><params>\n"
res = res..self:serialise(t)
res = res.."</params></methodResponse>\n"
return res
end
function obj:parseParam(param)
local element = param.value
if type(value) == 'string' then
return value
elseif element.struct then
local struct = {}
if element.struct.member then
for i=1,table.getn(element.struct.member) do
local name = element.struct.member[i].name
local value = self:parseParam(element.struct.member[i])
struct[name] = value
end
end
return struct
elseif element.array then
local array = {}
local values
if element.array.data.value then
if table.getn(element.array.data.value) == 0 then
values = {element.array.data.value}
else
values = element.array.data.value
end
for i=1,table.getn(values) do
table.insert(array,self:parseParam({value = values[i]}))
end
array.n = nil
end
return array
elseif type(element) == 'table' then
local dtype,dval = next(element)
if not self._FROMXML[dtype] then
error("Unknown Datatype:")
end
return self._FROMXML[dtype](dval)
else
error("Unknown Datatype:")
end
end
function obj:parseMethodCall(root)
local methodCall = root.methodCall
if not methodCall then
error("Invalid RPC Message")
end
local methodName = methodCall.methodName
local params = {}
if methodCall.params then
for i=1,table.getn(methodCall.params.param) do
table.insert(params,self:parseParam(methodCall.params.param[i]))
end
end
params.n = nil
return methodName,params
end
return obj
end