Class: SAXMachine::SAXHandler
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- SAXMachine::SAXHandler
- Defined in:
- lib/lazy-sax-machine/sax_handler.rb
Instance Attribute Summary collapse
-
#stack ⇒ Object
readonly
Returns the value of attribute stack.
Instance Method Summary collapse
- #cdata_block(string) ⇒ Object
-
#characters(string) ⇒ Object
characters might be called multiple times according to docs.
- #end_element(name) ⇒ Object
-
#initialize(object) ⇒ SAXHandler
constructor
A new instance of SAXHandler.
-
#mark_as_parsed(object, element_config) ⇒ Object
now we have an array where the next element represents the child of the previous once we come back to a parent element, the child’s hash should be removed.
- #parsed_config?(object, element_config) ⇒ Boolean
- #start_element(name, attrs = []) ⇒ Object
Constructor Details
#initialize(object) ⇒ SAXHandler
Returns a new instance of SAXHandler.
7 8 9 10 |
# File 'lib/lazy-sax-machine/sax_handler.rb', line 7 def initialize(object) @stack = [[object, nil, ""]] @parsed_configs = [] end |
Instance Attribute Details
#stack ⇒ Object (readonly)
Returns the value of attribute stack.
5 6 7 |
# File 'lib/lazy-sax-machine/sax_handler.rb', line 5 def stack @stack end |
Instance Method Details
#cdata_block(string) ⇒ Object
21 22 23 |
# File 'lib/lazy-sax-machine/sax_handler.rb', line 21 def cdata_block(string) characters(string) end |
#characters(string) ⇒ Object
characters might be called multiple times according to docs
13 14 15 16 17 18 19 |
# File 'lib/lazy-sax-machine/sax_handler.rb', line 13 def characters(string) if value = stack.last[2] value << string else stack.last.push(string) end end |
#end_element(name) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/lazy-sax-machine/sax_handler.rb', line 57 def end_element(name) (object, tag_config, _), (element, config, value) = stack[-2..-1] return unless stack.size > 1 && config && config.name == name stack.pop unless parsed_config?(object, config) if (element_value_config = config.data_class.respond_to?(:sax_config) && config.data_class.sax_config.element_values_for_element) element_value_config.each { |evc| element.send(evc.setter, value) } end if config.respond_to?(:accessor) subconfig = element.class.sax_config if element.class.respond_to?(:sax_config) if econf = subconfig.element_config_for_tag(name,[]) element.send(econf.setter, value) unless econf.value_configured? end object.send("add_#{config.accessor}", element) else if config.data_class tmp = value element.define_singleton_method(:inner_text) { tmp } value = element else value.define_singleton_method(:inner_text) { value } end object.send(config.setter, value) if value mark_as_parsed(object, config) end end end |
#mark_as_parsed(object, element_config) ⇒ Object
now we have an array where the next element represents the child of the previous once we come back to a parent element, the child’s hash should be removed
90 91 92 93 94 95 |
# File 'lib/lazy-sax-machine/sax_handler.rb', line 90 def mark_as_parsed(object, element_config) return if element_config.collection? (@parsed_configs[stack.size] ||= {})[ [object.object_id, element_config.object_id] ] = true end |
#parsed_config?(object, element_config) ⇒ Boolean
97 98 99 100 101 102 103 104 105 106 |
# File 'lib/lazy-sax-machine/sax_handler.rb', line 97 def parsed_config?(object, element_config) last_size = @stack_size || 0 @stack_size = stack.size if @stack_size < last_size # free memory @parsed_configs.slice!(@stack_size + 1 .. -1) end h = @parsed_configs[@stack_size] h and h[[object.object_id, element_config.object_id]] end |
#start_element(name, attrs = []) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/lazy-sax-machine/sax_handler.rb', line 25 def start_element(name, attrs = []) object, config, _ = stack.last sax_config = object.class.respond_to?(:sax_config) ? object.class.sax_config : nil if sax_config if collection_config = sax_config.collection_config(name, attrs) stack.push [object = collection_config.data_class.new, collection_config] sax_config = object.class.sax_config if (attribute_config = object.class.respond_to?(:sax_config) && object.class.sax_config.attribute_configs_for_element(attrs)) attribute_config.each { |ac| object.send(ac.setter, ac.value_from_attrs(attrs)) } end end sax_config.element_configs_for_attribute(name, attrs).each do |ec| unless parsed_config?(object, ec) object.send(ec.setter, ec.value_from_attrs(attrs)) mark_as_parsed(object, ec) end end if !collection_config && element_config = sax_config.element_config_for_tag(name, attrs) new_object = element_config.data_class ? element_config.data_class.new : object stack.push [new_object, element_config] if (attribute_config = new_object.class.respond_to?(:sax_config) && new_object.class.sax_config.attribute_configs_for_element(attrs)) attribute_config.each { |ac| new_object.send(ac.setter, ac.value_from_attrs(attrs)) } end end end end |