Class: SAXMachine::SAXHandler
- Inherits:
-
Nokogiri::XML::SAX::Document
- Object
- Nokogiri::XML::SAX::Document
- SAXMachine::SAXHandler
- Defined in:
- lib/sax-machine/sax_handler.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
Class Method Summary collapse
-
.decode_xml(str) ⇒ Object
Decodes XML special characters.
Instance Method Summary collapse
- #cdata_block(string) ⇒ Object
- #characaters_captured? ⇒ Boolean
- #characters(string) ⇒ Object
- #end_element(name) ⇒ Object
-
#initialize(object, nsstack = nil) ⇒ SAXHandler
constructor
A new instance of SAXHandler.
- #parse_collection_instance_attributes ⇒ Object
- #parse_element_attributes(element_configs) ⇒ Object
- #parsing_collection? ⇒ Boolean
- #reset_current_collection ⇒ Object
- #reset_current_tag ⇒ Object
- #sax_config ⇒ Object
- #set_element_config_for_element_value ⇒ Object
- #start_element(name, attrs = []) ⇒ Object
Constructor Details
#initialize(object, nsstack = nil) ⇒ SAXHandler
Returns a new instance of SAXHandler.
8 9 10 11 |
# File 'lib/sax-machine/sax_handler.rb', line 8 def initialize(object, nsstack=nil) @object = object @nsstack = nsstack || NSStack.new end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
6 7 8 |
# File 'lib/sax-machine/sax_handler.rb', line 6 def object @object end |
Class Method Details
.decode_xml(str) ⇒ Object
Decodes XML special characters.
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/sax-machine/sax_handler.rb', line 110 def self.decode_xml(str) return str.map &method(:decode_xml) if str.kind_of?(Array) # entities = { # '#38' => '&', # '#13' => "\r", # } # entities.keys.inject(str) { |string, key| # string.gsub(/&#{key};/, entities[key]) # } CGI.unescapeHTML(str) end |
Instance Method Details
#cdata_block(string) ⇒ Object
21 22 23 |
# File 'lib/sax-machine/sax_handler.rb', line 21 def cdata_block(string) characters(string) end |
#characaters_captured? ⇒ Boolean
65 66 67 |
# File 'lib/sax-machine/sax_handler.rb', line 65 def characaters_captured? !@value.nil? && !@value.empty? end |
#characters(string) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/sax-machine/sax_handler.rb', line 13 def characters(string) if parsing_collection? @collection_handler.characters(string) elsif @element_config @value << string end end |
#end_element(name) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/sax-machine/sax_handler.rb', line 48 def end_element(name) if parsing_collection? && @collection_config.name == name.split(':').last @collection_handler.end_element(name) @object.send(@collection_config.accessor) << @collection_handler.object reset_current_collection elsif parsing_collection? @collection_handler.end_element(name) elsif characaters_captured? @object.send(@element_config.setter, @value) end reset_current_tag @nsstack = @nsstack.pop end |
#parse_collection_instance_attributes ⇒ Object
73 74 75 76 77 78 |
# File 'lib/sax-machine/sax_handler.rb', line 73 def parse_collection_instance_attributes instance = @collection_handler.object @attrs.each_with_index do |attr_name,index| instance.send("#{attr_name}=", @attrs[index + 1]) if index % 2 == 0 && instance.methods.include?("#{attr_name}=") end end |
#parse_element_attributes(element_configs) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/sax-machine/sax_handler.rb', line 80 def parse_element_attributes(element_configs) element_configs.each do |ec| @object.send(ec.setter, ec.value_from_attrs(@attrs)) end @element_config = nil end |
#parsing_collection? ⇒ Boolean
69 70 71 |
# File 'lib/sax-machine/sax_handler.rb', line 69 def parsing_collection? !@collection_handler.nil? end |
#reset_current_collection ⇒ Object
92 93 94 95 |
# File 'lib/sax-machine/sax_handler.rb', line 92 def reset_current_collection @collection_handler = nil @collection_config = nil end |
#reset_current_tag ⇒ Object
97 98 99 100 101 102 |
# File 'lib/sax-machine/sax_handler.rb', line 97 def reset_current_tag @name = nil @attrs = nil @value = nil @element_config = nil end |
#sax_config ⇒ Object
104 105 106 |
# File 'lib/sax-machine/sax_handler.rb', line 104 def sax_config @object.class.sax_config end |
#set_element_config_for_element_value ⇒ Object
87 88 89 90 |
# File 'lib/sax-machine/sax_handler.rb', line 87 def set_element_config_for_element_value @value = "" @element_config = sax_config.element_config_for_tag(@name, @attrs, @nsstack) 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 |
# File 'lib/sax-machine/sax_handler.rb', line 25 def start_element(name, attrs = []) @name = name @attrs = attrs.map { |a| SAXHandler.decode_xml(a) } @nsstack = NSStack.new(@nsstack, @attrs) if parsing_collection? @collection_handler.start_element(@name, @attrs) elsif @collection_config = sax_config.collection_config(@name, @nsstack) @collection_handler = @collection_config.handler(@nsstack) if @object.class != @collection_handler.object.class @collection_handler.start_element(@name, @attrs) end elsif (element_configs = sax_config.element_configs_for_attribute(@name, @attrs)).any? parse_element_attributes(element_configs) set_element_config_for_element_value else set_element_config_for_element_value end end |