1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

enable *real* IO parsing for the libxml, nokogiri and rexml backends [#2659 state:resolved]

Signed-off-by: Joshua Peek <josh@joshpeek.com>
This commit is contained in:
Brian Lopez 2009-06-09 09:01:50 -05:00 committed by Joshua Peek
parent a94e7d7897
commit 69bc2043f9
3 changed files with 26 additions and 16 deletions

View file

@ -9,16 +9,18 @@ module ActiveSupport
# data:: # data::
# XML Document string or IO to parse # XML Document string or IO to parse
def parse(data) def parse(data)
if data.respond_to?(:read) if !data.respond_to?(:read)
data = data.read data = StringIO.new(data || '')
end end
LibXML::XML.default_keep_blanks = false LibXML::XML.default_keep_blanks = false
if data.blank? char = data.getc
if char.nil?
{} {}
else else
LibXML::XML::Parser.string(data.strip).parse.to_hash data.ungetc(char)
LibXML::XML::Parser.io(data).parse.to_hash
end end
end end

View file

@ -9,13 +9,15 @@ module ActiveSupport
# data:: # data::
# XML Document string or IO to parse # XML Document string or IO to parse
def parse(data) def parse(data)
if data.respond_to?(:read) if !data.respond_to?(:read)
data = data.read data = StringIO.new(data || '')
end end
if data.blank? char = data.getc
if char.nil?
{} {}
else else
data.ungetc(char)
doc = Nokogiri::XML(data) doc = Nokogiri::XML(data)
raise doc.errors.first if doc.errors.length > 0 raise doc.errors.first if doc.errors.length > 0
doc.to_hash doc.to_hash

View file

@ -15,13 +15,19 @@ module ActiveSupport
# data:: # data::
# XML Document string or IO to parse # XML Document string or IO to parse
def parse(data) def parse(data)
if data.respond_to?(:read) if !data.respond_to?(:read)
data = data.read data = StringIO.new(data || '')
end
char = data.getc
if char.nil?
{}
else
data.ungetc(char)
require 'rexml/document' unless defined?(REXML::Document)
doc = REXML::Document.new(data)
merge_element!({}, doc.root)
end end
require 'rexml/document' unless defined?(REXML::Document)
doc = REXML::Document.new(data)
merge_element!({}, doc.root)
end end
private private