2 # Creates XML-RPC call/response documents
4 # Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
10 require "xmlrpc/base64"
17 def ele(name, *children)
18 element(name, nil, *children)
22 element(name, nil, text(txt))
27 class Simple < Abstract
29 def document_to_str(doc)
38 "<?#{name} " + params.join(" ") + " ?>"
41 def element(name, attrs, *children)
42 raise "attributes not yet implemented" unless attrs.nil?
46 "<#{name}>" + children.join("") + "</#{name}>"
52 cleaned.gsub!(/&/, '&')
53 cleaned.gsub!(/</, '<')
54 cleaned.gsub!(/>/, '>')
61 class XMLParser < Abstract
64 require "xmltreebuilder"
67 def document_to_str(doc)
72 XML::SimpleTree::Document.new(*params)
76 XML::SimpleTree::ProcessingInstruction.new(name, *params)
79 def element(name, attrs, *children)
80 XML::SimpleTree::Element.new(name, attrs, *children)
84 XML::SimpleTree::Text.new(txt)
89 Classes = [Simple, XMLParser]
91 # yields an instance of each installed XML writer
92 def self.each_installed_writer
93 XMLRPC::XMLWriter::Classes.each do |klass|
101 end # module XMLWriter
105 def initialize(xml_writer = nil)
106 @writer = xml_writer || Config::DEFAULT_WRITER.new
110 def methodCall(name, *params)
113 if name !~ /[a-zA-Z0-9_.:\/]+/
114 raise ArgumentError, "Wrong XML-RPC method-name"
117 parameter = params.collect do |param|
118 @writer.ele("param", conv2value(param))
121 tree = @writer.document(
122 @writer.pi("xml", 'version="1.0"'),
123 @writer.ele("methodCall",
124 @writer.tag("methodName", name),
125 @writer.ele("params", *parameter)
129 @writer.document_to_str(tree) + "\n"
135 # generates a XML-RPC methodResponse document
137 # if is_ret == false then the params array must
138 # contain only one element, which is a structure
139 # of a fault return-value.
141 # if is_ret == true then a normal
142 # return-value of all the given params is created.
144 def methodResponse(is_ret, *params)
147 resp = params.collect do |param|
148 @writer.ele("param", conv2value(param))
151 resp = [@writer.ele("params", *resp)]
153 if params.size != 1 or params[0] === XMLRPC::FaultException
154 raise ArgumentError, "no valid fault-structure given"
156 resp = @writer.ele("fault", conv2value(params[0].to_h))
160 tree = @writer.document(
161 @writer.pi("xml", 'version="1.0"'),
162 @writer.ele("methodResponse", resp)
165 @writer.document_to_str(tree) + "\n"
170 #####################################
172 #####################################
175 # converts a Ruby object into
176 # a XML-RPC <value> tag
178 def conv2value(param)
182 @writer.tag("i4", param.to_s)
185 if Config::ENABLE_BIGINT
186 @writer.tag("i4", param.to_s)
188 if param >= -(2**31) and param <= (2**31-1)
189 @writer.tag("i4", param.to_s)
191 raise "Bignum is too big! Must be signed 32-bit integer!"
194 when TrueClass, FalseClass
195 @writer.tag("boolean", param ? "1" : "0")
198 @writer.tag("string", param.to_s)
201 @writer.tag("string", param)
204 if Config::ENABLE_NIL_CREATE
207 raise "Wrong type NilClass. Not allowed!"
211 @writer.tag("double", param.to_s)
214 h = param.members.collect do |key|
216 @writer.ele("member",
217 @writer.tag("name", key.to_s),
222 @writer.ele("struct", *h)
225 # TODO: can a Hash be empty?
227 h = param.collect do |key, value|
228 @writer.ele("member",
229 @writer.tag("name", key.to_s),
234 @writer.ele("struct", *h)
237 # TODO: can an Array be empty?
238 a = param.collect {|v| conv2value(v) }
241 @writer.ele("data", *a)
244 when Time, Date, ::DateTime
245 @writer.tag("dateTime.iso8601", param.strftime("%Y%m%dT%H:%M:%S"))
247 when XMLRPC::DateTime
248 @writer.tag("dateTime.iso8601",
249 format("%.4d%02d%02dT%02d:%02d:%02d", *param.to_a))
252 @writer.tag("base64", param.encoded)
255 if Config::ENABLE_MARSHALLING and param.class.included_modules.include? XMLRPC::Marshallable
256 # convert Ruby object into Hash
257 ret = {"___class___" => param.class.name}
258 param.instance_variables.each {|v|
260 val = param.instance_variable_get(v)
263 ret[name] = val if Config::ENABLE_NIL_CREATE
268 return conv2value(ret)
270 ok, pa = wrong_type(param)
272 return conv2value(pa)
279 @writer.ele("value", val)
282 def wrong_type(value)