From 0d1e3bd20406f85e4ea71c8c24201eab4d34877a Mon Sep 17 00:00:00 2001 From: rubiii Date: Thu, 28 Jan 2010 22:42:53 +0100 Subject: [PATCH] moved Savon::WSDLStream into its own file --- lib/savon.rb | 2 +- lib/savon/wsdl.rb | 89 ---------------------------------------- lib/savon/wsdl_stream.rb | 88 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 90 deletions(-) create mode 100644 lib/savon/wsdl_stream.rb diff --git a/lib/savon.rb b/lib/savon.rb index 510f2dbd..1271daf6 100644 --- a/lib/savon.rb +++ b/lib/savon.rb @@ -29,6 +29,6 @@ class SOAPFault < StandardError; end end # core files -%w(core_ext wsse soap request response wsdl client).each do |file| +%w(core_ext wsse soap request response wsdl_stream wsdl client).each do |file| require File.dirname(__FILE__) + "/savon/#{file}" end diff --git a/lib/savon/wsdl.rb b/lib/savon/wsdl.rb index 3b08704c..c0f67a36 100644 --- a/lib/savon/wsdl.rb +++ b/lib/savon/wsdl.rb @@ -62,93 +62,4 @@ def stream end end - - # Savon::WSDLStream - # - # Stream listener for parsing the WSDL document. - class WSDLStream - - # The main sections of a WSDL document. - Sections = %w(definitions types message portType binding service) - - def initialize - @path, @operations = [], {} - @namespaces = {} - end - - # Returns the namespace URI. - attr_reader :namespace_uri - - # Returns the SOAP operations. - attr_reader :operations - - # Returns the SOAP endpoint. - attr_reader :soap_endpoint - - # Hook method called when the stream parser encounters a starting tag. - def tag_start(tag, attrs) - - # read xml namespaces if root element - read_namespaces(attrs) if @path.empty? - - tag,namespace = tag.split(":").reverse - - @path << tag - - if @section == :binding && tag=="binding" - # ensure that we are in an wsdl/soap namespace - @section = nil unless @namespaces[namespace] == "http://schemas.xmlsoap.org/wsdl/soap/" - end - - @section = tag.to_sym if Sections.include?(tag) if depth <= 2 - - @namespace_uri ||= attrs["targetNamespace"] if @section == :definitions - @soap_endpoint ||= URI(attrs["location"]) if @section == :service && tag == "address" - - operation_from tag, attrs if @section == :binding && tag == "operation" - end - - def depth - @path.size - end - - # read namespace definitions from given hash - def read_namespaces(attrs) - for key, value in attrs - if key.start_with?("xmlns:") - @namespaces[key.split(':').last] = value - end - end - end - - # Hook method called when the stream parser encounters a closing tag. - def tag_end(tag) - @path.pop - - if @section == :binding && @input && tag.strip_namespace == "operation" - # no soapAction attribute found till now - operation_from tag, "soapAction" => @input - end - - end - - # Stores available operations from a given tag +name+ and +attrs+. - def operation_from(tag, attrs) - @input = attrs["name"] if attrs["name"] - - if attrs["soapAction"] - @action = !attrs["soapAction"].blank? ? attrs["soapAction"] : @input - @input = @action.split("/").last if !@input || @input.empty? - - @operations[@input.snakecase.to_sym] = { :action => @action, :input => @input } - @input, @action = nil, nil - @input = nil - end - end - - # Catches calls to unimplemented hook methods. - def method_missing(method, *args) - end - - end end diff --git a/lib/savon/wsdl_stream.rb b/lib/savon/wsdl_stream.rb new file mode 100644 index 00000000..a1fa470e --- /dev/null +++ b/lib/savon/wsdl_stream.rb @@ -0,0 +1,88 @@ +module Savon + + # Savon::WSDLStream + # + # Stream listener for parsing the WSDL document. + class WSDLStream + + # The main sections of a WSDL document. + Sections = %w(definitions types message portType binding service) + + def initialize + @path, @operations = [], {} + @namespaces = {} + end + + # Returns the namespace URI. + attr_reader :namespace_uri + + # Returns the SOAP operations. + attr_reader :operations + + # Returns the SOAP endpoint. + attr_reader :soap_endpoint + + # Hook method called when the stream parser encounters a starting tag. + def tag_start(tag, attrs) + # read xml namespaces if root element + read_namespaces(attrs) if @path.empty? + + tag,namespace = tag.split(":").reverse + @path << tag + + if @section == :binding && tag == "binding" + # ensure that we are in an wsdl/soap namespace + @section = nil unless @namespaces[namespace] == "http://schemas.xmlsoap.org/wsdl/soap/" + end + + @section = tag.to_sym if Sections.include?(tag) && depth <= 2 + + @namespace_uri ||= attrs["targetNamespace"] if @section == :definitions + @soap_endpoint ||= URI(attrs["location"]) if @section == :service && tag == "address" + + operation_from tag, attrs if @section == :binding && tag == "operation" + end + + # Returns our current depth in the WSDL document. + def depth + @path.size + end + + # Reads namespace definitions from a given +attrs+ Hash. + def read_namespaces(attrs) + attrs.each do |key, value| + @namespaces[key.split(":").last] = value if key.start_with? "xmlns:" + end + end + + # Hook method called when the stream parser encounters a closing tag. + def tag_end(tag) + @path.pop + + if @section == :binding && @input && tag.strip_namespace == "operation" + # no soapAction attribute found till now + operation_from tag, "soapAction" => @input + end + + end + + # Stores available operations from a given tag +name+ and +attrs+. + def operation_from(tag, attrs) + @input = attrs["name"] if attrs["name"] + + if attrs["soapAction"] + @action = !attrs["soapAction"].blank? ? attrs["soapAction"] : @input + @input = @action.split("/").last if !@input || @input.empty? + + @operations[@input.snakecase.to_sym] = { :action => @action, :input => @input } + @input, @action = nil, nil + @input = nil + end + end + + # Catches calls to unimplemented hook methods. + def method_missing(method, *args) + end + + end +end