Class: XMLStreamin::XMLStreamListener

Inherits:
Object
  • Object
show all
Includes:
REXML::StreamListener
Defined in:
lib/xmlstreamin.rb

Overview

This class extends REXML::StreamListener to provide an interface to a ‘tree’ of XMLSpec nodes that models the hierarchy of the XML document to be read.

Instance Method Summary collapse

Constructor Details

#initialize(root = $specXMLVoid, base = nil) ⇒ XMLStreamListener

Create a new XMLStreamListener with root as the root XMLSpec of the XML hierarchy to be parsed. base is an optional ‘context’, of any form suitable to the task, that will be passed to all XMLSpec methods invoked.



209
210
211
212
213
214
# File 'lib/xmlstreamin.rb', line 209

def initialize root=$specXMLVoid, base=nil
	@currSpec=root
	@currContext=base
	@prevspecs=[]
	@openTag=nil	
end

Instance Method Details

#tag_end(name) ⇒ Object

Invoked when the end tag is reached, with the name of the tag as argument. In the case of an empty tag (‘<tag/>’, tag_end will be called immediately after tag_start. If the element was not empty, the current XMLSpec#done method is called, otherwise the XMLSpec#empty method. Then the previous higher level) spec is restored.



238
239
240
241
242
243
244
245
246
# File 'lib/xmlstreamin.rb', line 238

def tag_end name
	@currContext = if (@openTag == name)
		@currSpec.empty(@currContext)
	  else
		@currSpec.done(@currContext, name)
	  end
	@openTag=nil	
	@currSpec = @prevspecs.pop
end

#tag_start(name, attrs) ⇒ Object

Invoked when a tag is encountered, with args:

  • name the tag name

  • attrs a Hash of attribute/value pairs. [NOT an array of arrays!] – i.e. a start tag like:

<tag attr1="value1" attr2="value2">
 will result in: 
 tag_start( "tag", {"attr1"=>"value1","attr2"=>"value2"})

This in turn determines the appropriate XMLSpec node that should handle the tag (by querying the current spec), sets this as the new current spec, and invokes its XMLSpec#start method.



226
227
228
229
230
231
# File 'lib/xmlstreamin.rb', line 226

def tag_start name, attrs
	@prevspecs.push(@currSpec)
	@openTag=name
	@currSpec = @currSpec.spec name
 	@currContext = @currSpec.start(@currContext,name,attrs)
end

#text(text) ⇒ Object

Invoked when text is encountered in the document, with the text content as argument. The current XMLSpec#text method is in turn called. (Note that if the text is interspersed with other elements, this method is invoked for each segment separately.)



252
253
254
255
# File 'lib/xmlstreamin.rb', line 252

def text text
	@openTag=nil	
	@currSpec.text(@currContext, text)
end