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.
- #node ⇒ Object
- #node_to_object(node) ⇒ Object
- #nodes ⇒ Object
- #object_by_name(xml_name, name) ⇒ Object
- #ordered_elements ⇒ Object
-
#parent ⇒ Object
Related objects.
- #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 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
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 |
#attributes ⇒ Object
145 146 147 |
# File 'lib/xsd_reader/shared.rb', line 145 def attributes @attributes ||= map_children('xs:attribute') end |
#base ⇒ Object
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_name ⇒ Object
72 73 74 |
# File 'lib/xsd_reader/shared.rb', line 72 def base_name base ? base.split(':').last : nil end |
#base_namespace ⇒ Object
76 77 78 |
# File 'lib/xsd_reader/shared.rb', line 76 def base_namespace base ? base.split(':').first : nil end |
#child_elements? ⇒ Boolean
141 142 143 |
# File 'lib/xsd_reader/shared.rb', line 141 def child_elements? elements.length > 0 end |
#choices ⇒ Object
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_content ⇒ Object
182 183 184 |
# File 'lib/xsd_reader/shared.rb', line 182 def complex_content complex_contents.first end |
#complex_contents ⇒ Object
178 179 180 |
# File 'lib/xsd_reader/shared.rb', line 178 def complex_contents @complex_contents ||= map_children("xs:complexContent") end |
#complex_type ⇒ Object
161 162 163 |
# File 'lib/xsd_reader/shared.rb', line 161 def complex_type complex_types.first || linked_complex_type end |
#complex_types ⇒ Object
157 158 159 |
# File 'lib/xsd_reader/shared.rb', line 157 def complex_types @complex_types ||= map_children("xs:complexType") end |
#direct_elements ⇒ Object
121 122 123 |
# File 'lib/xsd_reader/shared.rb', line 121 def direct_elements @direct_elements ||= map_children("xs:element") end |
#elements ⇒ Object
125 126 127 |
# File 'lib/xsd_reader/shared.rb', line 125 def elements direct_elements end |
#extension ⇒ Object
190 191 192 |
# File 'lib/xsd_reader/shared.rb', line 190 def extension extensions.first end |
#extensions ⇒ Object
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 = {}) = _opts || {} raise "#{self.class.to_s}.new expects a hash parameter" if !.is_a?(Hash) end |
#linked_complex_type ⇒ Object
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_type ⇒ Object
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 |
#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
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 |
#name ⇒ Object
attribute properties
51 52 53 |
# File 'lib/xsd_reader/shared.rb', line 51 def name node.attributes['name'].value end |
#node ⇒ Object
21 22 23 |
# File 'lib/xsd_reader/shared.rb', line 21 def node [: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(.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
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_elements ⇒ Object
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 |
#parent ⇒ Object
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 |
#schema ⇒ Object
216 217 218 219 220 |
# File 'lib/xsd_reader/shared.rb', line 216 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
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 |
#sequences ⇒ Object
149 150 151 |
# File 'lib/xsd_reader/shared.rb', line 149 def sequences @sequences ||= map_children("xs:sequence",) end |
#simple_content ⇒ Object
174 175 176 |
# File 'lib/xsd_reader/shared.rb', line 174 def simple_content simple_contents.first end |
#simple_contents ⇒ Object
170 171 172 |
# File 'lib/xsd_reader/shared.rb', line 170 def simple_contents @simple_contents ||= map_children("xs:simpleContent") end |
#simple_types ⇒ Object
194 195 196 |
# File 'lib/xsd_reader/shared.rb', line 194 def simple_types @simple_types ||= map_children("xs:simpleType") end |
#type ⇒ Object
55 56 57 |
# File 'lib/xsd_reader/shared.rb', line 55 def type node.attributes['type'] ? node.attributes['type'].value : nil end |
#type_name ⇒ Object
59 60 61 |
# File 'lib/xsd_reader/shared.rb', line 59 def type_name type ? type.split(':').last : nil end |
#type_namespace ⇒ Object
63 64 65 |
# File 'lib/xsd_reader/shared.rb', line 63 def type_namespace type ? type.split(':').first : nil end |