Class: Saxon::ItemType

Inherits:
Object
  • Object
show all
Defined in:
lib/saxon/item_type.rb,
lib/saxon/item_type/value_to_ruby.rb,
lib/saxon/item_type/lexical_string_conversion.rb

Overview

Represent XDM types abstractly

Defined Under Namespace

Modules: LexicalStringConversion, ValueToRuby Classes: LazyReadOnlyHash, UnmappedRubyTypeError, UnmappedXSDTypeNameError

Constant Summary collapse

TYPE_MAPPING =

A mapping of Ruby types to XDM type constants

{
  'String' => :STRING,
  'Array'  => :ANY_ARRAY,
  'Hash'   => :ANY_MAP,
  'TrueClass'  => :BOOLEAN,
  'FalseClass' => :BOOLEAN,
  'Date' => :DATE,
  'DateTime' => :DATE_TIME,
  'Time' => :DATE_TIME,
  'BigDecimal' => :DECIMAL,
  'Integer' => :INTEGER,
  'Fixnum' => :INTEGER, # Fixnum/Bignum needed for JRuby 9.1/Ruby 2.3
  'Bignum' => :INTEGER,
  'Float' => :FLOAT,
  'Numeric' => :NUMERIC
}.freeze
QNAME_MAPPING =

A mapping of QNames to XDM type constants

LazyReadOnlyHash.new do
  {
    'anyAtomicType' => :ANY_ATOMIC_VALUE,
    'anyURI' => :ANY_URI,
    'base64Binary' => :BASE64_BINARY,
    'boolean' => :BOOLEAN,
    'byte' => :BYTE,
    'date' => :DATE,
    'dateTime' => :DATE_TIME,
    'dateTimeStamp' => :DATE_TIME_STAMP,
    'dayTimeDuration' => :DAY_TIME_DURATION,
    'decimal' => :DECIMAL,
    'double' => :DOUBLE,
    'duration' => :DURATION,
    'ENTITY' => :ENTITY,
    'float' => :FLOAT,
    'gDay' => :G_DAY,
    'gMonth' => :G_MONTH,
    'gMonthDay' => :G_MONTH_DAY,
    'gYear' => :G_YEAR,
    'gYearMonth' => :G_YEAR_MONTH,
    'hexBinary' => :HEX_BINARY,
    'ID' => :ID,
    'IDREF' => :IDREF,
    'int' => :INT,
    'integer' => :INTEGER,
    'language' => :LANGUAGE,
    'long' => :LONG,
    'Name' => :NAME,
    'NCName' => :NCNAME,
    'negativeInteger' => :NEGATIVE_INTEGER,
    'NMTOKEN' => :NMTOKEN,
    'nonNegativeInteger' => :NON_NEGATIVE_INTEGER,
    'nonPositiveInteger' => :NON_POSITIVE_INTEGER,
    'normalizedString' => :NORMALIZED_STRING,
    'NOTATION' => :NOTATION,
    'numeric' => :NUMERIC,
    'positiveInteger' => :POSITIVE_INTEGER,
    'QName' => :QNAME,
    'short' => :SHORT,
    'string' => :STRING,
    'time' => :TIME,
    'token' => :TOKEN,
    'unsignedByte' => :UNSIGNED_BYTE,
    'unsignedInt' => :UNSIGNED_INT,
    'unsignedLong' => :UNSIGNED_LONG,
    'unsignedShort' => :UNSIGNED_SHORT,
    'untypedAtomic' => :UNTYPED_ATOMIC,
    'yearMonthDuration' => :YEAR_MONTH_DURATION
  }.map { |local_name, constant|
    qname = Saxon::QName.create({
      prefix: 'xs', uri: 'http://www.w3.org/2001/XMLSchema',
      local_name: local_name
    })
    [qname, constant]
  }.to_h.freeze
end
STR_MAPPING =

A mapping of type names/QNames to XDM type constants

