1# frozen_string_literal: false
2require "rexml/document"
3require "rexml/streamlistener"
4
5module RSS
6
7  class REXMLParser < BaseParser
8
9    class << self
10      def listener
11        REXMLListener
12      end
13    end
14
15    private
16    def _parse
17      begin
18        REXML::Document.parse_stream(@rss, @listener)
19      rescue RuntimeError => e
20        raise NotWellFormedError.new{e.message}
21      rescue REXML::ParseException => e
22        context = e.context
23        line = context[0] if context
24        raise NotWellFormedError.new(line){e.message}
25      end
26    end
27
28  end
29
30  class REXMLListener < BaseListener
31
32    include REXML::StreamListener
33    include ListenerMixin
34
35    class << self
36      def raise_for_undefined_entity?
37        false
38      end
39    end
40
41    def xmldecl(version, encoding, standalone)
42      super(version, encoding, standalone == "yes")
43      # Encoding is converted to UTF-8 when REXML parse XML.
44      @encoding = 'UTF-8'
45    end
46
47    alias_method(:cdata, :text)
48  end
49
50end
51