Module: XsdReader::Shared

Included in:
Attribute, Choice, ComplexContent, ComplexType, Element, Extension, Import, Schema, Sequence, SimpleContent, SimpleType
Defined in:
lib/xsd_reader/shared.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/xsd_reader/shared.rb', line 7

def options
  @options
end

Instance Method Details

#[](*args) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/xsd_reader/shared.rb', line 29

def [](*args)
  # now name is always an array
  names = args.flatten

  result = self

  names.each do |curname|
    next if result.nil?
    if curname.to_s =~ /^\@/ 
      attr_name = curname.to_s.gsub(/^\@/, '')
      result = result.attributes.find{|attr| attr.name == attr_name}
    else
      result = result.elements.find{|child| child.name == curname.to_s}
    end
  end

  return result
end

#all_elementsObject



137
138
139
# File 'lib/xsd_reader/shared.rb', line 137

def all_elements
  @all_elements ||= ordered_elements + (linked_complex_type ? linked_complex_type.ordered_elements : [])
end

#attributesObject



145
146
147
# File 'lib/xsd_reader/shared.rb', line 145

def attributes
  @attributes ||= map_children('xs:attribute')
end

#baseObject

base stuff belongs to extension type objects only, but let’s be flexible



68
69
70
# File 'lib/xsd_reader/shared.rb', line 68

def base
  node.attributes['base'] ? node.attributes['base'].value : nil
end

#base_nameObject



72
73
74
# File 'lib/xsd_reader/shared.rb', line 72

def base_name
  base ? base.split(':').last : nil
end

#base_namespaceObject



76
77
78
# File 'lib/xsd_reader/shared.rb', line 76

def base_namespace
  base ? base.split(':').first : nil
end

#child_elements?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/xsd_reader/shared.rb', line 141

def child_elements?
  elements.length > 0
end

#choicesObject



153
154
155
# File 'lib/xsd_reader/shared.rb', line 153

def choices
  @choices ||= map_children("xs:choice")
end

#class_for(n) ⇒ Object

Node to class mapping



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/xsd_reader/shared.rb', line 83

def class_for(n)
  class_mapping = {
    'xs:schema' => Schema,
    'xs:element' => Element,
    'xs:attribute' => Attribute,
    'xs:choice' => Choice,
    'xs:complexType' => ComplexType,
    'xs:sequence' => Sequence,
    'xs:simpleContent' => SimpleContent,
    'xs:complexContent' => ComplexContent,
    'xs:extension' => Extension,
    'xs:import' => Import,
    'xs:simpleType' => SimpleType
  }

  return class_mapping[n.is_a?(Nokogiri::XML::Node) ? n.name : n]
end

#complex_contentObject



182
183
184
# File 'lib/xsd_reader/shared.rb', line 182

def complex_content
  complex_contents.first
end

#complex_contentsObject



178
179
180
# File 'lib/xsd_reader/shared.rb', line 178

def complex_contents
  @complex_contents ||= map_children("xs:complexContent")
end

#complex_typeObject



161
162
163
# File 'lib/xsd_reader/shared.rb', line 161

def complex_type
  complex_types.first || linked_complex_type
end

#complex_typesObject



157
158
159
# File 'lib/xsd_reader/shared.rb', line 157

def complex_types
  @complex_types ||= map_children("xs:complexType")
end

#direct_elementsObject



121
122
123
# File 'lib/xsd_reader/shared.rb', line 121

def direct_elements
  @direct_elements ||= map_children("xs:element")
end

#elementsObject



125
126
127
# File 'lib/xsd_reader/shared.rb', line 125

def elements
  direct_elements
end

#extensionObject



190
191
192
# File 'lib/xsd_reader/shared.rb', line 190

def extension
  extensions.first
end

#extensionsObject



186
187
188
# File 'lib/xsd_reader/shared.rb', line 186

def extensions
  @extensions ||= map_children("xs:extension")
end

#initialize(_opts = {}) ⇒ Object



9
10
11
12
# File 'lib/xsd_reader/shared.rb', line 9

def initialize(_opts = {})
  @options = _opts || {}
  raise "#{self.class.to_s}.new expects a hash parameter" if !@options.is_a?(Hash)
end

#linked_complex_typeObject



165
166
167
168
# File 'lib/xsd_reader/shared.rb', line 165

def linked_complex_type
  @linked_complex_type ||= (schema_for_namespace(type_namespace) || schema).complex_types.find{|ct| ct.name == (type_name || type)}
  #@linked_complex_type ||= object_by_name('xs:complexType', type) || object_by_name('xs:complexType', type_name) 
end

#linked_simple_typeObject



198
199
200
201
# File 'lib/xsd_reader/shared.rb', line 198

def linked_simple_type
  @linked_simple_type ||= object_by_name('xs:simpleType', type) || object_by_name('xs:simpleType', type_name)
  # @linked_simple_type ||= (type_namespace ? schema_for_namespace(type_namespace) : schema).simple_types.find{|st| st.name == (type_name || type)}
