summaryrefslogtreecommitdiff
path: root/jni/ruby/test/rexml/test_stream.rb
blob: 3164d91c9194bb021afcfe584c2e8c60c3901194 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require "test/unit/testcase"
require "rexml/document"
require 'rexml/streamlistener'
require 'stringio'

module REXMLTests
  class MyListener
    include REXML::StreamListener
  end


  class StreamTester < Test::Unit::TestCase
    # Submitted by Han Holl
    def test_listener
      data = %Q{<session1 user="han" password="rootWeiler" />\n<session2 user="han" password="rootWeiler" />}

      b = RequestReader.new( data )
      b = RequestReader.new( data )
    end

    def test_ticket_49
      source = StringIO.new( <<-EOL )
      <!DOCTYPE foo [
        <!ENTITY ent "replace">
      ]>
      <a>&ent;</a>
      EOL
      REXML::Document.parse_stream(source, MyListener.new)
    end

    def test_ticket_10
      source = StringIO.new( <<-EOL )
      <!DOCTYPE foo [
        <!ENTITY ent "replace">
        <!ATTLIST a
         xmlns:human CDATA #FIXED "http://www.foo.com/human">
        <!ELEMENT bar (#PCDATA)>
        <!NOTATION n1 PUBLIC "-//HM//NOTATION TEST1//EN" 'urn:x-henrikmartensson.org:test5'>
      ]>
      <a/>
      EOL
      listener = MyListener.new
      class << listener
        attr_accessor :events
        def entitydecl( content )
          @events[ :entitydecl ] = true
        end
        def attlistdecl( element_name, attributes, raw_content )
          @events[ :attlistdecl ] = true
        end
        def elementdecl( content )
          @events[ :elementdecl ] = true
        end
        def notationdecl( content )
          @events[ :notationdecl ] = true
        end
      end
      listener.events = {}

      REXML::Document.parse_stream( source, listener )

      assert( listener.events[:entitydecl] )
      assert( listener.events[:attlistdecl] )
      assert( listener.events[:elementdecl] )
      assert( listener.events[:notationdecl] )
    end

    def test_entity
      listener = MyListener.new
      class << listener
        attr_accessor :entities
        def entity(content)
          @entities << content
        end
      end
      listener.entities = []

      source = StringIO.new(<<-XML)
<!DOCTYPE root [
<!ENTITY % ISOLat2
         SYSTEM "http://www.xml.com/iso/isolat2-xml.entities" >
%ISOLat2;
]>
<root/>
      XML
      REXML::Document.parse_stream(source, listener)

      assert_equal(["ISOLat2"], listener.entities)
    end
  end


  # For test_listener
  class RequestReader
    attr_reader :doc
    def initialize(io)
      @stack = []
      @doc = nil
      catch(:fini) do
        REXML::Document.parse_stream(io, self)
        raise IOError
      end
    end
    def tag_start(name, args)
      if @doc
        @stack.push(REXML::Element.new(name, @stack.last))
      else
        @doc = REXML::Document.new("<#{name}/>")
        @stack.push(@doc.root)
      end
      args.each do |attr,val|
        @stack.last.add_attribute(attr, val)
      end
    end
    def tag_end(name, *args)
      @stack.pop
      throw(:fini) if @stack.empty?
    end
    def text(str)
      @stack.last.text = str
    end
    def comment(str)
    end
    def doctype( name, pub_sys, long_name, uri )
    end
    def doctype_end
    end
  end
end