# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig. # License: Apache License, Version 2.0 module Java::OrgTmapiCore::Name include RTM::Name extend Superiseable # Returns the Topic this Name belongs to. # # :call-seq: # parent -> Topic # def parent getParent end alias :reverse_children :parent # Returns all Variants defined for this Name. # # :call-seq: # children -> Array of Variants # def children getVariants.to_a end alias :reverse_parent :children superised # Returns the value of this Name. # # :call-seq: # value -> String # def value getValue end # Sets the value of this name. The previous value is overridden. # # Argument is a String. # # :call-seq: # value=(argument) # def value=(argument) setValue(argument) end superised # Creates a Variant given the value and scope, if value is a Locator or # given value, datatype and scope, if the value is a String. # # Scope may be an Array of topic references and must not be empty. # # Datatype may be a String or Locator. # # :call-seq: # create_variant(value, scope) -> Variant # create_variant(value, datatype, scope) -> Variant # def create_variant(value, *args) # reading scope and datatype, if given if args.length == 1 if args.first.is_a?(Hash) # TODO else # scope given scope = args.first raise("create_variant(value, scope): scope must be an Array") unless scope.is_a?(Array) end elsif args.length == 2 # datatype and scope given datatype = args.first scope = args.last raise("create_variant(value, datatype, scope): scope must be an Array") unless scope.is_a?(Array) else raise("create_variant: arguments don't match - no scope given.") end # reading the value and setting the datatype dynamicaly if not set before unless datatype if value.is_a?(Java::OrgTmapiCore::Locator) datatype ||= RTM::PSI[:IRI] elsif value.is_a?(String) datatype ||= RTM::PSI[:String] elsif value.is_a?(Float) datatype ||= RTM::PSI[:Float] elsif value.is_a?(Fixnum) datatype ||= RTM::PSI[:Long] elsif value.is_a?(Bignum) datatype ||= RTM::PSI[:Integer] elsif value.is_a?(DateTime) #DateTime is a Time and a Date datatype ||= RTM::PSI[:DateTime] elsif value.is_a?(Time) datatype ||= RTM::PSI[:Time] elsif value.is_a?(Date) datatype ||= RTM::PSI[:Date] else raise NoDatatypeHandlerAvailableException.new(value) end end # creating locator for datatype datatype = topic_map.create_locator(datatype) # value must be a String value = value.to_s # if datatype is xsd:anyURI -> resolve value against base_iri if datatype.reference == RTM::PSI[:IRI] value = topic_map.create_locator(value).reference end # creating an array of topics for the scope scope = topic_map.get!(scope).to_java(org.tmapi.core.Topic) begin createVariant(value, datatype, scope) rescue java.lang.IllegalArgumentException => iae raise(iae.message) end end private class NoDatatypeHandlerAvailableException < Exception def initialize(value) super "Cannot map #{value} to a variant value+datatype. Maybe you need to add a handler for your Ruby object type." end end end