Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 112 lines (102 sloc) 3.115 kb
b7fef26 @jeremy Extract XmlMini. Namespace FileLike extension.
jeremy authored
1 # = XmlMini
2 # This is a derivitive work of XmlSimple 1.0.11
3 # Author:: Joseph Holsten <joseph@josephholsten.com>
4 # Copyright:: Copyright (c) 2008 Joseph Holsten
5 # Copyright:: Copyright (c) 2003-2006 Maik Schmidt <contact@maik-schmidt.de>
6 # License:: Distributes under the same terms as Ruby.
7 module XmlMini
8 extend self
9
10 CONTENT_KEY = '__content__'.freeze
11
12 # Parse an XML Document string into a simple hash
13 #
14 # Same as XmlSimple::xml_in but doesn't shoot itself in the foot,
15 # and uses the defaults from ActiveSupport
16 #
17 # string::
18 # XML Document string to parse
19 def parse(string)
20 require 'rexml/document'
21 doc = REXML::Document.new(string)
22 merge_element!({}, doc.root)
23 end
24
25 private
26 # Convert an XML element and merge into the hash
27 #
28 # hash::
29 # Hash to merge the converted element into.
30 # element::
31 # XML element to merge into hash
32 def merge_element!(hash, element)
33 merge!(hash, element.name, collapse(element))
34 end
35
36 # Actually converts an XML document element into a data structure.
37 #
38 # element::
39 # The document element to be collapsed.
40 def collapse(element)
41 hash = get_attributes(element)
42
43 if element.has_elements?
44 element.each_element {|child| merge_element!(hash, child) }
45 merge_texts!(hash, element) unless empty_content?(element)
46 hash
47 else
48 merge_texts!(hash, element)
49 end
50 end
51
52 # Merge all the texts of an element into the hash
53 #
54 # hash::
55 # Hash to add the converted emement to.
56 # element::
57 # XML element whose texts are to me merged into the hash
58 def merge_texts!(hash, element)
59 unless element.has_text?
60 hash
61 else
62 # must use value to prevent double-escaping
63 merge!(hash, CONTENT_KEY, element.texts.sum(&:value))
64 end
65 end
66
67 # Adds a new key/value pair to an existing Hash. If the key to be added
68 # already exists and the existing value associated with key is not
69 # an Array, it will be wrapped in an Array. Then the new value is
70 # appended to that Array.
71 #
72 # hash::
73 # Hash to add key/value pair to.
74 # key::
75 # Key to be added.
76 # value::
77 # Value to be associated with key.
78 def merge!(hash, key, value)
79 if hash.has_key?(key)
80 if hash[key].instance_of?(Array)
81 hash[key] << value
82 else
83 hash[key] = [hash[key], value]
84 end
85 elsif value.instance_of?(Array)
86 hash[key] = [value]
87 else
88 hash[key] = value
89 end
90 hash
91 end
92
93 # Converts the attributes array of an XML element into a hash.
94 # Returns an empty Hash if node has no attributes.
95 #
96 # element::
97 # XML element to extract attributes from.
98 def get_attributes(element)
99 attributes = {}
100 element.attributes.each { |n,v| attributes[n] = v }
101 attributes
102 end
103
104 # Determines if a document element has text content
105 #
106 # element::
107 # XML element to be checked.
108 def empty_content?(element)
109 element.texts.join.blank?
110 end
111 end
Something went wrong with that request. Please try again.