Class: XSD::XML

Inherits:
Object
  • Object
show all
Defined in:
lib/xsd/xml.rb

Constant Summary collapse

DEFAULT_RESOURCE_RESOLVER =
proc do |location, namespace|
  if location =~ /^https?:/
    Net::HTTP.get(URI(location))
  elsif Pathname.new(location).absolute?
    File.read(location)
  else
    raise ImportError, "Failed to locate import '#{location}'#{namespace ? " for namespace '#{namespace}'" : ''}"
  end
end
CLASS_MAP =
{
  'schema'         => Schema,
  'element'        => Element,
  'attribute'      => Attribute,
  'choice'         => Choice,
  'complexType'    => ComplexType,
  'sequence'       => Sequence,
  'simpleContent'  => SimpleContent,
  'complexContent' => ComplexContent,
  'extension'      => Extension,
  'import'         => Import,
  'include'        => Include,
  'simpleType'     => SimpleType,
  'all'            => All,
  'restriction'    => Restriction,
  'group'          => Group,
  'any'            => Any,
  'union'          => Union,
  'attributeGroup' => AttributeGroup,
  'list'           => List,
  'unique'         => Unique,
  'selector'       => Selector,
  'field'          => Field,
  'annotation'     => Annotation,
  'documentation'  => Documentation,
  'appinfo'        => Appinfo,
  'anyAttribute'   => AnyAttribute,
  'key'            => Key,
  'keyref'         => Keyref,
  # Restriction facets
  'minExclusive'   => Facet,
  'minInclusive'   => Facet,
  'maxExclusive'   => Facet,
  'maxInclusive'   => Facet,
  'totalDigits'    => Facet,
  'fractionDigits' => Facet,
  'length'         => Facet,
  'minLength'      => Facet,
  'maxLength'      => Facet,
  'enumeration'    => Facet,
  'whiteSpace'     => Facet,
  'pattern'        => Facet
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ XML

Returns a new instance of XML.



74
75
76
77
78
# File 'lib/xsd/xml.rb', line 74

def initialize(**options)
  @options      = options
  @object_cache = {}
  @schemas      = []
end

Instance Attribute Details

#object_cacheObject (readonly)

Returns the value of attribute object_cache.



8
9
10
# File 'lib/xsd/xml.rb', line 8

def object_cache
  @object_cache
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/xsd/xml.rb', line 8

def options
  @options
end

#schemasObject (readonly)

Returns the value of attribute schemas.



8
9
10
# File 'lib/xsd/xml.rb', line 8

def schemas
  @schemas
end

Class Method Details

.open(path, **options) ⇒ Object

Create reader from a file path

Parameters:

  • path (String)
  • options (Hash)

Returns:

  • XML



68
69
70
71
72
# File 'lib/xsd/xml.rb', line 68

def self.open(path, **options)
  reader = new(**options)
  reader.add_schema_xml(File.read(path))
  reader
end

Instance Method Details

#[](*args) ⇒ Object



128
129
130
131
132
133
134
135
# File 'lib/xsd/xml.rb', line 128

def [](*args)
  schemas.each do |schema|
    item = schema[*args]
    return item if item
  end

  nil
end

#add_schema_node(node) ⇒ Object

Add schema node to reader instance

Returns:

  • Schema

Raises:



107
108
109
110
111
112
113
# File 'lib/xsd/xml.rb', line 107

def add_schema_node(node)
  raise Error, 'Added schema must be of type Nokogiri::XML::Node' unless node.is_a?(Nokogiri::XML::Node)

  new_schema = Schema.new(options.merge(node: node, reader: self))
  schemas.push(new_schema)
  new_schema
end

#add_schema_xml(xml) ⇒ Object

Add schema xml to reader instance

Returns:

  • Schema

Raises:



98
99
100
101
102
103
# File 'lib/xsd/xml.rb', line 98

def add_schema_xml(xml)
  doc = read_document(xml)
  raise Error, 'Schema node not found, xml does not seem to be a valid XSD' unless doc.root&.name == 'schema'

  add_schema_node(doc.root)
end

#attribute_groupsObject



145
146
147
# File 'lib/xsd/xml.rb', line 145

def attribute_groups
  collect(:attribute_groups)
end

#attributes(*args) ⇒ Object



141
142
143
# File 'lib/xsd/xml.rb', line 141

def attributes(*args)
  collect(:attributes, *args)
end

#complex_typesObject



149
150
151
# File 'lib/xsd/xml.rb', line 149

def complex_types
  collect(:complex_types)
end

#elements(*args) ⇒ Object



137
138
139
# File 'lib/xsd/xml.rb', line 137

def elements(*args)
  collect(:elements, *args)
end

#groupsObject



157
158
159
# File 'lib/xsd/xml.rb', line 157

def groups
  collect(:groups)
end

#loggerObject



80
81
82
# File 'lib/xsd/xml.rb', line 80

def logger
  options[:logger] || default_logger
end

#read_document(xml) ⇒ Object



92
93
94
# File 'lib/xsd/xml.rb', line 92

def read_document(xml)
  Nokogiri::XML(xml)
end

#resource_resolverObject



84
85
86
# File 'lib/xsd/xml.rb', line 84

def resource_resolver
  @options[:resource_resolver] || DEFAULT_RESOURCE_RESOLVER
end

#schemaObject

Get first added (considered primary) schema

Returns:

  • Schema, nil



117
118
119
# File 'lib/xsd/xml.rb', line 117

def schema
  schemas.first
end

#schemas_for_namespace(namespace) ⇒ Object

Get schemas by namespace

Parameters:

  • namespace (String, nil)


124
125
126
# File 'lib/xsd/xml.rb', line 124

def schemas_for_namespace(namespace)
  schemas.select { |schema| schema.target_namespace == namespace }
end

#simple_typesObject



153
154
155
# File 'lib/xsd/xml.rb', line 153

def simple_types
  collect(:simple_types)
end

#tmp_dirObject



88
89
90
# File 'lib/xsd/xml.rb', line 88

def tmp_dir
  @tmp_dir ||= options[:tmp_dir]
end