Class: Ea::Export::Xsd::NamespaceRegistry

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespaces = {}) ⇒ NamespaceRegistry

Returns a new instance of NamespaceRegistry.

Parameters:

  • namespaces (Hash{String => Namespace}) (defaults to: {})


22
23
24
# File 'lib/ea/export/xsd/namespace_registry.rb', line 22

def initialize(namespaces = {})
  @namespaces = namespaces
end

Instance Attribute Details

#namespacesObject (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

Parameters:

  • doc (Nokogiri::XML::Document)
  • version (String, nil) (defaults to: nil)

    GML version filter

Returns:



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

Parameters:

  • path (String)

    path to GMLNamespaces.xml

  • version (String, nil) (defaults to: nil)

    GML version filter (e.g. "3.2.1")

Returns:



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?

Parameters:

  • prefix (String)

Returns:



70
71
72
# File 'lib/ea/export/xsd/namespace_registry.rb', line 70

def for(prefix)
  namespaces[prefix]
end

#versionsArray<String>

All distinct versions present (e.g. ["3.2.1", "3.3"]).

Returns:

  • (Array<String>)


76
77
78
# File 'lib/ea/export/xsd/namespace_registry.rb', line 76

def versions
  namespaces.values.map(&:version).compact.uniq
end