Class: ActiveSupport::XmlMini_REXMLSAX::HashBuilder
- Includes:
- REXML::SAX2Listener
- Defined in:
- lib/rfm/xml_mini/rexml_sax.rb
Overview
Class that will build the hash while the XML document is being parsed using streaming events.
Constant Summary collapse
- CONTENT_KEY =
'__content__'.freeze
- HASH_SIZE_KEY =
'__hash_size__'.freeze
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Instance Method Summary collapse
- #characters(string) ⇒ Object (also: #cdata)
- #current_hash ⇒ Object
- #end_document ⇒ Object
- #end_element(uri, name, qname) ⇒ Object
- #error(error_message) ⇒ Object
- #start_document ⇒ Object
- #start_element(uri, name, qname, attrs = []) ⇒ Object
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
20 21 22 |
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 20 def hash @hash end |
Instance Method Details
#characters(string) ⇒ Object Also known as: cdata
61 62 63 64 65 |
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 61 def characters(string) return unless string and current_hash[CONTENT_KEY] #puts "CHARACTERS #{string}" current_hash[CONTENT_KEY] << string end |
#current_hash ⇒ Object
22 23 24 |
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 22 def current_hash @hash_stack.last end |
#end_document ⇒ Object
31 32 33 |
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 31 def end_document raise "Parse stack not empty!" if @hash_stack.size > 1 end |
#end_element(uri, name, qname) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 54 def end_element(uri, name, qname) if current_hash.length > current_hash.delete(HASH_SIZE_KEY) && current_hash[CONTENT_KEY].blank? || current_hash[CONTENT_KEY] == '' current_hash.delete(CONTENT_KEY) end @hash_stack.pop end |
#error(error_message) ⇒ Object
35 36 37 |
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 35 def error() raise end |
#start_document ⇒ Object
26 27 28 29 |
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 26 def start_document @hash = {} @hash_stack = [@hash] end |
#start_element(uri, name, qname, attrs = []) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 39 def start_element(uri, name, qname, attrs = []) #puts "START_ELEMENT #{name}" #y attrs new_hash = { CONTENT_KEY => '' }.merge(Hash[attrs]) new_hash[HASH_SIZE_KEY] = new_hash.size + 1 case current_hash[name] when Array then current_hash[name] << new_hash when Hash then current_hash[name] = [current_hash[name], new_hash] when nil then current_hash[name] = new_hash end @hash_stack.push(new_hash) end |