123456789_123456789_123456789_123456789_123456789_

Class: XMLRPC::XMLParser::AbstractTreeParser

Relationships & Source Files
Inherits: Object
Defined in: lib/xmlrpc/parser.rb

Instance Method Summary

Instance Method Details

#array(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 308

def array(node)
  nodeMustBe(node, "array")
  hasOnlyOneChild(node, "data")
  data(node.firstChild)
end

#assert(b) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 225

def assert(b)
  if not b then
    raise "assert-fail"
  end
end

#base64(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 288

def base64(node)
  nodeMustBe(node, "base64")
  #hasOnlyOneChild(node)

  Convert.base64(text_zero_one(node))
end

#boolean(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 254

def boolean(node)
  nodeMustBe(node, "boolean")
  hasOnlyOneChild(node)

  Convert.boolean(text(node.firstChild))
end

#data(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 314

def data(node)
  nodeMustBe(node, "data")

  node.childNodes.to_a.collect do |val|
    value(val)
  end
end

#dateTime(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 281

def dateTime(node)
  nodeMustBe(node, "dateTime.iso8601")
  hasOnlyOneChild(node)

  Convert.dateTime( text(node.firstChild) )
end

#double(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 272

def double(node)
  #TODO: check string for float because to_f returnsa
  #      0.0 when wrong string
  nodeMustBe(node, "double")
  hasOnlyOneChild(node)

  Convert.double(text(node.firstChild))
end

#fault(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 363

def fault(node)
  nodeMustBe(node, "fault")
  hasOnlyOneChild(node, "value")
  f = value(node.firstChild)
  Convert.fault(f)
end

#hasOnlyOneChild(node, name = nil) (private)

Returns, when successfully the only child-node

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 215

def hasOnlyOneChild(node, name=nil)
  if node.childNodes.to_a.size != 1
    raise "wrong xml-rpc (size)"
  end
  if name != nil then
    nodeMustBe(node.firstChild, name)
  end
end

#integer(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 245

def integer(node)
  #TODO: check string for float because to_i returnsa
  #      0 when wrong string
   nodeMustBe(node, %w(i4 i8 int))
  hasOnlyOneChild(node)

  Convert.int(text(node.firstChild))
end

#member(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 295

def member(node)
  nodeMustBe(node, "member")
  assert( node.childNodes.to_a.size == 2 )

  [ name(node[0]), value(node[1]) ]
end

#methodCall(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 433

def methodCall(node)
  nodeMustBe(node, "methodCall")
  assert( (1..2).include?( node.childNodes.to_a.size ) )
  name = methodName(node[0])

  if node.childNodes.to_a.size == 2 then
    pa = params(node[1])
  else # no parameters given
    pa = []
  end
  [name, pa]
end

#methodName(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 344

def methodName(node)
  nodeMustBe(node, "methodName")
  hasOnlyOneChild(node)
  text(node.firstChild)
end

#methodResponse(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 328

def methodResponse(node)
  nodeMustBe(node, "methodResponse")
  hasOnlyOneChild(node, %w(params fault))
  child = node.firstChild

  case child.nodeName
  when "params"
    [ true, params(child,false) ]
  when "fault"
    [ false, fault(child) ]
  else
    raise "unexpected error"
  end

end

#name(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 302

def name(node)
  nodeMustBe(node, "name")
  #hasOnlyOneChild(node)
  text_zero_one(node)
end

#nodeMustBe(node, name) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 197

def nodeMustBe(node, name)
  cmp = case name
  when Array
    name.include?(node.nodeName)
  when String
    name == node.nodeName
  else
    raise "error"
  end

  if not cmp then
    raise "wrong xml-rpc (name)"
  end

  node
end

#param(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 322

def param(node)
  nodeMustBe(node, "param")
  hasOnlyOneChild(node, "value")
  value(node.firstChild)
end

#params(node, call = true) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 350

def params(node, call=true)
  nodeMustBe(node, "params")

  if call
    node.childNodes.to_a.collect do |n|
      param(n)
    end
  else # response (only one param)
    hasOnlyOneChild(node)
    param(node.firstChild)
  end
end

#parseMethodCall(str)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 161

def parseMethodCall(str)
  methodCall_document(createCleanedTree(str))
end

#parseMethodResponse(str)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 157

def parseMethodResponse(str)
  methodResponse_document(createCleanedTree(str))
end

#removeWhitespacesAndComments(node) (private)

Removes all whitespaces but in the tags i4, i8, int, boolean.… and all comments

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 169

def removeWhitespacesAndComments(node)
  remove = []
  childs = node.childNodes.to_a
  childs.each do |nd|
    case _nodeType(nd)
    when :TEXT
      # TODO: add nil?
      unless %w(i4 i8 int boolean string double dateTime.iso8601 base64).include? node.nodeName

         if node.nodeName == "value"
           if not node.childNodes.to_a.detect {|n| _nodeType(n) == :ELEMENT}.nil?
             remove << nd if nd.nodeValue.strip == ""
           end
         else
           remove << nd if nd.nodeValue.strip == ""
         end
      end
    when :COMMENT
      remove << nd
    else
      removeWhitespacesAndComments(nd)
    end
  end

  remove.each { |i| node.removeChild(i) }
end

#string(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 267

def string(node)
  nodeMustBe(node, "string")
  text_zero_one(node)
end

#struct(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 381

def struct(node)
  nodeMustBe(node, "struct")

  hash = {}
  node.childNodes.to_a.each do |me|
    n, v = member(me)
    hash[n] = v
  end

  Convert.struct(hash)
end

#text(node) (private)

_nodeType is defined in the subclass

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 373

def text(node)
  assert( _nodeType(node) == :TEXT )
  assert( node.hasChildNodes == false )
  assert( node.nodeValue != nil )

  node.nodeValue.to_s
end

#text_zero_one(node) (private)

The node node has empty string or string

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 232

def text_zero_one(node)
  nodes = node.childNodes.to_a.size

  if nodes == 1
    text(node.firstChild)
  elsif nodes == 0
    ""
  else
    raise "wrong xml-rpc (size)"
  end
end

#v_nil(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 261

def v_nil(node)
  nodeMustBe(node, "nil")
  assert( node.childNodes.to_a.size == 0 )
  nil
end

#value(node) (private)

[ GitHub ]

  
# File 'lib/xmlrpc/parser.rb', line 394

def value(node)
  nodeMustBe(node, "value")
  nodes = node.childNodes.to_a.size
  if nodes == 0
    return ""
  elsif nodes > 1
    raise "wrong xml-rpc (size)"
  end

  child = node.firstChild

  case _nodeType(child)
  when :TEXT
    text_zero_one(node)
  when :ELEMENT
    case child.nodeName
    when "i4", "i8", "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)
    when "array"            then array(child)
    when "nil"
      if Config::ENABLE_NIL_PARSER
        v_nil(child)
      else
        raise "wrong/unknown XML-RPC type 'nil'"
      end
    else
      raise "wrong/unknown XML-RPC type"
    end
  else
    raise "wrong type of node"
  end

end