Permalink
Browse files

Require libxml-ruby 0.9.4 or 0.9.7 pending a segfault bugfix for 1.0.

Delegate parsing to a switchable backend.
  • Loading branch information...
1 parent 146511e commit 87d82ef76e88d7ce97cfc09151b677b552f2840a @jeremy jeremy committed Mar 9, 2009
@@ -1,21 +1,19 @@
-# = XmlMini
module ActiveSupport
+ # = XmlMini
module XmlMini
extend self
+ delegate :parse, :to => :@backend
- CONTENT_KEY = '__content__'.freeze
-
- # Hook the correct parser into XmlMini
- def hook_parser
- begin
- require 'xml/libxml' unless defined? LibXML
- require "active_support/xml_mini/libxml.rb"
- rescue MissingSourceFile => e
- require "active_support/xml_mini/rexml.rb"
- end
+ def backend=(name)
+ require "active_support/xml_mini/#{name.to_s.downcase}.rb"
+ @backend = ActiveSupport.const_get("XmlMini_#{name}")
end
+ end
- hook_parser
-
+ begin
+ gem 'libxml-ruby', '=0.9.4', '=0.9.7'
+ XmlMini.backend = 'LibXML'
+ rescue Gem::LoadError
+ XmlMini.backend = 'REXML'
end
-end
+end
@@ -1,19 +1,19 @@
# = XML Mini Libxml implementation
module ActiveSupport
- module XmlMini
+ module XmlMini_LibXML #:nodoc:
extend self
# Parse an XML Document string into a simple hash using libxml.
# string::
# XML Document string to parse
def parse(string)
- require 'xml/libxml' unless defined? LibXML
-
- string.strip!
XML.default_keep_blanks = false
- return {} if string.blank?
- return XML::Parser.string(string).parse.to_hash
+ if string.blank?
+ {}
+ else
+ XML::Parser.string(string.strip).parse.to_hash
+ end
end
end
@@ -128,4 +128,4 @@ def yaml?
end
XML::Document.send(:include, XML::Conversions::Document)
-XML::Node.send(:include, XML::Conversions::Node)
+XML::Node.send(:include, XML::Conversions::Node)
@@ -1,8 +1,10 @@
# = XmlMini ReXML implementation
module ActiveSupport
- module XmlMini
+ module XmlMini_REXML #:nodoc:
extend self
+ CONTENT_KEY = '__content__'.freeze
+
# Parse an XML Document string into a simple hash
#
# Same as XmlSimple::xml_in but doesn't shoot itself in the foot,
@@ -103,4 +105,4 @@ def empty_content?(element)
element.texts.join.blank?
end
end
-end
+end
@@ -884,7 +884,8 @@ def test_array_values_are_not_sorted
end
def test_expansion_count_is_limited
- assert_raise RuntimeError do
+ expected = defined?(LibXML) ? LibXML::XML::Error : RuntimeError
+ assert_raise expected do
attack_xml = <<-EOT
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE member [

0 comments on commit 87d82ef

Please sign in to comment.