Class: Badgerhash::Handlers::SaxHandler
- Inherits:
-
Object
- Object
- Badgerhash::Handlers::SaxHandler
- Defined in:
- lib/badgerhash/handlers/sax_handler.rb
Overview
SaxHandler that is passed to a sax parser implementation
Instance Attribute Summary collapse
- #node ⇒ Object readonly
Instance Method Summary collapse
-
#attr(name, value) ⇒ SaxHandler
Sends message to handler that an attribute was encountered in the stream.
-
#end_element(name) ⇒ SaxHandler
Sends a message to the handler that the end of an element has been found in the stream.
-
#initialize(node = {}) ⇒ SaxHandler
constructor
private
Initialize the SaxHandler.
-
#start_element(name) ⇒ SaxHandler
Sends message to handler that a new element was encountered in stream.
-
#text(value) ⇒ SaxHandler
(also: #cdata)
Sends a message to handler that a text node or cdata section was found in the stream.
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
12 13 14 15 |
# File 'lib/badgerhash/handlers/sax_handler.rb', line 12 def initialize(node={}) @parents = [] @node = node end |
Instance Attribute Details
#node ⇒ Object (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
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
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
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
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 |