Class: Ea::Export::Xsd::NamespaceRegistry
- Inherits:
-
Object
- Object
- Ea::Export::Xsd::NamespaceRegistry
- Defined in:
- lib/ea/export/xsd/namespace_registry.rb
Overview
Loads EA's GMLNamespaces.xml and exposes per-prefix namespace declarations: prefix → { target_namespace:, xsd_document: }.
XML format:
<Namespaces>
<GMLNS version="3.2.1">
<Namespace xmlns="gml" targetNamespace="http://..."
xsdDocument="http://.../gml.xsd"/>
</GMLNS>
</Namespaces>
Defined Under Namespace
Classes: Namespace
Instance Attribute Summary collapse
-
#namespaces ⇒ Object
readonly
Returns the value of attribute namespaces.
Class Method Summary collapse
- .from_nokogiri(doc, version: nil) ⇒ NamespaceRegistry
- .from_path(path, version: nil) ⇒ NamespaceRegistry
Instance Method Summary collapse
- #for(prefix) ⇒ Namespace?
-
#initialize(namespaces = {}) ⇒ NamespaceRegistry
constructor
A new instance of NamespaceRegistry.
-
#versions ⇒ Array<String>
All distinct versions present (e.g. ["3.2.1", "3.3"]).
Constructor Details
#initialize(namespaces = {}) ⇒ NamespaceRegistry
Returns a new instance of NamespaceRegistry.
22 23 24 |
# File 'lib/ea/export/xsd/namespace_registry.rb', line 22 def initialize(namespaces = {}) @namespaces = namespaces end |
Instance Attribute Details
#namespaces ⇒ Object (readonly)
Returns the value of attribute namespaces.
19 20 21 |
# File 'lib/ea/export/xsd/namespace_registry.rb', line 19 def namespaces @namespaces end |
Class Method Details
.from_nokogiri(doc, version: nil) ⇒ NamespaceRegistry
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ea/export/xsd/namespace_registry.rb', line 41 def self.from_nokogiri(doc, version: nil) namespaces = {} # EA stores the prefix in a `xmlns` attribute, which XML # reserves for namespace declarations. Nokogiri interprets # it as the element's namespace, so we read it back via # `namespace` (the parsed NS) rather than `attribute`. # Use local-name() so the lookup is namespace-agnostic. doc.xpath("//*[local-name()='GMLNS']").each do |gmlns| next if version && gmlns["version"] != version gmlns.element_children.each do |node| next unless node.name == "Namespace" prefix = node.namespace&.href next unless prefix namespaces[prefix] = Namespace.new( prefix: prefix, target_namespace: node["targetNamespace"], xsd_document: node["xsdDocument"], version: gmlns["version"] ) end end new(namespaces) end |
.from_path(path, version: nil) ⇒ NamespaceRegistry
32 33 34 35 36 |
# File 'lib/ea/export/xsd/namespace_registry.rb', line 32 def self.from_path(path, version: nil) return new if path.nil? || !File.exist?(path) from_nokogiri(Nokogiri::XML(File.read(path)), version: version) end |
Instance Method Details
#for(prefix) ⇒ Namespace?
70 71 72 |
# File 'lib/ea/export/xsd/namespace_registry.rb', line 70 def for(prefix) namespaces[prefix] end |
#versions ⇒ Array<String>
All distinct versions present (e.g. ["3.2.1", "3.3"]).
76 77 78 |
# File 'lib/ea/export/xsd/namespace_registry.rb', line 76 def versions namespaces.values.map(&:version).compact.uniq end |