Class: Badgerhash::Handlers::SaxHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/badgerhash/handlers/sax_handler.rb

Overview

SaxHandler that is passed to a sax parser implementation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(node = {}) ⇒ SaxHandler

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize the SaxHandler

Parameters:

  • node (Hash) (defaults to: {})

    Used to preinitialzie the internal node.



12
13
14
15
# File 'lib/badgerhash/handlers/sax_handler.rb', line 12

def initialize(node={})
  @parents = []
  @node = node
end

Instance Attribute Details

#nodeObject (readonly)



6
7
8
# File 'lib/badgerhash/handlers/sax_handler.rb', line 6

def node
  @node
end

Instance Method Details

#attr(name, value) ⇒ SaxHandler

Sends message to handler that an attribute was encountered in the stream

Examples:

handler.attr "foo", "bar"  => handler

Parameters:

  • name (String)

    the name of the attribute

  • value (String)

    the attribute’s value

Returns:



51
52
53
54
55
56
57
58
59
# File 'lib/badgerhash/handlers/sax_handler.rb', line 51

def attr(name, value)
  if name =~ /^xmlns(:?(.*))/i
    key = $2.to_s.length > 0 ? $2 : "$"
    (@node["@xmlns"] ||= {})[key] = value
  else
    @node["@#{name}"] = value
  end
  self
end

#end_element(name) ⇒ SaxHandler

Sends a message to the handler that the end of an element has been found in the stream

Examples:

handler.end_element "foo" => handler

Parameters:

  • name (String)

    the name of the element

Returns:



83
84
85
86
# File 'lib/badgerhash/handlers/sax_handler.rb', line 83

def end_element(name)
  @node = @parents.pop || {}
  self
end

#start_element(name) ⇒ SaxHandler

Sends message to handler that a new element was encountered in stream

Examples:

handler.start_element "foo" => handler

Parameters:

  • name (String)

    the name of the new element

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/badgerhash/handlers/sax_handler.rb', line 25

def start_element(name)
  name = name.to_s
  element = node_namespaces

  @node[name] = case @node[name]
                when nil
                  element
                when Hash
                  [@node[name], element]
                else
                  @node[name] << element
                end

  @parents << @node
  @node = element
  self
end

#text(value) ⇒ SaxHandler Also known as: cdata

Sends a message to handler that a text node or cdata section was found in the stream

Examples:

handler.text "this is my text node" => handler

Parameters:

  • value (String)

    the text encountered

Returns:



68
69
70
71
72
# File 'lib/badgerhash/handlers/sax_handler.rb', line 68

def text(value)
  value = value.to_s.strip
  @node["$"] = value unless value.empty?
  self
end