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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #all_elements ⇒ Object
- #attributes ⇒ Object
-
#base ⇒ Object
base stuff belongs to extension type objects only, but let’s be flexible.
- #base_name ⇒ Object
- #base_namespace ⇒ Object
- #child_elements? ⇒ Boolean
- #choices ⇒ Object
-
#class_for(n) ⇒ Object
Node to class mapping.
- #complex_content ⇒ Object
- #complex_contents ⇒ Object
- #complex_type ⇒ Object
- #complex_types ⇒ Object
- #direct_elements ⇒ Object
- #elements ⇒ Object
- #extension ⇒ Object
- #extensions ⇒ Object
- #initialize(_opts = {}) ⇒ Object
- #linked_complex_type ⇒ Object
- #linked_simple_type ⇒ Object
- #logger ⇒ Object
- #map_children(xml_name) ⇒ Object
-
#mappable_children(xml_name) ⇒ Object
Child objects.
-
#name ⇒ Object
attribute properties.
- #name_local ⇒ Object
- #name_referenced ⇒ Object
- #node ⇒ Object
- #node_to_object(node) ⇒ Object
- #nodes ⇒ Object
- #object_by_name(xml_name, name) ⇒ Object
- #ordered_elements ⇒ Object
-
#parent ⇒ Object
Related objects.
- #ref ⇒ Object
- #referenced_element ⇒ Object
- #schema ⇒ Object
- #schema_for_namespace(_namespace) ⇒ Object
- #sequences ⇒ Object
- #simple_content ⇒ Object
- #simple_contents ⇒ Object
- #simple_types ⇒ Object
- #type ⇒ Object
- #type_name ⇒ Object
- #type_namespace ⇒ Object
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
7 8 9 |
# File 'lib/xsd_reader/shared.rb', line 7 def @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_elements ⇒ Object
157 158 159 160 161 |
# File 'lib/xsd_reader/shared.rb', line 157 def all_elements @all_elements ||= ordered_elements + (linked_complex_type ? linked_complex_type.all_elements : []) + (referenced_element ? referenced_element.all_elements : []) end |
#attributes ⇒ Object
167 168 169 170 |
# File 'lib/xsd_reader/shared.rb', line 167 def attributes @attributes ||= map_children('xs:attribute') #+ #(referenced_element ? referenced_element.attributes : []) end |
#base ⇒ Object
base stuff belongs to extension type objects only, but let’s be flexible
87 88 89 |
# File 'lib/xsd_reader/shared.rb', line 87 def base node.attributes['base'] ? node.attributes['base'].value : nil end |
#base_name ⇒ Object
91 92 93 |
# File 'lib/xsd_reader/shared.rb', line 91 def base_name base ? base.split(':').last : nil end |
#base_namespace ⇒ Object
95 96 97 |
# File 'lib/xsd_reader/shared.rb', line 95 def base_namespace base ? base.split(':').first : nil end |
#child_elements? ⇒ Boolean
163 164 165 |
# File 'lib/xsd_reader/shared.rb', line 163 def child_elements? elements.length > 0 end |
#choices ⇒ Object
176 177 178 |
# File 'lib/xsd_reader/shared.rb', line 176 def choices @choices ||= map_children("xs:choice") end |
#class_for(n) ⇒ Object
Node to class mapping
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/xsd_reader/shared.rb', line 103 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_content ⇒ Object
205 206 207 |
# File 'lib/xsd_reader/shared.rb', line 205 def complex_content complex_contents.first end |
#complex_contents ⇒ Object
201 202 203 |
# File 'lib/xsd_reader/shared.rb', line 201 def complex_contents @complex_contents ||= map_children("xs:complexContent") end |
#complex_type ⇒ Object
184 185 186 |
# File 'lib/xsd_reader/shared.rb', line 184 def complex_type complex_types.first || linked_complex_type || (referenced_element ? referenced_element.complex_type : nil) end |
#complex_types ⇒ Object
180 181 182 |
# File 'lib/xsd_reader/shared.rb', line 180 def complex_types @complex_types ||= map_children("xs:complexType") end |
#direct_elements ⇒ Object
141 142 143 |
# File 'lib/xsd_reader/shared.rb', line 141 def direct_elements @direct_elements ||= map_children("xs:element") end |
#elements ⇒ Object
145 146 147 |
# File 'lib/xsd_reader/shared.rb', line 145 def elements direct_elements end |
#extension ⇒ Object
213 214 215 |
# File 'lib/xsd_reader/shared.rb', line 213 def extension extensions.first end |
#extensions ⇒ Object
209 210 211 |
# File 'lib/xsd_reader/shared.rb', line 209 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_type ⇒ Object
188 189 190 191 |
# File 'lib/xsd_reader/shared.rb', line 188 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_type ⇒ Object
221 222 223 224 |
# File 'lib/xsd_reader/shared.rb', line 221 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 |
#logger ⇒ Object
14 15 16 17 18 19 |
# File 'lib/xsd_reader/shared.rb', line 14 def logger return @logger if @logger @logger ||= [:logger] || Logger.new(STDOUT) @logger.level = Logger::WARN return @logger end |
#map_children(xml_name) ⇒ Object
136 137 138 139 |
# File 'lib/xsd_reader/shared.rb', line 136 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
132 133 134 |
# File 'lib/xsd_reader/shared.rb', line 132 def mappable_children(xml_name) node.search("./#{xml_name}").to_a end |
#name ⇒ Object
attribute properties
51 52 53 |
# File 'lib/xsd_reader/shared.rb', line 51 def name name_local || name_referenced end |
#name_local ⇒ Object
55 56 57 |
# File 'lib/xsd_reader/shared.rb', line 55 def name_local node.attributes['name'] ? node.attributes['name'].value : nil end |
#name_referenced ⇒ Object
59 60 61 |
# File 'lib/xsd_reader/shared.rb', line 59 def name_referenced referenced_element ? referenced_element.name : nil end |
#node ⇒ Object
21 22 23 |
# File 'lib/xsd_reader/shared.rb', line 21 def node [:node] end |
#node_to_object(node) ⇒ Object
121 122 123 124 125 126 |
# File 'lib/xsd_reader/shared.rb', line 121 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(.merge(:node => node, :schema => schema)) end |
#nodes ⇒ Object
25 26 27 |
# File 'lib/xsd_reader/shared.rb', line 25 def nodes node.search("./*") end |
#object_by_name(xml_name, name) ⇒ Object
245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/xsd_reader/shared.rb', line 245 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_elements ⇒ Object
149 150 151 152 153 154 155 |
# File 'lib/xsd_reader/shared.rb', line 149 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 |
#parent ⇒ Object
Related objects
230 231 232 233 234 235 236 237 |
# File 'lib/xsd_reader/shared.rb', line 230 def parent if node && node.respond_to?(:parent) && node.parent return node_to_object(node.parent) end nil end |
#ref ⇒ Object
63 64 65 |
# File 'lib/xsd_reader/shared.rb', line 63 def ref node.attributes['ref'] ? node.attributes['ref'].value : nil end |
#referenced_element ⇒ Object
67 68 69 |
# File 'lib/xsd_reader/shared.rb', line 67 def referenced_element ref && schema ? schema.elements.find{|el| el.name == ref} : nil end |
#schema ⇒ Object
239 240 241 242 243 |
# File 'lib/xsd_reader/shared.rb', line 239 def schema return [:schema] if [:schema] schema_node = node.search('//xs:schema')[0] return schema_node.nil? ? nil : node_to_object(schema_node) end |
#schema_for_namespace(_namespace) ⇒ Object
260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/xsd_reader/shared.rb', line 260 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 |
#sequences ⇒ Object
172 173 174 |
# File 'lib/xsd_reader/shared.rb', line 172 def sequences @sequences ||= map_children("xs:sequence",) end |
#simple_content ⇒ Object
197 198 199 |
# File 'lib/xsd_reader/shared.rb', line 197 def simple_content simple_contents.first end |
#simple_contents ⇒ Object
193 194 195 |
# File 'lib/xsd_reader/shared.rb', line 193 def simple_contents @simple_contents ||= map_children("xs:simpleContent") end |
#simple_types ⇒ Object
217 218 219 |
# File 'lib/xsd_reader/shared.rb', line 217 def simple_types @simple_types ||= map_children("xs:simpleType") end |
#type ⇒ Object
72 73 74 75 |
# File 'lib/xsd_reader/shared.rb', line 72 def type type = node.attributes['type'] ? node.attributes['type'].value : nil type || (referenced_element ? referenced_element.type : nil) end |
#type_name ⇒ Object
77 78 79 |
# File 'lib/xsd_reader/shared.rb', line 77 def type_name type ? type.split(':').last : nil end |
#type_namespace ⇒ Object
81 82 83 |
# File 'lib/xsd_reader/shared.rb', line 81 def type_namespace type ? type.split(':').first : nil end |