Class: RGen::XSD::XSDInstantiator

Inherits:
Object
  • Object
show all
Defined in:
lib/rgen/xsd/xsd_instantiator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env, mm) ⇒ XSDInstantiator

Returns a new instance of XSDInstantiator.



10
11
12
13
14
15
# File 'lib/rgen/xsd/xsd_instantiator.rb', line 10

def initialize(env, mm)
  @env = env
  @mm = mm 
  @unresolved_refs = []
  @resolver = RGen::Instantiator::ReferenceResolver.new
end

Instance Attribute Details

#unresolved_refsObject (readonly)

Returns the value of attribute unresolved_refs.



8
9
10
# File 'lib/rgen/xsd/xsd_instantiator.rb', line 8

def unresolved_refs
  @unresolved_refs
end

Instance Method Details

#add_builtin_type(target_identifier, name) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/rgen/xsd/xsd_instantiator.rb', line 72

def add_builtin_type(target_identifier, name)
  @builtin_type_added ||= {}
  return if @builtin_type_added[target_identifier]
  type = @env.new(XMLSchemaMetamodel::TopLevelSimpleType, :name => name)
  @resolver.add_identifier(target_identifier, type)
  @builtin_type_added[target_identifier] = true
end

#add_missing_builtin_typesObject



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rgen/xsd/xsd_instantiator.rb', line 55

def add_missing_builtin_types
  unresolved_refs.each do |ur|
    if ur.proxy.targetIdentifier =~ /^http:\/\/www\.w3\.org\/2001\/XMLSchema:(\w+)$/
      name = $1
      if [ "anyType", "anySimpleType", "string", "normalizedString", "token", "language", "Name", "NCName", 
         "ID", "IDREF", "ENTITY", "NMTOKEN", "base64Binary", "hexBinary", "anyURI", "QName", 
         "NOTATION", "duration", "dateTime", "time", "date", "gYearMonth", "gYear", "gMonthDay", 
         "gDay", "gMonth", "IDREFS", "ENTITIES", "NMTOKENS", "float", "double",
         "decimal", "integer", "nonPositiveInteger", "negativeInteger", "long", "int", "short",
         "byte", "nonNegativeInteger", "unsignedLong", "unsignedInt", "unsignedShort", 
         "unsignedByte", "positiveInteger", "boolean" ].include?(name)
          add_builtin_type(ur.proxy.targetIdentifier, name)
      end
    end
  end
end

#instantiate(file_name) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rgen/xsd/xsd_instantiator.rb', line 17

def instantiate(file_name)
  inst = XSIInstantiator.new(@mm, @env)
  schema = inst.instantiate(file_name, 
    :root_class => XMLSchemaMetamodel::SchemaTYPE
  )
  urefs = inst.unresolved_refs
  urefs.each do |ur|
    ti = ur.proxy.targetIdentifier
    href, name = inst.resolve_namespace(ti)
    if href
      ur.proxy.targetIdentifier = "#{href}:#{name}"
    else
      # either namespace not found (warning in resolve_namespace) or no namespace at all (not even default namespace)
    end
  end
  @unresolved_refs += urefs 
  (schema.element + 
    schema.group + 
    schema.complexType + 
    schema.simpleType + 
    schema.attribute + 
    schema.attributeGroup).each do |e|
      if e.name
        if schema.targetNamespace
          @resolver.add_identifier("#{schema.targetNamespace}:#{e.name}", e)
        else
          @resolver.add_identifier(e.name, e)
        end
      end
  end
end

#resolve(problems = nil) ⇒ Object



49
50
51
52
53
# File 'lib/rgen/xsd/xsd_instantiator.rb', line 49

def resolve(problems=nil)
  @unresolved_refs = @resolver.resolve(unresolved_refs, :problems => [], :use_target_type => true)
  add_missing_builtin_types
  @unresolved_refs = @resolver.resolve(unresolved_refs, :problems => problems, :use_target_type => true)
end