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