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
-
#root ⇒ Object
readonly
Returns the value of attribute root.
Instance Method Summary collapse
-
#do_cdata_block(characters) ⇒ void
private
Handle CDATA block event.
-
#do_characters(characters) ⇒ void
private
Handle character data event.
-
#do_end_element(name) ⇒ void
private
Handle end element event.
-
#do_start_element(name, attributes) ⇒ void
private
Handle start element event.
-
#initialize {|Node| ... } ⇒ Object
Initialize the parser.
-
#parse_stream(io) ⇒ void
Parse the input stream.
Instance Attribute Details
#root ⇒ Object (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.
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.
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.
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.
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.
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.
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 |