Class: Dryml::Parser::TreeParser

Inherits:
REXML::Parsers::TreeParser
  • Object
show all
Defined in:
lib/dryml/parser/tree_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, build_context = Document.new) ⇒ TreeParser

Returns a new instance of TreeParser.



4
5
6
7
# File 'lib/dryml/parser/tree_parser.rb', line 4

def initialize( source, build_context = Document.new )
  @build_context = build_context
  @parser = Dryml::Parser::BaseParser.new(source)
end

Instance Method Details

#parseObject



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
# File 'lib/dryml/parser/tree_parser.rb', line 10

def parse
  tag_stack = []
  in_doctype = false
  entities = nil
  begin
    while true
      event = @parser.pull
      #STDERR.puts "TREEPARSER GOT #{event.inspect}"
      case event[0]
      when :end_document
        unless tag_stack.empty?
          #raise ParseException.new("No close tag for #{tag_stack.inspect}")
          raise ParseException.new("No close tag for #{@build_context.xpath}")
        end
        return
      when :start_element
        tag_stack.push(event[1])
        el = @build_context = @build_context.add_element( event[1] )
        event[2].each do |key, value|
          el.attributes[key]=Dryml::Parser::Attribute.new(key,value,self)
        end
        @build_context.start_tag_source = event[3]
        @build_context.source_offset = event[4]
      when :end_element
        tag_stack.pop
        @build_context.has_end_tag = event[2]
        @build_context = @build_context.parent
      when :text
        if not in_doctype
          if @build_context[-1].instance_of? Text
            @build_context[-1] << event[1]
          else
            @build_context.add(
                               Dryml::Parser::Text.new(event[1], @build_context.whitespace, nil, true)
                               ) unless (
                                         @build_context.ignore_whitespace_nodes and
                                         event[1].strip.size==0
                                         )
          end
        end
      when :comment
        c = REXML::Comment.new( event[1] )
        @build_context.add( c )
      when :cdata
        c = REXML::CData.new( event[1] )
        @build_context.add( c )
      when :processing_instruction
        @build_context.add( REXML::Instruction.new( event[1], event[2] ) )
      end
    end
  rescue REXML::Validation::ValidationException
    raise
  rescue
    raise REXML::ParseException.new( $!.message, @parser.source, @parser, $! )
  end
end