From e10bcf4f6543d8307cb3ba116375666fed486b24 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 9 Mar 2009 17:27:39 -0700 Subject: [PATCH] adding a nokogiri back end for xml_mini --- activesupport/lib/active_support/xml_mini.rb | 4 + .../lib/active_support/xml_mini/nokogiri.rb | 67 ++++++++++++ .../test/xml_mini/nokogiri_engine_test.rb | 112 ++++++++++++++++++++ activesupport/test/xml_mini/rexml_engine_test.rb | 15 +++ 4 files changed, 198 insertions(+), 0 deletions(-) create mode 100644 activesupport/lib/active_support/xml_mini/nokogiri.rb create mode 100644 activesupport/test/xml_mini/nokogiri_engine_test.rb create mode 100644 activesupport/test/xml_mini/rexml_engine_test.rb diff --git a/activesupport/lib/active_support/xml_mini.rb b/activesupport/lib/active_support/xml_mini.rb index 99158e4..0513c0d 100644 --- a/activesupport/lib/active_support/xml_mini.rb +++ b/activesupport/lib/active_support/xml_mini.rb @@ -8,6 +8,10 @@ module ActiveSupport extend self delegate :parse, :to => :@backend + class << self + attr_reader :backend + end + def backend=(name) require "active_support/xml_mini/#{name.to_s.downcase}.rb" @backend = ActiveSupport.const_get("XmlMini_#{name}") diff --git a/activesupport/lib/active_support/xml_mini/nokogiri.rb b/activesupport/lib/active_support/xml_mini/nokogiri.rb new file mode 100644 index 0000000..bfafa29 --- /dev/null +++ b/activesupport/lib/active_support/xml_mini/nokogiri.rb @@ -0,0 +1,67 @@ +# = XML Mini Nokogiri implementation +module ActiveSupport + module XmlMini_Nokogiri #:nodoc: + extend self + + # Parse an XML Document string into a simple hash using libxml / nokogiri. + # string:: + # XML Document string to parse + def parse(string) + return {} if string.blank? + doc = Nokogiri::XML(string).to_hash + end + + module Conversions + module Document + def to_hash + root.to_hash + end + end + + module Node + CONTENT_ROOT = '__content__' + + # Convert XML document to hash + # + # hash:: + # Hash to merge the converted element into. + def to_hash(hash = {}) + hash[name] ||= attributes_as_hash + + walker = lambda { |child, memo, callback| + next if child.blank? + + if child.text? + (memo[CONTENT_ROOT] ||= '') << child.content + next + end + + name = child.name + + if memo[name] + memo[name] = [memo[name]].flatten + memo[name] << child.attributes_as_hash + else + memo[name] = child.attributes_as_hash + end + + # Recusively walk children + child.children.each { |c| callback.call(c, memo[name], callback) } + } + + children.each { |c| walker.call(c, hash[name], walker) } + hash + end + + def attributes_as_hash + Hash[*(attribute_nodes.map { |node| + [node.node_name, node.value] + }.flatten)] + end + end + end + + Nokogiri::XML::Document.send(:include, Conversions::Document) + Nokogiri::XML::Node.send(:include, Conversions::Node) + end +end diff --git a/activesupport/test/xml_mini/nokogiri_engine_test.rb b/activesupport/test/xml_mini/nokogiri_engine_test.rb new file mode 100644 index 0000000..5c4002d --- /dev/null +++ b/activesupport/test/xml_mini/nokogiri_engine_test.rb @@ -0,0 +1,112 @@ +require 'abstract_unit' +require 'active_support/xml_mini' + + +begin + +require 'nokogiri' + +class NokogiriEngineTest < Test::Unit::TestCase + include ActiveSupport + + def setup + @default_backend = XmlMini.backend.to_s.split('_').last + XmlMini.backend = 'Nokogiri' + end + + def teardown + XmlMini.backend = @default_backend + end + + def test_setting_nokogiri_as_backend + XmlMini.backend = 'Nokogiri' + assert_equal XmlMini_Nokogiri, XmlMini.backend + end + + def test_blank_returns_empty_hash + assert_equal({}, XmlMini.parse(nil)) + assert_equal({}, XmlMini.parse('')) + end + + def test_one_node_document_as_hash + assert_equal_rexml(<<-eoxml) + + eoxml + end + + def test_one_node_with_attributes_document_as_hash + assert_equal_rexml(<<-eoxml) + + eoxml + end + + def test_products_node_with_book_node_as_hash + assert_equal_rexml(<<-eoxml) + + + + eoxml + end + + def test_products_node_with_two_book_nodes_as_hash + assert_equal_rexml(<<-eoxml) + + + + + eoxml + end + + def test_single_node_with_content_as_hash + assert_equal_rexml(<<-eoxml) + + hello world + + eoxml + end + + def test_children_with_children + assert_equal_rexml(<<-eoxml) + + + + + + eoxml + end + + def test_children_with_text + assert_equal_rexml(<<-eoxml) + + + hello everyone + + + eoxml + end + + def test_children_with_non_adjacent_text + assert_equal_rexml(<<-eoxml) + + good + + hello everyone + + morning + + eoxml + end + + private + def assert_equal_rexml(xml) + XmlMini.backend = 'REXML' + hash = XmlMini.parse(xml) + + XmlMini.backend = 'Nokogiri' + + assert_equal(hash, XmlMini.parse(xml)) + end +end +rescue LoadError + # Yay, no errors +end diff --git a/activesupport/test/xml_mini/rexml_engine_test.rb b/activesupport/test/xml_mini/rexml_engine_test.rb new file mode 100644 index 0000000..a412d8c --- /dev/null +++ b/activesupport/test/xml_mini/rexml_engine_test.rb @@ -0,0 +1,15 @@ +require 'abstract_unit' +require 'active_support/xml_mini' + +class REXMLEngineTest < Test::Unit::TestCase + include ActiveSupport + + def test_default_is_rexml + assert_equal XmlMini_REXML, XmlMini.backend + end + + def test_set_rexml_as_backend + XmlMini.backend = 'REXML' + assert_equal XmlMini_REXML, XmlMini.backend + end +end -- 1.6.1.3