Class: REXML::Parsers::PullParser

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
XMLTokens
Defined in:
lib/rexml/parsers/pullparser.rb

Overview

Using the Pull Parser

This API is experimental, and subject to change.

parser = PullParser.new( "<a>text<b att='val'/>txet</a>" )
while parser.has_next?
  res = parser.next
  puts res[1]['att'] if res.start_tag? and res[0] == 'b'
end

See the PullEvent class for information on the content of the results. The data is identical to the arguments passed for the various events to the StreamListener API.

Notice that:

parser = PullParser.new( "<a>BAD DOCUMENT" )
while parser.has_next?
  res = parser.next
  raise res[1] if res.error?
end

Nat Price gave me some good ideas for the API.

Constant Summary

Constants included from XMLTokens

XMLTokens::NAME, XMLTokens::NAMECHAR, XMLTokens::NAME_CHAR, XMLTokens::NAME_START_CHAR, XMLTokens::NAME_STR, XMLTokens::NCNAME_STR, XMLTokens::NMTOKEN, XMLTokens::NMTOKENS, XMLTokens::REFERENCE

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ PullParser

Returns a new instance of PullParser.



38
39
40
41
42
43
# File 'lib/rexml/parsers/pullparser.rb', line 38

def initialize stream
  @entities = {}
  @listeners = nil
  @parser = BaseParser.new( stream )
  @my_stack = []
end

Instance Method Details

#add_listener(listener) ⇒ Object



45
46
47
48
# File 'lib/rexml/parsers/pullparser.rb', line 45

def add_listener( listener )
  @listeners = [] unless @listeners
  @listeners << listener
end

#eachObject



62
63
64
65
66
# File 'lib/rexml/parsers/pullparser.rb', line 62

def each
  while has_next?
    yield self.pull
  end
end

#entity_expansion_countObject



50
51
52
# File 'lib/rexml/parsers/pullparser.rb', line 50

def entity_expansion_count
  @parser.entity_expansion_count
end

#entity_expansion_limit=(limit) ⇒ Object



54
55
56
# File 'lib/rexml/parsers/pullparser.rb', line 54

def entity_expansion_limit=( limit )
  @parser.entity_expansion_limit = limit
end

#entity_expansion_text_limit=(limit) ⇒ Object



58
59
60
# File 'lib/rexml/parsers/pullparser.rb', line 58

def entity_expansion_text_limit=( limit )
  @parser.entity_expansion_text_limit = limit
end

#peek(depth = 0) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/rexml/parsers/pullparser.rb', line 68

def peek depth=0
  if @my_stack.length <= depth
    (depth - @my_stack.length + 1).times {
      e = PullEvent.new(@parser.pull)
      @my_stack.push(e)
    }
  end
  @my_stack[depth]
end

#pullObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rexml/parsers/pullparser.rb', line 78

def pull
  return @my_stack.shift if @my_stack.length > 0

  event = @parser.pull
  case event[0]
  when :entitydecl
    @entities[ event[1] ] =
      event[2] unless event[2] =~ /PUBLIC|SYSTEM/
  when :text
    unnormalized = @parser.unnormalize( event[1], @entities )
    event << unnormalized
  end
  PullEvent.new( event )
end

#resetObject



97
98
99
# File 'lib/rexml/parsers/pullparser.rb', line 97

def reset
  @parser.reset
end

#unshift(token) ⇒ Object



93
94
95
# File 'lib/rexml/parsers/pullparser.rb', line 93

def unshift token
  @my_stack.unshift token
end