2004-01-31 11:10:53 -05:00
|
|
|
require "forwardable"
|
2007-02-06 06:33:50 -05:00
|
|
|
require "open-uri"
|
2004-01-31 11:10:53 -05:00
|
|
|
|
2004-01-27 22:46:13 -05:00
|
|
|
require "rss/rss"
|
2007-03-17 06:13:25 -04:00
|
|
|
require "rss/xml"
|
2004-01-27 22:46:13 -05:00
|
|
|
|
|
|
|
module RSS
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
class NotWellFormedError < Error
|
|
|
|
attr_reader :line, :element
|
2008-06-29 05:02:32 -04:00
|
|
|
|
|
|
|
# Create a new NotWellFormedError for an error at +line+
|
|
|
|
# in +element+. If a block is given the return value of
|
|
|
|
# the block ends up in the error message.
|
2004-10-16 00:51:15 -04:00
|
|
|
def initialize(line=nil, element=nil)
|
|
|
|
message = "This is not well formed XML"
|
|
|
|
if element or line
|
|
|
|
message << "\nerror occurred"
|
|
|
|
message << " in #{element}" if element
|
|
|
|
message << " at about #{line} line" if line
|
|
|
|
end
|
|
|
|
message << "\n#{yield}" if block_given?
|
|
|
|
super(message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class XMLParserNotFound < Error
|
|
|
|
def initialize
|
2008-06-29 05:02:32 -04:00
|
|
|
super("available XML parser was not found in " <<
|
2004-10-16 00:51:15 -04:00
|
|
|
"#{AVAILABLE_PARSER_LIBRARIES.inspect}.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class NotValidXMLParser < Error
|
|
|
|
def initialize(parser)
|
2008-06-29 05:02:32 -04:00
|
|
|
super("#{parser} is not an available XML parser. " <<
|
2008-06-29 05:05:21 -04:00
|
|
|
"Available XML parser" <<
|
|
|
|
(AVAILABLE_PARSERS.size > 1 ? "s are " : " is ") <<
|
2004-10-16 00:51:15 -04:00
|
|
|
"#{AVAILABLE_PARSERS.inspect}.")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class NSError < InvalidRSSError
|
|
|
|
attr_reader :tag, :prefix, :uri
|
|
|
|
def initialize(tag, prefix, require_uri)
|
|
|
|
@tag, @prefix, @uri = tag, prefix, require_uri
|
|
|
|
super("prefix <#{prefix}> doesn't associate uri " <<
|
|
|
|
"<#{require_uri}> in tag <#{tag}>")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Parser
|
|
|
|
|
|
|
|
extend Forwardable
|
|
|
|
|
|
|
|
class << self
|
|
|
|
|
|
|
|
@@default_parser = nil
|
|
|
|
|
|
|
|
def default_parser
|
|
|
|
@@default_parser || AVAILABLE_PARSERS.first
|
|
|
|
end
|
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
# Set @@default_parser to new_value if it is one of the
|
|
|
|
# available parsers. Else raise NotValidXMLParser error.
|
2004-10-16 00:51:15 -04:00
|
|
|
def default_parser=(new_value)
|
|
|
|
if AVAILABLE_PARSERS.include?(new_value)
|
|
|
|
@@default_parser = new_value
|
|
|
|
else
|
|
|
|
raise NotValidXMLParser.new(new_value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def parse(rss, do_validate=true, ignore_unknown_element=true,
|
|
|
|
parser_class=default_parser)
|
2004-10-16 00:51:15 -04:00
|
|
|
parser = new(rss, parser_class)
|
|
|
|
parser.do_validate = do_validate
|
|
|
|
parser.ignore_unknown_element = ignore_unknown_element
|
|
|
|
parser.parse
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def_delegators(:@parser, :parse, :rss,
|
|
|
|
:ignore_unknown_element,
|
|
|
|
:ignore_unknown_element=, :do_validate,
|
|
|
|
:do_validate=)
|
|
|
|
|
|
|
|
def initialize(rss, parser_class=self.class.default_parser)
|
2005-07-22 01:41:33 -04:00
|
|
|
@parser = parser_class.new(normalize_rss(rss))
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2008-06-29 05:02:32 -04:00
|
|
|
|
|
|
|
# Try to get the XML associated with +rss+.
|
|
|
|
# Return +rss+ if it already looks like XML, or treat it as a URI,
|
|
|
|
# or a file to get the XML,
|
2005-07-22 01:41:33 -04:00
|
|
|
def normalize_rss(rss)
|
|
|
|
return rss if maybe_xml?(rss)
|
|
|
|
|
|
|
|
uri = to_uri(rss)
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-07-22 01:41:33 -04:00
|
|
|
if uri.respond_to?(:read)
|
|
|
|
uri.read
|
|
|
|
elsif !rss.tainted? and File.readable?(rss)
|
|
|
|
File.open(rss) {|f| f.read}
|
|
|
|
else
|
|
|
|
rss
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
# maybe_xml? tests if source is a string that looks like XML.
|
2005-07-22 01:41:33 -04:00
|
|
|
def maybe_xml?(source)
|
|
|
|
source.is_a?(String) and /</ =~ source
|
|
|
|
end
|
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
# Attempt to convert rss to a URI, but just return it if
|
|
|
|
# there's a ::URI::Error
|
2005-07-22 01:41:33 -04:00
|
|
|
def to_uri(rss)
|
|
|
|
return rss if rss.is_a?(::URI::Generic)
|
|
|
|
|
|
|
|
begin
|
2007-03-17 06:13:25 -04:00
|
|
|
::URI.parse(rss)
|
2005-07-22 01:41:33 -04:00
|
|
|
rescue ::URI::Error
|
|
|
|
rss
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class BaseParser
|
|
|
|
|
2005-11-23 08:35:11 -05:00
|
|
|
class << self
|
|
|
|
def raise_for_undefined_entity?
|
|
|
|
listener.raise_for_undefined_entity?
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
def initialize(rss)
|
2005-11-23 08:35:11 -05:00
|
|
|
@listener = self.class.listener.new
|
2004-10-16 00:51:15 -04:00
|
|
|
@rss = rss
|
|
|
|
end
|
|
|
|
|
|
|
|
def rss
|
|
|
|
@listener.rss
|
|
|
|
end
|
|
|
|
|
|
|
|
def ignore_unknown_element
|
|
|
|
@listener.ignore_unknown_element
|
|
|
|
end
|
|
|
|
|
|
|
|
def ignore_unknown_element=(new_value)
|
|
|
|
@listener.ignore_unknown_element = new_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_validate
|
|
|
|
@listener.do_validate
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_validate=(new_value)
|
|
|
|
@listener.do_validate = new_value
|
|
|
|
end
|
|
|
|
|
|
|
|
def parse
|
|
|
|
if @listener.rss.nil?
|
|
|
|
_parse
|
|
|
|
end
|
|
|
|
@listener.rss
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class BaseListener
|
|
|
|
|
|
|
|
extend Utils
|
|
|
|
|
|
|
|
class << self
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
@@accessor_bases = {}
|
2004-10-16 00:51:15 -04:00
|
|
|
@@registered_uris = {}
|
2005-04-05 02:42:03 -04:00
|
|
|
@@class_names = {}
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
# return the setter for the uri, tag_name pair, or nil.
|
2004-10-16 00:51:15 -04:00
|
|
|
def setter(uri, tag_name)
|
2007-03-17 06:13:25 -04:00
|
|
|
_getter = getter(uri, tag_name)
|
|
|
|
if _getter
|
|
|
|
"#{_getter}="
|
|
|
|
else
|
2004-10-16 00:51:15 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def getter(uri, tag_name)
|
|
|
|
(@@accessor_bases[uri] || {})[tag_name]
|
|
|
|
end
|
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
# return the tag_names for setters associated with uri
|
2004-10-16 00:51:15 -04:00
|
|
|
def available_tags(uri)
|
2007-12-27 23:20:47 -05:00
|
|
|
(@@accessor_bases[uri] || {}).keys
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
# register uri against this name.
|
2005-03-30 21:10:18 -05:00
|
|
|
def register_uri(uri, name)
|
|
|
|
@@registered_uris[name] ||= {}
|
|
|
|
@@registered_uris[name][uri] = nil
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
# test if this uri is registered against this name
|
2005-03-30 21:10:18 -05:00
|
|
|
def uri_registered?(uri, name)
|
|
|
|
@@registered_uris[name].has_key?(uri)
|
|
|
|
end
|
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
# record class_name for the supplied uri and tag_name
|
2005-04-05 02:42:03 -04:00
|
|
|
def install_class_name(uri, tag_name, class_name)
|
|
|
|
@@class_names[uri] ||= {}
|
|
|
|
@@class_names[uri][tag_name] = class_name
|
|
|
|
end
|
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
# retrieve class_name for the supplied uri and tag_name
|
|
|
|
# If it doesn't exist, capitalize the tag_name
|
2005-04-05 02:42:03 -04:00
|
|
|
def class_name(uri, tag_name)
|
2007-12-27 23:20:47 -05:00
|
|
|
name = (@@class_names[uri] || {})[tag_name]
|
|
|
|
return name if name
|
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
tag_name = tag_name.gsub(/[_\-]([a-z]?)/) {$1.upcase}
|
2007-12-27 23:20:47 -05:00
|
|
|
tag_name[0, 1].upcase + tag_name[1..-1]
|
2005-04-05 02:42:03 -04:00
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
def install_get_text_element(uri, name, accessor_base)
|
|
|
|
install_accessor_base(uri, name, accessor_base)
|
2004-10-16 00:51:15 -04:00
|
|
|
def_get_text_element(uri, name, *get_file_and_line_from_caller(1))
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2005-11-23 08:35:11 -05:00
|
|
|
def raise_for_undefined_entity?
|
|
|
|
true
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
private
|
2008-06-29 05:02:32 -04:00
|
|
|
# set the accessor for the uri, tag_name pair
|
2007-03-17 06:13:25 -04:00
|
|
|
def install_accessor_base(uri, tag_name, accessor_base)
|
|
|
|
@@accessor_bases[uri] ||= {}
|
|
|
|
@@accessor_bases[uri][tag_name] = accessor_base.chomp("=")
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-08-04 23:03:05 -04:00
|
|
|
def def_get_text_element(uri, element_name, file, line)
|
|
|
|
register_uri(uri, element_name)
|
|
|
|
method_name = "start_#{element_name}"
|
|
|
|
unless private_method_defined?(method_name)
|
|
|
|
define_method(method_name) do |name, prefix, attrs, ns|
|
2007-03-17 06:13:25 -04:00
|
|
|
uri = _ns(ns, prefix)
|
2007-08-04 23:03:05 -04:00
|
|
|
if self.class.uri_registered?(uri, element_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
start_get_text_element(name, prefix, ns, uri)
|
|
|
|
else
|
|
|
|
start_else_element(name, prefix, attrs, ns)
|
|
|
|
end
|
|
|
|
end
|
2007-08-04 23:03:05 -04:00
|
|
|
private(method_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module ListenerMixin
|
|
|
|
attr_reader :rss
|
|
|
|
|
|
|
|
attr_accessor :ignore_unknown_element
|
|
|
|
attr_accessor :do_validate
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@rss = nil
|
|
|
|
@ignore_unknown_element = true
|
|
|
|
@do_validate = true
|
2007-03-17 06:13:25 -04:00
|
|
|
@ns_stack = [{"xml" => :xml}]
|
2004-10-16 00:51:15 -04:00
|
|
|
@tag_stack = [[]]
|
|
|
|
@text_stack = ['']
|
|
|
|
@proc_stack = []
|
|
|
|
@last_element = nil
|
|
|
|
@version = @encoding = @standalone = nil
|
|
|
|
@xml_stylesheets = []
|
2007-03-17 06:13:25 -04:00
|
|
|
@xml_child_mode = false
|
|
|
|
@xml_element = nil
|
|
|
|
@last_xml_element = nil
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2008-06-29 05:02:32 -04:00
|
|
|
# set instance vars for version, encoding, standalone
|
2004-10-16 00:51:15 -04:00
|
|
|
def xmldecl(version, encoding, standalone)
|
|
|
|
@version, @encoding, @standalone = version, encoding, standalone
|
|
|
|
end
|
|
|
|
|
|
|
|
def instruction(name, content)
|
|
|
|
if name == "xml-stylesheet"
|
|
|
|
params = parse_pi_content(content)
|
|
|
|
if params.has_key?("href")
|
2007-03-17 06:13:25 -04:00
|
|
|
@xml_stylesheets << XMLStyleSheet.new(params)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def tag_start(name, attributes)
|
|
|
|
@text_stack.push('')
|
|
|
|
|
|
|
|
ns = @ns_stack.last.dup
|
|
|
|
attrs = {}
|
|
|
|
attributes.each do |n, v|
|
2005-02-14 10:01:05 -05:00
|
|
|
if /\Axmlns(?:\z|:)/ =~ n
|
2004-10-16 00:51:15 -04:00
|
|
|
ns[$POSTMATCH] = v
|
|
|
|
else
|
|
|
|
attrs[n] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
@ns_stack.push(ns)
|
|
|
|
|
|
|
|
prefix, local = split_name(name)
|
2007-03-17 06:13:25 -04:00
|
|
|
@tag_stack.last.push([_ns(ns, prefix), local])
|
2004-10-16 00:51:15 -04:00
|
|
|
@tag_stack.push([])
|
2007-03-17 06:13:25 -04:00
|
|
|
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)
|
2004-10-16 00:51:15 -04:00
|
|
|
else
|
2007-03-17 06:13:25 -04:00
|
|
|
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
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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?
|
2005-02-14 10:01:05 -05:00
|
|
|
@ns_stack.pop
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def text(data)
|
2007-03-17 06:13:25 -04:00
|
|
|
if @xml_child_mode
|
|
|
|
@last_xml_element << data if @last_xml_element
|
|
|
|
else
|
|
|
|
@text_stack.last << data
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2007-03-17 06:13:25 -04:00
|
|
|
def _ns(ns, prefix)
|
|
|
|
ns.fetch(prefix, "")
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
|
|
|
CONTENT_PATTERN = /\s*([^=]+)=(["'])([^\2]+?)\2/
|
2008-06-29 05:02:32 -04:00
|
|
|
# Extract the first name="value" pair from content.
|
|
|
|
# Works with single quotes according to the constant
|
|
|
|
# CONTENT_PATTERN. Return a Hash.
|
2004-10-16 00:51:15 -04:00
|
|
|
def parse_pi_content(content)
|
|
|
|
params = {}
|
|
|
|
content.scan(CONTENT_PATTERN) do |name, quote, value|
|
|
|
|
params[name] = value
|
|
|
|
end
|
|
|
|
params
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_else_element(local, prefix, attrs, ns)
|
2007-03-17 06:13:25 -04:00
|
|
|
class_name = self.class.class_name(_ns(ns, prefix), local)
|
2004-10-16 00:51:15 -04:00
|
|
|
current_class = @last_element.class
|
2008-06-29 04:44:53 -04:00
|
|
|
if known_class?(current_class, class_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
next_class = current_class.const_get(class_name)
|
2007-08-13 11:06:58 -04:00
|
|
|
start_have_something_element(local, prefix, attrs, ns, next_class)
|
|
|
|
else
|
2007-03-17 06:13:25 -04:00
|
|
|
if !@do_validate or @ignore_unknown_element
|
2009-05-16 05:25:59 -04:00
|
|
|
@proc_stack.push(setup_next_element_in_unknown_element)
|
2004-10-16 00:51:15 -04:00
|
|
|
else
|
|
|
|
parent = "ROOT ELEMENT???"
|
|
|
|
if current_class.tag_name
|
|
|
|
parent = current_class.tag_name
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
raise NotExpectedTagError.new(local, _ns(ns, prefix), parent)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-06-29 04:44:53 -04:00
|
|
|
if Module.method(:const_defined?).arity == -1
|
|
|
|
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
|
|
|
|
else
|
|
|
|
def known_class?(target_class, class_name)
|
|
|
|
class_name and
|
|
|
|
(target_class.const_defined?(class_name) or
|
|
|
|
target_class.constants.include?(class_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-04-05 20:22:04 -04:00
|
|
|
NAMESPLIT = /^(?:([\w:][-\w.]*):)?([\w:][-\w.]*)/
|
2004-10-16 00:51:15 -04:00
|
|
|
def split_name(name)
|
|
|
|
name =~ NAMESPLIT
|
|
|
|
[$1 || '', $2]
|
|
|
|
end
|
|
|
|
|
2009-05-16 05:25:59 -04:00
|
|
|
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
|
2004-10-16 00:51:15 -04:00
|
|
|
raise NSError.new(tag_name, prefix, require_uri)
|
2007-03-17 06:13:25 -04:00
|
|
|
else
|
|
|
|
# Force bind required URI with prefix
|
|
|
|
@ns_stack.last[prefix] = require_uri
|
2009-05-16 05:25:59 -04:00
|
|
|
true
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def start_get_text_element(tag_name, prefix, ns, required_uri)
|
2007-03-17 06:13:25 -04:00
|
|
|
pr = Proc.new do |text, tags|
|
2004-10-16 00:51:15 -04:00
|
|
|
setter = self.class.setter(required_uri, tag_name)
|
2009-05-16 05:37:28 -04:00
|
|
|
if setter and @last_element.respond_to?(setter)
|
2007-03-17 06:13:25 -04:00
|
|
|
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
|
2005-11-23 04:49:02 -05:00
|
|
|
@last_element.__send__(setter, text.to_s)
|
2004-10-16 00:51:15 -04:00
|
|
|
else
|
2007-03-17 06:13:25 -04:00
|
|
|
if @do_validate and !@ignore_unknown_element
|
|
|
|
raise NotExpectedTagError.new(tag_name, _ns(ns, prefix),
|
|
|
|
@last_element.tag_name)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
end
|
|
|
|
@proc_stack.push(pr)
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def start_have_something_element(tag_name, prefix, attrs, ns, klass)
|
2009-05-16 05:25:59 -04:00
|
|
|
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
|
2007-12-27 23:20:47 -05:00
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-12-27 23:20:47 -05:00
|
|
|
def collect_attributes(tag_name, prefix, attrs, ns, klass)
|
2007-03-17 06:13:25 -04:00
|
|
|
attributes = {}
|
|
|
|
klass.get_attributes.each do |a_name, a_uri, required, element_name|
|
2004-10-16 00:51:15 -04:00
|
|
|
if a_uri.is_a?(String) or !a_uri.respond_to?(:include?)
|
|
|
|
a_uri = [a_uri]
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
unless a_uri == [""]
|
2004-10-16 00:51:15 -04:00
|
|
|
for prefix, uri in ns
|
|
|
|
if a_uri.include?(uri)
|
|
|
|
val = attrs["#{prefix}:#{a_name}"]
|
|
|
|
break if val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2007-03-17 06:13:25 -04:00
|
|
|
if val.nil? and a_uri.include?("")
|
2004-10-16 00:51:15 -04:00
|
|
|
val = attrs[a_name]
|
|
|
|
end
|
|
|
|
|
|
|
|
if @do_validate and required and val.nil?
|
2007-03-17 06:13:25 -04:00
|
|
|
unless a_uri.include?("")
|
2004-11-19 03:25:25 -05:00
|
|
|
for prefix, uri in ns
|
|
|
|
if a_uri.include?(uri)
|
|
|
|
a_name = "#{prefix}:#{a_name}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
raise MissingAttributeError.new(tag_name, a_name)
|
|
|
|
end
|
|
|
|
|
2007-03-17 06:13:25 -04:00
|
|
|
attributes[a_name] = val
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2007-12-27 23:20:47 -05:00
|
|
|
attributes
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
|
2007-12-27 23:20:47 -05:00
|
|
|
def setup_next_element(tag_name, klass, attributes)
|
2004-10-16 00:51:15 -04:00
|
|
|
previous = @last_element
|
2007-03-17 06:13:25 -04:00
|
|
|
next_element = klass.new(@do_validate, attributes)
|
2007-08-04 23:03:05 -04:00
|
|
|
previous.set_next_element(tag_name, next_element)
|
2004-10-16 00:51:15 -04:00
|
|
|
@last_element = next_element
|
2007-03-17 06:13:25 -04:00
|
|
|
@last_element.parent = previous if klass.need_parent?
|
|
|
|
@xml_child_mode = @last_element.have_xml_content?
|
2007-12-27 23:20:47 -05:00
|
|
|
|
|
|
|
Proc.new do |text, tags|
|
2004-10-16 00:51:15 -04:00
|
|
|
p(@last_element.class) if DEBUG
|
2007-03-17 06:13:25 -04:00
|
|
|
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?
|
2007-12-18 08:37:10 -05:00
|
|
|
text = text.lstrip.unpack("m").first
|
2007-03-17 06:13:25 -04:00
|
|
|
end
|
|
|
|
@last_element.content = text
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if @do_validate
|
|
|
|
@last_element.validate_for_stream(tags, @ignore_unknown_element)
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
@last_element = previous
|
2007-03-17 06:13:25 -04:00
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2009-05-16 05:25:59 -04:00
|
|
|
|
|
|
|
def setup_next_element_in_unknown_element
|
|
|
|
current_element, @last_element = @last_element, nil
|
|
|
|
Proc.new {@last_element = current_element}
|
|
|
|
end
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
unless const_defined? :AVAILABLE_PARSER_LIBRARIES
|
2013-11-09 00:56:09 -05:00
|
|
|
# The list of all available libraries for parsing.
|
2004-10-16 00:51:15 -04:00
|
|
|
AVAILABLE_PARSER_LIBRARIES = [
|
|
|
|
["rss/xmlparser", :XMLParserParser],
|
|
|
|
["rss/xmlscanner", :XMLScanParser],
|
|
|
|
["rss/rexmlparser", :REXMLParser],
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
2013-11-09 00:56:09 -05:00
|
|
|
# The list of all available parsers, in constant form.
|
2004-10-16 00:51:15 -04:00
|
|
|
AVAILABLE_PARSERS = []
|
|
|
|
|
|
|
|
AVAILABLE_PARSER_LIBRARIES.each do |lib, parser|
|
|
|
|
begin
|
|
|
|
require lib
|
|
|
|
AVAILABLE_PARSERS.push(const_get(parser))
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if AVAILABLE_PARSERS.empty?
|
|
|
|
raise XMLParserNotFound
|
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|