2009-03-10 14:36:25 -04:00
|
|
|
require 'libxml'
|
2010-01-01 15:12:19 -05:00
|
|
|
require 'active_support/core_ext/object/blank'
|
2011-01-18 14:45:52 -05:00
|
|
|
require 'stringio'
|
2009-03-10 14:36:25 -04:00
|
|
|
|
|
|
|
# = XmlMini LibXML implementation
|
2009-03-09 15:39:20 -04:00
|
|
|
module ActiveSupport
|
2009-03-09 15:46:06 -04:00
|
|
|
module XmlMini_LibXML #:nodoc:
|
2009-03-09 15:39:20 -04:00
|
|
|
extend self
|
|
|
|
|
2009-05-17 11:37:30 -04:00
|
|
|
# Parse an XML Document string or IO into a simple hash using libxml.
|
|
|
|
# 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 || '')
|
2009-05-17 11:37:30 -04:00
|
|
|
end
|
2010-01-01 04:12:30 -05:00
|
|
|
|
2009-06-09 10:01:50 -04:00
|
|
|
char = data.getc
|
|
|
|
if char.nil?
|
2009-03-09 15:46:06 -04:00
|
|
|
{}
|
|
|
|
else
|
2009-06-09 10:01:50 -04:00
|
|
|
data.ungetc(char)
|
|
|
|
LibXML::XML::Parser.io(data).parse.to_hash
|
2009-03-09 15:46:06 -04:00
|
|
|
end
|
2009-03-09 15:39:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-21 21:44:00 -04:00
|
|
|
module LibXML #:nodoc:
|
|
|
|
module Conversions #:nodoc:
|
|
|
|
module Document #:nodoc:
|
2009-03-09 15:39:20 -04:00
|
|
|
def to_hash
|
|
|
|
root.to_hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-21 21:44:00 -04:00
|
|
|
module Node #:nodoc:
|
2010-01-01 04:12:30 -05:00
|
|
|
CONTENT_ROOT = '__content__'.freeze
|
2009-03-09 15:39:20 -04:00
|
|
|
|
|
|
|
# Convert XML document to hash
|
|
|
|
#
|
|
|
|
# hash::
|
|
|
|
# Hash to merge the converted element into.
|
|
|
|
def to_hash(hash={})
|
2010-01-01 04:12:30 -05:00
|
|
|
node_hash = {}
|
2009-03-09 15:39:20 -04:00
|
|
|
|
2010-01-01 04:12:30 -05:00
|
|
|
# Insert node hash into parent hash correctly.
|
|
|
|
case hash[name]
|
|
|
|
when Array then hash[name] << node_hash
|
|
|
|
when Hash then hash[name] = [hash[name], node_hash]
|
|
|
|
when nil then hash[name] = node_hash
|
2009-03-09 15:39:20 -04:00
|
|
|
end
|
|
|
|
|
2010-01-01 04:12:30 -05:00
|
|
|
# Handle child elements
|
|
|
|
each_child do |c|
|
|
|
|
if c.element?
|
|
|
|
c.to_hash(node_hash)
|
|
|
|
elsif c.text? || c.cdata?
|
|
|
|
node_hash[CONTENT_ROOT] ||= ''
|
|
|
|
node_hash[CONTENT_ROOT] << c.content
|
2009-10-02 10:34:28 -04:00
|
|
|
end
|
2009-03-09 15:39:20 -04:00
|
|
|
end
|
|
|
|
|
2010-01-01 04:12:30 -05:00
|
|
|
# Remove content node if it is blank
|
|
|
|
if node_hash.length > 1 && node_hash[CONTENT_ROOT].blank?
|
|
|
|
node_hash.delete(CONTENT_ROOT)
|
2009-03-09 15:39:20 -04:00
|
|
|
end
|
|
|
|
|
2010-01-01 04:12:30 -05:00
|
|
|
# Handle attributes
|
|
|
|
each_attr { |a| node_hash[a.name] = a.value }
|
2009-03-09 15:39:20 -04:00
|
|
|
|
2010-01-01 04:12:30 -05:00
|
|
|
hash
|
|
|
|
end
|
2009-03-09 15:39:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-03-10 14:36:25 -04:00
|
|
|
LibXML::XML::Document.send(:include, LibXML::Conversions::Document)
|
|
|
|
LibXML::XML::Node.send(:include, LibXML::Conversions::Node)
|