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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/xsd_reader/shared.rb', line 33

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



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

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

#attributesObject



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

def attributes
  @attributes ||= map_children("attribute") #+
    #(referenced_element ? referenced_element.attributes : [])
end

#baseObject

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



91
92
93
# File 'lib/xsd_reader/shared.rb', line 91

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

#base_nameObject



95
96
97
# File 'lib/xsd_reader/shared.rb', line 95

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

#base_namespaceObject



99
100
101
# File 'lib/xsd_reader/shared.rb', line 99

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

#child_elements?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/xsd_reader/shared.rb', line 172

def child_elements?
  elements.length > 0
end

#choicesObject



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

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

#class_for(n) ⇒ Object

Node to class mapping



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/xsd_reader/shared.rb', line 107

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

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

#complex_contentObject



214
215
216
# File 'lib/xsd_reader/shared.rb', line 214

def complex_content
  complex_contents.first
end

#complex_contentsObject



210
211
212
# File 'lib/xsd_reader/shared.rb', line 210

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

#complex_typeObject



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

def complex_type
  complex_types.first || linked_complex_type || (referenced_element ? referenced_element.complex_type : nil)
end

#complex_typesObject



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

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

#direct_elementsObject



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

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

#elementsObject



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

def elements
  direct_elements
end

#extensionObject



222
223
224
# File 'lib/xsd_reader/shared.rb', line 222

def extension
  extensions.first
end

#extensionsObject



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

def extensions
  @extensions ||= map_children("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



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

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('#{schema_namespace_prefix}complexType', type) || object_by_name('#{schema_namespace_prefix}complexType', type_name)
end

#linked_simple_typeObject



230
231
232
233
# File 'lib/xsd_reader/shared.rb', line 230

def linked_simple_type
  @linked_simple_type ||= object_by_name("#{schema_namespace_prefix}simpleType", type) || object_by_name("#{schema_namespace_prefix}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



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

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



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

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

#nameObject

attribute properties



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

def name
  name_local || name_referenced
end

#name_localObject



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

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

#name_referencedObject



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

def name_referenced
  referenced_element ? referenced_element.name : nil
end

#nodeObject



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

def node
  options[:node]
end

#node_to_object(node) ⇒ Object



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

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



29
30
31
# File 'lib/xsd_reader/shared.rb', line 29

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

#object_by_name(xml_name, name) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/xsd_reader/shared.rb', line 254

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



158
159
160
161
162
163
164
# File 'lib/xsd_reader/shared.rb', line 158

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



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

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

    return node_to_object(node.parent)
  end

  nil
end

#prepend_namespace(name) ⇒ Object

Child objects



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

def prepend_namespace name
  name =~ /^#{schema_namespace_prefix}/ ? name : "#{schema_namespace_prefix}#{name}"
end

#refObject



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

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

#referenced_elementObject



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

def referenced_element
  ref && schema ? schema.elements.find{|el| el.name == ref} : nil
end

#schemaObject



248
249
250
251
252
# File 'lib/xsd_reader/shared.rb', line 248

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



269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/xsd_reader/shared.rb', line 269

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

#schema_namespace_prefixObject



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

def schema_namespace_prefix
  @schema_namespace_prefix ||= [(self.node.namespaces||{}).detect {|ns| ns[1]=~/XMLSchema/}.first.split(':')[1],nil].uniq.join(':')
end

#sequencesObject



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

def sequences
  @sequences ||= map_children("sequence")
end

#simple_contentObject



206
207
208
# File 'lib/xsd_reader/shared.rb', line 206

def simple_content
  simple_contents.first
end

#simple_contentsObject



202
203
204
# File 'lib/xsd_reader/shared.rb', line 202

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

#simple_typesObject



226
227
228
# File 'lib/xsd_reader/shared.rb', line 226

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

#typeObject



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

def type
  type = node.attributes['type'] ? node.attributes['type'].value : nil
  type || (referenced_element ? referenced_element.type : nil)
end

#type_nameObject



81
82
83
# File 'lib/xsd_reader/shared.rb', line 81

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

#type_namespaceObject



85
86
87
# File 'lib/xsd_reader/shared.rb', line 85

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