Class: ActiveSupport::XmlMini_REXMLSAX::HashBuilder

Inherits:
Object
  • Object
show all
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

CONTENT_KEY =
'__content__'.freeze
HASH_SIZE_KEY =
'__hash_size__'.freeze

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Instance Attribute Details

- (Object) hash (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

- (Object) characters(string) 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

- (Object) current_hash



22
23
24
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 22

def current_hash
  @hash_stack.last
end

- (Object) end_document



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

- (Object) end_element(uri, name, qname)



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

- (Object) error(error_message)



35
36
37
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 35

def error(error_message)
  raise error_message
end

- (Object) start_document



26
27
28
29
# File 'lib/rfm/xml_mini/rexml_sax.rb', line 26

def start_document
  @hash = {}
  @hash_stack = [@hash]
end

- (Object) start_element(uri, name, qname, attrs = [])



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