Class: Shale::Schema::XMLCompiler
- Inherits:
-
Object
- Object
- Shale::Schema::XMLCompiler
- Defined in:
- lib/shale/schema/xml_compiler.rb
Overview
Class for compiling XML schema into Ruby data model
Constant Summary collapse
- XML_NAMESPACE_URI =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML namespace URI
'http://www.w3.org/XML/1998/namespace'- XML_NAMESPACE_PREFIX =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML namespace prefix
'xml'- XS_NAMESPACE_URI =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema namespace URI
'http://www.w3.org/2001/XMLSchema'- XS_SCHEMA =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “schema” element name
"#{XS_NAMESPACE_URI}:schema".freeze
- XS_ELEMENT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “element” element name
"#{XS_NAMESPACE_URI}:element".freeze
- XS_ATTRIBUTE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “attribute” element name
"#{XS_NAMESPACE_URI}:attribute".freeze
- XS_SIMPLE_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “attribute” element name
"#{XS_NAMESPACE_URI}:simpleType".freeze
- XS_SIMPLE_CONTENT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “simpleContent” element name
"#{XS_NAMESPACE_URI}:simpleContent".freeze
- XS_RESTRICTION =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “restriction” element name
"#{XS_NAMESPACE_URI}:restriction".freeze
- XS_GROUP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “group” element name
"#{XS_NAMESPACE_URI}:group".freeze
- XS_ATTRIBUTE_GROUP =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “attributeGroup” element name
"#{XS_NAMESPACE_URI}:attributeGroup".freeze
- XS_COMPLEX_TYPE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “complexType” element name
"#{XS_NAMESPACE_URI}:complexType".freeze
- XS_COMPLEX_CONTENT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “complexContent” element name
"#{XS_NAMESPACE_URI}:complexContent".freeze
- XS_EXTENSION =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “extension” element name
"#{XS_NAMESPACE_URI}:extension".freeze
- XS_TYPE_ANY =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “anyType” type
"#{XS_NAMESPACE_URI}:anyType".freeze
- XS_TYPE_DATE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “date” types
[ "#{XS_NAMESPACE_URI}:date", ].freeze
- XS_TYPE_TIME =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “datetime” types
[ "#{XS_NAMESPACE_URI}:dateTime", ].freeze
- XS_TYPE_STRING =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “string” types
[ "#{XS_NAMESPACE_URI}:string", "#{XS_NAMESPACE_URI}:normalizedString", "#{XS_NAMESPACE_URI}:token", ].freeze
- XS_TYPE_FLOAT =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “float” types
[ "#{XS_NAMESPACE_URI}:float", "#{XS_NAMESPACE_URI}:double", ].freeze
- XS_TYPE_DECIMAL =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “decimal” types
[ "#{XS_NAMESPACE_URI}:decimal", ].freeze
- XS_TYPE_INTEGER =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “integer” types
[ "#{XS_NAMESPACE_URI}:integer", "#{XS_NAMESPACE_URI}:byte", "#{XS_NAMESPACE_URI}:int", "#{XS_NAMESPACE_URI}:long", "#{XS_NAMESPACE_URI}:negativeInteger", "#{XS_NAMESPACE_URI}:nonNegativeInteger", "#{XS_NAMESPACE_URI}:nonPositiveInteger", "#{XS_NAMESPACE_URI}:positiveInteger", "#{XS_NAMESPACE_URI}:short", "#{XS_NAMESPACE_URI}:unsignedLong", "#{XS_NAMESPACE_URI}:unsignedInt", "#{XS_NAMESPACE_URI}:unsignedShort", "#{XS_NAMESPACE_URI}:unsignedByte", ].freeze
- XS_TYPE_BOOLEAN =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
XML Schema “boolean” types
[ "#{XS_NAMESPACE_URI}:boolean", ].freeze
- MODEL_TEMPLATE =
This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.
Shale model template
ERB.new("require 'shale'\n<%- unless type.references.empty? -%>\n\n<%- type.references.each do |property| -%>\nrequire_relative '<%= type.relative_path(property.type.file_name) %>'\n<%- end -%>\n<%- end -%>\n\n<%- type.modules.each_with_index do |name, i| -%>\n<%= ' ' * i %>module <%= name %>\n<%- end -%>\n<%- indent = ' ' * type.modules.length -%>\n<%= indent %>class <%= type.root_name %> < Shale::Mapper\n <%- type.properties.select(&:content?).each do |property| -%>\n <%= indent %>attribute :<%= property.attribute_name %>, <%= property.type.name -%>\n <%- if property.collection? %>, collection: true<% end -%>\n <%- unless property.default.nil? %>, default: -> { <%= property.default %> }<% end %>\n <%- end -%>\n <%- type.properties.select(&:attribute?).each do |property| -%>\n <%= indent %>attribute :<%= property.attribute_name %>, <%= property.type.name -%>\n <%- if property.collection? %>, collection: true<% end -%>\n <%- unless property.default.nil? %>, default: -> { <%= property.default %> }<% end %>\n <%- end -%>\n <%- type.properties.select(&:element?).each do |property| -%>\n <%= indent %>attribute :<%= property.attribute_name %>, <%= property.type.name -%>\n <%- if property.collection? %>, collection: true<% end -%>\n <%- unless property.default.nil? %>, default: -> { <%= property.default %> }<% end %>\n <%- end -%>\n\n <%= indent %>xml do\n <%= indent %>root '<%= type.root %>'\n <%- if type.namespace -%>\n <%= indent %>namespace '<%= type.namespace %>', '<%= type.prefix %>'\n <%- end -%>\n <%- unless type.properties.empty? -%>\n\n <%- type.properties.select(&:content?).each do |property| -%>\n <%= indent %>map_content to: :<%= property.attribute_name %>\n <%- end -%>\n <%- type.properties.select(&:attribute?).each do |property| -%>\n <%= indent %>map_attribute '<%= property.mapping_name %>', to: :<%= property.attribute_name -%>\n <%- if property.namespace %>, prefix: '<%= property.prefix %>'<%- end -%>\n <%- if property.namespace %>, namespace: '<%= property.namespace %>'<% end %>\n <%- end -%>\n <%- type.properties.select(&:element?).each do |property| -%>\n <%= indent %>map_element '<%= property.mapping_name %>', to: :<%= property.attribute_name -%>\n <%- if type.namespace != property.namespace %>, prefix: <%= property.prefix ? \"'\\\#{property.prefix}'\" : 'nil' %><%- end -%>\n <%- if type.namespace != property.namespace %>, namespace: <%= property.namespace ? \"'\\\#{property.namespace}'\" : 'nil' %><% end %>\n <%- end -%>\n <%- end -%>\n <%= indent %>end\n<%= indent %>end\n<%- type.modules.length.times do |i| -%>\n<%= ' ' * (type.modules.length - i - 1) %>end\n<%- end -%>\n", trim_mode: '-')
Instance Method Summary collapse
-
#as_models(schemas, namespace_mapping: nil) ⇒ Array<Shale::Schema::Compiler::XMLComplex>
Generate Shale models from XML Schema and return them as a Ruby Array of objects.
-
#to_models(schemas, namespace_mapping: nil) ⇒ Hash<String, String>
Generate Shale models from XML Schema.
Instance Method Details
#as_models(schemas, namespace_mapping: nil) ⇒ Array<Shale::Schema::Compiler::XMLComplex>
Generate Shale models from XML Schema and return them as a Ruby Array of objects
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/shale/schema/xml_compiler.rb', line 215 def as_models(schemas, namespace_mapping: nil) unless Shale.xml_adapter raise AdapterError, XML_ADAPTER_NOT_SET_MESSAGE end if Shale.xml_adapter.name == 'Shale::Adapter::Ox' msg = "Ox doesn't support XML namespaces and can't be used to compile XML Schema" raise AdapterError, msg end schemas = schemas.map do |schema| Shale.xml_adapter.load(schema) end @namespace_mapping = namespace_mapping || {} @elements_repository = {} @attributes_repository = {} @simple_types_repository = {} @element_groups_repository = {} @attribute_groups_repository = {} @complex_types_repository = {} @complex_types = {} @types = [] schemas.each do |schema| build_repositories(schema) end resolve_nested_refs(@simple_types_repository) schemas.each do |schema| compile(schema) end @types = @types.uniq total_duplicates = Hash.new(0) duplicates = Hash.new(0) @types.each do |type| total_duplicates[type.name] += 1 end @types.each do |type| duplicates[type.name] += 1 if total_duplicates[type.name] > 1 type.root_name = format("#{type.root_name}%d", duplicates[type.name]) end end @types.reverse rescue ParseError => e raise SchemaError, "XML Schema document is invalid: #{e.message}" end |
#to_models(schemas, namespace_mapping: nil) ⇒ Hash<String, String>
Generate Shale models from XML Schema
284 285 286 287 288 289 290 |
# File 'lib/shale/schema/xml_compiler.rb', line 284 def to_models(schemas, namespace_mapping: nil) types = as_models(schemas, namespace_mapping: namespace_mapping) types.to_h do |type| [type.file_name, MODEL_TEMPLATE.result(binding)] end end |