8000 #214 Introduce `reset` option to PullParser · ruby/rexml@37c3994 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37c3994

Browse files
author
Dmitry Pogrebnoy
committed
#214 Introduce reset option to PullParser
1 parent 519ae6c commit 37c3994

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

lib/rexml/parsers/baseparser.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ def add_listener( listener )
181181

182182
def stream=( source )
183183
@source = SourceFactory.create_from( source )
184+
reset
185+
end
186+
187+
def reset
184188
@closed = nil
185189
@have_root = false
186190
@document_status = nil

lib/rexml/parsers/pullparser.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ def pull
9393
def unshift token
9494
@my_stack.unshift token
9595
end
96+
97+
def reset
98+
@parser.reset
99+
end
96100
end
97101

98102
# A parsing event. The contents of the event are accessed as an +Array?,

test/test_pullparser.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,43 @@ def test_peek
156156
assert_equal( 0, names.length )
157157
end
158158

159+
def test_reset
160+
xml_chunks = [
161+
"<message>First valid and complete message</message>",
162+
"<message>Second valid and complete message</message>",
163+
"<message>Third valid and complete message</message>"
164+
]
165+
166+
reader, writer = IO.pipe
167+
xml_chunks.each do |chunk|
168+
writer.write(chunk)
169+
end
170+
writer.close
171+
172+
parser = REXML::Parsers::PullParser.new(reader)
173+
messages = []
174+
while parser.has_next?
175+
start_event = parser.pull
176+
if start_event.start_element? and start_event[0] == 'message'
177+
text = parser.pull
178+
if text.text?
179+
messages.push(text[0])
180+
end
181+
end
182+
end_event = parser.pull
183+
if end_event.end_element? and end_event[0] == 'message'
184+
parser.reset
185+
end
186+
end
187+
188+
assert_equal(
189+
messages,
190+
["First valid and complete message",
191+
"Second valid and complete message",
192+
"Third valid and complete message"]
193+
)
194+
end
195+
159196
class EntityExpansionLimitTest < Test::Unit::TestCase
160197
class GeneralEntityTest < self
161198
def test_have_value

0 commit comments

Comments
 (0)
0