2003-07-19 06:05:54 -04:00
|
|
|
#
|
|
|
|
# Parser for XML-RPC call and response
|
2009-03-05 22:56:38 -05:00
|
|
|
#
|
2003-07-19 06:05:54 -04:00
|
|
|
# Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
|
|
|
|
#
|
|
|
|
# $Id$
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
|
|
require "date"
|
|
|
|
require "xmlrpc/base64"
|
|
|
|
require "xmlrpc/datetime"
|
|
|
|
|
|
|
|
|
|
|
|
# add some methods to NQXML::Node
|
|
|
|
module NQXML
|
|
|
|
class Node
|
|
|
|
|
|
|
|
def removeChild(node)
|
|
|
|
@children.delete(node)
|
|
|
|
end
|
|
|
|
def childNodes
|
|
|
|
@children
|
|
|
|
end
|
|
|
|
def hasChildNodes
|
|
|
|
not @children.empty?
|
|
|
|
end
|
|
|
|
def [] (index)
|
|
|
|
@children[index]
|
|
|
|
end
|
|
|
|
|
|
|
|
def nodeType
|
|
|
|
if @entity.instance_of? NQXML::Text then :TEXT
|
|
|
|
elsif @entity.instance_of? NQXML::Comment then :COMMENT
|
|
|
|
#elsif @entity.instance_of? NQXML::Element then :ELEMENT
|
|
|
|
elsif @entity.instance_of? NQXML::Tag then :ELEMENT
|
|
|
|
else :ELSE
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def nodeValue
|
|
|
|
#TODO: error when wrong Entity-type
|
|
|
|
@entity.text
|
|
|
|
end
|
|
|
|
def nodeName
|
|
|
|
#TODO: error when wrong Entity-type
|
|
|
|
@entity.name
|
|
|
|
end
|
|
|
|
end # class Node
|
|
|
|
end # module NQXML
|
|
|
|
|
|
|
|
module XMLRPC
|
|
|
|
|
* array.c: replace rb_protect_inspect() and rb_inspecting_p() by
rb_exec_recursive() in eval.c.
* eval.c (rb_exec_recursive): new function.
* array.c (rb_ary_join): use rb_exec_recursive().
* array.c (rb_ary_inspect, rb_ary_hash): ditto.
* file.c (rb_file_join): ditto.
* hash.c (rb_hash_inspect, rb_hash_to_s, rb_hash_hash): ditto.
* io.c (rb_io_puts): ditto.
* object.c (rb_obj_inspect): ditto
* struct.c (rb_struct_inspect): ditto.
* lib/set.rb (SortedSet::setup): a hack to shut up warning.
[ruby-talk:132866]
* lib/time.rb (Time::strptime): add new function. inspired by
[ruby-talk:132815].
* lib/parsedate.rb (ParseDate::strptime): ditto.
* regparse.c: move st_*_strend() functions from st.c. fixed some
potential memory leaks.
* exception error messages updated. [ruby-core:04497]
* ext/socket/socket.c (Init_socket): add bunch of Socket
constants. Patch from Sam Roberts <sroberts@uniserve.com>.
[ruby-core:04409]
* array.c (rb_ary_s_create): no need for negative argc check.
[ruby-core:04463]
* array.c (rb_ary_unshift_m): ditto.
* lib/xmlrpc/parser.rb (XMLRPC::FaultException): make it subclass
of StandardError class, not Exception class. [ruby-core:04429]
* parse.y (fcall_gen): lvar(arg) will be evaluated as
lvar.call(arg) when lvar is a defined local variable. [new]
* object.c (rb_class_initialize): call inherited method before
calling initializing block.
* eval.c (rb_thread_start_1): initialize newly pushed frame.
* lib/open3.rb (Open3::popen3): $? should not be EXIT_FAILURE.
fixed: [ruby-core:04444]
* eval.c (is_defined): NODE_IASGN is an assignment.
* ext/readline/readline.c (Readline.readline): use rl_outstream
and rl_instream. [ruby-dev:25699]
* ext/etc/etc.c (Init_etc): sGroup needs HAVE_ST_GR_PASSWD check
[ruby-dev:25675]
* misc/ruby-mode.el: [ruby-core:04415]
* lib/rdoc/generators/html_generator.rb: [ruby-core:04412]
* lib/rdoc/generators/ri_generator.rb: ditto.
* struct.c (make_struct): fixed: [ruby-core:04402]
* ext/curses/curses.c (window_color_set): [ruby-core:04393]
* ext/socket/socket.c (Init_socket): SO_REUSEPORT added.
[ruby-talk:130092]
* object.c: [ruby-doc:818]
* parse.y (open_args): fix too verbose warnings for the space
before argument parentheses. [ruby-dev:25492]
* parse.y (parser_yylex): ditto.
* parse.y (parser_yylex): the first expression in the parentheses
should not be a command. [ruby-dev:25492]
* lib/irb/context.rb (IRB::Context::initialize): [ruby-core:04330]
* object.c (Init_Object): remove Object#type. [ruby-core:04335]
* st.c (st_foreach): report success/failure by return value.
[ruby-Bugs-1396]
* parse.y: forgot to initialize parser struct. [ruby-dev:25492]
* parse.y (parser_yylex): no tLABEL on EXPR_BEG.
[ruby-talk:127711]
* document updates - [ruby-core:04296], [ruby-core:04301],
[ruby-core:04302], [ruby-core:04307]
* dir.c (rb_push_glob): should work for NUL delimited patterns.
* dir.c (rb_glob2): should aware of offset in the pattern.
* string.c (rb_str_new4): should propagate taintedness.
* env.h: rename member names in struct FRAME; last_func -> callee,
orig_func -> this_func, last_class -> this_class.
* struct.c (rb_struct_set): use original method name, not callee
name, to retrieve member slot. [ruby-core:04268]
* time.c (time_strftime): protect from format modification from GC
finalizers.
* object.c (Init_Object): remove rb_obj_id_obsolete()
* eval.c (rb_mod_define_method): incomplete subclass check.
[ruby-dev:25464]
* gc.c (rb_data_object_alloc): klass may be NULL.
[ruby-list:40498]
* bignum.c (rb_big_rand): should return positive random number.
[ruby-dev:25401]
* bignum.c (rb_big_rand): do not use rb_big_modulo to generate
random bignums. [ruby-dev:25396]
* variable.c (rb_autoload): [ruby-dev:25373]
* eval.c (svalue_to_avalue): [ruby-dev:25366]
* string.c (rb_str_justify): [ruby-dev:25367]
* io.c (rb_f_select): [ruby-dev:25312]
* ext/socket/socket.c (sock_s_getservbyport): [ruby-talk:124072]
* struct.c (make_struct): [ruby-dev:25249]
* dir.c (dir_open_dir): new function. [ruby-dev:25242]
* io.c (rb_f_open): add type check for return value from to_open.
* lib/pstore.rb (PStore#transaction): Use the empty content when a
file is not found. [ruby-dev:24561]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8068 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-03-04 01:47:45 -05:00
|
|
|
class FaultException < StandardError
|
2003-07-19 06:05:54 -04:00
|
|
|
attr_reader :faultCode, :faultString
|
|
|
|
|
2005-04-06 08:36:47 -04:00
|
|
|
alias message faultString
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
def initialize(faultCode, faultString)
|
|
|
|
@faultCode = faultCode
|
|
|
|
@faultString = faultString
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
# returns a hash
|
|
|
|
def to_h
|
|
|
|
{"faultCode" => @faultCode, "faultString" => @faultString}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module Convert
|
|
|
|
def self.int(str)
|
|
|
|
str.to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.boolean(str)
|
|
|
|
case str
|
|
|
|
when "0" then false
|
|
|
|
when "1" then true
|
|
|
|
else
|
2009-03-05 22:56:38 -05:00
|
|
|
raise "RPC-value of type boolean is wrong"
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.double(str)
|
|
|
|
str.to_f
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.dateTime(str)
|
2004-11-15 20:11:49 -05:00
|
|
|
case str
|
|
|
|
when /^(-?\d\d\d\d)-?(\d\d)-?(\d\d)T(\d\d):(\d\d):(\d\d)(?:Z|([+-])(\d\d):?(\d\d))?$/
|
|
|
|
a = [$1, $2, $3, $4, $5, $6].collect{|i| i.to_i}
|
|
|
|
if $7
|
|
|
|
ofs = $8.to_i*3600 + $9.to_i*60
|
|
|
|
ofs = -ofs if $7=='+'
|
2007-10-24 17:24:09 -04:00
|
|
|
utc = Time.utc(*a) + ofs
|
2004-11-15 20:11:49 -05:00
|
|
|
a = [ utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec ]
|
|
|
|
end
|
|
|
|
XMLRPC::DateTime.new(*a)
|
|
|
|
when /^(-?\d\d)-?(\d\d)-?(\d\d)T(\d\d):(\d\d):(\d\d)(Z|([+-]\d\d):(\d\d))?$/
|
2003-07-19 06:05:54 -04:00
|
|
|
a = [$1, $2, $3, $4, $5, $6].collect{|i| i.to_i}
|
2004-11-15 20:11:49 -05:00
|
|
|
if a[0] < 70
|
|
|
|
a[0] += 2000
|
|
|
|
else
|
|
|
|
a[0] += 1900
|
|
|
|
end
|
|
|
|
if $7
|
|
|
|
ofs = $8.to_i*3600 + $9.to_i*60
|
|
|
|
ofs = -ofs if $7=='+'
|
2007-10-24 17:24:09 -04:00
|
|
|
utc = Time.utc(*a) + ofs
|
2004-11-15 20:11:49 -05:00
|
|
|
a = [ utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec ]
|
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
XMLRPC::DateTime.new(*a)
|
|
|
|
else
|
2004-11-15 20:11:49 -05:00
|
|
|
raise "wrong dateTime.iso8601 format " + str
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.base64(str)
|
|
|
|
XMLRPC::Base64.decode(str)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.struct(hash)
|
|
|
|
# convert to marhalled object
|
|
|
|
klass = hash["___class___"]
|
2009-03-05 22:56:38 -05:00
|
|
|
if klass.nil? or Config::ENABLE_MARSHALLING == false
|
2003-07-19 06:05:54 -04:00
|
|
|
hash
|
|
|
|
else
|
|
|
|
begin
|
|
|
|
mod = Module
|
2003-10-16 13:47:19 -04:00
|
|
|
klass.split("::").each {|const| mod = mod.const_get(const.strip)}
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2004-11-15 18:26:20 -05:00
|
|
|
obj = mod.allocate
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
hash.delete "___class___"
|
2009-03-05 22:56:38 -05:00
|
|
|
hash.each {|key, value|
|
2010-04-05 23:01:52 -04:00
|
|
|
obj.instance_variable_set("@#{ key }", value) if key =~ /^([a-zA-Z_]\w*)$/
|
2004-11-15 18:26:20 -05:00
|
|
|
}
|
2003-07-19 06:05:54 -04:00
|
|
|
obj
|
|
|
|
rescue
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.fault(hash)
|
2009-03-05 22:56:38 -05:00
|
|
|
if hash.kind_of? Hash and hash.size == 2 and
|
|
|
|
hash.has_key? "faultCode" and hash.has_key? "faultString" and
|
2003-07-19 06:05:54 -04:00
|
|
|
hash["faultCode"].kind_of? Integer and hash["faultString"].kind_of? String
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
XMLRPC::FaultException.new(hash["faultCode"], hash["faultString"])
|
2003-07-19 06:05:54 -04:00
|
|
|
else
|
|
|
|
raise "wrong fault-structure: #{hash.inspect}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end # module Convert
|
|
|
|
|
|
|
|
module XMLParser
|
|
|
|
|
|
|
|
class AbstractTreeParser
|
|
|
|
|
|
|
|
def parseMethodResponse(str)
|
2008-10-01 09:46:53 -04:00
|
|
|
methodResponse_document(createCleanedTree(str))
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def parseMethodCall(str)
|
2008-10-01 09:46:53 -04:00
|
|
|
methodCall_document(createCleanedTree(str))
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
#
|
|
|
|
# remove all whitespaces but in the tags i4, int, boolean....
|
|
|
|
# and all comments
|
|
|
|
#
|
|
|
|
def removeWhitespacesAndComments(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
remove = []
|
|
|
|
childs = node.childNodes.to_a
|
|
|
|
childs.each do |nd|
|
|
|
|
case _nodeType(nd)
|
|
|
|
when :TEXT
|
2003-07-19 06:05:54 -04:00
|
|
|
# TODO: add nil?
|
|
|
|
unless %w(i4 int boolean string double dateTime.iso8601 base64).include? node.nodeName
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
if node.nodeName == "value"
|
2003-07-19 06:05:54 -04:00
|
|
|
if not node.childNodes.to_a.detect {|n| _nodeType(n) == :ELEMENT}.nil?
|
2009-03-05 22:56:38 -05:00
|
|
|
remove << nd if nd.nodeValue.strip == ""
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
else
|
|
|
|
remove << nd if nd.nodeValue.strip == ""
|
|
|
|
end
|
2008-10-01 09:46:53 -04:00
|
|
|
end
|
|
|
|
when :COMMENT
|
|
|
|
remove << nd
|
|
|
|
else
|
|
|
|
removeWhitespacesAndComments(nd)
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2008-10-01 09:46:53 -04:00
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
remove.each { |i| node.removeChild(i) }
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def nodeMustBe(node, name)
|
2008-10-01 09:46:53 -04:00
|
|
|
cmp = case name
|
2009-03-05 22:56:38 -05:00
|
|
|
when Array
|
2008-10-01 09:46:53 -04:00
|
|
|
name.include?(node.nodeName)
|
|
|
|
when String
|
|
|
|
name == node.nodeName
|
|
|
|
else
|
|
|
|
raise "error"
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
if not cmp then
|
|
|
|
raise "wrong xml-rpc (name)"
|
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
node
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
#
|
|
|
|
# returns, when successfully the only child-node
|
|
|
|
#
|
|
|
|
def hasOnlyOneChild(node, name=nil)
|
2008-10-01 09:46:53 -04:00
|
|
|
if node.childNodes.to_a.size != 1
|
|
|
|
raise "wrong xml-rpc (size)"
|
|
|
|
end
|
|
|
|
if name != nil then
|
|
|
|
nodeMustBe(node.firstChild, name)
|
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def assert(b)
|
2008-10-01 09:46:53 -04:00
|
|
|
if not b then
|
2009-03-05 22:56:38 -05:00
|
|
|
raise "assert-fail"
|
2008-10-01 09:46:53 -04:00
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# the node `node` has empty string or string
|
|
|
|
def text_zero_one(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodes = node.childNodes.to_a.size
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
if nodes == 1
|
|
|
|
text(node.firstChild)
|
|
|
|
elsif nodes == 0
|
|
|
|
""
|
|
|
|
else
|
|
|
|
raise "wrong xml-rpc (size)"
|
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
def integer(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
#TODO: check string for float because to_i returnsa
|
|
|
|
# 0 when wrong string
|
2009-03-05 22:56:38 -05:00
|
|
|
nodeMustBe(node, %w(i4 int))
|
2008-10-01 09:46:53 -04:00
|
|
|
hasOnlyOneChild(node)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
Convert.int(text(node.firstChild))
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def boolean(node)
|
2009-03-05 22:56:38 -05:00
|
|
|
nodeMustBe(node, "boolean")
|
2008-10-01 09:46:53 -04:00
|
|
|
hasOnlyOneChild(node)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
Convert.boolean(text(node.firstChild))
|
|
|
|
end
|
|
|
|
|
|
|
|
def v_nil(node)
|
|
|
|
nodeMustBe(node, "nil")
|
2008-10-01 09:46:53 -04:00
|
|
|
assert( node.childNodes.to_a.size == 0 )
|
2003-07-19 06:05:54 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def string(node)
|
2009-03-05 22:56:38 -05:00
|
|
|
nodeMustBe(node, "string")
|
2008-10-01 09:46:53 -04:00
|
|
|
text_zero_one(node)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def double(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
#TODO: check string for float because to_f returnsa
|
|
|
|
# 0.0 when wrong string
|
2009-03-05 22:56:38 -05:00
|
|
|
nodeMustBe(node, "double")
|
2008-10-01 09:46:53 -04:00
|
|
|
hasOnlyOneChild(node)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
Convert.double(text(node.firstChild))
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def dateTime(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "dateTime.iso8601")
|
|
|
|
hasOnlyOneChild(node)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
Convert.dateTime( text(node.firstChild) )
|
|
|
|
end
|
|
|
|
|
|
|
|
def base64(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "base64")
|
|
|
|
#hasOnlyOneChild(node)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
Convert.base64(text_zero_one(node))
|
|
|
|
end
|
|
|
|
|
|
|
|
def member(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "member")
|
2009-03-05 22:56:38 -05:00
|
|
|
assert( node.childNodes.to_a.size == 2 )
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
[ name(node[0]), value(node[1]) ]
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def name(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "name")
|
|
|
|
#hasOnlyOneChild(node)
|
2009-03-05 22:56:38 -05:00
|
|
|
text_zero_one(node)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def array(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "array")
|
2009-03-05 22:56:38 -05:00
|
|
|
hasOnlyOneChild(node, "data")
|
|
|
|
data(node.firstChild)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def data(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "data")
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
node.childNodes.to_a.collect do |val|
|
|
|
|
value(val)
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def param(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "param")
|
|
|
|
hasOnlyOneChild(node, "value")
|
2009-03-05 22:56:38 -05:00
|
|
|
value(node.firstChild)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
def methodResponse(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "methodResponse")
|
|
|
|
hasOnlyOneChild(node, %w(params fault))
|
|
|
|
child = node.firstChild
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
case child.nodeName
|
|
|
|
when "params"
|
2009-03-05 22:56:38 -05:00
|
|
|
[ true, params(child,false) ]
|
2008-10-01 09:46:53 -04:00
|
|
|
when "fault"
|
|
|
|
[ false, fault(child) ]
|
|
|
|
else
|
|
|
|
raise "unexpected error"
|
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def methodName(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "methodName")
|
|
|
|
hasOnlyOneChild(node)
|
2009-03-05 22:56:38 -05:00
|
|
|
text(node.firstChild)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def params(node, call=true)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "params")
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
if call
|
2008-10-01 09:46:53 -04:00
|
|
|
node.childNodes.to_a.collect do |n|
|
|
|
|
param(n)
|
|
|
|
end
|
|
|
|
else # response (only one param)
|
|
|
|
hasOnlyOneChild(node)
|
|
|
|
param(node.firstChild)
|
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def fault(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "fault")
|
|
|
|
hasOnlyOneChild(node, "value")
|
2009-03-05 22:56:38 -05:00
|
|
|
f = value(node.firstChild)
|
2003-07-19 06:05:54 -04:00
|
|
|
Convert.fault(f)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# _nodeType is defined in the subclass
|
|
|
|
def text(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
assert( _nodeType(node) == :TEXT )
|
|
|
|
assert( node.hasChildNodes == false )
|
|
|
|
assert( node.nodeValue != nil )
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
node.nodeValue.to_s
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def struct(node)
|
2009-03-05 22:56:38 -05:00
|
|
|
nodeMustBe(node, "struct")
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
hash = {}
|
|
|
|
node.childNodes.to_a.each do |me|
|
2009-03-05 22:56:38 -05:00
|
|
|
n, v = member(me)
|
2008-10-01 09:46:53 -04:00
|
|
|
hash[n] = v
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
Convert.struct(hash)
|
2008-10-01 09:46:53 -04:00
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
|
|
|
|
def value(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "value")
|
|
|
|
nodes = node.childNodes.to_a.size
|
2009-03-05 22:56:38 -05:00
|
|
|
if nodes == 0
|
2003-07-19 06:05:54 -04:00
|
|
|
return ""
|
2009-03-05 22:56:38 -05:00
|
|
|
elsif nodes > 1
|
2008-10-01 09:46:53 -04:00
|
|
|
raise "wrong xml-rpc (size)"
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
child = node.firstChild
|
2003-07-19 06:05:54 -04:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
case _nodeType(child)
|
|
|
|
when :TEXT
|
2003-07-19 06:05:54 -04:00
|
|
|
text_zero_one(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
when :ELEMENT
|
|
|
|
case child.nodeName
|
|
|
|
when "i4", "int" then integer(child)
|
|
|
|
when "boolean" then boolean(child)
|
|
|
|
when "string" then string(child)
|
|
|
|
when "double" then double(child)
|
|
|
|
when "dateTime.iso8601" then dateTime(child)
|
|
|
|
when "base64" then base64(child)
|
|
|
|
when "struct" then struct(child)
|
2009-03-05 22:56:38 -05:00
|
|
|
when "array" then array(child)
|
|
|
|
when "nil"
|
2003-07-19 06:05:54 -04:00
|
|
|
if Config::ENABLE_NIL_PARSER
|
|
|
|
v_nil(child)
|
|
|
|
else
|
|
|
|
raise "wrong/unknown XML-RPC type 'nil'"
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
else
|
2008-10-01 09:46:53 -04:00
|
|
|
raise "wrong/unknown XML-RPC type"
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise "wrong type of node"
|
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def methodCall(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
nodeMustBe(node, "methodCall")
|
2009-03-05 22:56:38 -05:00
|
|
|
assert( (1..2).include?( node.childNodes.to_a.size ) )
|
2008-10-01 09:46:53 -04:00
|
|
|
name = methodName(node[0])
|
|
|
|
|
|
|
|
if node.childNodes.to_a.size == 2 then
|
|
|
|
pa = params(node[1])
|
|
|
|
else # no parameters given
|
|
|
|
pa = []
|
|
|
|
end
|
|
|
|
[name, pa]
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end # module TreeParserMixin
|
|
|
|
|
|
|
|
class AbstractStreamParser
|
|
|
|
def parseMethodResponse(str)
|
|
|
|
parser = @parser_class.new
|
|
|
|
parser.parse(str)
|
|
|
|
raise "No valid method response!" if parser.method_name != nil
|
|
|
|
if parser.fault != nil
|
|
|
|
# is a fault structure
|
2009-03-05 22:56:38 -05:00
|
|
|
[false, parser.fault]
|
2003-07-19 06:05:54 -04:00
|
|
|
else
|
|
|
|
# is a normal return value
|
|
|
|
raise "Missing return value!" if parser.params.size == 0
|
2004-03-29 02:54:38 -05:00
|
|
|
raise "Too many return values. Only one allowed!" if parser.params.size > 1
|
2003-07-19 06:05:54 -04:00
|
|
|
[true, parser.params[0]]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def parseMethodCall(str)
|
|
|
|
parser = @parser_class.new
|
|
|
|
parser.parse(str)
|
|
|
|
raise "No valid method call - missing method name!" if parser.method_name.nil?
|
|
|
|
[parser.method_name, parser.params]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module StreamParserMixin
|
|
|
|
attr_reader :params
|
|
|
|
attr_reader :method_name
|
|
|
|
attr_reader :fault
|
|
|
|
|
|
|
|
def initialize(*a)
|
|
|
|
super(*a)
|
|
|
|
@params = []
|
|
|
|
@values = []
|
|
|
|
@val_stack = []
|
|
|
|
|
|
|
|
@names = []
|
|
|
|
@name = []
|
|
|
|
|
|
|
|
@structs = []
|
|
|
|
@struct = {}
|
|
|
|
|
|
|
|
@method_name = nil
|
|
|
|
@fault = nil
|
|
|
|
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def startElement(name, attrs=[])
|
|
|
|
@data = nil
|
|
|
|
case name
|
|
|
|
when "value"
|
|
|
|
@value = nil
|
|
|
|
when "nil"
|
|
|
|
raise "wrong/unknown XML-RPC type 'nil'" unless Config::ENABLE_NIL_PARSER
|
2009-03-05 22:56:38 -05:00
|
|
|
@value = :nil
|
2003-07-19 06:05:54 -04:00
|
|
|
when "array"
|
|
|
|
@val_stack << @values
|
|
|
|
@values = []
|
|
|
|
when "struct"
|
|
|
|
@names << @name
|
|
|
|
@name = []
|
|
|
|
|
|
|
|
@structs << @struct
|
2009-03-05 22:56:38 -05:00
|
|
|
@struct = {}
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def endElement(name)
|
|
|
|
@data ||= ""
|
|
|
|
case name
|
|
|
|
when "string"
|
|
|
|
@value = @data
|
|
|
|
when "i4", "int"
|
|
|
|
@value = Convert.int(@data)
|
|
|
|
when "boolean"
|
|
|
|
@value = Convert.boolean(@data)
|
|
|
|
when "double"
|
|
|
|
@value = Convert.double(@data)
|
|
|
|
when "dateTime.iso8601"
|
|
|
|
@value = Convert.dateTime(@data)
|
|
|
|
when "base64"
|
|
|
|
@value = Convert.base64(@data)
|
|
|
|
when "value"
|
|
|
|
@value = @data if @value.nil?
|
2009-03-05 22:56:38 -05:00
|
|
|
@values << (@value == :nil ? nil : @value)
|
2003-07-19 06:05:54 -04:00
|
|
|
when "array"
|
|
|
|
@value = @values
|
|
|
|
@values = @val_stack.pop
|
|
|
|
when "struct"
|
|
|
|
@value = Convert.struct(@struct)
|
|
|
|
|
|
|
|
@name = @names.pop
|
|
|
|
@struct = @structs.pop
|
|
|
|
when "name"
|
2009-03-05 22:56:38 -05:00
|
|
|
@name[0] = @data
|
2003-07-19 06:05:54 -04:00
|
|
|
when "member"
|
2009-03-05 22:56:38 -05:00
|
|
|
@struct[@name[0]] = @values.pop
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
when "param"
|
|
|
|
@params << @values[0]
|
|
|
|
@values = []
|
|
|
|
|
|
|
|
when "fault"
|
|
|
|
@fault = Convert.fault(@values[0])
|
|
|
|
|
|
|
|
when "methodName"
|
2009-03-05 22:56:38 -05:00
|
|
|
@method_name = @data
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
@data = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def character(data)
|
|
|
|
if @data
|
|
|
|
@data << data
|
|
|
|
else
|
|
|
|
@data = data
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end # module StreamParserMixin
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class XMLStreamParser < AbstractStreamParser
|
|
|
|
def initialize
|
|
|
|
require "xmlparser"
|
2004-11-17 06:51:08 -05:00
|
|
|
@parser_class = Class.new(::XMLParser) {
|
|
|
|
include StreamParserMixin
|
2003-07-19 06:05:54 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end # class XMLStreamParser
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class NQXMLStreamParser < AbstractStreamParser
|
|
|
|
def initialize
|
|
|
|
require "nqxml/streamingparser"
|
|
|
|
@parser_class = XMLRPCParser
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
class XMLRPCParser
|
2003-07-19 06:05:54 -04:00
|
|
|
include StreamParserMixin
|
|
|
|
|
|
|
|
def parse(str)
|
|
|
|
parser = NQXML::StreamingParser.new(str)
|
|
|
|
parser.each do |ele|
|
|
|
|
case ele
|
|
|
|
when NQXML::Text
|
|
|
|
@data = ele.text
|
|
|
|
#character(ele.text)
|
|
|
|
when NQXML::Tag
|
|
|
|
if ele.isTagEnd
|
|
|
|
endElement(ele.name)
|
|
|
|
else
|
|
|
|
startElement(ele.name, ele.attrs)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end # do
|
|
|
|
end # method parse
|
|
|
|
end # class XMLRPCParser
|
|
|
|
|
|
|
|
end # class NQXMLStreamParser
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class XMLTreeParser < AbstractTreeParser
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
require "xmltreebuilder"
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
# The new XMLParser library (0.6.2+) uses a slightly different DOM implementation.
|
2003-07-19 06:05:54 -04:00
|
|
|
# The following code removes the differences between both versions.
|
2009-03-05 22:56:38 -05:00
|
|
|
if defined? XML::DOM::Builder
|
2003-07-19 06:05:54 -04:00
|
|
|
return if defined? XML::DOM::Node::DOCUMENT # code below has been already executed
|
|
|
|
klass = XML::DOM::Node
|
|
|
|
klass.const_set("DOCUMENT", klass::DOCUMENT_NODE)
|
|
|
|
klass.const_set("TEXT", klass::TEXT_NODE)
|
|
|
|
klass.const_set("COMMENT", klass::COMMENT_NODE)
|
|
|
|
klass.const_set("ELEMENT", klass::ELEMENT_NODE)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def _nodeType(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
tp = node.nodeType
|
|
|
|
if tp == XML::SimpleTree::Node::TEXT then :TEXT
|
2009-03-05 22:56:38 -05:00
|
|
|
elsif tp == XML::SimpleTree::Node::COMMENT then :COMMENT
|
|
|
|
elsif tp == XML::SimpleTree::Node::ELEMENT then :ELEMENT
|
2008-10-01 09:46:53 -04:00
|
|
|
else :ELSE
|
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def methodResponse_document(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
assert( node.nodeType == XML::SimpleTree::Node::DOCUMENT )
|
|
|
|
hasOnlyOneChild(node, "methodResponse")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
methodResponse(node.firstChild)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def methodCall_document(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
assert( node.nodeType == XML::SimpleTree::Node::DOCUMENT )
|
|
|
|
hasOnlyOneChild(node, "methodCall")
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-10-01 09:46:53 -04:00
|
|
|
methodCall(node.firstChild)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def createCleanedTree(str)
|
2008-10-01 09:46:53 -04:00
|
|
|
doc = XML::SimpleTreeBuilder.new.parse(str)
|
|
|
|
doc.documentElement.normalize
|
|
|
|
removeWhitespacesAndComments(doc)
|
|
|
|
doc
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end # class XMLParser
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class NQXMLTreeParser < AbstractTreeParser
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
require "nqxml/treeparser"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def _nodeType(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
node.nodeType
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def methodResponse_document(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
methodResponse(node)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def methodCall_document(node)
|
2008-10-01 09:46:53 -04:00
|
|
|
methodCall(node)
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def createCleanedTree(str)
|
2009-03-05 22:56:38 -05:00
|
|
|
doc = ::NQXML::TreeParser.new(str).document.rootNode
|
2008-10-01 09:46:53 -04:00
|
|
|
removeWhitespacesAndComments(doc)
|
|
|
|
doc
|
2003-07-19 06:05:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end # class NQXMLTreeParser
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class REXMLStreamParser < AbstractStreamParser
|
|
|
|
def initialize
|
|
|
|
require "rexml/document"
|
|
|
|
@parser_class = StreamListener
|
|
|
|
end
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
class StreamListener
|
2003-07-19 06:05:54 -04:00
|
|
|
include StreamParserMixin
|
|
|
|
|
|
|
|
alias :tag_start :startElement
|
|
|
|
alias :tag_end :endElement
|
|
|
|
alias :text :character
|
2003-11-11 08:08:16 -05:00
|
|
|
alias :cdata :character
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
def method_missing(*a)
|
|
|
|
# ignore
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse(str)
|
2010-11-08 15:59:01 -05:00
|
|
|
REXML::Document.parse_stream(str, self)
|
2008-10-01 09:46:53 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
end
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
class XMLScanStreamParser < AbstractStreamParser
|
|
|
|
def initialize
|
|
|
|
require "xmlscan/parser"
|
|
|
|
@parser_class = XMLScanParser
|
|
|
|
end
|
|
|
|
|
|
|
|
class XMLScanParser
|
|
|
|
include StreamParserMixin
|
|
|
|
|
|
|
|
Entities = {
|
|
|
|
"lt" => "<",
|
|
|
|
"gt" => ">",
|
|
|
|
"amp" => "&",
|
|
|
|
"quot" => '"',
|
|
|
|
"apos" => "'"
|
|
|
|
}
|
|
|
|
|
|
|
|
def parse(str)
|
|
|
|
parser = XMLScan::XMLParser.new(self)
|
|
|
|
parser.parse(str)
|
|
|
|
end
|
|
|
|
|
|
|
|
alias :on_stag :startElement
|
|
|
|
alias :on_etag :endElement
|
|
|
|
|
|
|
|
def on_stag_end(name); end
|
|
|
|
|
|
|
|
def on_stag_end_empty(name)
|
|
|
|
startElement(name)
|
|
|
|
endElement(name)
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
def on_chardata(str)
|
|
|
|
character(str)
|
|
|
|
end
|
|
|
|
|
2003-11-11 08:08:16 -05:00
|
|
|
def on_cdata(str)
|
|
|
|
character(str)
|
|
|
|
end
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
def on_entityref(ent)
|
|
|
|
str = Entities[ent]
|
|
|
|
if str
|
|
|
|
character(str)
|
|
|
|
else
|
|
|
|
raise "unknown entity"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_charref(code)
|
|
|
|
character(code.chr)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_charref_hex(code)
|
|
|
|
character(code.chr)
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(*a)
|
|
|
|
end
|
|
|
|
|
|
|
|
# TODO: call/implement?
|
|
|
|
# valid_name?
|
|
|
|
# valid_chardata?
|
|
|
|
# valid_char?
|
2009-03-05 22:56:38 -05:00
|
|
|
# parse_error
|
2003-07-19 06:05:54 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# ---------------------------------------------------------------------------
|
|
|
|
XMLParser = XMLTreeParser
|
|
|
|
NQXMLParser = NQXMLTreeParser
|
|
|
|
|
2009-03-05 22:56:38 -05:00
|
|
|
Classes = [XMLStreamParser, XMLTreeParser,
|
|
|
|
NQXMLStreamParser, NQXMLTreeParser,
|
2003-07-19 06:05:54 -04:00
|
|
|
REXMLStreamParser, XMLScanStreamParser]
|
|
|
|
|
2004-11-15 18:26:20 -05:00
|
|
|
# yields an instance of each installed parser
|
|
|
|
def self.each_installed_parser
|
|
|
|
XMLRPC::XMLParser::Classes.each do |klass|
|
|
|
|
begin
|
|
|
|
yield klass.new
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-07-19 06:05:54 -04:00
|
|
|
end # module XMLParser
|
|
|
|
|
|
|
|
|
|
|
|
end # module XMLRPC
|
|
|
|
|