end

#loggerObject



14
15
16
17
18
19
# File 'lib/xsd_reader/shared.rb', line 14

def logger
  return @logger if @logger
  @logger ||= options[:logger] || Logger.new(STDOUT)
  @logger.level = Logger::WARN
  return @logger
end

#map_children(xml_name) ⇒ Object



116
117
118
119
# File 'lib/xsd_reader/shared.rb', line 116

def map_children(xml_name)
  # puts "Map Children with #{xml_name} for #{self.class.to_s}"
  mappable_children(xml_name).map{|current_node| node_to_object(current_node)}
end

#mappable_children(xml_name) ⇒ Object

Child objects



112
113
114
# File 'lib/xsd_reader/shared.rb', line 112

def mappable_children(xml_name)
  node.search("./#{xml_name}").to_a
end

#nameObject

attribute properties



51
52
53
# File 'lib/xsd_reader/shared.rb', line 51

def name
  node.attributes['name'].value
end

#nodeObject



21
22
23
# File 'lib/xsd_reader/shared.rb', line 21

def node
  options[:node]
end

#node_to_object(node) ⇒ Object



101
102
103
104
105
106
# File 'lib/xsd_reader/shared.rb', line 101

def node_to_object(node)
  fullname = [node.namespace ? node.namespace.prefix : nil, node.name].reject{|str| str.nil? || str == ''}.join(':')
  klass = class_for(fullname)
  # logger.debug "node_to_object, klass: #{klass.to_s}, fullname: #{fullname}"
  klass.nil? ? nil : klass.new(options.merge(:node => node, :schema => schema))
end

#nodesObject



25
26
27
# File 'lib/xsd_reader/shared.rb', line 25

def nodes
  node.search("./*")
end

#object_by_name(xml_name, name) ⇒ Object



222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/xsd_reader/shared.rb', line 222

def object_by_name(xml_name, name)
  # find in local schema, then look in imported schemas
  nod = node.search("//#{xml_name}[@name=\"#{name}\"]").first
  return node_to_object(nod) if nod

  # try to find in any of the importers
  self.schema.imports.each do |import|
    if obj = import.reader.schema.object_by_name(xml_name, name)
      return obj
    end
  end

  return nil
end

#ordered_elementsObject



129
130
131
132
133
134
135
# File 'lib/xsd_reader/shared.rb', line 129

def ordered_elements
  # loop over each interpretable child xml node, and if we can convert a child node
  # to an XsdReader object, let it give its compilation of all_elements
  nodes.map{|node| node_to_object(node)}.compact.map do |obj|
    obj.is_a?(Element) ? obj : obj.ordered_elements
  end.flatten
end

#parentObject

Related objects



207
208
209
210
211
212
213
214
# File 'lib/xsd_reader/shared.rb', line 207

def parent
  if node && node.respond_to?(:parent) && node.parent

    return node_to_object(node.parent)
  end

  nil
end

#schemaObject



216
217
218
219
220
# File 'lib/xsd_reader/shared.rb', line 216

def schema
  return options[:schema] if options[:schema]
  schema_node = node.search('//xs:schema')[0]
  return schema_node.nil? ? nil : node_to_object(schema_node)
end

#schema_for_namespace(_namespace) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/xsd_reader/shared.rb', line 237

def schema_for_namespace(_namespace)
  logger.debug "Shared#schema_for_namespace with _namespace: #{_namespace}"
  return schema if schema.targets_namespace?(_namespace)

  if import = schema.import_by_namespace(_namespace)
    logger.debug "Shared#schema_for_namespace found import schema"
    return import.reader.schema
  end

  logger.debug "Shared#schema_for_namespace no result"
  return nil
end

#sequencesObject



149
150
151
# File 'lib/xsd_reader/shared.rb', line 149

def sequences
  @sequences ||= map_children("xs:sequence",)
end

#simple_contentObject



174
175
176
# File 'lib/xsd_reader/shared.rb', line 174

def simple_content
  simple_contents.first
end

#simple_contentsObject



170
171
172
# File 'lib/xsd_reader/shared.rb', line 170

def simple_contents
  @simple_contents ||= map_children("xs:simpleContent")
end

#simple_typesObject



194
195
196
# File 'lib/xsd_reader/shared.rb', line 194

def simple_types
  @simple_types ||= map_children("xs:simpleType")
end

#typeObject



55
56
57
# File 'lib/xsd_reader/shared.rb', line 55

def type
  node.attributes['type'] ? node.attributes['type'].value : nil
end

#type_nameObject



59
60
61
# File 'lib/xsd_reader/shared.rb', line 59

def type_name
  type ? type.split(':').last : nil
end

#type_namespaceObject



63
64
65
# File 'lib/xsd_reader/shared.rb', line 63

def type_namespace
  type ? type.split(':').first : nil
end