Welcome to the "trac"-ing site of soap4r!
[soap4r] [httpclient] [openpgp4u] [pkcs1] [logger] [csv] [vtr]

Changeset 1846

Show
Ignore:
Timestamp:
06/12/07 23:55:54 (1 year ago)
Author:
nahi
Message:
  • SOAPElement.from_obj(obj): allow to set XML attribute.
    This method is called when you pass a Hash or [['key1', 'value1'], ...] to
    literal service so you can easily add XML attribute to a request.

when obj is a Hash or an Array:

when key starts with "xmlattr_":

value is added as an XML attribute with the key name however the
"xmlattr_" is dropped from the name.

when key starts with "xmlele_":

value is added as an XML element with the key name however the
"xmlele_" is dropped from the name.

if else:

value is added as an XML element with the key name.

closes #331.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/soap/baseData.rb

    r1840 r1846  
    686686  end 
    687687 
     688  # when obj is a Hash or an Array: 
     689  #   when key starts with "xmlattr_": 
     690  #     value is added as an XML attribute with the key name however the 
     691  #     "xmlattr_" is dropped from the name. 
     692  #   when key starts with "xmlele_": 
     693  #     value is added as an XML element with the key name however the 
     694  #     "xmlele_" is dropped from the name. 
     695  #   if else: 
     696  #     value is added as an XML element with the key name. 
    688697  def self.from_obj(obj, namespace = nil) 
    689698    o = SOAPElement.new(nil) 
     
    692701      o.text = nil 
    693702    when Hash, Array 
    694       obj.each do |elename, value| 
     703      obj.each do |name, value| 
     704        addname, is_attr = parse_name(name, namespace) 
    695705        if value.is_a?(Array) 
    696706          value.each do |subvalue| 
    697             child = from_obj(subvalue, namespace) 
    698             child.elename = to_elename(elename, namespace) 
     707            if is_attr 
     708              o.extraattr[addname] = subvalue 
     709            else 
     710              child = from_obj(subvalue, namespace) 
     711              child.elename = addname 
     712              o.add(child) 
     713            end 
     714          end 
     715        else 
     716          if is_attr 
     717            o.extraattr[addname] = value 
     718          else 
     719            child = from_obj(value, namespace) 
     720            child.elename = addname 
    699721            o.add(child) 
    700722          end 
    701         else 
    702           child = from_obj(value, namespace) 
    703           child.elename = to_elename(elename, namespace) 
    704           o.add(child) 
    705723        end 
    706724      end 
     
    711729  end 
    712730 
    713   def self.to_elename(obj, namespace = nil) 
     731  def self.parse_name(obj, namespace = nil) 
     732    qname = to_qname(obj, namespace) 
     733    if /\Axmlele_/ =~ qname.name 
     734      qname = XSD::QName.new(qname.namespace, qname.name.sub(/\Axmlele_/, '')) 
     735      return qname, false 
     736    elsif /\Axmlattr_/ =~ qname.name 
     737      qname = XSD::QName.new(qname.namespace, qname.name.sub(/\Axmlattr_/, '')) 
     738      return qname, true 
     739    else 
     740      return qname, false 
     741    end 
     742  end 
     743 
     744  def self.to_qname(obj, namespace = nil) 
    714745    if obj.is_a?(XSD::QName) 
    715746      obj 
  • trunk/lib/soap/soap.rb

    r1824 r1846  
    1414 
    1515 
    16 VERSION = Version = '1.5.6-SNAPSHOT
     16VERSION = Version = '1.5.6
    1717PropertyName = 'soap/property' 
    1818 
  • trunk/test/soap/test_soapelement.rb

    r1801 r1846  
    115115    assert_equal(nil, SOAPElement.from_obj(source).to_obj) 
    116116  end 
     117 
     118  def test_from_obj_xmlattr 
     119    source = { "xmlattr_c1" => "t1", 
     120      "ymlattr_c2" => { 
     121        XSD::QName.new("urn:foo", "xmlattr_c2") => "t2", 
     122        XSD::QName.new("urn:foo", "ymlattr_c3") => "t3" }} 
     123    obj = SOAPElement.from_obj(source) 
     124    assert_equal("t1", obj.extraattr[XSD::QName.new(nil, "c1")]) 
     125    assert_equal("t2", obj["ymlattr_c2"].extraattr[XSD::QName.new("urn:foo", "c2")]) 
     126    assert_equal("t3", obj["ymlattr_c2"]["ymlattr_c3"].text) 
     127    # 
     128    source = { "xmlattr_xmlattr_c1" => "t1", 
     129      "xmlele_xmlattr_c2" => { 
     130        XSD::QName.new("urn:foo", "xmlele_xmlele_c3") => "t3" }} 
     131    obj = SOAPElement.from_obj(source) 
     132    assert_equal("t1", obj.extraattr[XSD::QName.new(nil, "xmlattr_c1")]) 
     133    assert_equal("t3", obj["xmlattr_c2"]["xmlele_c3"].text) 
     134  end 
    117135end 
    118136