8f53dadbc1860d749e1262d4fa7ce7c2d3c86773
[booh] / lib / booh / rexml / parsers / lightparser.rb
1 require 'booh/rexml/parsers/streamparser'
2 require 'booh/rexml/parsers/baseparser'
3 require 'booh/rexml/light/node'
4
5 module REXML
6         module Parsers
7                 class LightParser
8                         def initialize stream
9                                 @stream = stream
10                                 @parser = REXML::Parsers::BaseParser.new( stream )
11                         end
12
13       def add_listener( listener )
14         @parser.add_listener( listener )
15       end
16
17       def rewind
18         @stream.rewind
19         @parser.stream = @stream
20       end
21
22                         def parse
23                                 root = context = [ :document ]
24                                 while true
25                                         event = @parser.pull
26                                         case event[0]
27                                         when :end_document
28                                                 break
29                                         when :end_doctype
30                                                 context = context[1]
31                                         when :start_element, :start_doctype
32                                                 new_node = event
33                                                 context << new_node
34                                                 new_node[1,0] = [context]
35                                                 context = new_node
36                                         when :end_element, :end_doctype
37                                                 context = context[1]
38                                         else
39                                                 new_node = event
40                                                 context << new_node
41                                                 new_node[1,0] = [context]
42                                         end
43                                 end
44                                 root
45                         end
46                 end
47
48                 # An element is an array.  The array contains:
49                 #  0                    The parent element
50                 #  1                    The tag name
51                 #  2                    A hash of attributes
52                 #  3..-1        The child elements
53                 # An element is an array of size > 3
54                 # Text is a String
55                 # PIs are [ :processing_instruction, target, data ]
56                 # Comments are [ :comment, data ]
57                 # DocTypes are DocType structs
58                 # The root is an array with XMLDecls, Text, DocType, Array, Text
59         end
60 end