diff options
author | (no author) <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2004-07-03 15:38:36 +0000 |
---|---|---|
committer | (no author) <(no author)@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2004-07-03 15:38:36 +0000 |
commit | 5548c7bff292de16abb9f6e8d9cc4999e0b22c71 (patch) | |
tree | ce6b571d063d031d1b5cba3ba7671ca138405a3a /lib | |
parent | ab31bf0d4d44942e46d98d8848b788ac6df32a46 (diff) |
This commit was manufactured by cvs2svn to create branch 'ruby_1_8'.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@6568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r-- | lib/soap/header/handler.rb | 57 | ||||
-rw-r--r-- | lib/soap/header/handlerset.rb | 58 | ||||
-rw-r--r-- | lib/soap/header/simplehandler.rb | 44 | ||||
-rw-r--r-- | lib/soap/rpc/httpserver.rb | 105 | ||||
-rw-r--r-- | lib/wsdl/soap/cgiStubCreator.rb | 73 | ||||
-rw-r--r-- | lib/wsdl/soap/classDefCreator.rb | 112 | ||||
-rw-r--r-- | lib/wsdl/soap/classDefCreatorSupport.rb | 106 | ||||
-rw-r--r-- | lib/wsdl/soap/clientSkeltonCreator.rb | 78 | ||||
-rw-r--r-- | lib/wsdl/soap/driverCreator.rb | 84 | ||||
-rw-r--r-- | lib/wsdl/soap/mappingRegistryCreator.rb | 90 | ||||
-rw-r--r-- | lib/wsdl/soap/methodDefCreator.rb | 148 | ||||
-rw-r--r-- | lib/wsdl/soap/servantSkeltonCreator.rb | 65 | ||||
-rw-r--r-- | lib/wsdl/soap/standaloneServerStubCreator.rb | 79 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/enumeration.rb | 36 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/simpleRestriction.rb | 48 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/simpleType.rb | 81 | ||||
-rw-r--r-- | lib/xsd/codegen.rb | 12 | ||||
-rw-r--r-- | lib/xsd/codegen/classdef.rb | 203 | ||||
-rw-r--r-- | lib/xsd/codegen/commentdef.rb | 34 | ||||
-rw-r--r-- | lib/xsd/codegen/gensupport.rb | 112 | ||||
-rw-r--r-- | lib/xsd/codegen/methoddef.rb | 63 | ||||
-rw-r--r-- | lib/xsd/codegen/moduledef.rb | 191 |
22 files changed, 1879 insertions, 0 deletions
diff --git a/lib/soap/header/handler.rb b/lib/soap/header/handler.rb new file mode 100644 index 0000000000..7da2836e24 --- /dev/null +++ b/lib/soap/header/handler.rb @@ -0,0 +1,57 @@ +# SOAP4R - SOAP Header handler item +# Copyright (C) 2003, 2003 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 'soap/element' + + +module SOAP +module Header + + +class Handler + attr_reader :elename + attr_reader :mustunderstand + attr_reader :encodingstyle + + def initialize(elename) + @elename = elename + @mustunderstand = false + @encodingstyle = nil + end + + # Should return a SOAP/OM, a SOAPHeaderItem or nil. + def on_outbound + nil + end + + # Given header is a SOAPHeaderItem or nil. + def on_inbound(header, mustunderstand = false) + # do something. + end + + def on_outbound_headeritem + item = on_outbound + if item.nil? + nil + elsif item.is_a?(::SOAP::SOAPHeaderItem) + item.elename = @elename + item + else + item.elename = @elename + ::SOAP::SOAPHeaderItem.new(item, @mustunderstand, @encodingstyle) + end + end + + def on_inbound_headeritem(header) + on_inbound(header.element, header.mustunderstand) + end +end + + +end +end diff --git a/lib/soap/header/handlerset.rb b/lib/soap/header/handlerset.rb new file mode 100644 index 0000000000..499d6bb8a1 --- /dev/null +++ b/lib/soap/header/handlerset.rb @@ -0,0 +1,58 @@ +# SOAP4R - SOAP Header handler set +# Copyright (C) 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 'xsd/namedelements' + + +module SOAP +module Header + + +class HandlerSet + def initialize + @store = XSD::NamedElements.new + end + + def add(handler) + @store << handler + end + alias << add + + def delete(handler) + @store.delete(handler) + end + + def include?(handler) + @store.include?(handler) + end + + # returns: Array of SOAPHeaderItem + def on_outbound + @store.collect { |handler| + handler.on_outbound_headeritem + }.compact + end + + # headers: SOAPHeaderItem enumerable object + def on_inbound(headers) + headers.each do |name, item| + handler = @store.find { |handler| + handler.elename == item.element.elename + } + if handler + handler.on_inbound_headeritem(item) + elsif item.mustunderstand + raise UnhandledMustUnderstandHeaderError.new(item.element.elename.to_s) + end + end + end +end + + +end +end diff --git a/lib/soap/header/simplehandler.rb b/lib/soap/header/simplehandler.rb new file mode 100644 index 0000000000..e7268e04a3 --- /dev/null +++ b/lib/soap/header/simplehandler.rb @@ -0,0 +1,44 @@ +# SOAP4R - SOAP Simple header item handler +# Copyright (C) 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 'soap/header/handler' +require 'soap/baseData' + + +module SOAP +module Header + + +class SimpleHandler < SOAP::Header::Handler + def initialize(elename) + super(elename) + end + + # Should return a Hash or nil. + def on_simple_outbound + nil + end + + # Given header is a Hash or nil. + def on_simple_inbound(header, mustunderstand) + end + + def on_outbound + h = on_simple_outbound + h ? SOAPElement.from_obj(h) : nil + end + + def on_inbound(header, mustunderstand) + h = header.to_obj + on_simple_inbound(h, mustunderstand) + end +end + + +end +end diff --git a/lib/soap/rpc/httpserver.rb b/lib/soap/rpc/httpserver.rb new file mode 100644 index 0000000000..7b1f961d9e --- /dev/null +++ b/lib/soap/rpc/httpserver.rb @@ -0,0 +1,105 @@ +# SOAP4R - WEBrick HTTP Server +# Copyright (C) 2003, 2004 by 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 'logger' +require 'soap/rpc/soaplet' +require 'soap/streamHandler' +require 'webrick' + + +module SOAP +module RPC + + +class HTTPServer < Logger::Application + attr_reader :server + attr_accessor :default_namespace + + def initialize(config) + super(config[:SOAPHTTPServerApplicationName] || self.class.name) + @default_namespace = config[:SOAPDefaultNamespace] + @webrick_config = config.dup + @webrick_config[:Logger] ||= @log + @server = nil + @soaplet = ::SOAP::RPC::SOAPlet.new + self.level = Logger::Severity::INFO + on_init + end + + def on_init + # define extra methods in derived class. + end + + def status + if @server + @server.status + else + nil + end + end + + def shutdown + @server.shutdown if @server + end + + def mapping_registry + @soaplet.app_scope_router.mapping_registry + end + + def mapping_registry=(mapping_registry) + @soaplet.app_scope_router.mapping_registry = mapping_registry + end + + def add_rpc_request_servant(factory, namespace = @default_namespace, + mapping_registry = nil) + @soaplet.add_rpc_request_servant(factory, namespace, mapping_registry) + end + + def add_rpc_servant(obj, namespace = @default_namespace) + @soaplet.add_rpc_servant(obj, namespace) + end + + def add_rpc_request_headerhandler(factory) + @soaplet.add_rpc_request_headerhandler(factory) + end + + def add_rpc_headerhandler(obj) + @soaplet.add_rpc_headerhandler(obj) + end + + def add_method(obj, name, *param) + add_method_as(obj, name, name, *param) + end + + def add_method_as(obj, name, name_as, *param) + qname = XSD::QName.new(@default_namespace, name_as) + soapaction = nil + method = obj.method(name) + param_def = if param.size == 1 and param[0].is_a?(Array) + param[0] + elsif param.empty? + ::SOAP::RPC::SOAPMethod.create_param_def( + (1..method.arity.abs).collect { |i| "p#{ i }" }) + else + SOAP::RPC::SOAPMethod.create_param_def(param) + end + @soaplet.app_scope_router.add_method(obj, qname, soapaction, name, param_def) + end + +private + + def run + @server = WEBrick::HTTPServer.new(@webrick_config) + @server.mount('/', @soaplet) + @server.start + end +end + + +end +end diff --git a/lib/wsdl/soap/cgiStubCreator.rb b/lib/wsdl/soap/cgiStubCreator.rb new file mode 100644 index 0000000000..e5b64336e7 --- /dev/null +++ b/lib/wsdl/soap/cgiStubCreator.rb @@ -0,0 +1,73 @@ +# WSDL4R - Creating CGI stub code from WSDL. +# Copyright (C) 2002, 2003 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/info' +require 'wsdl/soap/mappingRegistryCreator' +require 'wsdl/soap/methodDefCreator' +require 'wsdl/soap/classDefCreatorSupport' + + +module WSDL +module SOAP + + +class CGIStubCreator + include ClassDefCreatorSupport + + attr_reader :definitions + + def initialize(definitions) + @definitions = definitions + end + + def dump(service_name) + STDERR.puts "!!! IMPORTANT !!!" + STDERR.puts "- CGI stub can only 1 port. Creating stub for the first port... Rests are ignored." + STDERR.puts "!!! IMPORTANT !!!" + port = @definitions.service(service_name).ports[0] + dump_porttype(port.porttype.name) + end + +private + + def dump_porttype(name) + class_name = create_class_name(name) + methoddef, types = MethodDefCreator.new(@definitions).dump(name) + mr_creator = MappingRegistryCreator.new(@definitions) + c1 = ::XSD::CodeGen::ClassDef.new(class_name) + c1.def_require("soap/rpc/cgistub") + c1.def_require("soap/mapping/registry") + c1.def_const("MappingRegistry", "::SOAP::Mapping::Registry.new") + c1.def_code(mr_creator.dump(types)) + c1.def_code <<-EOD +Methods = [ +#{ methoddef.gsub(/^/, " ") } +] + EOD + c2 = ::XSD::CodeGen::ClassDef.new(class_name + "App", + "::SOAP::RPC::CGIStub") + c2.def_method("initialize", "*arg") do + <<-EOD + super(*arg) + servant = #{class_name}.new + #{class_name}::Methods.each do |name_as, name, params, soapaction, ns| + add_method_with_namespace_as(ns, servant, name, name_as, params, soapaction) + end + self.mapping_registry = #{class_name}::MappingRegistry + self.level = Logger::Severity::ERROR + EOD + end + c1.dump + "\n" + c2.dump + format(<<-EOD) + #{class_name}App.new('app', nil).start + EOD + end +end + + +end +end diff --git a/lib/wsdl/soap/classDefCreator.rb b/lib/wsdl/soap/classDefCreator.rb new file mode 100644 index 0000000000..6c7d381932 --- /dev/null +++ b/lib/wsdl/soap/classDefCreator.rb @@ -0,0 +1,112 @@ +# WSDL4R - Creating class definition from WSDL +# Copyright (C) 2002, 2003 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) + @simpletypes = definitions.collect_simpletypes + @complextypes = definitions.collect_complextypes + @faulttypes = definitions.collect_faulttypes + end + + def dump(class_name = nil) + result = "" + if class_name + result = dump_classdef(class_name) + else + @complextypes.each do |type| + case type.compoundtype + when :TYPE_STRUCT + result << dump_classdef(type) + when :TYPE_ARRAY + result << dump_arraydef(type) + else + raise RuntimeError.new("Unknown complexContent definition...") + end + result << "\n" + end + + result << @simpletypes.collect { |type| + dump_simpletypedef(type) + }.join("\n") + end + result + end + +private + + def dump_simpletypedef(simpletype) + qname = simpletype.name + if simpletype.restriction.enumeration.empty? + STDERR.puts("#{qname}: simpleType which is not enum type not supported.") + return "" + end + c = XSD::CodeGen::ModuleDef.new(create_class_name(qname)) + c.comment = "#{ qname.namespace }" + simpletype.restriction.enumeration.each do |value| + c.def_const(safeconstname(value), value.dump) + end + c.dump + end + + def dump_classdef(complextype) + qname = complextype.name + if @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.namespace }" + c.def_classvar("schema_type", qname.name.dump) + c.def_classvar("schema_ns", qname.namespace.dump) + init_lines = "" + params = [] + complextype.each_element do |element| + name = element.name.name + varname = safevarname(name) + c.def_attr(name, true, varname) + init_lines << "@#{ varname } = #{ varname }\n" + params << "#{ varname } = nil" + end + complextype.attributes.each do |attribute| + name = "attr_" + attribute.name + varname = safevarname(name) + c.def_attr(name, true, varname) + init_lines << "@#{ varname } = #{ varname }\n" + params << "#{ varname } = nil" + end + c.def_method("initialize", *params) do + init_lines + end + c.dump + end + + def dump_arraydef(complextype) + qname = complextype.name + c = XSD::CodeGen::ClassDef.new(create_class_name(qname), "::Array") + c.comment = "#{ qname.namespace }" + c.def_classvar("schema_type", qname.name.dump) + c.def_classvar("schema_ns", qname.namespace.dump) + c.dump + end +end + + +end +end diff --git a/lib/wsdl/soap/classDefCreatorSupport.rb b/lib/wsdl/soap/classDefCreatorSupport.rb new file mode 100644 index 0000000000..dbcc55f7b9 --- /dev/null +++ b/lib/wsdl/soap/classDefCreatorSupport.rb @@ -0,0 +1,106 @@ +# WSDL4R - Creating class code support from WSDL. +# Copyright (C) 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/info' +require 'soap/mapping' +require 'soap/mapping/typeMap' +require 'xsd/codegen/gensupport' + + +module WSDL +module SOAP + + +module ClassDefCreatorSupport + include XSD::CodeGen::GenSupport + + def create_class_name(qname) + if klass = basetype_mapped_class(qname) + ::SOAP::Mapping::DefaultRegistry.find_mapped_obj_class(klass.name) + else + safeconstname(qname.name) + end + end + + def basetype_mapped_class(name) + ::SOAP::TypeMap[name] + end + + def dump_method_signature(operation) + name = operation.name.name + input = operation.input + output = operation.output + fault = operation.fault + signature = "#{ name }#{ dump_inputparam(input) }" + str = <<__EOD__ +# SYNOPSIS +# #{name}#{dump_inputparam(input)} +# +# ARGS +#{dump_inout_type(input).chomp} +# +# RETURNS +#{dump_inout_type(output).chomp} +# +__EOD__ + unless fault.empty? + faultstr = (fault.collect { |f| dump_inout_type(f).chomp }).join(', ') + str <<<<__EOD__ +# RAISES +# #{faultstr} +# +__EOD__ + end + str + end + +private + + def dump_inout_type(param) + if param + message = param.find_message + params = "" + message.parts.each do |part| + next unless part.type + name = safevarname(part.name) + typename = safeconstname(part.type.name) + params << add_at("# #{name}", "#{typename} - #{part.type}\n", 20) + end + unless params.empty? + return params + end + end + "# N/A\n" + end + + def dump_inputparam(input) + message = input.find_message + params = "" + message.parts.each do |part| + params << ", " unless params.empty? + params << safevarname(part.name) + end + if params.empty? + "" + else + "(#{ params })" + end + end + + def add_at(base, str, pos) + if base.size >= pos + base + ' ' + str + else + base + ' ' * (pos - base.size) + str + end + end +end + + +end +end diff --git a/lib/wsdl/soap/clientSkeltonCreator.rb b/lib/wsdl/soap/clientSkeltonCreator.rb new file mode 100644 index 0000000000..9c538dd612 --- /dev/null +++ b/lib/wsdl/soap/clientSkeltonCreator.rb @@ -0,0 +1,78 @@ +# WSDL4R - Creating client skelton code from WSDL. +# Copyright (C) 2002, 2003 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/info' +require 'wsdl/soap/classDefCreatorSupport' + + +module WSDL +module SOAP + + +class ClientSkeltonCreator + include ClassDefCreatorSupport + + attr_reader :definitions + + def initialize(definitions) + @definitions = definitions + end + + def dump(service_name) + result = "" + @definitions.service(service_name).ports.each do |port| + result << dump_porttype(port.porttype.name) + result << "\n" + end + result + end + +private + + def dump_porttype(name) + drv_name = create_class_name(name) + + result = "" + result << <<__EOD__ +endpoint_url = ARGV.shift +obj = #{ drv_name }.new(endpoint_url) + +# Uncomment the below line to see SOAP wiredumps. +# obj.wiredump_dev = STDERR + +__EOD__ + @definitions.porttype(name).operations.each do |operation| + result << dump_method_signature(operation) + result << dump_input_init(operation.input) << "\n" + result << dump_operation(operation) << "\n\n" + end + result + end + + def dump_operation(operation) + name = operation.name + input = operation.input + "puts obj.#{ safemethodname(name.name) }#{ dump_inputparam(input) }" + end + + def dump_input_init(input) + result = input.find_message.parts.collect { |part| + "#{ uncapitalize(part.name) }" + }.join(" = ") + if result.empty? + "" + else + result << " = nil" + end + result + end +end + + +end +end diff --git a/lib/wsdl/soap/driverCreator.rb b/lib/wsdl/soap/driverCreator.rb new file mode 100644 index 0000000000..50be8ed1dc --- /dev/null +++ b/lib/wsdl/soap/driverCreator.rb @@ -0,0 +1,84 @@ +# WSDL4R - Creating driver code from WSDL. +# Copyright (C) 2002, 2003 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/info' +require 'wsdl/soap/mappingRegistryCreator' +require 'wsdl/soap/methodDefCreator' +require 'wsdl/soap/classDefCreatorSupport' +require 'xsd/codegen' + + +module WSDL +module SOAP + + +class DriverCreator + include ClassDefCreatorSupport + + attr_reader :definitions + + def initialize(definitions) + @definitions = definitions + end + + def dump(porttype = nil) + if porttype.nil? + result = "" + @definitions.porttypes.each do |type| + result << dump_porttype(type.name) + result << "\n" + end + else + result = dump_porttype(porttype) + end + result + end + +private + + def dump_porttype(name) + class_name = create_class_name(name) + methoddef, types = MethodDefCreator.new(@definitions).dump(name) + mr_creator = MappingRegistryCreator.new(@definitions) + binding = @definitions.bindings.find { |item| item.type == name } + addresses = @definitions.porttype(name).locations + + c = ::XSD::CodeGen::ClassDef.new(class_name, "::SOAP::RPC::Driver") + c.def_require("soap/rpc/driver") + c.def_const("MappingRegistry", "::SOAP::Mapping::Registry.new") + c.def_const("DefaultEndpointUrl", addresses[0].dump) + c.def_code(mr_creator.dump(types)) + c.def_code <<-EOD +Methods = [ +#{ methoddef.gsub(/^/, " ") } +] + EOD + c.def_method("initialize", "endpoint_url = nil") do + <<-EOD + endpoint_url ||= DefaultEndpointUrl + super(endpoint_url, nil) + self.mapping_registry = MappingRegistry + init_methods + EOD + end + c.def_privatemethod("init_methods") do + <<-EOD + Methods.each do |name_as, name, params, soapaction, namespace| + qname = ::XSD::QName.new(namespace, name_as) + @proxy.add_method(qname, soapaction, name, params) + add_rpc_method_interface(name, params) + end + EOD + end + c.dump + end +end + + +end +end diff --git a/lib/wsdl/soap/mappingRegistryCreator.rb b/lib/wsdl/soap/mappingRegistryCreator.rb new file mode 100644 index 0000000000..d3b28f47e0 --- /dev/null +++ b/lib/wsdl/soap/mappingRegistryCreator.rb @@ -0,0 +1,90 @@ +# WSDL4R - Creating MappingRegistry code from WSDL. +# Copyright (C) 2002, 2003 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/info' +require 'wsdl/soap/classDefCreatorSupport' + + +module WSDL +module SOAP + + +class MappingRegistryCreator + include ClassDefCreatorSupport + + attr_reader :definitions + + def initialize(definitions) + @definitions = definitions + @complextypes = @definitions.collect_complextypes + @types = nil + end + + def dump(types) + @types = types + map_cache = [] + map = "" + @types.each do |type| + if map_cache.index(type).nil? + map_cache << type + if type.namespace != XSD::Namespace + if typemap = dump_typemap(type) + map << typemap + end + end + end + end + return map + end + +private + + def dump_typemap(type) + if definedtype = @complextypes[type] + case definedtype.compoundtype + when :TYPE_STRUCT + dump_struct_typemap(definedtype) + when :TYPE_ARRAY + dump_array_typemap(definedtype) + else + raise NotImplementedError.new("Must not reach here.") + end + end + end + + def dump_struct_typemap(definedtype) + ele = definedtype.name + return <<__EOD__ +MappingRegistry.set( + #{ create_class_name(ele) }, + ::SOAP::SOAPStruct, + ::SOAP::Mapping::Registry::TypedStructFactory, + { :type => ::XSD::QName.new("#{ ele.namespace }", "#{ ele.name }") } +) +__EOD__ + end + + def dump_array_typemap(definedtype) + ele = definedtype.name + arytype = definedtype.find_arytype || XSD::AnyTypeName + type = XSD::QName.new(arytype.namespace, arytype.name.sub(/\[(?:,)*\]$/, '')) + @types << type + return <<__EOD__ +MappingRegistry.set( + #{ create_class_name(ele) }, + ::SOAP::SOAPArray, + ::SOAP::Mapping::Registry::TypedArrayFactory, + { :type => ::XSD::QName.new("#{ type.namespace }", "#{ type.name }") } +) +__EOD__ + end +end + + +end +end diff --git a/lib/wsdl/soap/methodDefCreator.rb b/lib/wsdl/soap/methodDefCreator.rb new file mode 100644 index 0000000000..eded972cdc --- /dev/null +++ b/lib/wsdl/soap/methodDefCreator.rb @@ -0,0 +1,148 @@ +# WSDL4R - Creating driver code from WSDL. +# Copyright (C) 2002, 2003 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/info' +require 'wsdl/soap/classDefCreatorSupport' + + +module WSDL +module SOAP + + +class MethodDefCreator + include ClassDefCreatorSupport + + attr_reader :definitions + + def initialize(definitions) + @definitions = definitions + @simpletypes = @definitions.collect_simpletypes + @complextypes = @definitions.collect_complextypes + @elements = @definitions.collect_elements + @types = nil + end + + def dump(porttype) + @types = [] + result = "" + operations = @definitions.porttype(porttype).operations + binding = @definitions.porttype_binding(porttype) + operations.each do |operation| + op_bind = binding.operations[operation.name] + result << ",\n" unless result.empty? + result << dump_method(operation, op_bind).chomp + end + return result, @types + end + +private + + def dump_method(operation, binding) + name = safemethodname(operation.name.name) + name_as = operation.name.name + params = collect_parameter(operation) + soapaction = binding.soapoperation.soapaction + namespace = binding.input.soapbody.namespace + paramstr = param2str(params) + if paramstr.empty? + paramstr = '[]' + else + paramstr = "[\n" << paramstr.gsub(/^/, ' ') << "\n ]" + end + return <<__EOD__ +[#{ dq(name_as) }, #{ dq(name) }, + #{ paramstr }, + #{ soapaction ? dq(soapaction) : "nil" }, #{ dq(namespace) } +] +__EOD__ + end + + def collect_parameter(operation) + result = operation.inputparts.collect { |part| + collect_type(part.type) + param_set('in', definedtype(part), part.name) + } + outparts = operation.outputparts + if outparts.size > 0 + retval = outparts[0] + collect_type(retval.type) + result << param_set('retval', definedtype(retval), retval.name) + cdr(outparts).each { |part| + collect_type(part.type) + result << param_set('out', definedtype(part), part.name) + } + end + result + end + + def definedtype(part) + if mapped = basetype_mapped_class(part.type) + ['::' + mapped.name] + elsif definedelement = @elements[part.element] + raise RuntimeError.new("Part: #{part.name} should be typed for RPC service for now.") + elsif definedtype = @simpletypes[part.type] + ['::' + basetype_mapped_class(definedtype.base).name] + elsif definedtype = @complextypes[part.type] + case definedtype.compoundtype + when :TYPE_STRUCT + ['::SOAP::SOAPStruct', part.type.namespace, part.type.name] + when :TYPE_ARRAY + arytype = definedtype.find_arytype || XSD::AnyTypeName + ns = arytype.namespace + name = arytype.name.sub(/\[(?:,)*\]$/, '') + ['::SOAP::SOAPArray', ns, name] + else + raise NotImplementedError.new("Must not reach here.") + end + else + raise RuntimeError.new("Part: #{part.name} cannot be resolved.") + end + end + + def param_set(io_type, type, name) + [io_type, type, name] + end + + def collect_type(type) + # ignore inline type definition. + return if type.nil? + @types << type + return unless @complextypes[type] + @complextypes[type].each_element do |element| + collect_type(element.type) + end + end + + def param2str(params) + params.collect { |param| + "[#{ dq(param[0]) }, #{ dq(param[2]) }, #{ type2str(param[1]) }]" + }.join(",\n") + end + + def type2str(type) + if type.size == 1 + "[#{ type[0] }]" + else + "[#{ type[0] }, #{ dq(type[1]) }, #{ dq(type[2]) }]" + end + end + + def dq(ele) + "\"" << ele << "\"" + end + + def cdr(ary) + result = ary.dup + result.shift + result + end +end + + +end +end diff --git a/lib/wsdl/soap/servantSkeltonCreator.rb b/lib/wsdl/soap/servantSkeltonCreator.rb new file mode 100644 index 0000000000..bf293949b8 --- /dev/null +++ b/lib/wsdl/soap/servantSkeltonCreator.rb @@ -0,0 +1,65 @@ +# WSDL4R - Creating servant skelton code from WSDL. +# Copyright (C) 2002, 2003 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/info' +require 'wsdl/soap/classDefCreatorSupport' +require 'xsd/codegen' + + +module WSDL +module SOAP + + +class ServantSkeltonCreator + include ClassDefCreatorSupport + include ::XSD::CodeGen::GenSupport + + attr_reader :definitions + + def initialize(definitions) + @definitions = definitions + end + + def dump(porttype = nil) + if porttype.nil? + result = "" + @definitions.porttypes.each do |type| + result << dump_porttype(type.name) + result << "\n" + end + else + result = dump_porttype(porttype) + end + result + end + +private + + def dump_porttype(name) + class_name = create_class_name(name) + c = ::XSD::CodeGen::ClassDef.new(class_name) + operations = @definitions.porttype(name).operations + operations.each do |operation| + name = operation.name.name + input = operation.input + m = ::XSD::CodeGen::MethodDef.new(name, + input.find_message.parts.collect { |part| safevarname(part.name) }) do + <<-EOD + raise NotImplementedError.new + EOD + end + m.comment = dump_method_signature(operation) + c.add_method(m) + end + c.dump + end +end + + +end +end diff --git a/lib/wsdl/soap/standaloneServerStubCreator.rb b/lib/wsdl/soap/standaloneServerStubCreator.rb new file mode 100644 index 0000000000..34bcfdbba9 --- /dev/null +++ b/lib/wsdl/soap/standaloneServerStubCreator.rb @@ -0,0 +1,79 @@ +# WSDL4R - Creating standalone server stub code from WSDL. +# Copyright (C) 2002, 2003 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/info' +require 'wsdl/soap/mappingRegistryCreator' +require 'wsdl/soap/methodDefCreator' +require 'wsdl/soap/classDefCreatorSupport' + + +module WSDL +module SOAP + + +class StandaloneServerStubCreator + include ClassDefCreatorSupport + + attr_reader :definitions + + def initialize(definitions) + @definitions = definitions + end + + def dump(service_name) + STDERR.puts "!!! IMPORTANT !!!" + STDERR.puts "- Standalone stub can have only 1 port for now. So creating stub for the first port and rests are ignored." + STDERR.puts "- Standalone server stub ignores port location defined in WSDL. Location is http://localhost:10080/ by default. Generated client from WSDL must be configured to point this endpoint by hand." + STDERR.puts "!!! IMPORTANT !!!" + port = @definitions.service(service_name).ports[0] + dump_porttype(port.porttype.name) + end + +private + + def dump_porttype(name) + class_name = create_class_name(name) + methoddef, types = MethodDefCreator.new(@definitions).dump(name) + mr_creator = MappingRegistryCreator.new(@definitions) + + c1 = ::XSD::CodeGen::ClassDef.new(class_name) + c1.def_require("soap/rpc/standaloneServer") + c1.def_require("soap/mapping/registry") + c1.def_const("MappingRegistry", "::SOAP::Mapping::Registry.new") + c1.def_code(mr_creator.dump(types)) + c1.def_code <<-EOD +Methods = [ +#{ methoddef.gsub(/^/, " ") } +] + EOD + c2 = ::XSD::CodeGen::ClassDef.new(class_name + "App", + "::SOAP::RPC::StandaloneServer") + c2.def_method("initialize", "*arg") do + <<-EOD + super(*arg) + servant = #{class_name}.new + #{class_name}::Methods.each do |name_as, name, params, soapaction, ns| + qname = XSD::QName.new(ns, name_as) + @soaplet.app_scope_router.add_method(servant, qname, soapaction, name, params) + end + self.mapping_registry = #{class_name}::MappingRegistry + EOD + end + c1.dump + "\n" + c2.dump + format(<<-EOD) + + if $0 == __FILE__ + # Change listen port. + #{class_name}App.new('app', nil, '0.0.0.0', 10080).start + end + EOD + end +end + + +end +end diff --git a/lib/wsdl/xmlSchema/enumeration.rb b/lib/wsdl/xmlSchema/enumeration.rb new file mode 100644 index 0000000000..cd61572d07 --- /dev/null +++ b/lib/wsdl/xmlSchema/enumeration.rb @@ -0,0 +1,36 @@ +# WSDL4R - XMLSchema enumeration definition for WSDL. +# Copyright (C) 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/info' + + +module WSDL +module XMLSchema + + +class Enumeration < Info + def initialize + super + end + + def parse_element(element) + nil + end + + def parse_attr(attr, value) + case attr + when ValueAttrName + parent.enumeration << value + value + end + end +end + + +end +end diff --git a/lib/wsdl/xmlSchema/simpleRestriction.rb b/lib/wsdl/xmlSchema/simpleRestriction.rb new file mode 100644 index 0000000000..6986e74423 --- /dev/null +++ b/lib/wsdl/xmlSchema/simpleRestriction.rb @@ -0,0 +1,48 @@ +# WSDL4R - XMLSchema simpleType definition for WSDL. +# Copyright (C) 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/info' +require 'xsd/namedelements' + + +module WSDL +module XMLSchema + + +class SimpleRestriction < Info + attr_reader :base + attr_reader :enumeration + + def initialize + super + @base = nil + @enumeration = [] # NamedElements? + end + + def valid?(value) + @enumeration.include?(value) + end + + def parse_element(element) + case element + when EnumerationName + Enumeration.new # just a parsing handler + end + end + + def parse_attr(attr, value) + case attr + when BaseAttrName + @base = value + end + end +end + + +end +end diff --git a/lib/wsdl/xmlSchema/simpleType.rb b/lib/wsdl/xmlSchema/simpleType.rb new file mode 100644 index 0000000000..830086f99e --- /dev/null +++ b/lib/wsdl/xmlSchema/simpleType.rb @@ -0,0 +1,81 @@ +# WSDL4R - XMLSchema simpleType definition for WSDL. +# Copyright (C) 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/info' +require 'xsd/namedelements' + + +module WSDL +module XMLSchema + + +class SimpleType < Info + attr_accessor :name + attr_reader :derivetype + attr_reader :restriction + + def check_lexical_format(value) + if @restriction + check_restriction(value) + elsif @extension + raise NotImplementedError + # ToDo + else + raise ArgumentError.new("incomplete simpleType") + end + end + + def base + if @restriction + @restriction.base + elsif @extension + @extension.base + else + raise ArgumentError.new("incomplete simpleType") + end + end + + def initialize(name = nil) + super() + @name = name + @derivetype = nil + @restriction = nil + end + + def targetnamespace + parent.targetnamespace + end + + def parse_element(element) + case element + when RestrictionName + @restriction = SimpleRestriction.new + @derivetype = element.name + @restriction + end + end + + def parse_attr(attr, value) + case attr + when NameAttrName + @name = XSD::QName.new(targetnamespace, value) + end + end + +private + + def check_restriction(value) + unless @restriction.valid?(value) + raise ::XSD::ValueSpaceError.new("#{@name}: cannot accept '#{value}'.") + end + end +end + + +end +end diff --git a/lib/xsd/codegen.rb b/lib/xsd/codegen.rb new file mode 100644 index 0000000000..d820ebf1f2 --- /dev/null +++ b/lib/xsd/codegen.rb @@ -0,0 +1,12 @@ +# XSD4R - Generating code library +# Copyright (C) 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 'xsd/codegen/gensupport' +require 'xsd/codegen/moduledef' +require 'xsd/codegen/classdef' +require 'xsd/codegen/methoddef' diff --git a/lib/xsd/codegen/classdef.rb b/lib/xsd/codegen/classdef.rb new file mode 100644 index 0000000000..8f72e95efd --- /dev/null +++ b/lib/xsd/codegen/classdef.rb @@ -0,0 +1,203 @@ +# XSD4R - Generating class definition code +# Copyright (C) 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 'xsd/codegen/gensupport' +require 'xsd/codegen/moduledef' +require 'xsd/codegen/methoddef' + + +module XSD +module CodeGen + + +class ClassDef < ModuleDef + include GenSupport + + def initialize(name, baseclass = nil) + super(name) + @baseclass = baseclass + @classvar = [] + @attrdef = [] + end + + def def_classvar(var, value) + var = var.sub(/\A@@/, "") + unless safevarname?(var) + raise ArgumentError.new("#{var} seems to be unsafe") + end + @classvar << [var, value] + end + + def def_attr(attrname, writable = true, varname = nil) + unless safevarname?(varname || attrname) + raise ArgumentError.new("#{varname || attrname} seems to be unsafe") + end + @attrdef << [attrname, writable, varname] + end + + def dump + buf = "" + unless @requirepath.empty? + buf << dump_requirepath + end + buf << dump_emptyline unless buf.empty? + package = @name.split(/::/)[0..-2] + buf << dump_package_def(package) unless package.empty? + buf << dump_comment if @comment + buf << dump_class_def + spacer = false + unless @classvar.empty? + spacer = true + buf << dump_classvar + end + unless @const.empty? + buf << dump_emptyline if spacer + spacer = true + buf << dump_const + end + unless @code.empty? + buf << dump_emptyline if spacer + spacer = true + buf << dump_code + end + unless @attrdef.empty? + buf << dump_emptyline if spacer + spacer = true + buf << dump_attributes + end + unless @methoddef.empty? + buf << dump_emptyline if spacer + spacer = true + buf << dump_methods + end + buf << dump_class_def_end + buf << dump_package_def_end(package) unless package.empty? + buf + end + +private + + def dump_class_def + name = @name.to_s.split(/::/) + if @baseclass + format("class #{name.last} < #{@baseclass}") + else + format("class #{name.last}") + end + end + + def dump_class_def_end + str = format("end") + end + + def dump_classvar + dump_static( + @classvar.collect { |var, value| + %Q(@@#{var.sub(/^@@/, "")} = #{dump_value(value)}) + }.join("\n") + ) + end + + def dump_attributes + str = "" + @attrdef.each do |attrname, writable, varname| + varname ||= attrname + if attrname == varname + str << format(dump_accessor(attrname, writable), 2) + end + end + @attrdef.each do |attrname, writable, varname| + varname ||= attrname + if attrname != varname + str << "\n" unless str.empty? + str << format(dump_attribute(attrname, writable, varname), 2) + end + end + str + end + + def dump_accessor(attrname, writable) + if writable + "attr_accessor :#{attrname}" + else + "attr_reader :#{attrname}" + end + end + + def dump_attribute(attrname, writable, varname) + str = nil + mr = MethodDef.new(attrname) + mr.definition = "@#{varname}" + str = mr.dump + if writable + mw = MethodDef.new(attrname + "=", 'value') + mw.definition = "@#{varname} = value" + str << "\n" + mw.dump + end + str + end +end + + +end +end + + +if __FILE__ == $0 + require 'xsd/codegen/classdef' + include XSD::CodeGen + c = ClassDef.new("Foo::Bar::HobbitName", String) + c.def_require("foo/bar") + c.comment = <<-EOD + foo + bar + baz + EOD + c.def_const("FOO", 1) + c.def_classvar("@@foo", "var".dump) + c.def_classvar("baz", "1".dump) + c.def_attr("Foo", true, "foo") + c.def_attr("bar") + c.def_attr("baz", true) + c.def_attr("Foo2", true, "foo2") + c.def_attr("foo3", false, "foo3") + c.def_method("foo") do + <<-EOD + foo.bar = 1 +\tbaz.each do |ele| +\t ele + end + EOD + end + c.def_method("baz", "qux") do + <<-EOD + [1, 2, 3].each do |i| + p i + end + EOD + end + + m = MethodDef.new("qux", "quxx", "quxxx") do + <<-EOD + p quxx + quxxx + EOD + end + m.comment = "hello world\n123" + c.add_method(m) + c.def_code <<-EOD + Foo.new + Bar.z + EOD + c.def_code <<-EOD + Foo.new + Bar.z + EOD + c.def_privatemethod("foo", "baz", "*arg", "&block") + + puts c.dump +end diff --git a/lib/xsd/codegen/commentdef.rb b/lib/xsd/codegen/commentdef.rb new file mode 100644 index 0000000000..f98fade57d --- /dev/null +++ b/lib/xsd/codegen/commentdef.rb @@ -0,0 +1,34 @@ +# XSD4R - Generating comment definition code +# Copyright (C) 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 'xsd/codegen/gensupport' + + +module XSD +module CodeGen + + +module CommentDef + include GenSupport + + attr_accessor :comment + +private + + def dump_comment + if /^#/ =~ @comment + format(@comment) + else + format(@comment).gsub(/^/, "# ") + end + end +end + + +end +end diff --git a/lib/xsd/codegen/gensupport.rb b/lib/xsd/codegen/gensupport.rb new file mode 100644 index 0000000000..df90550fa0 --- /dev/null +++ b/lib/xsd/codegen/gensupport.rb @@ -0,0 +1,112 @@ +# XSD4R - Code generation support +# Copyright (C) 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. + + +module XSD +module CodeGen + + +module GenSupport + def capitalize(target) + target.sub(/^([a-z])/) { $1.tr!('[a-z]', '[A-Z]') } + end + module_function :capitalize + + def uncapitalize(target) + target.sub(/^([A-Z])/) { $1.tr!('[A-Z]', '[a-z]') } + end + module_function :uncapitalize + + def safeconstname(name) + safename = name.scan(/[a-zA-Z0-9_]+/).collect { |ele| + GenSupport.capitalize(ele) + }.join + unless /^[A-Z]/ =~ safename + safename = "C_#{safename}" + end + safename + end + module_function :safeconstname + + def safeconstname?(name) + /\A[A-Z][a-zA-Z0-9_]*\z/ =~ name + end + module_function :safeconstname? + + def safemethodname(name) + safevarname(name) + end + module_function :safemethodname + + def safemethodname?(name) + /\A[a-zA-Z_][a-zA-Z0-9_]*[=!?]?\z/ =~ name + end + module_function :safemethodname? + + def safevarname(name) + safename = name.scan(/[a-zA-Z0-9_]+/).join('_') + safename = uncapitalize(safename) + unless /^[a-z]/ =~ safename + safename = "m_#{safename}" + end + safename + end + module_function :safevarname + + def safevarname?(name) + /\A[a-z_][a-zA-Z0-9_]*\z/ =~ name + end + module_function :safevarname? + + def format(str, indent = nil) + str = trim_eol(str) + str = trim_indent(str) + if indent + str.gsub(/^/, " " * indent) + else + str + end + end + +private + + def trim_eol(str) + str.collect { |line| + line.sub(/\r?\n$/, "") + "\n" + }.join + end + + def trim_indent(str) + indent = nil + str = str.collect { |line| untab(line) }.join + str.each do |line| + head = line.index(/\S/) + if !head.nil? and (indent.nil? or head < indent) + indent = head + end + end + return str unless indent + str.collect { |line| + line.sub(/^ {0,#{indent}}/, "") + }.join + end + + def untab(line, ts = 8) + while pos = line.index(/\t/) + line = line.sub(/\t/, " " * (ts - (pos % ts))) + end + line + end + + def dump_emptyline + "\n" + end +end + + +end +end diff --git a/lib/xsd/codegen/methoddef.rb b/lib/xsd/codegen/methoddef.rb new file mode 100644 index 0000000000..797a4f024e --- /dev/null +++ b/lib/xsd/codegen/methoddef.rb @@ -0,0 +1,63 @@ +# XSD4R - Generating method definition code +# Copyright (C) 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 'xsd/codegen/gensupport' +require 'xsd/codegen/commentdef' + + +module XSD +module CodeGen + + +class MethodDef + include GenSupport + include CommentDef + + attr_accessor :definition + + def initialize(name, *params) + unless safemethodname?(name) + raise ArgumentError.new("#{name} seems to be unsafe") + end + @name = name + @params = params + @comment = nil + @definition = yield if block_given? + end + + def dump + buf = "" + buf << dump_comment if @comment + buf << dump_method_def + buf << dump_definition if @definition + buf << dump_method_def_end + buf + end + +private + + def dump_method_def + if @params.empty? + format("def #{@name}") + else + format("def #{@name}(#{@params.join(", ")})") + end + end + + def dump_method_def_end + format("end") + end + + def dump_definition + format(@definition, 2) + end +end + + +end +end diff --git a/lib/xsd/codegen/moduledef.rb b/lib/xsd/codegen/moduledef.rb new file mode 100644 index 0000000000..dc2746b2ee --- /dev/null +++ b/lib/xsd/codegen/moduledef.rb @@ -0,0 +1,191 @@ +# XSD4R - Generating module definition code +# Copyright (C) 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 'xsd/codegen/gensupport' +require 'xsd/codegen/methoddef' +require 'xsd/codegen/commentdef' + + +module XSD +module CodeGen + + +class ModuleDef + include GenSupport + include CommentDef + + def initialize(name) + @name = name + @comment = nil + @const = [] + @code = [] + @requirepath = [] + @methoddef = [] + end + + def def_require(path) + @requirepath << path + end + + def def_const(const, value) + unless safeconstname?(const) + raise ArgumentError.new("#{const} seems to be unsafe") + end + @const << [const, value] + end + + def def_code(code) + @code << code + end + + def def_method(name, *params) + add_method(MethodDef.new(name, *params) { yield if block_given? }, :public) + end + alias def_publicmethod def_method + + def def_protectedmethod(name, *params) + add_method(MethodDef.new(name, *params) { yield if block_given? }, + :protected) + end + + def def_privatemethod(name, *params) + add_method(MethodDef.new(name, *params) { yield if block_given? }, :private) + end + + def add_method(m, visibility = :public) + @methoddef << [visibility, m] + end + + def dump + buf = "" + unless @requirepath.empty? + buf << dump_requirepath + end + buf << dump_emptyline unless buf.empty? + package = @name.split(/::/)[0..-2] + buf << dump_package_def(package) unless package.empty? + buf << dump_comment if @comment + buf << dump_module_def + spacer = false + unless @const.empty? + buf << dump_emptyline if spacer + spacer = true + buf << dump_const + end + unless @code.empty? + buf << dump_emptyline if spacer + spacer = true + buf << dump_code + end + unless @methoddef.empty? + buf << dump_emptyline if spacer + spacer = true + buf << dump_methods + end + buf << dump_module_def_end + buf << dump_package_def_end(package) unless package.empty? + buf + end + +private + + def dump_requirepath + format( + @requirepath.collect { |path| + %Q(require '#{path}') + }.join("\n") + ) + end + + def dump_const + dump_static( + @const.sort.collect { |var, value| + %Q(#{var} = #{dump_value(value)}) + }.join("\n") + ) + end + + def dump_code + dump_static(@code.join("\n")) + end + + def dump_static(str) + format(str, 2) + end + + def dump_methods + methods = {} + @methoddef.each do |visibility, method| + (methods[visibility] ||= []) << method + end + str = "" + [:public, :protected, :private].each do |visibility| + if methods[visibility] + str << "\n" unless str.empty? + str << visibility.to_s << "\n\n" unless visibility == :public + str << methods[visibility].collect { |m| format(m.dump, 2) }.join("\n") + end + end + str + end + + def dump_value(value) + if value.respond_to?(:to_src) + value.to_src + else + value + end + end + + def dump_package_def(package) + format(package.collect { |ele| "module #{ele}" }.join("; ")) + "\n\n" + end + + def dump_package_def_end(package) + "\n\n" + format(package.collect { |ele| "end" }.join("; ")) + end + + def dump_module_def + name = @name.to_s.split(/::/) + format("module #{name.last}") + end + + def dump_module_def_end + format("end") + end +end + + +end +end + + +if __FILE__ == $0 + require 'xsd/codegen/moduledef' + include XSD::CodeGen + m = ModuleDef.new("Foo::Bar::HobbitName") + m.def_require("foo/bar") + m.def_require("baz") + m.comment = <<-EOD + foo + bar + baz + EOD + m.def_method("foo") do + <<-EOD + foo.bar = 1 + baz.each do |ele| + ele + 1 + end + EOD + end + m.def_method("baz", "qux") + #m.def_protectedmethod("aaa") + m.def_privatemethod("bbb") + puts m.dump +end |