mirror of
https://github.com/fog/fog.git
synced 2022-11-09 13:51:43 -05:00
add initial ToHashParser
This commit is contained in:
parent
0150e77e28
commit
5abc390c91
1 changed files with 59 additions and 0 deletions
|
@ -24,3 +24,62 @@ module Fog
|
|||
end
|
||||
end
|
||||
end
|
||||
|
||||
module Fog
|
||||
class ToHashParser
|
||||
|
||||
def initialize
|
||||
@stack = []
|
||||
end
|
||||
|
||||
def characters(string)
|
||||
@value ||= ''
|
||||
@value << string.strip
|
||||
end
|
||||
|
||||
def end_element(name)
|
||||
@stack.pop
|
||||
unless @value.empty?
|
||||
@stack.last[name.to_sym] = @value
|
||||
@value = ''
|
||||
end
|
||||
end
|
||||
|
||||
def body
|
||||
@stack.first
|
||||
end
|
||||
|
||||
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.to_sym] = value
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue