Class: Saxon::QName

Inherits:
Object
  • Object
show all
Defined in:
lib/saxon/qname.rb

Overview

Represents QNames

Defined Under Namespace

Classes: PrefixedStringWithoutNSURIError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s9_qname) ⇒ QName

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of QName.



74
75
76
# File 'lib/saxon/qname.rb', line 74

def initialize(s9_qname)
  @s9_qname = s9_qname
end

Class Method Details

.clark(clark_string) ⇒ Object



6
7
8
9
# File 'lib/saxon/qname.rb', line 6

def self.clark(clark_string)
  s9_qname = Saxon::S9API::QName.fromClarkName(clark_string)
  new(s9_qname)
end

.create(opts = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/saxon/qname.rb', line 16

def self.create(opts = {})
  prefix = opts[:prefix]
  uri = opts[:uri]
  begin
    local_name = opts.fetch(:local_name)
  rescue KeyError
    raise ArgumentError, "The :local_name option must be passed"
  end

  s9_qname = Saxon::S9API::QName.new(*[prefix, uri, local_name].compact)
  new(s9_qname)
end

.eqname(eqname_string) ⇒ Object



11
12
13
14
# File 'lib/saxon/qname.rb', line 11

def self.eqname(eqname_string)
  s9_qname = Saxon::S9API::QName.fromEQName(eqname_string)
  new(s9_qname)
end

.resolve(qname_or_string, namespaces = {}) ⇒ Saxon::QName

Resolve a QName string into a Saxon::QName.

If the arg is a Saxon::QName already, it just gets returned. If it’s an instance of the underlying Saxon Java QName, it’ll be wrapped into a Saxon::QName

If the arg is a string, it’s resolved by using resolve_variable_name

Parameters:

  • qname_or_string (String, Symbol, Saxon::QName)

    the qname to resolve

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

    the set of namespaces as a hash of "prefix" => "namespace-uri"

Returns:



40
41
42
43
44
45
46
47
48
49
# File 'lib/saxon/qname.rb', line 40

def self.resolve(qname_or_string, namespaces = {})
  case qname_or_string
  when String, Symbol
    resolve_qname_string(qname_or_string, namespaces)
  when self
    qname_or_string
  when Saxon::S9API::QName
    new(qname_or_string)
  end
end

.resolve_qname_string(qname_string, namespaces = {}) ⇒ Saxon::QName

Resolve a QName string of the form "prefix:local-name" into a Saxon::QName by looking up the namespace URI in a hash of "prefix" => "namespace-uri"

Parameters:

  • qname_string (String, Symbol)

    the QName as a "prefix:local-name" string

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

    the set of namespaces as a hash of "prefix" => "namespace-uri"

Returns:



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/saxon/qname.rb', line 58

def self.resolve_qname_string(qname_string, namespaces = {})
  local_name, prefix = qname_string.to_s.split(':').reverse
  uri = nil

  if prefix
    uri = namespaces[prefix]
    raise self::PrefixedStringWithoutNSURIError.new(qname_string, prefix) if uri.nil?
  end

  create(prefix: prefix, uri: uri, local_name: local_name)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



113
114
115
116
# File 'lib/saxon/qname.rb', line 113

def ==(other)
  return false unless other.is_a?(QName)
  s9_qname.equals(other.to_java)
end

#clarkString

Return a Clark notation representation of the QName:

"{http://ns.url}local-name"

Note that the prefix is lost in Clark notation.

Returns:

  • (String)

    The QName represented using Clark notation.



99
100
101
# File 'lib/saxon/qname.rb', line 99

def clark
  @s9_qname.getClarkName
end

#eqnameString

Return a Extended QName notation representation of the QName:

"Q{http://ns.url}local-name"

Note that the prefix is lost in EQName notation.

Returns:

  • (String)

    The QName represented using EQName notation.



109
110
111
# File 'lib/saxon/qname.rb', line 109

def eqname
  @s9_qname.getEQName
end

#hashObject



119
120
121
# File 'lib/saxon/qname.rb', line 119

def hash
  @hash ||= (local_name + uri).hash
end

#inspectObject



131
132
133
# File 'lib/saxon/qname.rb', line 131

def inspect
  "<Saxon::QName @prefix=#{prefix} @uri=#{uri} @local_name=#{local_name}>"
end

#local_nameString

Returns The local name part of the QName.

Returns:

  • (String)

    The local name part of the QName



79
80
81
# File 'lib/saxon/qname.rb', line 79

def local_name
  @s9_qname.getLocalName
end

#prefixString

Returns The prefix part of the QName (” if unset).

Returns:

  • (String)

    The prefix part of the QName (” if unset)



84
85
86
# File 'lib/saxon/qname.rb', line 84

def prefix
  @s9_qname.getPrefix
end

#to_javaObject



123
124
125
# File 'lib/saxon/qname.rb', line 123

def to_java
  s9_qname
end

#to_sObject



127
128
129
# File 'lib/saxon/qname.rb', line 127

def to_s
  s9_qname.to_s
end

#uriString

Returns The namespace URI part of the QName (” if unset).

Returns:

  • (String)

    The namespace URI part of the QName (” if unset)



89
90
91
# File 'lib/saxon/qname.rb', line 89

def uri
  @s9_qname.getNamespaceURI
end