diff options
author | nahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-05-22 13:03:38 +0000 |
---|---|---|
committer | nahi <nahi@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2005-05-22 13:03:38 +0000 |
commit | eb3f829be932514064a983eb98fa7f840137f985 (patch) | |
tree | c17440206af25b52f47b1c391f818e126a994d0a /lib/wsdl/xmlSchema | |
parent | 7aea792d3bdf42c349170e710953d7de29de57ab (diff) |
* 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
Diffstat (limited to 'lib/wsdl/xmlSchema')
-rw-r--r-- | lib/wsdl/xmlSchema/annotation.rb | 34 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/attribute.rb | 60 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/complexContent.rb | 5 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/data.rb | 11 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/element.rb | 72 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/import.rb | 25 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/importer.rb | 81 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/include.rb | 54 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/length.rb | 35 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/parser.rb | 39 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/pattern.rb | 36 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/schema.rb | 36 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/simpleContent.rb | 56 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/simpleExtension.rb | 54 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/simpleRestriction.rb | 29 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/simpleType.rb | 12 | ||||
-rw-r--r-- | lib/wsdl/xmlSchema/xsd2ruby.rb | 107 |
17 files changed, 667 insertions, 79 deletions
diff --git a/lib/wsdl/xmlSchema/annotation.rb b/lib/wsdl/xmlSchema/annotation.rb new file mode 100644 index 0000000000..633bd196f1 --- /dev/null +++ b/lib/wsdl/xmlSchema/annotation.rb @@ -0,0 +1,34 @@ +# WSDL4R - WSDL SOAP documentation element. +# Copyright (C) 2003, 2005 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 Annotation < Info + def initialize + super + end + + def parse_element(element) + # Accepts any element. + self + end + + def parse_attr(attr, value) + # Accepts any attribute. + true + end +end + + +end +end diff --git a/lib/wsdl/xmlSchema/attribute.rb b/lib/wsdl/xmlSchema/attribute.rb index 6861fc171e..cfd4c68422 100644 --- a/lib/wsdl/xmlSchema/attribute.rb +++ b/lib/wsdl/xmlSchema/attribute.rb @@ -1,5 +1,5 @@ # WSDL4R - XMLSchema attribute definition for WSDL. -# Copyright (C) 2002, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2002, 2003, 2005 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; @@ -14,34 +14,74 @@ module XMLSchema class Attribute < Info - attr_accessor :ref - attr_accessor :use - attr_accessor :form - attr_accessor :name - attr_accessor :type - attr_accessor :default - attr_accessor :fixed + class << self + if RUBY_VERSION > "1.7.0" + def attr_reader_ref(symbol) + name = symbol.to_s + self.__send__(:define_method, name, proc { + instance_variable_get("@#{name}") || + (refelement ? refelement.__send__(name) : nil) + }) + end + else + def attr_reader_ref(symbol) + name = symbol.to_s + module_eval <<-EOS + def #{name} + @#{name} || (refelement ? refelement.#{name} : nil) + end + EOS + end + end + end + + attr_writer :use + attr_writer :form + attr_writer :name + attr_writer :type + attr_writer :local_simpletype + attr_writer :default + attr_writer :fixed + + attr_reader_ref :use + attr_reader_ref :form + attr_reader_ref :name + attr_reader_ref :type + attr_reader_ref :local_simpletype + attr_reader_ref :default + attr_reader_ref :fixed + attr_accessor :ref attr_accessor :arytype def initialize super - @ref = nil @use = nil @form = nil @name = nil @type = nil + @local_simpletype = nil @default = nil @fixed = nil + @ref = nil + @refelement = nil @arytype = nil end + def refelement + @refelement ||= root.collect_attributes[@ref] + end + def targetnamespace parent.targetnamespace end def parse_element(element) - nil + case element + when SimpleTypeName + @local_simpletype = SimpleType.new + @local_simpletype + end end def parse_attr(attr, value) diff --git a/lib/wsdl/xmlSchema/complexContent.rb b/lib/wsdl/xmlSchema/complexContent.rb index 66ad9e251d..eddb52f5ef 100644 --- a/lib/wsdl/xmlSchema/complexContent.rb +++ b/lib/wsdl/xmlSchema/complexContent.rb @@ -26,12 +26,17 @@ class ComplexContent < Info @derivetype = nil @content = nil @attributes = XSD::NamedElements.new + @basetype = nil end def targetnamespace parent.targetnamespace end + def basetype + @basetype ||= root.collect_complextypes[@base] + end + def parse_element(element) case element when RestrictionName, ExtensionName diff --git a/lib/wsdl/xmlSchema/data.rb b/lib/wsdl/xmlSchema/data.rb index 10bc343adb..23ab1adf0b 100644 --- a/lib/wsdl/xmlSchema/data.rb +++ b/lib/wsdl/xmlSchema/data.rb @@ -1,5 +1,5 @@ # WSDL4R - XMLSchema data definitions. -# Copyright (C) 2002, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2002, 2003, 2005 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; @@ -7,10 +7,13 @@ require 'xsd/datatypes' +require 'wsdl/xmlSchema/annotation' require 'wsdl/xmlSchema/schema' require 'wsdl/xmlSchema/import' +require 'wsdl/xmlSchema/include' require 'wsdl/xmlSchema/simpleType' require 'wsdl/xmlSchema/simpleRestriction' +require 'wsdl/xmlSchema/simpleExtension' require 'wsdl/xmlSchema/complexType' require 'wsdl/xmlSchema/complexContent' require 'wsdl/xmlSchema/simpleContent' @@ -22,12 +25,15 @@ require 'wsdl/xmlSchema/sequence' require 'wsdl/xmlSchema/attribute' require 'wsdl/xmlSchema/unique' require 'wsdl/xmlSchema/enumeration' +require 'wsdl/xmlSchema/length' +require 'wsdl/xmlSchema/pattern' module WSDL module XMLSchema AllName = XSD::QName.new(XSD::Namespace, 'all') +AnnotationName = XSD::QName.new(XSD::Namespace, 'annotation') AnyName = XSD::QName.new(XSD::Namespace, 'any') AttributeName = XSD::QName.new(XSD::Namespace, 'attribute') ChoiceName = XSD::QName.new(XSD::Namespace, 'choice') @@ -37,6 +43,9 @@ ElementName = XSD::QName.new(XSD::Namespace, 'element') EnumerationName = XSD::QName.new(XSD::Namespace, 'enumeration') ExtensionName = XSD::QName.new(XSD::Namespace, 'extension') ImportName = XSD::QName.new(XSD::Namespace, 'import') +IncludeName = XSD::QName.new(XSD::Namespace, 'include') +LengthName = XSD::QName.new(XSD::Namespace, 'length') +PatternName = XSD::QName.new(XSD::Namespace, 'pattern') RestrictionName = XSD::QName.new(XSD::Namespace, 'restriction') SequenceName = XSD::QName.new(XSD::Namespace, 'sequence') SchemaName = XSD::QName.new(XSD::Namespace, 'schema') diff --git a/lib/wsdl/xmlSchema/element.rb b/lib/wsdl/xmlSchema/element.rb index cc9d4e9ed8..584afe9dc6 100644 --- a/lib/wsdl/xmlSchema/element.rb +++ b/lib/wsdl/xmlSchema/element.rb @@ -1,5 +1,5 @@ # WSDL4R - XMLSchema element definition for WSDL. -# Copyright (C) 2002, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2002, 2003, 2005 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; @@ -14,23 +14,62 @@ module XMLSchema class Element < Info - attr_accessor :name # required - attr_accessor :type - attr_accessor :local_complextype - attr_accessor :constraint - attr_accessor :maxoccurs - attr_accessor :minoccurs - attr_accessor :nillable - - def initialize(name = nil, type = XSD::AnyTypeName) + class << self + if RUBY_VERSION > "1.7.0" + def attr_reader_ref(symbol) + name = symbol.to_s + self.__send__(:define_method, name, proc { + instance_variable_get("@#{name}") || + (refelement ? refelement.__send__(name) : nil) + }) + end + else + def attr_reader_ref(symbol) + name = symbol.to_s + module_eval <<-EOS + def #{name} + @#{name} || (refelement ? refelement.#{name} : nil) + end + EOS + end + end + end + + attr_writer :name # required + attr_writer :type + attr_writer :local_simpletype + attr_writer :local_complextype + attr_writer :constraint + attr_writer :maxoccurs + attr_writer :minoccurs + attr_writer :nillable + + attr_reader_ref :name + attr_reader_ref :type + attr_reader_ref :local_simpletype + attr_reader_ref :local_complextype + attr_reader_ref :constraint + attr_reader_ref :maxoccurs + attr_reader_ref :minoccurs + attr_reader_ref :nillable + + attr_accessor :ref + + def initialize(name = nil, type = nil) super() @name = name @type = type - @local_complextype = nil + @local_simpletype = @local_complextype = nil @constraint = nil @maxoccurs = '1' @minoccurs = '1' @nillable = nil + @ref = nil + @refelement = nil + end + + def refelement + @refelement ||= root.collect_elements[@ref] end def targetnamespace @@ -44,6 +83,9 @@ class Element < Info def parse_element(element) case element + when SimpleTypeName + @local_simpletype = SimpleType.new + @local_simpletype when ComplexTypeName @type = nil @local_complextype = ComplexType.new @@ -62,19 +104,19 @@ class Element < Info @name = XSD::QName.new(targetnamespace, value.source) when TypeAttrName @type = value + when RefAttrName + @ref = value when MaxOccursAttrName if parent.is_a?(All) if value.source != '1' - raise Parser::AttrConstraintError.new( - "Cannot parse #{ value } for #{ attr }.") + raise Parser::AttrConstraintError.new("cannot parse #{value} for #{attr}") end end @maxoccurs = value.source when MinOccursAttrName if parent.is_a?(All) unless ['0', '1'].include?(value.source) - raise Parser::AttrConstraintError.new( - "Cannot parse #{ value } for #{ attr }.") + raise Parser::AttrConstraintError.new("cannot parse #{value} for #{attr}") end end @minoccurs = value.source diff --git a/lib/wsdl/xmlSchema/import.rb b/lib/wsdl/xmlSchema/import.rb index e65641330d..d3487af934 100644 --- a/lib/wsdl/xmlSchema/import.rb +++ b/lib/wsdl/xmlSchema/import.rb @@ -1,5 +1,5 @@ # WSDL4R - XMLSchema import definition. -# Copyright (C) 2002, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2002, 2003, 2005 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; @@ -7,6 +7,7 @@ require 'wsdl/info' +require 'wsdl/xmlSchema/importer' module WSDL @@ -16,11 +17,13 @@ module XMLSchema class Import < Info attr_reader :namespace attr_reader :schemalocation + attr_reader :content def initialize super @namespace = nil @schemalocation = nil + @content = nil end def parse_element(element) @@ -32,11 +35,29 @@ class Import < Info when NamespaceAttrName @namespace = value.source when SchemaLocationAttrName - @schemalocation = value.source + @schemalocation = URI.parse(value.source) + if @schemalocation.relative? and !parent.location.nil? and + !parent.location.relative? + @schemalocation = parent.location + @schemalocation + end + if root.importedschema.key?(@schemalocation) + @content = root.importedschema[@schemalocation] + else + root.importedschema[@schemalocation] = nil # placeholder + @content = import(@schemalocation) + root.importedschema[@schemalocation] = @content + end + @schemalocation else nil end end + +private + + def import(location) + Importer.import(location, root) + end end diff --git a/lib/wsdl/xmlSchema/importer.rb b/lib/wsdl/xmlSchema/importer.rb new file mode 100644 index 0000000000..8829d5240c --- /dev/null +++ b/lib/wsdl/xmlSchema/importer.rb @@ -0,0 +1,81 @@ +# WSDL4R - XSD importer library. +# Copyright (C) 2003, 2005 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/httpconfigloader' +require 'wsdl/xmlSchema/parser' + + +module WSDL +module XMLSchema + + +class Importer + def self.import(location, originalroot = nil) + new.import(location, originalroot) + end + + def initialize + @web_client = nil + end + + def import(location, originalroot = nil) + unless location.is_a?(URI) + location = URI.parse(location) + end + content = parse(fetch(location), location, originalroot) + content.location = location + content + end + +private + + def parse(content, location, originalroot) + opt = { + :location => location, + :originalroot => originalroot + } + WSDL::XMLSchema::Parser.new(opt).parse(content) + end + + def fetch(location) + warn("importing: #{location}") if $DEBUG + content = nil + if location.scheme == 'file' or + (location.relative? and FileTest.exist?(location.path)) + content = File.open(location.path).read + else + client = web_client.new(nil, "WSDL4R") + client.proxy = ::SOAP::Env::HTTP_PROXY + client.no_proxy = ::SOAP::Env::NO_PROXY + if opt = ::SOAP::Property.loadproperty(::SOAP::PropertyName) + ::SOAP::HTTPConfigLoader.set_options(client, opt["client.protocol.http"]) + end + content = client.get_content(location) + end + content + end + + def web_client + @web_client ||= begin + require 'http-access2' + if HTTPAccess2::VERSION < "2.0" + raise LoadError.new("http-access/2.0 or later is required.") + end + HTTPAccess2::Client + rescue LoadError + warn("Loading http-access2 failed. Net/http is used.") if $DEBUG + require 'soap/netHttpClient' + ::SOAP::NetHttpClient + end + @web_client + end +end + + +end +end diff --git a/lib/wsdl/xmlSchema/include.rb b/lib/wsdl/xmlSchema/include.rb new file mode 100644 index 0000000000..af1ef942bb --- /dev/null +++ b/lib/wsdl/xmlSchema/include.rb @@ -0,0 +1,54 @@ +# WSDL4R - XMLSchema include definition. +# Copyright (C) 2005 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/xmlSchema/importer' + + +module WSDL +module XMLSchema + + +class Include < Info + attr_reader :schemalocation + attr_reader :content + + def initialize + super + @schemalocation = nil + @content = nil + end + + def parse_element(element) + nil + end + + def parse_attr(attr, value) + case attr + when SchemaLocationAttrName + @schemalocation = URI.parse(value.source) + if @schemalocation.relative? + @schemalocation = parent.location + @schemalocation + end + @content = import(@schemalocation) + @schemalocation + else + nil + end + end + +private + + def import(location) + Importer.import(location) + end +end + + +end +end diff --git a/lib/wsdl/xmlSchema/length.rb b/lib/wsdl/xmlSchema/length.rb new file mode 100644 index 0000000000..7f61602da9 --- /dev/null +++ b/lib/wsdl/xmlSchema/length.rb @@ -0,0 +1,35 @@ +# WSDL4R - XMLSchema length definition for WSDL. +# Copyright (C) 2005 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 Length < Info + def initialize + super + end + + def parse_element(element) + nil + end + + def parse_attr(attr, value) + case attr + when ValueAttrName + value.source + end + end +end + + +end +end diff --git a/lib/wsdl/xmlSchema/parser.rb b/lib/wsdl/xmlSchema/parser.rb index a7f1c29fd4..057d9d9b70 100644 --- a/lib/wsdl/xmlSchema/parser.rb +++ b/lib/wsdl/xmlSchema/parser.rb @@ -1,5 +1,5 @@ # WSDL4R - WSDL XML Instance parser library. -# Copyright (C) 2002, 2003 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2002, 2003, 2005 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; @@ -51,6 +51,9 @@ public @parser = XSD::XMLParser.create_parser(self, opt) @parsestack = nil @lastnode = nil + @ignored = {} + @location = opt[:location] + @originalroot = opt[:originalroot] end def parse(string_or_readable) @@ -94,7 +97,7 @@ public def end_element(name) lastframe = @parsestack.pop unless name == lastframe.name - raise UnexpectedElementError.new("Closing element name '#{ name }' does not match with opening element '#{ lastframe.name }'.") + raise UnexpectedElementError.new("closing element name '#{name}' does not match with opening element '#{lastframe.name}'") end decode_tag_end(lastframe.ns, lastframe.node) @lastnode = lastframe.node @@ -104,20 +107,31 @@ private def decode_tag(ns, name, attrs, parent) o = nil - element = ns.parse(name) + elename = ns.parse(name) if !parent - if element == SchemaName - o = Schema.parse_element(element) + if elename == SchemaName + o = Schema.parse_element(elename) + o.location = @location else - raise UnknownElementError.new("Unknown element #{ element }.") + raise UnknownElementError.new("unknown element: #{elename}") end + o.root = @originalroot if @originalroot # o.root = o otherwise else - o = parent.parse_element(element) + if elename == AnnotationName + # only the first annotation element is allowed for each element. + o = Annotation.new + else + o = parent.parse_element(elename) + end unless o - STDERR.puts("Unknown element #{ element }.") + unless @ignored.key?(elename) + warn("ignored element: #{elename} of #{parent.class}") + @ignored[elename] = elename + end o = Documentation.new # which accepts any element. end # node could be a pseudo element. pseudo element has its own parent. + o.root = parent.root o.parent = parent if o.parent.nil? end attrs.each do |key, value| @@ -127,9 +141,12 @@ private if attr_ele == IdAttrName o.id = value_ele else - unless o.parse_attr(attr_ele, value_ele) - STDERR.puts("Unknown attr #{ attr_ele }.") - end + unless o.parse_attr(attr_ele, value_ele) + unless @ignored.key?(attr_ele) + warn("ignored attr: #{attr_ele}") + @ignored[attr_ele] = attr_ele + end + end end end o diff --git a/lib/wsdl/xmlSchema/pattern.rb b/lib/wsdl/xmlSchema/pattern.rb new file mode 100644 index 0000000000..f826be4578 --- /dev/null +++ b/lib/wsdl/xmlSchema/pattern.rb @@ -0,0 +1,36 @@ +# WSDL4R - XMLSchema pattern definition for WSDL. +# Copyright (C) 2005 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 Pattern < Info + def initialize + super + end + + def parse_element(element) + nil + end + + def parse_attr(attr, value) + case attr + when ValueAttrName + parent.pattern = /\A#{value.source}\z/n + value.source + end + end +end + + +end +end diff --git a/lib/wsdl/xmlSchema/schema.rb b/lib/wsdl/xmlSchema/schema.rb index ddd231bd97..43447f9fbf 100644 --- a/lib/wsdl/xmlSchema/schema.rb +++ b/lib/wsdl/xmlSchema/schema.rb @@ -24,6 +24,8 @@ class Schema < Info attr_accessor :attributeformdefault attr_accessor :elementformdefault + attr_reader :importedschema + def initialize super @targetnamespace = nil @@ -33,6 +35,17 @@ class Schema < Info @attributes = XSD::NamedElements.new @imports = [] @elementformdefault = "qualified" + @importedschema = {} + @location = nil + @root = self + end + + def location + @location || (root.nil? ? nil : root.location) + end + + def location=(location) + @location = location end def parse_element(element) @@ -41,6 +54,10 @@ class Schema < Info o = Import.new @imports << o o + when IncludeName + o = Include.new + @imports << o + o when ComplexTypeName o = ComplexType.new @complextypes << o @@ -55,6 +72,7 @@ class Schema < Info o when AttributeName o = Attribute.new + @attributes << o o else nil @@ -74,21 +92,39 @@ class Schema < Info end end + def collect_attributes + result = XSD::NamedElements.new + result.concat(@attributes) + @imports.each do |import| + result.concat(import.content.collect_attributes) if import.content + end + result + end + def collect_elements result = XSD::NamedElements.new result.concat(@elements) + @imports.each do |import| + result.concat(import.content.collect_elements) if import.content + end result end def collect_complextypes result = XSD::NamedElements.new result.concat(@complextypes) + @imports.each do |import| + result.concat(import.content.collect_complextypes) if import.content + end result end def collect_simpletypes result = XSD::NamedElements.new result.concat(@simpletypes) + @imports.each do |import| + result.concat(import.content.collect_simpletypes) if import.content + end result end diff --git a/lib/wsdl/xmlSchema/simpleContent.rb b/lib/wsdl/xmlSchema/simpleContent.rb index 0d83678a01..e1f35c88b8 100644 --- a/lib/wsdl/xmlSchema/simpleContent.rb +++ b/lib/wsdl/xmlSchema/simpleContent.rb @@ -1,5 +1,5 @@ # WSDL4R - XMLSchema simpleContent definition for WSDL. -# Copyright (C) 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2004, 2005 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; @@ -15,17 +15,21 @@ module XMLSchema class SimpleContent < Info - attr_accessor :base - attr_reader :derivetype - attr_reader :content - attr_reader :attributes + attr_reader :restriction + attr_reader :extension + + def check_lexical_format(value) + check(value) + end def initialize super - @base = nil - @derivetype = nil - @content = nil - @attributes = XSD::NamedElements.new + @restriction = nil + @extension = nil + end + + def base + content.base end def targetnamespace @@ -34,28 +38,24 @@ class SimpleContent < Info def parse_element(element) case element - when RestrictionName, ExtensionName - @derivetype = element.name - self - when AttributeName - if @derivetype.nil? - raise Parser::ElementConstraintError.new("base attr not found.") - end - o = Attribute.new - @attributes << o - o + when RestrictionName + @restriction = SimpleRestriction.new + @restriction + when ExtensionName + @extension = SimpleExtension.new + @extension end end - def parse_attr(attr, value) - if @derivetype.nil? - return nil - end - case attr - when BaseAttrName - @base = value - else - nil +private + + def content + @restriction || @extension + end + + def check(value) + unless content.valid?(value) + raise XSD::ValueSpaceError.new("#{@name}: cannot accept '#{value}'") end end end diff --git a/lib/wsdl/xmlSchema/simpleExtension.rb b/lib/wsdl/xmlSchema/simpleExtension.rb new file mode 100644 index 0000000000..3c53a7328c --- /dev/null +++ b/lib/wsdl/xmlSchema/simpleExtension.rb @@ -0,0 +1,54 @@ +# WSDL4R - XMLSchema simpleType extension definition for WSDL. +# Copyright (C) 2005 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 SimpleExtension < Info + attr_reader :base + attr_reader :attributes + + def initialize + super + @base = nil + @attributes = XSD::NamedElements.new + end + + def targetnamespace + parent.targetnamespace + end + + def valid?(value) + true + end + + def parse_element(element) + case element + when AttributeName + o = Attribute.new + @attributes << o + o + end + end + + def parse_attr(attr, value) + case attr + when BaseAttrName + @base = value + end + end +end + + +end +end diff --git a/lib/wsdl/xmlSchema/simpleRestriction.rb b/lib/wsdl/xmlSchema/simpleRestriction.rb index 6986e74423..e8bf3ebfa5 100644 --- a/lib/wsdl/xmlSchema/simpleRestriction.rb +++ b/lib/wsdl/xmlSchema/simpleRestriction.rb @@ -1,4 +1,4 @@ -# WSDL4R - XMLSchema simpleType definition for WSDL. +# WSDL4R - XMLSchema simpleContent restriction definition for WSDL. # Copyright (C) 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. # This program is copyrighted free software by NAKAMURA, Hiroshi. You can @@ -17,21 +17,32 @@ module XMLSchema class SimpleRestriction < Info attr_reader :base attr_reader :enumeration + attr_accessor :length + attr_accessor :pattern def initialize super @base = nil @enumeration = [] # NamedElements? + @length = nil + @pattern = nil end def valid?(value) - @enumeration.include?(value) + return false unless check_restriction(value) + return false unless check_length(value) + return false unless check_pattern(value) + true end def parse_element(element) case element when EnumerationName Enumeration.new # just a parsing handler + when LengthName + Length.new # just a parsing handler + when PatternName + Pattern.new # just a parsing handler end end @@ -41,6 +52,20 @@ class SimpleRestriction < Info @base = value end end + +private + + def check_restriction(value) + @enumeration.empty? or @enumeration.include?(value) + end + + def check_length(value) + @length.nil? or value.size == @length + end + + def check_pattern(value) + @pattern.nil? or @pattern =~ value + end end diff --git a/lib/wsdl/xmlSchema/simpleType.rb b/lib/wsdl/xmlSchema/simpleType.rb index d9f76f345c..e808c318c4 100644 --- a/lib/wsdl/xmlSchema/simpleType.rb +++ b/lib/wsdl/xmlSchema/simpleType.rb @@ -1,5 +1,5 @@ # WSDL4R - XMLSchema simpleType definition for WSDL. -# Copyright (C) 2004 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. +# Copyright (C) 2004, 2005 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; @@ -16,15 +16,11 @@ 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 @@ -33,8 +29,6 @@ class SimpleType < Info def base if @restriction @restriction.base - elsif @extension - @extension.base else raise ArgumentError.new("incomplete simpleType") end @@ -43,7 +37,6 @@ class SimpleType < Info def initialize(name = nil) super() @name = name - @derivetype = nil @restriction = nil end @@ -55,7 +48,6 @@ class SimpleType < Info case element when RestrictionName @restriction = SimpleRestriction.new - @derivetype = element.name @restriction end end @@ -71,7 +63,7 @@ private def check_restriction(value) unless @restriction.valid?(value) - raise ::XSD::ValueSpaceError.new("#{@name}: cannot accept '#{value}'.") + raise XSD::ValueSpaceError.new("#{@name}: cannot accept '#{value}'") end end end diff --git a/lib/wsdl/xmlSchema/xsd2ruby.rb b/lib/wsdl/xmlSchema/xsd2ruby.rb new file mode 100644 index 0000000000..afe5fc5ada --- /dev/null +++ b/lib/wsdl/xmlSchema/xsd2ruby.rb @@ -0,0 +1,107 @@ +# XSD4R - XSD to ruby mapping library. +# Copyright (C) 2005 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 'wsdl/xmlSchema/importer' +require 'wsdl/soap/classDefCreator' + + +module WSDL +module XMLSchema + + +class XSD2Ruby + attr_accessor :location + attr_reader :opt + attr_accessor :logger + attr_accessor :basedir + + def run + unless @location + raise RuntimeError, "XML Schema location not given" + end + @xsd = import(@location) + @name = create_classname(@xsd) + create_file + end + +private + + def initialize + @location = nil + @opt = {} + @logger = Logger.new(STDERR) + @basedir = nil + @xsd = nil + @name = nil + end + + def create_file + create_classdef + end + + def create_classdef + @logger.info { "Creating class definition." } + @classdef_filename = @name + '.rb' + check_file(@classdef_filename) or return + write_file(@classdef_filename) do |f| + f << WSDL::SOAP::ClassDefCreator.new(@xsd).dump + end + end + + def write_file(filename) + if @basedir + filename = File.join(basedir, filename) + end + File.open(filename, "w") do |f| + yield f + end + end + + def check_file(filename) + if @basedir + filename = File.join(basedir, filename) + end + if FileTest.exist?(filename) + if @opt.key?('force') + @logger.warn { + "File '#{filename}' exists but overrides it." + } + true + else + @logger.warn { + "File '#{filename}' exists. #{$0} did not override it." + } + false + end + else + @logger.info { "Creates file '#{filename}'." } + true + end + end + + def create_classname(xsd) + name = nil + if xsd.targetnamespace + name = xsd.targetnamespace.scan(/[a-zA-Z0-9]+$/)[0] + end + if name.nil? + 'default' + else + XSD::CodeGen::GenSupport.safevarname(name) + end + end + + def import(location) + WSDL::XMLSchema::Importer.import(location) + end +end + + +end +end |