Included Modules

Class/Module Index [+]

Quicksearch

ECoreXMLInstantiator

Public Class Methods

new(env, loglevel=ERROR) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 13
def initialize(env, loglevel=ERROR)
  @env = env
  @rolestack = []
  @elementstack = []
  @element_by_id = {}
  @loglevel = loglevel
end

Public Instance Methods

eAllAttributes(element) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 117
def eAllAttributes(element)
  @eAllAttributes ||= {}
  @eAllAttributes[element.class] ||= element.class.ecore.eAllAttributes
end
eAllReferences(element) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 112
def eAllReferences(element)
  @eAllReferences ||= {}
  @eAllReferences[element.class] ||= element.class.ecore.eAllReferences
end
eAllStructuralFeatures(element) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 122
def eAllStructuralFeatures(element)
  @eAllStructuralFeatures ||= {}
  @eAllStructuralFeatures[element.class] ||= element.class.ecore.eAllStructuralFeatures
end
end_tag(prefix, tag) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 65
def end_tag(prefix, tag)
  @elementstack.pop
end
find_in_context(context, desc_elements) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 146
def find_in_context(context, desc_elements)
  if context.is_a?(EPackage)
    r = (context.eClassifiers + context.eSubpackages).find{|c| c.name == desc_elements.first}
  elsif context.is_a?(EClass)
    r = context.eStructuralFeatures.find{|s| s.name == desc_elements.first}
  else
    raise StandardError.new("Don't know how to find #{desc_elements.join('/')} in context #{context}")
  end
  if r
    if desc_elements.size > 1
      find_in_context(r, desc_elements[1..-1])
    else
      r
    end
  else
    log WARN, "Can not follow path, element #{desc_elements.first} not found within #{context}(#{context.name})"
  end
end
find_referenced(context, desc) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 127
def find_referenced(context, desc)
  desc.split(/\s+/).collect do |r|
    if r =~ /^#([^\/]+)$/
      @element_by_id[$1]
    elsif r =~ /^#\/\d*\/([\w\/]+)/
      find_in_context(context, $1.split('/'))
    elsif r =~ /#\/\/(\w+)$/
      case $1
        when "EString";     RGen::ECore::EString
        when "EInt";        RGen::ECore::EInt
        when "EBoolean";    RGen::ECore::EBoolean
        when "EFloat";      RGen::ECore::EFloat
        when "EJavaObject"; RGen::ECore::EJavaObject
        when "EJavaClass";  RGen::ECore::EJavaClass
      end
    end
  end.compact
end
instantiate(str) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 94
  def instantiate(str)
    @resolver_descs = []
#    puts "Instantiating ..."
    super(str, 1000)
    rootpackage = @env.find(:class => EPackage).first
#    puts "Resolving ..."
    @resolver_descs.each do |rd|
      refed = find_referenced(rootpackage, rd.value)
      feature = eAllStructuralFeatures(rd.object).find{|f| f.name == rd.attribute}
      raise StandardError.new("StructuralFeature not found: #{rd.attribute}") unless feature
      if feature.many
        rd.object.setGeneric(feature.name, refed)
      else
        rd.object.setGeneric(feature.name, refed.first)
      end
    end
  end
log(level, msg) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 165
def log(level, msg)
  puts %(INFO WARN ERROR)[level] + ": " + msg if level >= @loglevel
end
set_attribute(attr, value) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 71
def set_attribute(attr, value)
  # do nothing, already handled by start_tag/set_attribute_internal
end
set_attribute_internal(attr, value) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 75
def set_attribute_internal(attr, value)
  return unless @elementstack.last
  eFeat = eAllStructuralFeatures(@elementstack.last).find{|a| a.name == attr}
  if eFeat.is_a?(EReference)
    rd = ResolverDescription.new
    rd.object = @elementstack.last
    rd.attribute = attr
    rd.value = value
    @resolver_descs << rd
  elsif eFeat
    value = true if value == "true" && eFeat.eType == EBoolean
    value = false if value == "false" && eFeat.eType == EBoolean
    value = value.to_i if eFeat.eType == EInt
    @elementstack.last.setGeneric(attr, value)
  else
    log WARN, "Feature not found: #{attr} on #{@elementstack.last}"
  end
end
start_tag(prefix, tag, namespaces, attributes) click to toggle source
# File lib/rgen/instantiator/ecore_xml_instantiator.rb, line 21
def start_tag(prefix, tag, namespaces, attributes)
  eRef = nil
  if @elementstack.last
    eRef = eAllReferences(@elementstack.last).find{|r|r.name == tag}
    if eRef
      if attributes["xsi:type"] && attributes["xsi:type"] =~ /ecore:(\w+)/
        class_name = $1
        attributes.delete("xsi:type")
      else 
        class_name = eRef.eType.name
      end
    else
      raise "Reference not found: #{tag} on #{@elementstack.last}"
    end
  else
    class_name = tag
  end
  
  eClass = RGen::ECore.ecore.eClassifiers.find{|c| c.name == class_name}
  if eClass
    obj = RGen::ECore.const_get(class_name).new
    if attributes["xmi:id"]
      @element_by_id[attributes["xmi:id"]] = obj
      attributes.delete("xmi:id")
    end
    if eRef
      if eRef.many
        @elementstack.last.addGeneric(eRef.name, obj)
      else
        @elementstack.last.setGeneric(eRef.name, obj)
      end
    end
    @env << obj
    @elementstack.push obj
  else
    log WARN, "Class not found: #{class_name}"
    @elementstack.push nil
  end

  attributes.each_pair do |attr, value|
    set_attribute_internal(attr, value)
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.