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: Factory, UnmappedRubyTypeError, UnmappedXSDTypeNameError

Constant Summary collapse

TYPE_CACHE_MUTEX =
Mutex.new
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,
  'Float' => :FLOAT,
  'Numeric' => :NUMERIC
}.freeze
QNAME_MAPPING =

A mapping of QNames to XDM type constants

Hash[{
  '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]
}].freeze
STR_MAPPING =

A mapping of type names/QNames to XDM type constants

{
  'array(*)' => :ANY_ARRAY,
  'item()' => :ANY_ITEM,
  'map(*)' => :ANY_MAP,
  'node()' => :ANY_NODE
}.merge(
  Hash[QNAME_MAPPING.map { |qname, v| [qname.to_s, v] }]
).freeze
ATOMIC_VALUE_LEXICAL_STRING_CONVERTORS =
Hash[
  LexicalStringConversion::Convertors.constants.map { |const|
    [S9API::ItemType.const_get(const), LexicalStringConversion::Convertors.const_get(const)]
  }
].freeze
ATOMIC_VALUE_TO_RUBY_CONVERTORS =
Hash[
  ValueToRuby::Convertors.constants.map { |const|
    [S9API::ItemType.const_get(const), ValueToRuby::Convertors.const_get(const)]
  }
].freeze

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.



227
228
229
# File 'lib/saxon/item_type.rb', line 227

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:



161
162
163
164
165
166
167
168
# File 'lib/saxon/item_type.rb', line 161

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)


246
247
248
249
# File 'lib/saxon/item_type.rb', line 246

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

#hashObject



253
254
255
# File 'lib/saxon/item_type.rb', line 253

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



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

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:



274
275
276
# File 'lib/saxon/item_type.rb', line 274

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



239
240
241
# File 'lib/saxon/item_type.rb', line 239

def to_java
  s9_item_type
end

#type_nameSaxon::QName

Return the QName which represents this type

Returns:



234
235
236
# File 'lib/saxon/item_type.rb', line 234

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