Class: RGen::Instantiator::DefaultXMLInstantiator

Inherits:
NodebasedXMLInstantiator show all
Includes:
NameHelper
Defined in:
lib/rgen/instantiator/default_xml_instantiator.rb

Overview

A default XML instantiator. Derive your own instantiator from this class or use it as is.

Defined Under Namespace

Classes: NamespaceDescriptor

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NameHelper

#camelize, #className, #firstToLower, #firstToUpper, #normalize, #saneClassName, #saneMethodName

Methods inherited from NodebasedXMLInstantiator

#end_element, #instantiate, #instantiate_file, #namespaces, #on_chardata, #parse, prune_level, set_prune_level, #start_element

Methods inherited from AbstractInstantiator

resolve, resolve_by_id

Constructor Details

#initialize(env, default_module, create_mm = false) ⇒ DefaultXMLInstantiator

Returns a new instance of DefaultXMLInstantiator.



28
29
30
31
32
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 28

def initialize(env, default_module, create_mm=false)
	super(env)
	@default_module = default_module
	@create_mm = create_mm
end

Dynamic Method Handling

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

Class Method Details

.map_tag_ns(from, to, prefix = "") ⇒ Object



17
18
19
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 17

def map_tag_ns(from, to, prefix="")
	tag_ns_map[from] = NamespaceDescriptor.new(prefix, to)
end

.tag_ns_mapObject

:nodoc:



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

def tag_ns_map # :nodoc:
	@tag_ns_map ||={}
	@tag_ns_map
end

Instance Method Details

#assoc_p2c(parent, child) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 68

def assoc_p2c(parent, child)
       return unless parent.object && child.object
       method_name = method_name(className(child.object))
	build_on_error(NoMethodError, :build_p2c_assoc, parent, child, method_name) do
		parent.object.addGeneric(method_name, child.object)
		child.object.setGeneric("parent", parent.object)
	end
end

#build_attribute(node, attr, value) ⇒ Object



89
90
91
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 89

def build_attribute(node, attr, value)
	node.object.class.has_attr(method_name(attr))
end

#build_class(name, mod) ⇒ Object



60
61
62
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 60

def build_class(name, mod)
	mod.const_set(name, Class.new(RGen::MetamodelBuilder::MMBase))
end

#build_p2c_assoc(parent, child, method_name) ⇒ Object



77
78
79
80
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 77

def build_p2c_assoc(parent, child, method_name)
	parent.object.class.has_many(method_name, child.object.class)
	child.object.class.has_one("parent", RGen::MetamodelBuilder::MMBase)
end

#class_name(str) ⇒ Object



47
48
49
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 47

def class_name(str)
  saneClassName(str)
end

#method_name(str) ⇒ Object



64
65
66
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 64

def method_name(str)
  saneMethodName(str)
end

#new_object(node) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 51

def new_object(node)
	ns_desc = self.class.tag_ns_map[node.namespace]
	class_name = class_name(ns_desc.nil? ? node.qtag : ns_desc.prefix+node.tag)
	mod = (ns_desc && ns_desc.target) || @default_module		
	build_on_error(NameError, :build_class, class_name, mod) do
		mod.const_get(class_name).new
	end
end

#on_ascent(node) ⇒ Object



41
42
43
44
45
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 41

def on_ascent(node)
	node.children.each { |c| assoc_p2c(node, c) }
	node.object.class.has_attr 'chardata', Object unless node.object.respond_to?(:chardata)
	set_attribute(node, "chardata", node.chardata)
end

#on_descent(node) ⇒ Object



34
35
36
37
38
39
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 34

def on_descent(node)
	obj = new_object(node)
	@env << obj unless obj.nil?
	node.object = obj
	node.attributes.each_pair { |k,v| set_attribute(node, k, v) }
end

#set_attribute(node, attr, value) ⇒ Object



82
83
84
85
86
87
# File 'lib/rgen/instantiator/default_xml_instantiator.rb', line 82

def set_attribute(node, attr, value)
   return unless node.object
   	build_on_error(NoMethodError, :build_attribute, node, attr, value) do
		node.object.setGeneric(method_name(attr), value)
	end
end