LazyReadOnlyHash.new do
  {
    'array(*)' => :ANY_ARRAY,
    'item()' => :ANY_ITEM,
    'map(*)' => :ANY_MAP,
    'node()' => :ANY_NODE
  }.merge(
    Hash[QNAME_MAPPING.map { |qname, v| [qname.to_s, v] }]
  ).freeze
end
ATOMIC_VALUE_LEXICAL_STRING_CONVERTORS =

convertors to generate lexical strings for a given Saxon::ItemType, as a hash keyed on the ItemType

LazyReadOnlyHash.new do
  LexicalStringConversion::Convertors.constants.map { |const|
    [S9API::ItemType.const_get(const), LexicalStringConversion::Convertors.const_get(const)]
  }.to_h.freeze
end
ATOMIC_VALUE_TO_RUBY_CONVERTORS =

convertors from XDM::AtomicValue to a ruby primitve value, as a hash keyed on the ItemType

LazyReadOnlyHash.new do
  ValueToRuby::Convertors.constants.map { |const|
    [S9API::ItemType.const_get(const), ValueToRuby::Convertors.const_get(const)]
  }.to_h.freeze
end

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s9_item_type) ⇒ ItemType

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 ItemType.



265
266
267
# File 'lib/saxon/item_type.rb', line 265

def initialize(s9_item_type)
  @s9_item_type = s9_item_type
end

Class Method Details

.get_type(ruby_class) ⇒ Saxon::ItemType .get_type(type_name) ⇒ Saxon::ItemType .get_type(item_type) ⇒ Saxon::ItemType

Get an appropriate Saxon::ItemType for a Ruby type or given a type name as a string

Overloads:

Returns:



197
198
199
200
201
202
203
204
# File 'lib/saxon/item_type.rb', line 197

def get_type(arg)
  case arg
  when Saxon::ItemType
    arg
  else
    fetch_type_instance(get_s9_type(arg))
  end
end

Instance Method Details

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

compares two Saxon::ItemTypes using the underlying Saxon and XDM comparision rules

Parameters:

Returns:

  • (Boolean)


284
285
286
287
# File 'lib/saxon/item_type.rb', line 284

def ==(other)
  return false unless other.is_a?(ItemType)
  s9_item_type.equals(other.to_java)
end

#hashFixnum

Return a hash code so this can be used as a key in a Hash.

Returns:

  • (Fixnum)

    the hash code



293
294
295
# File 'lib/saxon/item_type.rb', line 293

def hash
  @hash ||= s9_item_type.hashCode
end

#lexical_string(value) ⇒ String

Generate the appropriate lexical string representation of the value given the ItemType’s schema definition.

Types with no explcit formatter defined just get to_s called on them…

Parameters:

  • value (Object)

    The Ruby value to generate the lexical string representation of

Returns:

  • (String)

    The XML Schema-defined lexical string representation of the value



306
307
308
# File 'lib/saxon/item_type.rb', line 306

def lexical_string(value)
  lexical_string_convertor.call(value, self)
end

#ruby_value(xdm_atomic_value) ⇒ Object

Convert an XDM Atomic Value to an instance of an appropriate Ruby class, or return the lexical string.

It’s assumed that the XDM::AtomicValue is of this type, otherwise an error is raised.

Parameters:



314
315
316
# File 'lib/saxon/item_type.rb', line 314

def ruby_value(xdm_atomic_value)
  value_to_ruby_convertor.call(xdm_atomic_value)
end

#to_javaSaxon::S9API::ItemType

Returns The underlying Saxon Java ItemType object.

Returns:

  • (Saxon::S9API::ItemType)

    The underlying Saxon Java ItemType object



277
278
279
# File 'lib/saxon/item_type.rb', line 277

def to_java
  s9_item_type
end

#type_nameSaxon::QName

Return the QName which represents this type

Returns:



272
273
274
# File 'lib/saxon/item_type.rb', line 272

def type_name
  @type_name ||= Saxon::QName.new(s9_item_type.getTypeName)
end