123456789_123456789_123456789_123456789_123456789_

Module: ActiveSupport::XmlMini_REXML

Do not use. This module is for internal use only.
Relationships & Source Files
Defined in: activesupport/lib/active_support/xml_mini/rexml.rb

Constant Summary

Instance Method Summary

Instance Method Details

#collapse(element, depth) (private)

Actually converts an XML document element into a data structure.

element

The document element to be collapsed.

[ GitHub ]

  
# File 'activesupport/lib/active_support/xml_mini/rexml.rb', line 63

def collapse(element, depth)
  hash = get_attributes(element)

  if element.has_elements?
    element.each_element { |child| merge_element!(hash, child, depth - 1) }
    merge_texts!(hash, element) unless empty_content?(element)
    hash
  else
    merge_texts!(hash, element)
  end
end

#empty_content?(element) ⇒ Boolean (private)

Determines if a document element has text content

element

XML element to be checked.

[ GitHub ]

  
# File 'activesupport/lib/active_support/xml_mini/rexml.rb', line 133

def empty_content?(element)
  element.texts.join.blank?
end

#get_attributes(element) (private)

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/xml_mini/rexml.rb', line 123

def get_attributes(element)
  attributes = {}
  element.attributes.each { |n, v| attributes[n] = v }
  attributes
end

#merge!(hash, key, value) (private)

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.

[ GitHub ]

  
# File 'activesupport/lib/active_support/xml_mini/rexml.rb', line 103

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

#merge_element!(hash, element, depth) (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

Raises:

  • (REXML::ParseException)
[ GitHub ]

  
# File 'activesupport/lib/active_support/xml_mini/rexml.rb', line 54

def merge_element!(hash, element, depth)
  raise REXML::ParseException, "The document is too deep" if depth == 0
  merge!(hash, element.name, collapse(element, depth))
end

#merge_texts!(hash, element) (private)

Merge all the texts of an element into the hash

hash

Hash to add the converted element to.

element

XML element whose texts are to me merged into the hash

[ GitHub ]

  
# File 'activesupport/lib/active_support/xml_mini/rexml.rb', line 81

def merge_texts!(hash, element)
  unless element.has_text?
    hash
  else
    # must use value to prevent double-escaping
    texts = +""
    element.texts.each { |t| texts << t.value }
    merge!(hash, CONTENT_KEY, texts)
  end
end

#parse(data)

Parse an XML Document string or ::IO into a simple hash.

Same as XmlSimple.xml_in but doesn’t shoot itself in the foot, and uses the defaults from Active Support.

data

XML Document string or IO to parse

[ GitHub ]

  
# File 'activesupport/lib/active_support/xml_mini/rexml.rb', line 20

def parse(data)
  if !data.respond_to?(:read)
    data = StringIO.new(data || "")
  end

  if data.eof?
    {}
  else
    require_rexml unless defined?(REXML::Document)
    doc = REXML::Document.new(data)

    if doc.root
      merge_element!({}, doc.root, XmlMini.depth)
    else
      raise REXML::ParseException,
        "The document #{doc.to_s.inspect} does not have a valid root"
    end
  end
end

#require_rexml (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/xml_mini/rexml.rb', line 41

def require_rexml
  silence_warnings { require "rexml/document" }
rescue LoadError => e
  warn "You don't have rexml installed in your application. Please add it to your Gemfile and run bundle install"
  raise e
end