Class: Oga::XML::SaxParser

Inherits:
Parser
  • Object
show all
Defined in:
lib/oga/xml/sax_parser.rb

Overview

The SaxParser class provides the basic interface for writing custom SAX parsers. All callback methods defined in Parser are delegated to a dedicated handler class.

To write a custom handler for the SAX parser, create a class that implements one (or many) of the following callback methods:

  • on_document
  • on_doctype
  • on_cdata
  • on_comment
  • on_proc_ins
  • on_xml_decl
  • on_text
  • on_element
  • on_element_children
  • after_element

For example:

class SaxHandler
  def on_element(namespace, name, attrs = {})
    puts name
  end
end

You can then use it as following:

handler = SaxHandler.new
parser  = Oga::XML::SaxParser.new(handler, '<foo />')

parser.parse

For information on the callback arguments see the documentation of the corresponding methods in Parser.

Element Callbacks

The SAX parser changes the behaviour of both on_element and after_element. The latter in the regular parser only takes a Element instance. In the SAX parser it will instead take a namespace name and the element name. This eases the process of figuring out what element a callback is associated with.

An example:

class SaxHandler
  def on_element(namespace, name, attrs = {})
    # ...
  end

  def after_element(namespace, name)
    puts name # => "foo", "bar", etc
  end
end

Direct Known Subclasses

HTML::SaxParser

Instance Method Summary collapse

Constructor Details

#initialize(handler, *args) ⇒ SaxParser

Returns a new instance of SaxParser

Parameters:

  • handler (Object)

    The SAX handler to delegate callbacks to.

See Also:

  • Oga::XML::SaxParser.[Oga[Oga::XML[Oga::XML::Parser[Oga::XML::Parser#initialize]

65
66
67
68
69
# File 'lib/oga/xml/sax_parser.rb', line 65

def initialize(handler, *args)
  @handler = handler

  super(*args)
end

Instance Method Details

#after_element(namespace_with_name) ⇒ Object

Manually overwrite after_element so it can take a namespace and name. This differs a bit from the regular after_element which only takes an Element instance.

Parameters:

  • namespace_with_name (Array)

102
103
104
# File 'lib/oga/xml/sax_parser.rb', line 102

def after_element(namespace_with_name)
  run_callback(:after_element, *namespace_with_name)
end

#on_element(namespace, name, attrs = {}) ⇒ Array

Manually overwrite on_element so we can ensure that after_element always receives the namespace and name.

Returns:

  • (Array)

See Also:

  • Oga::XML::SaxParser.[Oga[Oga::XML[Oga::XML::Parser[Oga::XML::Parser#on_element]

89
90
91
92
93
# File 'lib/oga/xml/sax_parser.rb', line 89

def on_element(namespace, name, attrs = {})
  run_callback(:on_element, namespace, name, attrs)

  return namespace, name
end

#run_callback(method, *args) ⇒ Object (private)

Parameters:

  • method (Symbol)
  • args (Array)

112
113
114
# File 'lib/oga/xml/sax_parser.rb', line 112

def run_callback(method, *args)
  @handler.send(method, *args) if @handler.respond_to?(method)
end