From fcbf63e62c627deae76c1b8cb8c0876c536ed811 Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Mon, 16 Mar 2020 18:49:26 +0900 Subject: Fresh start --- jni/ruby/lib/rss/xmlparser.rb | 94 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 jni/ruby/lib/rss/xmlparser.rb (limited to 'jni/ruby/lib/rss/xmlparser.rb') diff --git a/jni/ruby/lib/rss/xmlparser.rb b/jni/ruby/lib/rss/xmlparser.rb new file mode 100644 index 0000000..46df94a --- /dev/null +++ b/jni/ruby/lib/rss/xmlparser.rb @@ -0,0 +1,94 @@ +begin + require "xml/parser" +rescue LoadError + require "xmlparser" +end + +begin + require "xml/encoding-ja" +rescue LoadError + require "xmlencoding-ja" + if defined?(Kconv) + module XMLEncoding_ja + class SJISHandler + include Kconv + end + end + end +end + +module XML + class Parser + unless defined?(Error) + # This error is legacy, so we just set it to the new one + Error = ::XMLParserError # :nodoc: + end + end +end + +module RSS + + class REXMLLikeXMLParser < ::XML::Parser + + include ::XML::Encoding_ja + + def listener=(listener) + @listener = listener + end + + def startElement(name, attrs) + @listener.tag_start(name, attrs) + end + + def endElement(name) + @listener.tag_end(name) + end + + def character(data) + @listener.text(data) + end + + def xmlDecl(version, encoding, standalone) + @listener.xmldecl(version, encoding, standalone == 1) + end + + def processingInstruction(target, content) + @listener.instruction(target, content) + end + + end + + class XMLParserParser < BaseParser + + class << self + def listener + XMLParserListener + end + end + + private + def _parse + begin + parser = REXMLLikeXMLParser.new + parser.listener = @listener + parser.parse(@rss) + rescue ::XML::Parser::Error => e + raise NotWellFormedError.new(parser.line){e.message} + end + end + + end + + class XMLParserListener < BaseListener + + include ListenerMixin + + def xmldecl(version, encoding, standalone) + super + # Encoding is converted to UTF-8 when XMLParser parses XML. + @encoding = 'UTF-8' + end + + end + +end -- cgit v1.2.3