Last active
December 20, 2015 19:18
-
-
Save take-five/6182113 to your computer and use it in GitHub Desktop.
ox push parser (enumerator)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'enumerator' | |
require 'ox' | |
module Ox | |
class PushParser | |
DEFAULT_BUFSIZE = 4096 | |
def initialize(handler, options = {}, &blk) | |
@buf = '' | |
@generator = Enumerator.new(&blk) | |
@handler, @options = handler, options | |
end | |
def read(maxlen = nil) | |
while @buf.empty? | |
@buf << @generator.next | |
end | |
@buf.slice!(0, maxlen || @buf.length) | |
rescue StopIteration | |
'' | |
end | |
def parse | |
Ox.sax_parse(@handler, self, @options) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Sample < ::Ox::Sax | |
def start_element(name); puts "start: #{name}"; end | |
def end_element(name); puts "end: #{name}"; end | |
def attr(name, value); puts " #{name} => #{value}"; end | |
def text(value); puts "text #{value}"; end | |
end | |
handler = Sample.new | |
p = Ox::PushParser.new(handler) do |parser| | |
parser << '<top name="sample"' | |
parser << '><middle name="second' | |
parser << '">helo</middle>' | |
parser << '</top>' | |
end | |
p.parse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment