Module: XmlNodeStream::Parser::Base

Defined in:
lib/xml_node_stream/parser/base.rb

Overview

This is the base parser syntax that normalizes the SAX callbacks by providing a common interface so that the actual parser implementation doesn't matter.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/xml_node_stream/parser/base.rb', line 8

def root
  @root
end

Instance Method Details

#do_cdata_block(characters) ⇒ void

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.

This method returns an undefined value.

Handle CDATA block event.

Parameters:

  • characters (String)

    the CDATA content



72
73
74
# File 'lib/xml_node_stream/parser/base.rb', line 72

def do_cdata_block(characters)
  @nodes.last.append_cdata(characters) unless @nodes.empty?
end

#do_characters(characters) ⇒ void

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.

This method returns an undefined value.

Handle character data event.

Parameters:

  • characters (String)

    the character data



63
64
65
# File 'lib/xml_node_stream/parser/base.rb', line 63

def do_characters(characters)
  @nodes.last.append(characters) unless @nodes.empty?
end

#do_end_element(name) ⇒ void

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.

This method returns an undefined value.

Handle end element event.

Parameters:

  • name (String)

    the element name



51
52
53
54
55
56
# File 'lib/xml_node_stream/parser/base.rb', line 51

def do_end_element(name)
  node = @nodes.pop
  node.finish!
  @root = node if @nodes.empty?
  @parse_block&.call(node)
end

#do_start_element(name, attributes) ⇒ void

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.

This method returns an undefined value.

Handle start element event.

Parameters:

  • name (String)

    the element name

  • attributes (Hash)

    the element attributes



41
42
43
44
# File 'lib/xml_node_stream/parser/base.rb', line 41

def do_start_element(name, attributes)
  node = XmlNodeStream::Node.new(name, @nodes.last, attributes)
  @nodes.push(node)
end

#initialize {|Node| ... } ⇒ Object

Initialize the parser.

Yields:

  • (Node)

    each node as it is parsed



13
14
15
16
17
# File 'lib/xml_node_stream/parser/base.rb', line 13

def initialize(&block)
  @nodes = []
  @parse_block = block
  @root = nil
end

#parse_stream(io) ⇒ void

This method returns an undefined value.

Parse the input stream.

Parameters:

  • io (IO)

    the input stream to parse

Raises:

  • (NotImplementedError)

    if the parser gem is not loaded



24
25
26
27
28
29
30
31
32
33
# File 'lib/xml_node_stream/parser/base.rb', line 24

def parse_stream(io)
  parser_name = self.class.name.split("::").last.sub("Parser", "").downcase
  gem_name = case parser_name
  when "nokogiri" then "nokogiri"
  when "libxml" then "libxml-ruby"
  when "rexml" then "rexml"
  else "unknown"
  end
  raise NotImplementedError.new("Parser gem not loaded: #{gem_name}. Install it with: gem install #{gem_name.split(" ").first}")
end