2009-09-13 11:53:12 -04:00
|
|
|
require 'active_support/core_ext/kernel/reporting'
|
2009-04-22 20:41:28 -04:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2011-01-18 14:45:52 -05:00
|
|
|
require 'stringio'
|
2009-04-22 20:41:28 -04:00
|
|
|
|
2009-03-09 15:39:20 -04:00
|
|
|
# = XmlMini ReXML implementation
|
|
|
|
module ActiveSupport
|
2009-03-09 15:46:06 -04:00
|
|
|
module XmlMini_REXML #:nodoc:
|
2009-03-09 15:39:20 -04:00
|
|
|
extend self
|
|
|
|
|
2009-03-09 15:46:06 -04:00
|
|
|
CONTENT_KEY = '__content__'.freeze
|
|
|
|
|
2009-05-17 11:37:30 -04:00
|
|
|
# Parse an XML Document string or IO into a simple hash
|
2009-03-09 15:39:20 -04:00
|
|
|
#
|
|
|
|
# Same as XmlSimple::xml_in but doesn't shoot itself in the foot,
|
2010-06-14 17:21:53 -04:00
|
|
|
# and uses the defaults from Active Support.
|
2009-03-09 15:39:20 -04:00
|
|
|
#
|
2009-05-17 11:37:30 -04:00
|
|
|
# data::
|
|
|
|
# XML Document string or IO to parse
|
|
|
|
def parse(data)
|
2009-06-09 10:01:50 -04:00
|
|
|
if !data.respond_to?(:read)
|
|
|
|
data = StringIO.new(data || '')
|
|
|
|
end
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2009-06-09 10:01:50 -04:00
|
|
|
char = data.getc
|
|
|
|
if char.nil?
|
|
|
|
{}
|
|
|
|
else
|
|
|
|
data.ungetc(char)
|
2009-09-13 08:56:30 -04:00
|
|
|
silence_warnings { require 'rexml/document' } unless defined?(REXML::Document)
|
2009-06-09 10:01:50 -04:00
|
|
|
doc = REXML::Document.new(data)
|
2010-03-29 02:21:19 -04:00
|
|
|
|
|
|
|
if doc.root
|
|
|
|
merge_element!({}, doc.root)
|
|
|
|
else
|
|
|
|
raise REXML::ParseException,
|
|
|
|
"The document #{doc.to_s.inspect} does not have a valid root"
|
|
|
|
end
|
2009-05-17 11:37:30 -04:00
|
|
|
end
|
2009-03-09 15:39:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
# Convert an XML element and merge into the hash
|
|
|
|
#
|
|
|
|
# hash::
|
|
|
|
# Hash to merge the converted element into.
|
|
|
|
# element::
|
|
|
|
# XML element to merge into hash
|
|
|
|
def merge_element!(hash, element)
|
|
|
|
merge!(hash, element.name, collapse(element))
|
|
|
|
end
|
|
|
|
|
|
|
|
# Actually converts an XML document element into a data structure.
|
|
|
|
#
|
|
|
|
# element::
|
|
|
|
# The document element to be collapsed.
|
|
|
|
def collapse(element)
|
|
|
|
hash = get_attributes(element)
|
|
|
|
|
|
|
|
if element.has_elements?
|
|
|
|
element.each_element {|child| merge_element!(hash, child) }
|
|
|
|
merge_texts!(hash, element) unless empty_content?(element)
|
|
|
|
hash
|
|
|
|
else
|
|
|
|
merge_texts!(hash, element)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Merge all the texts of an element into the hash
|
|
|
|
#
|
|
|
|
# hash::
|
2009-07-25 11:03:58 -04:00
|
|
|
# Hash to add the converted element to.
|
2009-03-09 15:39:20 -04:00
|
|
|
# element::
|
|
|
|
# XML element whose texts are to me merged into the hash
|
|
|
|
def merge_texts!(hash, element)
|
|
|
|
unless element.has_text?
|
|
|
|
hash
|
|
|
|
else
|
|
|
|
# must use value to prevent double-escaping
|
2009-03-29 02:55:18 -04:00
|
|
|
texts = ''
|
|
|
|
element.texts.each { |t| texts << t.value }
|
|
|
|
merge!(hash, CONTENT_KEY, texts)
|
2009-03-09 15:39:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Adds a new key/value pair to an existing Hash. If the key to be added
|
|
|
|
# already exists and the existing value associated with key is not
|
|
|
|
# an Array, it will be wrapped in an Array. Then the new value is
|
|
|
|
# appended to that Array.
|
|
|
|
#
|
|
|
|
# hash::
|
|
|
|
# Hash to add key/value pair to.
|
|
|
|
# key::
|
|
|
|
# Key to be added.
|
|
|
|
# value::
|
|
|
|
# Value to be associated with key.
|
|
|
|
def merge!(hash, key, value)
|
|
|
|
if hash.has_key?(key)
|
|
|
|
if hash[key].instance_of?(Array)
|
|
|
|
hash[key] << value
|
|
|
|
else
|
|
|
|
hash[key] = [hash[key], value]
|
|
|
|
end
|
|
|
|
elsif value.instance_of?(Array)
|
|
|
|
hash[key] = [value]
|
|
|
|
else
|
|
|
|
hash[key] = value
|
|
|
|
end
|
|
|
|
hash
|
|
|
|
end
|
|
|
|
|
|
|
|
# Converts the attributes array of an XML element into a hash.
|
|
|
|
# Returns an empty Hash if node has no attributes.
|
|
|
|
#
|
|
|
|
# element::
|
|
|
|
# XML element to extract attributes from.
|
|
|
|
def get_attributes(element)
|
|
|
|
attributes = {}
|
|
|
|
element.attributes.each { |n,v| attributes[n] = v }
|
|
|
|
attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
# Determines if a document element has text content
|
|
|
|
#
|
|
|
|
# element::
|
|
|
|
# XML element to be checked.
|
|
|
|
def empty_content?(element)
|
|
|
|
element.texts.join.blank?
|
|
|
|
end
|
|
|
|
end
|
2009-03-09 15:46:06 -04:00
|
|
|
end
|