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

Changeset 1779

Show
Ignore:
Timestamp:
05/17/07 10:47:54 (1 year ago)
Author:
nahi
Message:
  • allow to send XML. pass a REXML::Element(includes REXML::Document) or an object which responds to to_xml to get serialized XML. closes #21.
Files:

Legend:

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

    r1776 r1779  
    765765 
    766766 
     767class SOAPRawData < SOAPElement 
     768  def initialize(obj) 
     769    super(XSD::QName::EMPTY) 
     770    @obj = obj 
     771  end 
     772 
     773  def to_xml 
     774    @obj.to_xml 
     775  end 
     776end 
     777 
     778 
    767779class SOAPArray < XSD::NSDBase 
    768780  include SOAPCompoundtype 
  • trunk/lib/soap/generator.rb

    r1765 r1779  
    6969 
    7070  def encode_data(ns, obj, parent) 
    71     if obj.is_a?(SOAPEnvelopeElement) 
     71    if obj.respond_to?(:to_xml) 
     72      @buf << "\n#{obj.to_xml}" 
     73      return 
     74    elsif obj.is_a?(SOAPEnvelopeElement) 
    7275      encode_element(ns, obj, parent) 
    7376      return 
  • trunk/lib/soap/mapping/mapping.rb

    r1775 r1779  
    119119  end 
    120120 
     121  class REXMLElementWrap 
     122    def initialize(ele) 
     123      @ele = ele 
     124    end 
     125 
     126    def to_xml 
     127      @ele.to_s 
     128    end 
     129  end 
     130 
    121131  def self._obj2soap(obj, registry, type = nil) 
    122     if referent = Thread.current[:SOAPMapping][:MarshalKey][obj.__id__] and 
     132    if obj.respond_to?(:to_xml) 
     133      SOAPRawData.new(obj) 
     134    elsif defined?(::REXML) and obj.is_a?(::REXML::Element) 
     135      SOAPRawData.new(REXMLElementWrap.new(obj)) 
     136    elsif referent = Thread.current[:SOAPMapping][:MarshalKey][obj.__id__] and 
    123137        !Thread.current[:SOAPMapping][:NoReference] 
    124138      SOAPReference.new(referent) 
  • trunk/test/soap/asp.net/test_aspdotnet.rb

    r1724 r1779  
    6969  end 
    7070 
     71  def test_xml 
     72    @client = SOAP::RPC::Driver.new(Endpoint, Server::Namespace) 
     73    @client.wiredump_dev = STDOUT if $DEBUG 
     74    @client.add_document_method('sayHello', Server::Namespace + 'SayHello', 
     75      XSD::QName.new(Server::Namespace, 'SayHello'), 
     76      XSD::QName.new(Server::Namespace, 'SayHelloResponse')) 
     77    require 'rexml/document' 
     78    xml = <<__XML__ 
     79<n1:sayHello xmlns:n1="http://localhost/WebService/"> 
     80  <n1:name>Mike</n1:name> 
     81</n1:sayHello> 
     82__XML__ 
     83    ele = REXML::Document.new(xml) 
     84    assert_equal("Hello Mike", @client.sayHello(ele)) 
     85    def xml.to_xml; to_s; end 
     86    assert_equal("Hello Mike", @client.sayHello(xml)) 
     87  end 
     88 
    7189  def test_aspdotnethandler 
    7290    @client = SOAP::RPC::Driver.new(Endpoint, Server::Namespace) 
  • trunk/test/wsdl/document/test_rpc.rb

    r1753 r1779  
    218218  end 
    219219 
     220  def test_to_xml 
     221    @client = ::SOAP::RPC::Driver.new("http://localhost:#{Port}/") 
     222    @client.add_document_method('echo', 'urn:docrpc:echo', 
     223      XSD::QName.new('urn:docrpc', 'echoele'), 
     224      XSD::QName.new('urn:docrpc', 'echo_response')) 
     225    @client.literal_mapping_registry = EchoMappingRegistry::LiteralRegistry 
     226    @client.wiredump_dev = STDOUT if $DEBUG 
     227 
     228    require 'rexml/document' 
     229    echo = REXML::Document.new(<<__XML__.chomp) 
     230<foo attr-int="5" attr_string="attr_string"> 
     231  <struct1 m_attr="myattr1"> 
     232    <m_string>mystring1</m_string> 
     233    <m_datetime>2005-03-17T19:47:31+01:00</m_datetime> 
     234  </struct1> 
     235  <struct-2 m_attr="myattr2"> 
     236    <m_string>mystring2</m_string> 
     237    <m_datetime>2005-03-17T19:47:32+02:00</m_datetime> 
     238  </struct-2> 
     239</foo> 
     240__XML__ 
     241    ret = @client.echo(echo) 
     242    timeformat = "%Y-%m-%dT%H:%M:%S" 
     243    assert_equal('mystring2', ret.struct1.m_string) 
     244    assert_equal('2005-03-17T19:47:32', 
     245      ret.struct1.m_datetime.strftime(timeformat)) 
     246    assert_equal("mystring1", ret.struct_2.m_string) 
     247    assert_equal('2005-03-17T19:47:31', 
     248      ret.struct_2.m_datetime.strftime(timeformat)) 
     249    assert_equal('attr_string', ret.xmlattr_attr_string) 
     250    assert_equal(5, ret.xmlattr_attr_int) 
     251    # 
     252    echoele = REXML::Document.new(<<__XML__.chomp) 
     253<n1:echoele xmlns:n1="urn:docrpc"> 
     254  <struct-2> 
     255    <m_datetime>2005-03-17T19:47:32+02:00</m_datetime> 
     256    <m_string>mystring2</m_string> 
     257  </struct-2> 
     258  <struct1> 
     259    <m_datetime>2005-03-17T19:47:31+01:00</m_datetime> 
     260    <m_string>mystring1</m_string> 
     261  </struct1> 
     262</n1:echoele> 
     263__XML__ 
     264    ret = @client.echo(echoele) 
     265    timeformat = "%Y-%m-%dT%H:%M:%S" 
     266    assert_equal('mystring2', ret.struct1.m_string) 
     267    assert_equal('2005-03-17T19:47:32', 
     268      ret.struct1.m_datetime.strftime(timeformat)) 
     269    assert_equal("mystring1", ret.struct_2.m_string) 
     270    assert_equal('2005-03-17T19:47:31', 
     271      ret.struct_2.m_datetime.strftime(timeformat)) 
     272  end 
     273 
    220274  def test_nil 
    221275    @client = ::SOAP::RPC::Driver.new("http://localhost:#{Port}/")