Module: SAXMachine::SAXAbstractHandler

Included in:
SAXNokogiriHandler, SAXOgaHandler, SAXOxHandler
Defined in:
lib/sax-machine/handlers/sax_abstract_handler.rb

Defined Under Namespace

Classes: StackNode

Constant Summary collapse

NO_BUFFER =
:no_buffer

Instance Method Summary collapse

Instance Method Details

#_characters(data) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/sax-machine/handlers/sax_abstract_handler.rb', line 26

def _characters(data)
  node = stack.last

  if node.buffer == NO_BUFFER
    node.buffer = data.dup
  else
    node.buffer << data
  end
end

#_end_element(name) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/sax-machine/handlers/sax_abstract_handler.rb', line 81

def _end_element(name)
  name = normalize_name(name)

  start_tag = stack[-2]
  close_tag = stack[-1]

  return unless start_tag && close_tag

  object  = start_tag.object
  element = close_tag.object
  config  = close_tag.config
  value   = close_tag.buffer

  return unless config.name == name

  unless parsed_config?(object, config)
    if (element_value_config = element_values_for(config))
      element_value_config.each { |evc| element.send(evc.setter, value) }
    end

    if config.respond_to?(:accessor)
      subconfig = sax_config_for(element)

      if econf = subconfig.element_config_for_tag(name, [])
        element.send(econf.setter, value) unless econf.value_configured?
      end

      object.send(config.accessor) << element
    else
      value = data_class_value(config.data_class, value) || element
      object.send(config.setter, value) if value != NO_BUFFER
      mark_as_parsed(object, config)
    end

    # try to set the ancestor
    if (sax_config = sax_config_for(element))
      sax_config.ancestors.each do |ancestor|
        element.send(ancestor.setter, object)
      end
    end
  end

  stack.pop
end

#_error(string) ⇒ Object



126
127
128
129
130
# File 'lib/sax-machine/handlers/sax_abstract_handler.rb', line 126

def _error(string)
  if @on_error
    @on_error.call(string)
  end
end

#_initialize(object, on_error = nil, on_warning = nil) ⇒ Object



19
20
21
22
23
24
# File 'lib/sax-machine/handlers/sax_abstract_handler.rb', line 19

def _initialize(object, on_error = nil, on_warning = nil)
  @stack = [ StackNode.new(object) ]
  @parsed_configs = {}
  @on_error = on_error
  @on_warning = on_warning
end

#_start_element(name, attrs = []) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sax-machine/handlers/sax_abstract_handler.rb', line 36

def _start_element(name, attrs = [])
  name   = normalize_name(name)
  node   = stack.last
  object = node.object

  sax_config = sax_config_for(object)

  if sax_config
    attrs = Hash[attrs]

    if collection_config = sax_config.collection_config(name, attrs)
      object     = collection_config.data_class.new
      sax_config = sax_config_for(object)

      stack.push(StackNode.new(object, collection_config))

      set_attributes_on(object, attrs)
    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 =
        case element_config.data_class.to_s
        when "Integer" then 0
        when "Float"   then 0.0
        when "Symbol"  then nil
        when "Time"    then Time.at(0)
        when ""        then object
        else
          element_config.data_class.new
        end

      stack.push(StackNode.new(new_object, element_config))

      set_attributes_on(new_object, attrs)
    end
  end
end

#_warning(string) ⇒ Object



132
133
134
135
136
# File 'lib/sax-machine/handlers/sax_abstract_handler.rb', line 132

def _warning(string)
  if @on_warning
    @on_warning.call(string)
  end
end

#sax_parse(xml_input) ⇒ Object

Raises:

  • (NotImplementedError)


15
16
17
# File 'lib/sax-machine/handlers/sax_abstract_handler.rb', line 15

def sax_parse(xml_input)
  raise NotImplementedError
end