1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/core/parser.rb

117 lines
2.7 KiB
Ruby
Raw Normal View History

require "nokogiri"
2011-02-16 20:25:50 -05:00
2009-05-19 02:06:49 -04:00
module Fog
module Parsers
class Base < Nokogiri::XML::SAX::Document
attr_reader :response
2009-05-19 02:06:49 -04:00
def initialize
reset
end
def attr_value(name, attrs)
(entry = attrs.detect {|a, v| a == name }) && entry.last
end
2009-05-19 02:06:49 -04:00
def reset
@response = {}
2009-05-19 02:06:49 -04:00
end
def characters(string)
@value ||= ''
@value << string
2009-05-19 02:06:49 -04:00
end
# ###############################################################################
# This is a workaround. Original implementation from Nokogiri is overwritten with
# one that does not join namespace prefix with local name.
def start_element_namespace name, attrs = [], prefix = nil, uri = nil, ns = []
start_element name, attrs
end
def end_element_namespace name, prefix = nil, uri = nil
end_element name
end
# ###############################################################################
2009-05-19 02:06:49 -04:00
def start_element(name, attrs = [])
@value = nil
2009-05-19 02:06:49 -04:00
end
def value
@value && @value.dup
2011-01-08 22:53:54 -05:00
end
2009-05-19 02:06:49 -04:00
end
end
end
2010-06-18 17:35:29 -04:00
module Fog
class ToHashDocument < Nokogiri::XML::SAX::Document
2010-06-18 17:35:29 -04:00
def initialize
@stack = []
end
def characters(string)
@value ||= ''
@value << string.strip
end
def end_element(name)
2010-06-18 17:53:35 -04:00
last = @stack.pop
if last.empty? && @value.empty?
2010-06-18 17:53:35 -04:00
@stack.last[name.to_sym] = ''
elsif last == {:i_nil=>"true"}
@stack.last[name.to_sym] = nil
elsif !@value.empty?
2010-06-18 17:35:29 -04:00
@stack.last[name.to_sym] = @value
end
2010-06-18 17:53:35 -04:00
@value = ''
2010-06-18 17:35:29 -04:00
end
def body
@stack.first
end
def response
body
end
2010-06-18 17:35:29 -04:00
def start_element(name, attributes = [])
@value = ''
parsed_attributes = {}
until attributes.empty?
if attributes.first.is_a?(Array)
key, value = attributes.shift
else
key, value = attributes.shift, attributes.shift
end
parsed_attributes[key.gsub(':','_').to_sym] = value
2010-06-18 17:35:29 -04:00
end
if @stack.last.is_a?(Array)
@stack.last << {name.to_sym => parsed_attributes}
else
data = if @stack.empty?
@stack.push(parsed_attributes)
parsed_attributes
elsif @stack.last[name.to_sym]
unless @stack.last[name.to_sym].is_a?(Array)
@stack.last[name.to_sym] = [@stack.last[name.to_sym]]
end
@stack.last[name.to_sym] << parsed_attributes
@stack.last[name.to_sym].last
else
@stack.last[name.to_sym] = {}
@stack.last[name.to_sym].merge!(parsed_attributes)
@stack.last[name.to_sym]
end
@stack.push(data)
end
end
end
end