1 require 'rexml/validation/validationexception'
2 require 'rexml/undefinednamespaceexception'
7 def initialize( source, build_context = Document.new )
8 @build_context = build_context
9 @parser = Parsers::BaseParser.new( source )
12 def add_listener( listener )
13 @parser.add_listener( listener )
23 #STDERR.puts "TREEPARSER GOT #{event.inspect}"
26 unless tag_stack.empty?
27 #raise ParseException.new("No close tag for #{tag_stack.inspect}")
28 raise ParseException.new("No close tag for #{@build_context.xpath}")
32 tag_stack.push(event[1])
33 el = @build_context = @build_context.add_element( event[1], event[2] )
36 @build_context = @build_context.parent
39 if @build_context[-1].instance_of? Text
40 @build_context[-1] << event[1]
43 Text.new(event[1], @build_context.whitespace, nil, true)
45 @build_context.ignore_whitespace_nodes and
46 event[1].strip.size==0
51 c = Comment.new( event[1] )
52 @build_context.add( c )
54 c = CData.new( event[1] )
55 @build_context.add( c )
56 when :processing_instruction
57 @build_context.add( Instruction.new( event[1], event[2] ) )
60 entities.each { |k,v| entities[k] = @build_context.entities[k].value }
61 @build_context = @build_context.parent
63 doctype = DocType.new( event[1..-1], @build_context )
64 @build_context = doctype
68 n = AttlistDecl.new( event[1..-1] )
69 @build_context.add( n )
71 n = ExternalEntity.new( event[1] )
72 @build_context.add( n )
74 n = ElementDecl.new( event[1] )
77 entities[ event[1] ] = event[2] unless event[2] =~ /PUBLIC|SYSTEM/
78 @build_context.add(Entity.new(event))
80 n = NotationDecl.new( *event[1..-1] )
81 @build_context.add( n )
83 x = XMLDecl.new( event[1], event[2], event[3] )
84 @build_context.add( x )
87 rescue REXML::Validation::ValidationException
89 rescue REXML::UndefinedNamespaceException
92 raise ParseException.new( $!.message, @parser.source, @parser, $! )