Skip to content

Commit

Permalink
ruby#214 Introduce reset option to PullParser
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Pogrebnoy committed Oct 30, 2024
1 parent 519ae6c commit 37c3994
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/rexml/parsers/baseparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ def add_listener( listener )

def stream=( source )
@source = SourceFactory.create_from( source )
reset
end

def reset
@closed = nil
@have_root = false
@document_status = nil
Expand Down
4 changes: 4 additions & 0 deletions lib/rexml/parsers/pullparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ def pull
def unshift token
@my_stack.unshift token
end

def reset
@parser.reset
end
end

# A parsing event. The contents of the event are accessed as an +Array?,
Expand Down
37 changes: 37 additions & 0 deletions test/test_pullparser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,43 @@ def test_peek
assert_equal( 0, names.length )
end

def test_reset
xml_chunks = [
"<message>First valid and complete message</message>",
"<message>Second valid and complete message</message>",
"<message>Third valid and complete message</message>"
]

reader, writer = IO.pipe
xml_chunks.each do |chunk|
writer.write(chunk)
end
writer.close

parser = REXML::Parsers::PullParser.new(reader)
messages = []
while parser.has_next?
start_event = parser.pull
if start_event.start_element? and start_event[0] == 'message'
text = parser.pull
if text.text?
messages.push(text[0])
end
end
end_event = parser.pull
if end_event.end_element? and end_event[0] == 'message'
parser.reset
end
end

assert_equal(
messages,
["First valid and complete message",
"Second valid and complete message",
"Third valid and complete message"]
)
end

class EntityExpansionLimitTest < Test::Unit::TestCase
class GeneralEntityTest < self
def test_have_value
Expand Down

0 comments on commit 37c3994

Please sign in to comment.