2010-09-17 09:14:14 -04:00
|
|
|
require "test/unit/testcase"
|
|
|
|
require "rexml/document"
|
|
|
|
require 'rexml/streamlistener'
|
|
|
|
require 'stringio'
|
|
|
|
|
2014-05-27 08:07:40 -04:00
|
|
|
module REXMLTests
|
2014-05-27 09:10:55 -04:00
|
|
|
class MyListener
|
|
|
|
include REXML::StreamListener
|
|
|
|
end
|
2010-09-17 09:14:14 -04:00
|
|
|
|
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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" />}
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
b = RequestReader.new( data )
|
|
|
|
b = RequestReader.new( data )
|
|
|
|
end
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2010-09-17 09:14:14 -04:00
|
|
|
end
|
2014-05-27 09:10:55 -04:00
|
|
|
listener.events = {}
|
2011-05-15 07:55:52 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
REXML::Document.parse_stream( source, listener )
|
2010-09-17 09:14:14 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
assert( listener.events[:entitydecl] )
|
|
|
|
assert( listener.events[:attlistdecl] )
|
|
|
|
assert( listener.events[:elementdecl] )
|
|
|
|
assert( listener.events[:notationdecl] )
|
|
|
|
end
|
2013-07-26 10:17:24 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
def test_entity
|
|
|
|
listener = MyListener.new
|
|
|
|
class << listener
|
|
|
|
attr_accessor :entities
|
|
|
|
def entity(content)
|
|
|
|
@entities << content
|
|
|
|
end
|
2013-07-26 10:17:24 -04:00
|
|
|
end
|
2014-05-27 09:10:55 -04:00
|
|
|
listener.entities = []
|
2013-07-26 10:17:24 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
source = StringIO.new(<<-XML)
|
2013-07-26 10:17:24 -04:00
|
|
|
<!DOCTYPE root [
|
|
|
|
<!ENTITY % ISOLat2
|
|
|
|
SYSTEM "http://www.xml.com/iso/isolat2-xml.entities" >
|
|
|
|
%ISOLat2;
|
|
|
|
]>
|
|
|
|
<root/>
|
2014-05-27 09:10:55 -04:00
|
|
|
XML
|
|
|
|
REXML::Document.parse_stream(source, listener)
|
2013-07-26 10:17:24 -04:00
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
assert_equal(["ISOLat2"], listener.entities)
|
|
|
|
end
|
2013-07-26 10:17:24 -04:00
|
|
|
end
|
2010-09-17 09:14:14 -04:00
|
|
|
|
|
|
|
|
2014-05-27 09:10:55 -04:00
|
|
|
# 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
|
2010-09-17 09:14:14 -04:00
|
|
|
end
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2010-09-17 09:14:14 -04:00
|
|
|
end
|
2014-05-27 09:10:55 -04:00
|
|
|
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
|
2010-09-17 09:14:14 -04:00
|
|
|
end
|
|
|
|
end
|
2014-05-27 06:21:10 -04:00
|
|
|
end
|