Class: SXRB::Parser
- Inherits:
-
Object
- Object
- SXRB::Parser
- Defined in:
- lib/sxrb/parser.rb
Overview
Main class of gem, which allows parsing XML data.
Class Method Summary collapse
-
.define_rules { ... } ⇒ Callbacks
Define set of reusable rules for SXRB parser.
-
.parse_file(filename, callbacks = nil) {|Proxy| ... } ⇒ nil
Parse file containing XML.
-
.parse_io(io, callbacks = nil) {|Proxy| ... } ⇒ nil
Parse IO containing XML.
-
.parse_string(string, callbacks = nil) {|Proxy| ... } ⇒ nil
Parse string containing XML.
Instance Method Summary collapse
-
#initialize(input, opts = {}, &block) ⇒ Parser
constructor
deprecated
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.
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.
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.
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.
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.
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.
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 |