Class: SXRB::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/sxrb/parser.rb

Overview

Main class of gem, which allows parsing XML data.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, opts = {}, &block) ⇒ Parser

Deprecated.

This method should not be used anymore. In version 1.0 it will work completely different so it is preferable to migrate to use parse_* methods right now.

Create parser object and parse provided input.

Parameters:

  • input (Filename, IO, String)

    Data stream to be parsed.

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

    a customizable set of options

Options Hash (opts):

  • :mode (Symbol) — default: :string

    Instructs SXRB how to treat input parameter. Possible values are: :string, :file, :io. Please note, that in case of :file mode input should be filename.

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sxrb/parser.rb', line 18

def initialize(input, opts = {}, &block)
  raise ArgumentError.new("Block expected") if block.nil?
  case opts[:mode]
  when :file
    self.class.parse_file(input, &block)
  when :io
    self.class.parse_io(input, &block)
  else
    self.class.parse_string(input, &block)
  end
end

Class Method Details

.define_rules { ... } ⇒ Callbacks

Define set of reusable rules for SXRB parser.

Yields:

  • Proxy See Proxy documentation for details on available methods and defining rules.

Returns:

  • (Callbacks)

    Set of rules which can be feeded to parse_* methods.



36
37
38
39
40
# File 'lib/sxrb/parser.rb', line 36

def self.define_rules(&block)
  Callbacks.new.tap do |cb|
    yield Proxy.new(cb)
  end
end

.parse_file(filename, callbacks = nil) {|Proxy| ... } ⇒ nil

Parse file containing XML.

Parameters:

  • filename (String)

    Path to file to be parsed.

  • callbacks (optional, Callbacks) (defaults to: nil)

    Set of rules generated by define_rules.

Yields:

  • (Proxy)

    If callbacks parameter is not provided rules have to be defined directly in provided block.

Returns:

  • (nil)

Raises:

  • (ArgumentError)


67
68
69
70
71
72
73
# File 'lib/sxrb/parser.rb', line 67

def self.parse_file(filename, callbacks = nil, &block)
  raise ArgumentError unless !!callbacks ^ !!block
  LibXML::XML::SaxParser.file(filename).tap do |parser|
	parser.callbacks = callbacks || define_rules(&block)
	parser.parse
  end
end

.parse_io(io, callbacks = nil) {|Proxy| ... } ⇒ nil

Parse IO containing XML.

Parameters:

  • io (IO)

    Path to stream to be parsed.

  • callbacks (optional, Callbacks) (defaults to: nil)

    Set of rules generated by define_rules.

Yields:

  • (Proxy)

    If callbacks parameter is not provided rules have to be defined directly in provided block.

Returns:

  • (nil)

Raises:

  • (ArgumentError)


83
84
85
86
87
88
89
# File 'lib/sxrb/parser.rb', line 83

def self.parse_io(io, callbacks = nil, &block)
  raise ArgumentError unless !!callbacks ^ !!block
  LibXML::XML::SaxParser.io(io).tap do |parser|
	parser.callbacks = callbacks || define_rules(&block)
	parser.parse
  end
end

.parse_string(string, callbacks = nil) {|Proxy| ... } ⇒ nil

Parse string containing XML.

Parameters:

  • string (String)

    Input string

  • callbacks (optional, Callbacks) (defaults to: nil)

    Set of rules generated by define_rules.

Yields:

  • (Proxy)

    If callbacks parameter is not provided rules have to be defined directly in provided block.

Returns:

  • (nil)

Raises:

  • (ArgumentError)


50
51
52
53
54
55
56
57
# File 'lib/sxrb/parser.rb', line 50

def self.parse_string(string, callbacks = nil, &block)
  raise ArgumentError unless !!callbacks ^ !!block
  LibXML::XML::SaxParser.string(string).tap do |parser|
	parser.callbacks = callbacks || define_rules(&block)
	parser.parse
  end
  nil
end