class Vines::Stream::Parser
Constants
- IGNORE
- STREAM_NAME
- STREAM_URI
Public Class Methods
new(&block)
click to toggle source
# File lib/vines/stream/parser.rb, line 11 def initialize(&block) @listeners, @node = Hash.new {|h, k| h[k] = []}, nil @parser = Nokogiri::XML::SAX::PushParser.new(self) instance_eval(&block) if block end
Public Instance Methods
<<(data)
click to toggle source
# File lib/vines/stream/parser.rb, line 23 def <<(data) @parser << data self end
characters(chars)
click to toggle source
# File lib/vines/stream/parser.rb, line 49 def characters(chars) @node << Text.new(chars, @node.document) if @node end
Also aliased as: cdata_block
end_element_namespace(name, prefix=nil, uri=nil)
click to toggle source
# File lib/vines/stream/parser.rb, line 38 def end_element_namespace(name, prefix=nil, uri=nil) if stream?(name, uri) notify(:stream_close) elsif @node.parent != @node.document @node = @node.parent else notify(:stanza, @node) @node = nil end end
start_element_namespace(name, attrs=[], prefix=nil, uri=nil, ns=[])
click to toggle source
# File lib/vines/stream/parser.rb, line 28 def start_element_namespace(name, attrs=[], prefix=nil, uri=nil, ns=[]) el = node(name, attrs, prefix, uri, ns) if stream?(name, uri) notify(:stream_open, el) else @node << el if @node @node = el end end
Private Instance Methods
node(name, attrs=[], prefix=nil, uri=nil, ns=[])
click to toggle source
# File lib/vines/stream/parser.rb, line 66 def node(name, attrs=[], prefix=nil, uri=nil, ns=[]) ignore = stream?(name, uri) ? [] : IGNORE doc = @node ? @node.document : Document.new node = doc.create_element(name) do |node| attrs.each {|attr| node[attr.localname] = attr.value } ns.each {|prefix, uri| node.add_namespace(prefix, uri) unless ignore.include?(uri) } doc << node unless @node end node.namespace = node.add_namespace(prefix, uri) unless ignore.include?(uri) node end
notify(msg, node=nil)
click to toggle source
# File lib/vines/stream/parser.rb, line 56 def notify(msg, node=nil) @listeners[msg].each do |b| (node ? b.call(node) : b.call) rescue nil end end
stream?(name, uri)
click to toggle source
# File lib/vines/stream/parser.rb, line 62 def stream?(name, uri) name == STREAM_NAME && uri == STREAM_URI end