From 822c41d69d9228c9912d29ac45155d3a16bb5c50 Mon Sep 17 00:00:00 2001 From: Bart ten Brinke Date: Thu, 26 Feb 2009 00:24:42 +0100 Subject: XmlMini supports different backend parsers, starting with libxml [#2084 state:committed] Signed-off-by: Jeremy Kemper --- .../lib/active_support/xml_mini/libxml.rb | 131 +++++++++++++++++++++ activesupport/lib/active_support/xml_mini/rexml.rb | 106 +++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 activesupport/lib/active_support/xml_mini/libxml.rb create mode 100644 activesupport/lib/active_support/xml_mini/rexml.rb (limited to 'activesupport/lib/active_support/xml_mini') diff --git a/activesupport/lib/active_support/xml_mini/libxml.rb b/activesupport/lib/active_support/xml_mini/libxml.rb new file mode 100644 index 0000000000..dd271dc587 --- /dev/null +++ b/activesupport/lib/active_support/xml_mini/libxml.rb @@ -0,0 +1,131 @@ +# = XML Mini Libxml implementation +module ActiveSupport + module XmlMini + 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 + end + + end +end + +module XML + module Conversions + module Document + def to_hash + root.to_hash + end + end + + module Node + CONTENT_ROOT = '__content__' + LIB_XML_LIMIT = 30000000 # Hardcoded LibXML limit + + # Convert XML document to hash + # + # hash:: + # Hash to merge the converted element into. + def to_hash(hash={}) + if text? + raise RuntimeError if content.length >= LIB_XML_LIMIT + hash[CONTENT_ROOT] = content + else + sub_hash = insert_name_into_hash(hash, name) + attributes_to_hash(sub_hash) + if array? + children_array_to_hash(sub_hash) + elsif yaml? + children_yaml_to_hash(sub_hash) + else + children_to_hash(sub_hash) + end + end + hash + end + + protected + + # Insert name into hash + # + # hash:: + # Hash to merge the converted element into. + # name:: + # name to to merge into hash + def insert_name_into_hash(hash, name) + sub_hash = {} + if hash[name] + if !hash[name].kind_of? Array + hash[name] = [hash[name]] + end + hash[name] << sub_hash + else + hash[name] = sub_hash + end + sub_hash + end + + # Insert children into hash + # + # hash:: + # Hash to merge the children into. + def children_to_hash(hash={}) + each { |child| child.to_hash(hash) } + attributes_to_hash(hash) + hash + end + + # Convert xml attributes to hash + # + # hash:: + # Hash to merge the attributes into + def attributes_to_hash(hash={}) + each_attr { |attr| hash[attr.name] = attr.value } + hash + end + + # Convert array into hash + # + # hash:: + # Hash to merge the array into + def children_array_to_hash(hash={}) + hash[child.name] = map do |child| + returning({}) { |sub_hash| child.children_to_hash(sub_hash) } + end + hash + end + + # Convert yaml into hash + # + # hash:: + # Hash to merge the yaml into + def children_yaml_to_hash(hash = {}) + hash[CONTENT_ROOT] = content unless content.blank? + hash + end + + # Check if child is of type array + def array? + child? && child.next? && child.name == child.next.name + end + + # Check if child is of type yaml + def yaml? + attributes.collect{|x| x.value}.include?('yaml') + end + + end + end +end + +XML::Document.send(:include, XML::Conversions::Document) +XML::Node.send(:include, XML::Conversions::Node) \ No newline at end of file diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb new file mode 100644 index 0000000000..655eff168c --- /dev/null +++ b/activesupport/lib/active_support/xml_mini/rexml.rb @@ -0,0 +1,106 @@ +# = XmlMini ReXML implementation +module ActiveSupport + module XmlMini + extend self + + # Parse an XML Document string into a simple hash + # + # Same as XmlSimple::xml_in but doesn't shoot itself in the foot, + # and uses the defaults from ActiveSupport + # + # string:: + # XML Document string to parse + def parse(string) + require 'rexml/document' unless defined?(REXML::Document) + doc = REXML::Document.new(string) + merge_element!({}, doc.root) + 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:: + # Hash to add the converted emement to. + # 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 + merge!(hash, CONTENT_KEY, element.texts.sum(&:value)) + 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 +end \ No newline at end of file -- cgit v1.2.3 From a995a738ca10f9bef023689df70d26aad8931b9a Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Mon, 9 Mar 2009 19:00:21 +0100 Subject: Revert "XmlMini supports different backend parsers, starting with libxml" Spews a ton undefined method `default_keep_blanks=' for XML:Module errors. This reverts commit 822c41d69d9228c9912d29ac45155d3a16bb5c50. --- .../lib/active_support/xml_mini/libxml.rb | 131 --------------------- activesupport/lib/active_support/xml_mini/rexml.rb | 106 ----------------- 2 files changed, 237 deletions(-) delete mode 100644 activesupport/lib/active_support/xml_mini/libxml.rb delete mode 100644 activesupport/lib/active_support/xml_mini/rexml.rb (limited to 'activesupport/lib/active_support/xml_mini') diff --git a/activesupport/lib/active_support/xml_mini/libxml.rb b/activesupport/lib/active_support/xml_mini/libxml.rb deleted file mode 100644 index dd271dc587..0000000000 --- a/activesupport/lib/active_support/xml_mini/libxml.rb +++ /dev/null @@ -1,131 +0,0 @@ -# = XML Mini Libxml implementation -module ActiveSupport - module XmlMini - 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 - end - - end -end - -module XML - module Conversions - module Document - def to_hash - root.to_hash - end - end - - module Node - CONTENT_ROOT = '__content__' - LIB_XML_LIMIT = 30000000 # Hardcoded LibXML limit - - # Convert XML document to hash - # - # hash:: - # Hash to merge the converted element into. - def to_hash(hash={}) - if text? - raise RuntimeError if content.length >= LIB_XML_LIMIT - hash[CONTENT_ROOT] = content - else - sub_hash = insert_name_into_hash(hash, name) - attributes_to_hash(sub_hash) - if array? - children_array_to_hash(sub_hash) - elsif yaml? - children_yaml_to_hash(sub_hash) - else - children_to_hash(sub_hash) - end - end - hash - end - - protected - - # Insert name into hash - # - # hash:: - # Hash to merge the converted element into. - # name:: - # name to to merge into hash - def insert_name_into_hash(hash, name) - sub_hash = {} - if hash[name] - if !hash[name].kind_of? Array - hash[name] = [hash[name]] - end - hash[name] << sub_hash - else - hash[name] = sub_hash - end - sub_hash - end - - # Insert children into hash - # - # hash:: - # Hash to merge the children into. - def children_to_hash(hash={}) - each { |child| child.to_hash(hash) } - attributes_to_hash(hash) - hash - end - - # Convert xml attributes to hash - # - # hash:: - # Hash to merge the attributes into - def attributes_to_hash(hash={}) - each_attr { |attr| hash[attr.name] = attr.value } - hash - end - - # Convert array into hash - # - # hash:: - # Hash to merge the array into - def children_array_to_hash(hash={}) - hash[child.name] = map do |child| - returning({}) { |sub_hash| child.children_to_hash(sub_hash) } - end - hash - end - - # Convert yaml into hash - # - # hash:: - # Hash to merge the yaml into - def children_yaml_to_hash(hash = {}) - hash[CONTENT_ROOT] = content unless content.blank? - hash - end - - # Check if child is of type array - def array? - child? && child.next? && child.name == child.next.name - end - - # Check if child is of type yaml - def yaml? - attributes.collect{|x| x.value}.include?('yaml') - end - - end - end -end - -XML::Document.send(:include, XML::Conversions::Document) -XML::Node.send(:include, XML::Conversions::Node) \ No newline at end of file diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb deleted file mode 100644 index 655eff168c..0000000000 --- a/activesupport/lib/active_support/xml_mini/rexml.rb +++ /dev/null @@ -1,106 +0,0 @@ -# = XmlMini ReXML implementation -module ActiveSupport - module XmlMini - extend self - - # Parse an XML Document string into a simple hash - # - # Same as XmlSimple::xml_in but doesn't shoot itself in the foot, - # and uses the defaults from ActiveSupport - # - # string:: - # XML Document string to parse - def parse(string) - require 'rexml/document' unless defined?(REXML::Document) - doc = REXML::Document.new(string) - merge_element!({}, doc.root) - 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:: - # Hash to add the converted emement to. - # 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 - merge!(hash, CONTENT_KEY, element.texts.sum(&:value)) - 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 -end \ No newline at end of file -- cgit v1.2.3 From 146511e0181a8c6703dcff7b5236d81b64841dd5 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 9 Mar 2009 12:39:20 -0700 Subject: Revert "Revert "XmlMini supports different backend parsers, starting with libxml"" Will change to require a known-working libxml-ruby. This reverts commit a995a738ca10f9bef023689df70d26aad8931b9a. --- .../lib/active_support/xml_mini/libxml.rb | 131 +++++++++++++++++++++ activesupport/lib/active_support/xml_mini/rexml.rb | 106 +++++++++++++++++ 2 files changed, 237 insertions(+) create mode 100644 activesupport/lib/active_support/xml_mini/libxml.rb create mode 100644 activesupport/lib/active_support/xml_mini/rexml.rb (limited to 'activesupport/lib/active_support/xml_mini') diff --git a/activesupport/lib/active_support/xml_mini/libxml.rb b/activesupport/lib/active_support/xml_mini/libxml.rb new file mode 100644 index 0000000000..dd271dc587 --- /dev/null +++ b/activesupport/lib/active_support/xml_mini/libxml.rb @@ -0,0 +1,131 @@ +# = XML Mini Libxml implementation +module ActiveSupport + module XmlMini + 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 + end + + end +end + +module XML + module Conversions + module Document + def to_hash + root.to_hash + end + end + + module Node + CONTENT_ROOT = '__content__' + LIB_XML_LIMIT = 30000000 # Hardcoded LibXML limit + + # Convert XML document to hash + # + # hash:: + # Hash to merge the converted element into. + def to_hash(hash={}) + if text? + raise RuntimeError if content.length >= LIB_XML_LIMIT + hash[CONTENT_ROOT] = content + else + sub_hash = insert_name_into_hash(hash, name) + attributes_to_hash(sub_hash) + if array? + children_array_to_hash(sub_hash) + elsif yaml? + children_yaml_to_hash(sub_hash) + else + children_to_hash(sub_hash) + end + end + hash + end + + protected + + # Insert name into hash + # + # hash:: + # Hash to merge the converted element into. + # name:: + # name to to merge into hash + def insert_name_into_hash(hash, name) + sub_hash = {} + if hash[name] + if !hash[name].kind_of? Array + hash[name] = [hash[name]] + end + hash[name] << sub_hash + else + hash[name] = sub_hash + end + sub_hash + end + + # Insert children into hash + # + # hash:: + # Hash to merge the children into. + def children_to_hash(hash={}) + each { |child| child.to_hash(hash) } + attributes_to_hash(hash) + hash + end + + # Convert xml attributes to hash + # + # hash:: + # Hash to merge the attributes into + def attributes_to_hash(hash={}) + each_attr { |attr| hash[attr.name] = attr.value } + hash + end + + # Convert array into hash + # + # hash:: + # Hash to merge the array into + def children_array_to_hash(hash={}) + hash[child.name] = map do |child| + returning({}) { |sub_hash| child.children_to_hash(sub_hash) } + end + hash + end + + # Convert yaml into hash + # + # hash:: + # Hash to merge the yaml into + def children_yaml_to_hash(hash = {}) + hash[CONTENT_ROOT] = content unless content.blank? + hash + end + + # Check if child is of type array + def array? + child? && child.next? && child.name == child.next.name + end + + # Check if child is of type yaml + def yaml? + attributes.collect{|x| x.value}.include?('yaml') + end + + end + end +end + +XML::Document.send(:include, XML::Conversions::Document) +XML::Node.send(:include, XML::Conversions::Node) \ No newline at end of file diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb new file mode 100644 index 0000000000..655eff168c --- /dev/null +++ b/activesupport/lib/active_support/xml_mini/rexml.rb @@ -0,0 +1,106 @@ +# = XmlMini ReXML implementation +module ActiveSupport + module XmlMini + extend self + + # Parse an XML Document string into a simple hash + # + # Same as XmlSimple::xml_in but doesn't shoot itself in the foot, + # and uses the defaults from ActiveSupport + # + # string:: + # XML Document string to parse + def parse(string) + require 'rexml/document' unless defined?(REXML::Document) + doc = REXML::Document.new(string) + merge_element!({}, doc.root) + 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:: + # Hash to add the converted emement to. + # 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 + merge!(hash, CONTENT_KEY, element.texts.sum(&:value)) + 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 +end \ No newline at end of file -- cgit v1.2.3 From 87d82ef76e88d7ce97cfc09151b677b552f2840a Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 9 Mar 2009 12:46:06 -0700 Subject: Require libxml-ruby 0.9.4 or 0.9.7 pending a segfault bugfix for 1.0. Delegate parsing to a switchable backend. --- activesupport/lib/active_support/xml_mini/libxml.rb | 14 +++++++------- activesupport/lib/active_support/xml_mini/rexml.rb | 6 ++++-- 2 files changed, 11 insertions(+), 9 deletions(-) (limited to 'activesupport/lib/active_support/xml_mini') diff --git a/activesupport/lib/active_support/xml_mini/libxml.rb b/activesupport/lib/active_support/xml_mini/libxml.rb index dd271dc587..e1549d8c58 100644 --- a/activesupport/lib/active_support/xml_mini/libxml.rb +++ b/activesupport/lib/active_support/xml_mini/libxml.rb @@ -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 @@ module XML end XML::Document.send(:include, XML::Conversions::Document) -XML::Node.send(:include, XML::Conversions::Node) \ No newline at end of file +XML::Node.send(:include, XML::Conversions::Node) diff --git a/activesupport/lib/active_support/xml_mini/rexml.rb b/activesupport/lib/active_support/xml_mini/rexml.rb index 655eff168c..a8fdeca967 100644 --- a/activesupport/lib/active_support/xml_mini/rexml.rb +++ b/activesupport/lib/active_support/xml_mini/rexml.rb @@ -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 @@ module ActiveSupport element.texts.join.blank? end end -end \ No newline at end of file +end -- cgit v1.2.3 From d4091d3bc79731f55491cfb51c604a66502c944f Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Mar 2009 11:36:25 -0700 Subject: Properly set up libxml includes. Don't include LibXML in toplevel. [#2084 state:resolved] --- activesupport/lib/active_support/xml_mini/libxml.rb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'activesupport/lib/active_support/xml_mini') diff --git a/activesupport/lib/active_support/xml_mini/libxml.rb b/activesupport/lib/active_support/xml_mini/libxml.rb index e1549d8c58..3586b24a6b 100644 --- a/activesupport/lib/active_support/xml_mini/libxml.rb +++ b/activesupport/lib/active_support/xml_mini/libxml.rb @@ -1,4 +1,6 @@ -# = XML Mini Libxml implementation +require 'libxml' + +# = XmlMini LibXML implementation module ActiveSupport module XmlMini_LibXML #:nodoc: extend self @@ -7,19 +9,19 @@ module ActiveSupport # string:: # XML Document string to parse def parse(string) - XML.default_keep_blanks = false + LibXML::XML.default_keep_blanks = false if string.blank? {} else - XML::Parser.string(string.strip).parse.to_hash + LibXML::XML::Parser.string(string.strip).parse.to_hash end end end end -module XML +module LibXML module Conversions module Document def to_hash @@ -37,7 +39,7 @@ module XML # Hash to merge the converted element into. def to_hash(hash={}) if text? - raise RuntimeError if content.length >= LIB_XML_LIMIT + raise LibXML::XML::Error if content.length >= LIB_XML_LIMIT hash[CONTENT_ROOT] = content else sub_hash = insert_name_into_hash(hash, name) @@ -127,5 +129,5 @@ module XML end end -XML::Document.send(:include, XML::Conversions::Document) -XML::Node.send(:include, XML::Conversions::Node) +LibXML::XML::Document.send(:include, LibXML::Conversions::Document) +LibXML::XML::Node.send(:include, LibXML::Conversions::Node) -- cgit v1.2.3 From 694998ee4fb8d257ba78424cab630846327a0889 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Mon, 9 Mar 2009 17:27:39 -0700 Subject: Nokogiri backend for XmlMini [#2190 state:committed] Signed-off-by: Jeremy Kemper --- .../lib/active_support/xml_mini/nokogiri.rb | 67 ++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 activesupport/lib/active_support/xml_mini/nokogiri.rb (limited to 'activesupport/lib/active_support/xml_mini') 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 0000000000..bfafa29dd5 --- /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 -- cgit v1.2.3 From 37cf224fdb7259c139450bc33c68ec09489be9c2 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Tue, 10 Mar 2009 12:08:42 -0700 Subject: Make it easier to swap XmlMini backends. Require Nokogiri >= 1.1.1 for XmlMini backend tests. --- activesupport/lib/active_support/xml_mini/nokogiri.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'activesupport/lib/active_support/xml_mini') diff --git a/activesupport/lib/active_support/xml_mini/nokogiri.rb b/activesupport/lib/active_support/xml_mini/nokogiri.rb index bfafa29dd5..5c8a6bfe89 100644 --- a/activesupport/lib/active_support/xml_mini/nokogiri.rb +++ b/activesupport/lib/active_support/xml_mini/nokogiri.rb @@ -1,4 +1,4 @@ -# = XML Mini Nokogiri implementation +# = XmlMini Nokogiri implementation module ActiveSupport module XmlMini_Nokogiri #:nodoc: extend self @@ -7,8 +7,11 @@ module ActiveSupport # string:: # XML Document string to parse def parse(string) - return {} if string.blank? - doc = Nokogiri::XML(string).to_hash + if string.blank? + {} + else + Nokogiri::XML(string).to_hash + end end module Conversions -- cgit v1.2.3 From b9e021df974217b9c6ee273bd6c98b40ebde0cd3 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Tue, 10 Mar 2009 20:45:14 -0700 Subject: adding more nokogiri tests and making the main rails tests pass [#2190 state:resolved] Signed-off-by: Jeremy Kemper --- .../lib/active_support/xml_mini/nokogiri.rb | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'activesupport/lib/active_support/xml_mini') diff --git a/activesupport/lib/active_support/xml_mini/nokogiri.rb b/activesupport/lib/active_support/xml_mini/nokogiri.rb index 5c8a6bfe89..10281584fc 100644 --- a/activesupport/lib/active_support/xml_mini/nokogiri.rb +++ b/activesupport/lib/active_support/xml_mini/nokogiri.rb @@ -1,3 +1,5 @@ +require 'nokogiri' + # = XmlMini Nokogiri implementation module ActiveSupport module XmlMini_Nokogiri #:nodoc: @@ -10,7 +12,9 @@ module ActiveSupport if string.blank? {} else - Nokogiri::XML(string).to_hash + doc = Nokogiri::XML(string) + raise doc.errors.first if doc.errors.length > 0 + doc.to_hash end end @@ -31,8 +35,8 @@ module ActiveSupport def to_hash(hash = {}) hash[name] ||= attributes_as_hash - walker = lambda { |child, memo, callback| - next if child.blank? + walker = lambda { |memo, parent, child, callback| + next if child.blank? && 'file' != parent['type'] if child.text? (memo[CONTENT_ROOT] ||= '') << child.content @@ -41,18 +45,21 @@ module ActiveSupport name = child.name + child_hash = child.attributes_as_hash if memo[name] memo[name] = [memo[name]].flatten - memo[name] << child.attributes_as_hash + memo[name] << child_hash else - memo[name] = child.attributes_as_hash + memo[name] = child_hash end # Recusively walk children - child.children.each { |c| callback.call(c, memo[name], callback) } + child.children.each { |c| + callback.call(child_hash, child, c, callback) + } } - children.each { |c| walker.call(c, hash[name], walker) } + children.each { |c| walker.call(hash[name], self, c, walker) } hash end -- cgit v1.2.3