Class: XSD::XML
- Inherits:
-
Object
- Object
- XSD::XML
- 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
-
#object_cache ⇒ Object
readonly
Returns the value of attribute object_cache.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#schemas ⇒ Object
readonly
Returns the value of attribute schemas.
Class Method Summary collapse
-
.open(path, **options) ⇒ Object
Create reader from a file path.
Instance Method Summary collapse
- #[](*args) ⇒ Object
-
#add_schema_node(node) ⇒ Object
Add schema node to reader instance.
-
#add_schema_xml(xml) ⇒ Object
Add schema xml to reader instance.
- #attribute_groups ⇒ Object
- #attributes(*args) ⇒ Object
- #complex_types ⇒ Object
- #elements(*args) ⇒ Object
- #groups ⇒ Object
-
#initialize(**options) ⇒ XML
constructor
A new instance of XML.
- #logger ⇒ Object
- #read_document(xml) ⇒ Object
- #resource_resolver ⇒ Object
-
#schema ⇒ Object
Get first added (considered primary) schema.
-
#schemas_for_namespace(namespace) ⇒ Object
Get schemas by namespace.
- #simple_types ⇒ Object
- #tmp_dir ⇒ Object
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 = @object_cache = {} @schemas = [] end |
Instance Attribute Details
#object_cache ⇒ Object (readonly)
Returns the value of attribute object_cache.
8 9 10 |
# File 'lib/xsd/xml.rb', line 8 def object_cache @object_cache end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/xsd/xml.rb', line 8 def @options end |
#schemas ⇒ Object (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
68 69 70 71 72 |
# File 'lib/xsd/xml.rb', line 68 def self.open(path, **) reader = new(**) 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
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(.merge(node: node, reader: self)) schemas.push(new_schema) new_schema end |
#add_schema_xml(xml) ⇒ Object
Add schema xml to reader instance
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_groups ⇒ Object
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_types ⇒ Object
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 |
#groups ⇒ Object
157 158 159 |
# File 'lib/xsd/xml.rb', line 157 def groups collect(:groups) end |
#logger ⇒ Object
80 81 82 |
# File 'lib/xsd/xml.rb', line 80 def logger [: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_resolver ⇒ Object
84 85 86 |
# File 'lib/xsd/xml.rb', line 84 def resource_resolver @options[:resource_resolver] || DEFAULT_RESOURCE_RESOLVER end |
#schema ⇒ Object
Get first added (considered primary) schema
117 118 119 |
# File 'lib/xsd/xml.rb', line 117 def schema schemas.first end |
#schemas_for_namespace(namespace) ⇒ Object
Get schemas by namespace
124 125 126 |
# File 'lib/xsd/xml.rb', line 124 def schemas_for_namespace(namespace) schemas.select { |schema| schema.target_namespace == namespace } end |
#simple_types ⇒ Object
153 154 155 |
# File 'lib/xsd/xml.rb', line 153 def simple_types collect(:simple_types) end |
#tmp_dir ⇒ Object
88 89 90 |
# File 'lib/xsd/xml.rb', line 88 def tmp_dir @tmp_dir ||= [:tmp_dir] end |