module ActiveSupport::XmlMini_JDOM

def collapse(element, depth)

The document element to be collapsed.
element::

Actually converts an XML document element into a data structure.
def collapse(element, depth)
  hash = get_attributes(element)
  child_nodes = element.child_nodes
  if child_nodes.length > 0
    (0...child_nodes.length).each do |i|
      child = child_nodes.item(i)
      merge_element!(hash, child, depth - 1) unless child.node_type == Node::TEXT_NODE
    end
    merge_texts!(hash, element) unless empty_content?(element)
    hash
  else
    merge_texts!(hash, element)
  end
end

def delete_empty(hash)

def delete_empty(hash)
  hash.delete(CONTENT_KEY) if hash[CONTENT_KEY] == ""
end

def empty_content?(element)

XML element to be checked.
element::

Determines if a document element has text content
def empty_content?(element)
  text = +""
  child_nodes = element.child_nodes
  (0...child_nodes.length).each do |i|
    item = child_nodes.item(i)
    if item.node_type == Node::TEXT_NODE
      text << item.get_data.strip
    end
  end
  text.strip.length == 0
end

def get_attributes(element)

XML element to extract attributes from.
element::

Returns an empty Hash if node has no attributes.
Converts the attributes array of an XML element into a hash.
def get_attributes(element)
  attribute_hash = {}
  attributes = element.attributes
  (0...attributes.length).each do |i|
    attribute_hash[CONTENT_KEY] ||= ""
    attribute_hash[attributes.item(i).name] = attributes.item(i).value
  end
  attribute_hash
end

def merge!(hash, key, value)

Value to be associated with key.
value::
Key to be added.
key::
Hash to add key/value pair to.
hash::

appended to that Array.
an Array, it will be wrapped in an Array. Then the new value is
already exists and the existing value associated with key is not
Adds a new key/value pair to an existing Hash. If the key to be added
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

def merge_element!(hash, element, depth)

XML element to merge into hash
element::
Hash to merge the converted element into.
hash::

Convert an XML element and merge into the hash
def merge_element!(hash, element, depth)
  raise "Document too deep!" if depth == 0
  delete_empty(hash)
  merge!(hash, element.tag_name, collapse(element, depth))
end

def merge_texts!(hash, element)

XML element whose texts are to me merged into the hash
element::
Hash to add the converted element to.
hash::

Merge all the texts of an element into the hash
def merge_texts!(hash, element)
  delete_empty(hash)
  text_children = texts(element)
  if text_children.join.empty?
    hash
  else
    # must use value to prevent double-escaping
    merge!(hash, CONTENT_KEY, text_children.join)
  end
end

def parse(data)

XML Document string or IO to parse
data::
Parse an XML Document string or IO into a simple hash using Java's jdom.
def parse(data)
  if data.respond_to?(:read)
    data = data.read
  end
  if data.blank?
    {}
  else
    @dbf = DocumentBuilderFactory.new_instance
    # secure processing of java xml
    # https://archive.is/9xcQQ
    @dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false)
    @dbf.setFeature("http://xml.org/sax/features/external-general-entities", false)
    @dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false)
    @dbf.setFeature(javax.xml.XMLConstants::FEATURE_SECURE_PROCESSING, true)
    xml_string_reader = StringReader.new(data)
    xml_input_source = InputSource.new(xml_string_reader)
    doc = @dbf.new_document_builder.parse(xml_input_source)
    merge_element!({ CONTENT_KEY => "" }, doc.document_element, XmlMini.depth)
  end
end

def texts(element)

XML element to be checked.
element::

Determines if a document element has text content
def texts(element)
  texts = []
  child_nodes = element.child_nodes
  (0...child_nodes.length).each do |i|
    item = child_nodes.item(i)
    if item.node_type == Node::TEXT_NODE
      texts << item.get_data
    end
  end
  texts
end