def Document.parse_stream( source, listener )
if source.kind_of? Source
# do nothingelsif source.kind_of? IO
source = IOSource.new(source)
elsif source.kind_of? String
source = Source.new source
else
raise "Unknown source type!"endwhilenot source.empty?
word = source.match( /^\s*(<.*?)>/um )
source.match( /^\s*/um, true )
word = word[1] unless word.nil?
case word
whennil
word = source.match( /\s*(\S+)/um, true )
returnif word.nil?
word = word[0]
raise "data found outside of root element ('#{word}')" if word.strip.length > 0
when Comment::START_RE
Comment.parse_stream source, listener
when DocType::START_RE
DocType.parse_stream source, listener
when XMLDecl::START_RE
XMLDecl.parse_stream source, listener
when Instruction::START_RE
Instruction.parse_stream source, listener
else
Element.parse_stream source, listener
endend# Here we need to check for invalid documents.end