2004-01-27 22:46:13 -05:00
|
|
|
require 'xmlscan/scanner'
|
|
|
|
|
|
|
|
module RSS
|
2004-10-16 00:51:15 -04:00
|
|
|
|
|
|
|
class XMLScanParser < BaseParser
|
|
|
|
|
|
|
|
private
|
|
|
|
def listener
|
|
|
|
XMLScanListener
|
|
|
|
end
|
|
|
|
|
|
|
|
def _parse
|
|
|
|
begin
|
|
|
|
XMLScan::XMLScanner.new(@listener).parse(@rss)
|
|
|
|
rescue XMLScan::Error => e
|
|
|
|
raise NotWellFormedError.new(e.lineno){e.message}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class XMLScanListener < BaseListener
|
|
|
|
|
|
|
|
include XMLScan::Visitor
|
|
|
|
include ListenerMixin
|
|
|
|
|
|
|
|
ENTITIES = {
|
|
|
|
'lt' => '<',
|
|
|
|
'gt' => '>',
|
|
|
|
'amp' => '&',
|
|
|
|
'quot' => '"',
|
|
|
|
'apos' => '\''
|
|
|
|
}
|
2004-01-27 22:46:13 -05:00
|
|
|
|
|
|
|
def on_xmldecl_version(str)
|
2004-10-16 00:51:15 -04:00
|
|
|
@version = str
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_xmldecl_encoding(str)
|
2004-10-16 00:51:15 -04:00
|
|
|
@encoding = str
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_xmldecl_standalone(str)
|
2004-10-16 00:51:15 -04:00
|
|
|
@standalone = str
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_xmldecl_end
|
2004-11-19 03:25:25 -05:00
|
|
|
xmldecl(@version, @encoding, @standalone == "yes")
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
alias_method(:on_pi, :instruction)
|
|
|
|
alias_method(:on_chardata, :text)
|
|
|
|
alias_method(:on_cdata, :text)
|
2004-01-27 22:46:13 -05:00
|
|
|
|
|
|
|
def on_etag(name)
|
|
|
|
tag_end(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def on_entityref(ref)
|
2004-10-16 00:51:15 -04:00
|
|
|
text(ENTITIES[ref])
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_charref(code)
|
2004-10-16 00:51:15 -04:00
|
|
|
text([code].pack('U'))
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
alias_method(:on_charref_hex, :on_charref)
|
2004-01-27 22:46:13 -05:00
|
|
|
|
|
|
|
def on_stag(name)
|
2004-10-16 00:51:15 -04:00
|
|
|
@attrs = {}
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_attribute(name)
|
2004-10-16 00:51:15 -04:00
|
|
|
@attrs[name] = @current_attr = ''
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_attr_value(str)
|
2004-10-16 00:51:15 -04:00
|
|
|
@current_attr << str
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_attr_entityref(ref)
|
2004-10-16 00:51:15 -04:00
|
|
|
@current_attr << ENTITIES[ref]
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_attr_charref(code)
|
2004-10-16 00:51:15 -04:00
|
|
|
@current_attr << [code].pack('U')
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
alias_method(:on_attr_charref_hex, :on_attr_charref)
|
2004-01-27 22:46:13 -05:00
|
|
|
|
|
|
|
def on_stag_end(name)
|
2004-10-16 00:51:15 -04:00
|
|
|
tag_start(name, @attrs)
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_stag_end_empty(name)
|
2004-10-16 00:51:15 -04:00
|
|
|
tag_start(name, @attrs)
|
|
|
|
tag_end(name)
|
2004-01-27 22:46:13 -05:00
|
|
|
end
|
|
|
|
|
2004-10-16 00:51:15 -04:00
|
|
|
end
|
2004-01-27 22:46:13 -05:00
|
|
|
|
|
|
|
end
|