0
-# This is a derivitive work of XmlSimple 1.0.11
0
-# Author:: Joseph Holsten <joseph@josephholsten.com>
0
-# Copyright:: Copyright (c) 2008 Joseph Holsten
0
-# Copyright:: Copyright (c) 2003-2006 Maik Schmidt <contact@maik-schmidt.de>
0
-# License:: Distributes under the same terms as Ruby.
0
- require 'rexml/document'
0
- CONTENT_KEY = '__content__'
0
- # Parse an XML Document string into a simple hash
0
- # Same as XmlSimple::xml_in but doesn't shoot itself in the foot,
0
- # and uses the defaults from ActiveSupport
0
- # XML Document string to parse
0
- def self.parse(string)
0
- doc = REXML::Document.new(string)
0
- merge_element!({}, doc.root)
0
- # Convert an XML element and merge into the hash
0
- # Hash to merge the converted element into.
0
- # XML element to merge into hash
0
- def self.merge_element!(hash, element)
0
- merge!(hash, element.name, collapse(element))
0
- # Actually converts an XML document element into a data structure.
0
- # The document element to be collapsed.
0
- def self.collapse(element)
0
- hash = get_attributes(element)
0
- if element.has_elements?
0
- element.each_element {|child| merge_element!(hash, child) }
0
- merge_texts!(hash, element) unless empty_content?(element)
0
- return merge_texts!(hash, element)
0
- # Merge all the texts of an element into the hash
0
- # Hash to add the converted emement to.
0
- # XML element whose texts are to me merged into the hash
0
- def self.merge_texts!(hash, element)
0
- unless element.has_text?
0
- # must use value to prevent double-escaping
0
- text_values = element.texts.map {|t| t.value }
0
- merge!(hash, CONTENT_KEY, text_values.join)
0
- # Adds a new key/value pair to an existing Hash. If the key to be added
0
- # already exists and the existing value associated with key is not
0
- # an Array, it will be wrapped in an Array. Then the new value is
0
- # appended to that Array.
0
- # Hash to add key/value pair to.
0
- # Value to be associated with key.
0
- def self.merge!(hash, key, value)
0
- if hash[key].instance_of?(Array)
0
- hash[key] = [ hash[key], value ]
0
- elsif value.instance_of?(Array)
0
- # Converts the attributes array of an XML element into a hash.
0
- # Returns an empty Hash if node has no attributes.
0
- # XML element to extract attributes from.
0
- def self.get_attributes(element)
0
- element.attributes.each { |n,v| attributes[n] = v }
0
- # Determines if a document element has text content
0
- # XML element to be checked.
0
- def self.empty_content?(element)
0
- element.texts.join.strip.empty?
0
-# This module exists to decorate files deserialized using Hash.from_xml with
0
-# the <tt>original_filename</tt> and <tt>content_type</tt> methods.
0
-module FileLike #:nodoc:
0
- attr_writer :original_filename, :content_type
0
- @original_filename || 'untitled'
0
- @content_type || 'application/octet-stream'
0
+require 'active_support/xml_mini'
0
module ActiveSupport #:nodoc:
0
module CoreExtensions #:nodoc:
0
+ # This module exists to decorate files deserialized using Hash.from_xml with
0
+ # the <tt>original_filename</tt> and <tt>content_type</tt> methods.
0
+ module FileLike #:nodoc:
0
+ attr_writer :original_filename, :content_type
0
+ @original_filename || 'untitled'
0
+ @content_type || 'application/octet-stream'