PageRenderTime 53ms CodeModel.GetById 46ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/rexml/parsers/lightparser.rb

http://github.com/agross/netopenspace
Ruby | 60 lines | 44 code | 5 blank | 11 comment | 1 complexity | 996ede7b86485e2ec3bbe09dbf1d407b MD5 | raw file
 1require 'rexml/parsers/streamparser'
 2require 'rexml/parsers/baseparser'
 3require 'rexml/light/node'
 4
 5module 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
60end