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.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/sax-machine/sax_handler.rb', line 105

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

#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

#characters_captured?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/sax-machine/sax_handler.rb', line 67

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

#end_element(name) ⇒ Object



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

def end_element(name)
  if parsing_collection? && @collection_config.name == name.split(COLON, 2).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 characters_captured? && !@element_config.has_value?
    @object.send(@element_config.setter, @value)
  end

  reset_current_tag
  @nsstack = @nsstack.pop
end

#parse_element_attributes(element_configs) ⇒ Object



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

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)


71
72
73
# File 'lib/sax-machine/sax_handler.rb', line 71

def parsing_collection?
  !(@collection_handler ||= nil).nil?
end

#reset_current_collectionObject



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

def reset_current_collection
  @collection_handler = nil
  @collection_config  = nil
end

#reset_current_tagObject



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

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

#sax_configObject



99
100
101
# File 'lib/sax-machine/sax_handler.rb', line 99

def sax_config
  @object.class.sax_config
end

#set_element_config_for_element_valueObject



82
83
84
85
# File 'lib/sax-machine/sax_handler.rb', line 82

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 || []).map do |k, v|
    # Do we actually need to decode the attribute key or just the value?
    [SAXHandler.decode_xml(k), SAXHandler.decode_xml(v)]
  end
  @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