Class: SAXMachine::SAXHandler

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/sax-machine/sax_handler.rb

Direct Known Subclasses

SAXEventRecorder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#objectObject (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.



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/sax-machine/sax_handler.rb', line 114

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



25
26
27
# File 'lib/sax-machine/sax_handler.rb', line 25

def cdata_block(string)
  characters(string)
end

#characaters_captured?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/sax-machine/sax_handler.rb', line 69

def characaters_captured?
  !@value.nil? && !@value.empty?
end

#characters(string) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sax-machine/sax_handler.rb', line 13

def characters(string)
  if parsing_collection?
    @collection_handler.characters(string)
  elsif @element_config
    if !@value || @value == EMPTY_STRING
      @value = string
    else
      @value << string
    end
  end
end

#end_element(name) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sax-machine/sax_handler.rb', line 52

def end_element(name)
  if parsing_collection? && @collection_config.name == name.split(COLON).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_attributesObject



77
78
79
80
81
82
# File 'lib/sax-machine/sax_handler.rb', line 77

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



84
85
86
87
88
89
# File 'lib/sax-machine/sax_handler.rb', line 84

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

Returns:

  • (Boolean)


73
74
75
# File 'lib/sax-machine/sax_handler.rb', line 73

def parsing_collection?
  !@collection_handler.nil?
end

#reset_current_collectionObject



96
97
98
99
# File 'lib/sax-machine/sax_handler.rb', line 96

def reset_current_collection
  @collection_handler = nil
  @collection_config  = nil
end

#reset_current_tagObject



101
102
103
104
105
106
# File 'lib/sax-machine/sax_handler.rb', line 101

def reset_current_tag
  @name   = nil
  @attrs  = nil
  @value  = nil
  @element_config = nil
end

#sax_configObject



108
109
110
# File 'lib/sax-machine/sax_handler.rb', line 108

def sax_config
  @object.class.sax_config
end

#set_element_config_for_element_valueObject



91
92
93
94
# File 'lib/sax-machine/sax_handler.rb', line 91

def set_element_config_for_element_value
  @value = EMPTY_STRING
  @element_config = sax_config.element_config_for_tag(@name, @attrs, @nsstack)
end

#start_element(name, attrs = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/sax-machine/sax_handler.rb', line 29

def start_element(name, attrs = nil)

  @name   = name
  @attrs  = attrs ? attrs.map { |a| SAXHandler.decode_xml(a) } : nil
  @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