module RSS::ListenerMixin
Constants
- CONTENT_PATTERN
- NAMESPLIT
Attributes
do_validate[RW]
ignore_unknown_element[RW]
rss[R]
Public Class Methods
new()
click to toggle source
# File lib/rss/parser.rb, line 267 def initialize @rss = nil @ignore_unknown_element = true @do_validate = true @ns_stack = [{"xml" => :xml}] @tag_stack = [[]] @text_stack = [''] @proc_stack = [] @last_element = nil @version = @encoding = @standalone = nil @xml_stylesheets = [] @xml_child_mode = false @xml_element = nil @last_xml_element = nil end
Public Instance Methods
instruction(name, content)
click to toggle source
# File lib/rss/parser.rb, line 288 def instruction(name, content) if name == "xml-stylesheet" params = parse_pi_content(content) if params.has_key?("href") @xml_stylesheets << XMLStyleSheet.new(params) end end end
tag_end(name)
click to toggle source
# File lib/rss/parser.rb, line 350 def tag_end(name) if DEBUG p "end tag #{name}" p @tag_stack end text = @text_stack.pop tags = @tag_stack.pop pr = @proc_stack.pop pr.call(text, tags) unless pr.nil? @ns_stack.pop end
tag_start(name, attributes)
click to toggle source
# File lib/rss/parser.rb, line 297 def tag_start(name, attributes) @text_stack.push('') ns = @ns_stack.last.dup attrs = {} attributes.each do |n, v| if /\Axmlns(?:\z|:)/ =~ n ns[$POSTMATCH] = v else attrs[n] = v end end @ns_stack.push(ns) prefix, local = split_name(name) @tag_stack.last.push([_ns(ns, prefix), local]) @tag_stack.push([]) if @xml_child_mode previous = @last_xml_element element_attrs = attributes.dup unless previous ns.each do |ns_prefix, value| next if ns_prefix == "xml" key = ns_prefix.empty? ? "xmlns" : "xmlns:#{ns_prefix}" element_attrs[key] ||= value end end next_element = XML::Element.new(local, prefix.empty? ? nil : prefix, _ns(ns, prefix), element_attrs) previous << next_element if previous @last_xml_element = next_element pr = Proc.new do |text, tags| if previous @last_xml_element = previous else @xml_element = @last_xml_element @last_xml_element = nil end end @proc_stack.push(pr) else if @rss.nil? and respond_to?("initial_start_#{local}", true) __send__("initial_start_#{local}", local, prefix, attrs, ns.dup) elsif respond_to?("start_#{local}", true) __send__("start_#{local}", local, prefix, attrs, ns.dup) else start_else_element(local, prefix, attrs, ns.dup) end end end
text(data)
click to toggle source
# File lib/rss/parser.rb, line 362 def text(data) if @xml_child_mode @last_xml_element << data if @last_xml_element else @text_stack.last << data end end
xmldecl(version, encoding, standalone)
click to toggle source
set instance vars for version, encoding, standalone
# File lib/rss/parser.rb, line 284 def xmldecl(version, encoding, standalone) @version, @encoding, @standalone = version, encoding, standalone end
Private Instance Methods
_ns(ns, prefix)
click to toggle source
# File lib/rss/parser.rb, line 371 def _ns(ns, prefix) ns.fetch(prefix, "") end
check_ns(tag_name, prefix, ns, require_uri, ignore_unknown_element=nil)
click to toggle source
# File lib/rss/parser.rb, line 426 def check_ns(tag_name, prefix, ns, require_uri, ignore_unknown_element=nil) if _ns(ns, prefix) == require_uri true else if ignore_unknown_element.nil? ignore_unknown_element = @ignore_unknown_element end if ignore_unknown_element false elsif @do_validate raise NSError.new(tag_name, prefix, require_uri) else # Force bind required URI with prefix @ns_stack.last[prefix] = require_uri true end end end
collect_attributes(tag_name, prefix, attrs, ns, klass)
click to toggle source
# File lib/rss/parser.rb, line 476 def collect_attributes(tag_name, prefix, attrs, ns, klass) attributes = {} klass.get_attributes.each do |a_name, a_uri, required, element_name| if a_uri.is_a?(String) or !a_uri.respond_to?(:include?) a_uri = [a_uri] end unless a_uri == [""] for prefix, uri in ns if a_uri.include?(uri) val = attrs["#{prefix}:#{a_name}"] break if val end end end if val.nil? and a_uri.include?("") val = attrs[a_name] end if @do_validate and required and val.nil? unless a_uri.include?("") for prefix, uri in ns if a_uri.include?(uri) a_name = "#{prefix}:#{a_name}" end end end raise MissingAttributeError.new(tag_name, a_name) end attributes[a_name] = val end attributes end
initial_start_RDF(tag_name, prefix, attrs, ns)
click to toggle source
# File lib/rss/1.0.rb, line 438 def initial_start_RDF(tag_name, prefix, attrs, ns) check_ns(tag_name, prefix, ns, RDF::URI, false) @rss = RDF.new(@version, @encoding, @standalone) @rss.do_validate = @do_validate @rss.xml_stylesheets = @xml_stylesheets @last_element = @rss pr = Proc.new do |text, tags| @rss.validate_for_stream(tags, @ignore_unknown_element) if @do_validate end @proc_stack.push(pr) end
initial_start_entry(tag_name, prefix, attrs, ns)
click to toggle source
# File lib/rss/atom.rb, line 824 def initial_start_entry(tag_name, prefix, attrs, ns) check_ns(tag_name, prefix, ns, Atom::URI, false) @rss = Atom::Entry.new(@version, @encoding, @standalone) @rss.do_validate = @do_validate @rss.xml_stylesheets = @xml_stylesheets @rss.lang = attrs["xml:lang"] @rss.base = attrs["xml:base"] @last_element = @rss pr = Proc.new do |text, tags| @rss.validate_for_stream(tags) if @do_validate end @proc_stack.push(pr) end
initial_start_feed(tag_name, prefix, attrs, ns)
click to toggle source
# File lib/rss/atom.rb, line 809 def initial_start_feed(tag_name, prefix, attrs, ns) check_ns(tag_name, prefix, ns, Atom::URI, false) @rss = Atom::Feed.new(@version, @encoding, @standalone) @rss.do_validate = @do_validate @rss.xml_stylesheets = @xml_stylesheets @rss.lang = attrs["xml:lang"] @rss.base = attrs["xml:base"] @last_element = @rss pr = Proc.new do |text, tags| @rss.validate_for_stream(tags) if @do_validate end @proc_stack.push(pr) end
initial_start_rss(tag_name, prefix, attrs, ns)
click to toggle source
# File lib/rss/0.9.rb, line 413 def initial_start_rss(tag_name, prefix, attrs, ns) check_ns(tag_name, prefix, ns, "", false) @rss = Rss.new(attrs['version'], @version, @encoding, @standalone) @rss.do_validate = @do_validate @rss.xml_stylesheets = @xml_stylesheets @last_element = @rss pr = Proc.new do |text, tags| @rss.validate_for_stream(tags, @ignore_unknown_element) if @do_validate end @proc_stack.push(pr) end
known_class?(target_class, class_name)
click to toggle source
# File lib/rss/parser.rb, line 407 def known_class?(target_class, class_name) class_name and (target_class.const_defined?(class_name, false) or target_class.constants.include?(class_name.to_sym)) end
parse_pi_content(content)
click to toggle source
Extract the first name=“value” pair from content. Works with single quotes according to the constant CONTENT_PATTERN. Return a Hash.
# File lib/rss/parser.rb, line 379 def parse_pi_content(content) params = {} content.scan(CONTENT_PATTERN) do |name, quote, value| params[name] = value end params end
setup_next_element(tag_name, klass, attributes)
click to toggle source
# File lib/rss/parser.rb, line 510 def setup_next_element(tag_name, klass, attributes) previous = @last_element next_element = klass.new(@do_validate, attributes) previous.set_next_element(tag_name, next_element) @last_element = next_element @last_element.parent = previous if klass.need_parent? @xml_child_mode = @last_element.have_xml_content? Proc.new do |text, tags| p(@last_element.class) if DEBUG if @xml_child_mode @last_element.content = @xml_element.to_s xml_setter = @last_element.class.xml_setter @last_element.__send__(xml_setter, @xml_element) @xml_element = nil @xml_child_mode = false else if klass.have_content? if @last_element.need_base64_encode? text = text.lstrip.unpack("m").first end @last_element.content = text end end if @do_validate @last_element.validate_for_stream(tags, @ignore_unknown_element) end @last_element = previous end end
setup_next_element_in_unknown_element()
click to toggle source
# File lib/rss/parser.rb, line 541 def setup_next_element_in_unknown_element current_element, @last_element = @last_element, nil Proc.new {@last_element = current_element} end
split_name(name)
click to toggle source
# File lib/rss/parser.rb, line 421 def split_name(name) name =~ NAMESPLIT [$1 || '', $2] end
start_else_element(local, prefix, attrs, ns)
click to toggle source
# File lib/rss/parser.rb, line 387 def start_else_element(local, prefix, attrs, ns) class_name = self.class.class_name(_ns(ns, prefix), local) current_class = @last_element.class if known_class?(current_class, class_name) next_class = current_class.const_get(class_name) start_have_something_element(local, prefix, attrs, ns, next_class) else if !@do_validate or @ignore_unknown_element @proc_stack.push(setup_next_element_in_unknown_element) else parent = "ROOT ELEMENT???" if current_class.tag_name parent = current_class.tag_name end raise NotExpectedTagError.new(local, _ns(ns, prefix), parent) end end end
start_get_text_element(tag_name, prefix, ns, required_uri)
click to toggle source
# File lib/rss/parser.rb, line 446 def start_get_text_element(tag_name, prefix, ns, required_uri) pr = Proc.new do |text, tags| setter = self.class.setter(required_uri, tag_name) if setter and @last_element.respond_to?(setter) if @do_validate getter = self.class.getter(required_uri, tag_name) if @last_element.__send__(getter) raise TooMuchTagError.new(tag_name, @last_element.tag_name) end end @last_element.__send__(setter, text.to_s) else if @do_validate and !@ignore_unknown_element raise NotExpectedTagError.new(tag_name, _ns(ns, prefix), @last_element.tag_name) end end end @proc_stack.push(pr) end
start_have_something_element(tag_name, prefix, attrs, ns, klass)
click to toggle source
# File lib/rss/parser.rb, line 467 def start_have_something_element(tag_name, prefix, attrs, ns, klass) if check_ns(tag_name, prefix, ns, klass.required_uri) attributes = collect_attributes(tag_name, prefix, attrs, ns, klass) @proc_stack.push(setup_next_element(tag_name, klass, attributes)) else @proc_stack.push(setup_next_element_in_unknown_element) end end