Class: RGen::Instantiator::NodebasedXMLInstantiator

Inherits:
AbstractInstantiator show all
Defined in:
lib/rgen/instantiator/nodebased_xml_instantiator.rb

Direct Known Subclasses

DefaultXMLInstantiator

Defined Under Namespace

Classes: XMLNodeDescriptor, XMLScanVisitor

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AbstractInstantiator

resolve, resolve_by_id

Constructor Details

#initialize(env) ⇒ NodebasedXMLInstantiator

Returns a new instance of NodebasedXMLInstantiator.



86
87
88
89
90
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 86

def initialize(env)
	super
	@env = env
	@stack = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RGen::Instantiator::AbstractInstantiator

Class Method Details

.prune_levelObject

:nodoc:



25
26
27
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 25

def prune_level # :nodoc:
	@prune_level ||= 0
end

.set_prune_level(level) ⇒ Object

The prune level is the number of parent/children associations which is kept when the instantiator ascents the XML tree. If the level is 2, information for the node’s children and the childrens’ children will be available as an XMLNodeDescriptor object. If the level is 0 no pruning will take place, i.e. the whole information is kept until the end of the instantiation process. 0 is default.



21
22
23
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 21

def set_prune_level(level)
	@prune_level = level
end

Instance Method Details

#end_elementObject



115
116
117
118
119
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 115

def end_element
	node = @stack.pop
	on_ascent(node)
	prune_children(node, self.class.prune_level - 1) if self.class.prune_level > 0
end

#instantiate(text) ⇒ Object



97
98
99
100
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 97

def instantiate(text)
	parse(text)
	resolve
end

#instantiate_file(file) ⇒ Object



92
93
94
95
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 92

def instantiate_file(file)
	File.open(file) { |f| parse(f.read)}
	resolve
end

#namespacesObject



140
141
142
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 140

def namespaces
  @visitor.namespaces if @visitor
end

#on_ascent(node) ⇒ Object

This method is called when the XML parser goes up the tree. An XMLNodeDescriptor node describes the current node. Implementing classes must overwrite this method.



136
137
138
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 136

def on_ascent(node)
	raise "Overwrite this method !"
end

#on_chardata(str) ⇒ Object



121
122
123
124
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 121

def on_chardata(str)
	node = @stack.last
	node.chardata << str
end

#on_descent(node) ⇒ Object

This method is called when the XML parser goes down the tree. An XMLNodeDescriptor node describes the current node. Implementing classes must overwrite this method.



129
130
131
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 129

def on_descent(node)
	raise "Overwrite this method !"
end

#parse(src) ⇒ Object



102
103
104
105
106
107
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 102

def parse(src)
    @visitor = XMLScanVisitor.new(self)
	parser = XMLScan::XMLParserNS.new(@visitor)
    parser.parse(src)
	@visitor = nil
end

#start_element(ns, qtag, prefix, tag, attributes) ⇒ Object



109
110
111
112
113
# File 'lib/rgen/instantiator/nodebased_xml_instantiator.rb', line 109

def start_element(ns, qtag, prefix, tag, attributes)
	node = XMLNodeDescriptor.new(ns, qtag, prefix, tag, @stack[-1], [], attributes)
	@stack.push node
	on_descent(node)
end