1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00
ruby--ruby/lib/wsdl/soap/classDefCreator.rb
nahi eb3f829be9 * lib/{soap,wsdl,xsd}, test/{soap,wsdl,xsd}: imported soap4r/1.5.4.
== SOAP client and server ==

	  === for both client side and server side ===

	  * improved document/literal service support.
	    style(rpc,document)/use(encoding, literal) combination are all
	    supported.  for the detail about combination, see
	    test/soap/test_style.rb.

	  * let WSDLEncodedRegistry#soap2obj map SOAP/OM to Ruby according to
	    WSDL as well as obj2soap.  closes #70.

	  * let SOAP::Mapping::Object handle XML attribute for doc/lit service.
	    you can set/get XML attribute via accessor methods which as a name
	    'xmlattr_' prefixed (<foo name="bar"/> -> Foo#xmlattr_name).

	  === client side ===

	  * WSDLDriver capitalized name operation bug fixed.  from
	    1.5.3-ruby1.8.2, operation which has capitalized name (such as
	    KeywordSearchRequest in AWS) is defined as a method having
	    uncapitalized name. (converted with GenSupport.safemethodname
	    to handle operation name 'foo-bar').  it introduced serious
	    incompatibility; in the past, it was defined as a capitalized.
	    define capitalized method as well under that circumstance.

	  * added new factory interface 'WSDLDriverFactory#create_rpc_driver'
	    to create RPC::Driver, not WSDLDriver (RPC::Driver and WSDLDriver
	    are merged).  'WSDLDriverFactory#create_driver' still creates
	    WSDLDriver for compatibility but it warns that the method is
	    deprecated.  please use create_rpc_driver instead of create_driver.

	  * allow to use an URI object as an endpoint_url even with net/http,
	    not http-access2.

	  === server side ===

	  * added mod_ruby support to SOAP::CGIStub.  rename a CGI script
	    server.cgi to server.rb and let mod_ruby's RubyHandler handles the
	    script.  CGIStub detects if it's running under mod_ruby environment
	    or not.

	  * added fcgi support to SOAP::CGIStub.  see the sample at
	    sample/soap/calc/server.fcgi.  (almost same as server.cgi but has
	    fcgi handler at the bottom.)

	  * allow to return a SOAPFault object to respond customized SOAP fault.

	  * added the interface 'generate_explicit_type' for server side
	    (CGIStub, HTTPServer).  call 'self.generate_explicit_type = true'
	    if you want to return simplified XML even if it's rpc/encoded
	    service.

	  == WSDL ==

	  === WSDL definition ===

	  * improved XML Schema support such as extension, restriction,
	    simpleType, complexType + simpleContent, ref, length, import,
	    include.

	  * reduced "unknown element/attribute" warnings (warn only 1 time for
	    each QName).

	  * importing XSD file at schemaLocation with xsd:import.

	  === code generation from WSDL ===

	  * generator crashed when there's '-' in defined element/attribute
	    name.

	  * added ApacheMap WSDL definition.

	* sample/{soap,wsdl}: removed.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@8500 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-05-22 13:03:38 +00:00

272 lines
7.1 KiB
Ruby

# WSDL4R - Creating class definition from WSDL
# Copyright (C) 2002, 2003, 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>.
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can
# redistribute it and/or modify it under the same terms of Ruby's license;
# either the dual license version in 2003, or any later version.
require 'wsdl/data'
require 'wsdl/soap/classDefCreatorSupport'
require 'xsd/codegen'
module WSDL
module SOAP
class ClassDefCreator
include ClassDefCreatorSupport
def initialize(definitions)
@elements = definitions.collect_elements
@simpletypes = definitions.collect_simpletypes
@complextypes = definitions.collect_complextypes
@faulttypes = nil
if definitions.respond_to?(:collect_faulttypes)
@faulttypes = definitions.collect_faulttypes
end
end
def dump(type = nil)
result = "require 'xsd/qname'\n"
if type
result = dump_classdef(type.name, type)
else
str = dump_element
unless str.empty?
result << "\n" unless result.empty?
result << str
end
str = dump_complextype
unless str.empty?
result << "\n" unless result.empty?
result << str
end
str = dump_simpletype
unless str.empty?
result << "\n" unless result.empty?
result << str
end
end
result
end
private
def dump_element
@elements.collect { |ele|
if ele.local_complextype
dump_classdef(ele.name, ele.local_complextype)
elsif ele.local_simpletype
dump_simpletypedef(ele.name, ele.local_simpletype)
else
nil
end
}.compact.join("\n")
end
def dump_simpletype
@simpletypes.collect { |type|
dump_simpletypedef(type.name, type)
}.compact.join("\n")
end
def dump_complextype
@complextypes.collect { |type|
case type.compoundtype
when :TYPE_STRUCT, :TYPE_EMPTY
dump_classdef(type.name, type)
when :TYPE_ARRAY
dump_arraydef(type)
when :TYPE_SIMPLE
dump_simpleclassdef(type)
when :TYPE_MAP
# mapped as a general Hash
nil
else
raise RuntimeError.new(
"unknown kind of complexContent: #{type.compoundtype}")
end
}.compact.join("\n")
end
def dump_simpletypedef(qname, simpletype)
if !simpletype.restriction or simpletype.restriction.enumeration.empty?
return nil
end
c = XSD::CodeGen::ModuleDef.new(create_class_name(qname))
c.comment = "#{qname}"
const = {}
simpletype.restriction.enumeration.each do |value|
constname = safeconstname(value)
const[constname] ||= 0
if (const[constname] += 1) > 1
constname += "_#{const[constname]}"
end
c.def_const(constname, ndq(value))
end
c.dump
end
def dump_simpleclassdef(type_or_element)
qname = type_or_element.name
base = create_class_name(type_or_element.simplecontent.base)
c = XSD::CodeGen::ClassDef.new(create_class_name(qname), base)
c.comment = "#{qname}"
c.dump
end
def dump_classdef(qname, typedef)
if @faulttypes and @faulttypes.index(qname)
c = XSD::CodeGen::ClassDef.new(create_class_name(qname),
'::StandardError')
else
c = XSD::CodeGen::ClassDef.new(create_class_name(qname))
end
c.comment = "#{qname}"
c.def_classvar('schema_type', ndq(qname.name))
c.def_classvar('schema_ns', ndq(qname.namespace))
schema_element = []
init_lines = ''
params = []
typedef.each_element do |element|
if element.type == XSD::AnyTypeName
type = nil
elsif klass = element_basetype(element)
type = klass.name
elsif element.type
type = create_class_name(element.type)
else
type = nil # means anyType.
# do we define a class for local complexType from it's name?
# type = create_class_name(element.name)
# <element>
# <complexType>
# <seq...>
# </complexType>
# </element>
end
name = name_element(element).name
attrname = safemethodname?(name) ? name : safemethodname(name)
varname = safevarname(name)
c.def_attr(attrname, true, varname)
init_lines << "@#{varname} = #{varname}\n"
if element.map_as_array?
params << "#{varname} = []"
type << '[]' if type
else
params << "#{varname} = nil"
end
eleqname = (varname == name) ? nil : element.name
schema_element << [varname, eleqname, type]
end
unless typedef.attributes.empty?
define_attribute(c, typedef.attributes)
init_lines << "@__xmlattr = {}\n"
end
c.def_classvar('schema_element',
'[' +
schema_element.collect { |varname, name, type|
'[' +
(
if name
varname.dump + ', [' + ndq(type) + ', ' + dqname(name) + ']'
else
varname.dump + ', ' + ndq(type)
end
) +
']'
}.join(', ') +
']'
)
c.def_method('initialize', *params) do
init_lines
end
c.dump
end
def element_basetype(ele)
if klass = basetype_class(ele.type)
klass
elsif ele.local_simpletype
basetype_class(ele.local_simpletype.base)
else
nil
end
end
def attribute_basetype(attr)
if klass = basetype_class(attr.type)
klass
elsif attr.local_simpletype
basetype_class(attr.local_simpletype.base)
else
nil
end
end
def basetype_class(type)
return nil if type.nil?
if simpletype = @simpletypes[type]
basetype_mapped_class(simpletype.base)
else
basetype_mapped_class(type)
end
end
def define_attribute(c, attributes)
schema_attribute = []
attributes.each do |attribute|
name = name_attribute(attribute)
if klass = attribute_basetype(attribute)
type = klass.name
else
type = nil
end
methodname = safemethodname('xmlattr_' + name.name)
c.def_method(methodname) do <<-__EOD__
(@__xmlattr ||= {})[#{dqname(name)}]
__EOD__
end
c.def_method(methodname + '=', 'value') do <<-__EOD__
(@__xmlattr ||= {})[#{dqname(name)}] = value
__EOD__
end
schema_attribute << [name, type]
end
c.def_classvar('schema_attribute',
'{' +
schema_attribute.collect { |name, type|
dqname(name) + ' => ' + ndq(type)
}.join(', ') +
'}'
)
end
def name_element(element)
return element.name if element.name
return element.ref if element.ref
raise RuntimeError.new("cannot define name of #{element}")
end
def name_attribute(attribute)
return attribute.name if attribute.name
return attribute.ref if attribute.ref
raise RuntimeError.new("cannot define name of #{attribute}")
end
def dump_arraydef(complextype)
qname = complextype.name
c = XSD::CodeGen::ClassDef.new(create_class_name(qname), '::Array')
c.comment = "#{qname}"
type = complextype.child_type
c.def_classvar('schema_type', ndq(type.name))
c.def_classvar('schema_ns', ndq(type.namespace))
c.dump
end
end
end
end