rtm-javatmapi 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rtm/javatmapi.rb CHANGED
@@ -7,31 +7,31 @@ rescue Exception => e
7
7
  raise "TMAPI Implementations are supported for JRuby only"
8
8
  end
9
9
 
10
- begin
10
+ if Object.const_defined?("Gem") && rtmgem = Gem.loaded_specs["rtm-javatmapi"]
11
11
  require 'rtm'
12
- rescue LoadError
12
+ else
13
13
  rtm_path = File.expand_path(File.join(File.dirname(__FILE__), "../../../rtm/lib"))
14
14
  if File.directory?(rtm_path)
15
- $:.unshift rtm_path
15
+ $LOAD_PATH.unshift rtm_path
16
16
  require 'rtm'
17
17
  end
18
18
  end
19
19
 
20
20
  Dir[File.join(File.dirname(__FILE__), 'javatmapi/javalibs/*.jar')].each {|file| require file }
21
21
 
22
+ # provide easy access to java classes in a net.* package
22
23
  def net
23
24
  Java::Net
24
25
  end
25
26
 
27
+ # provide easy access to java classes in a de.* package
26
28
  def de
27
29
  Java::De
28
30
  end
29
31
 
30
32
  import org.tmapi.core.TopicMapSystemFactory
31
- require "rtm/javatmapi/java_implementations"
32
- require "rtm/javatmapi/inclusion"
33
- require "rtm/javatmapi/ext"
34
33
  require "rtm/javatmapi/superiseable"
34
+ require "rtm/javatmapi/ext"
35
35
  require "rtm/javatmapi/core"
36
36
  require "rtm/javatmapi/aliases"
37
37
 
@@ -41,7 +41,6 @@ module RTM
41
41
  self == JavaTMAPI
42
42
  end
43
43
 
44
- include RTM::TopicMapSystem
45
44
  extend Superiseable
46
45
  include org.tmapi.core.TopicMapSystem
47
46
  attr_reader :tmsf
@@ -56,7 +55,21 @@ module RTM
56
55
  in_file = java.io.FileInputStream.new(properties)
57
56
  props = java.util.Properties.new
58
57
  props.load(in_file)
59
- @tmsf.setProperties(props)
58
+ if @tmsf.respond_to?(:setProperties)
59
+ # This is not part of the TMAPI interfaces but e.g. Ontopia supports it
60
+ @tmsf.setProperties(props)
61
+ else
62
+ # Try to read a java properties file. This ignores file continuations with backslash and may be incomplete in other respects
63
+ in_file = File.read_lines(features)
64
+ in_file.each do |line|
65
+ line.strip! # strip whitespaces at begin and end
66
+ next if line.blank? # skip empty lines
67
+ next if line =~ /^[#!]/ # skip comments
68
+ key, value = line.split(/\s*[=:\s]\s*/, 2) # split at "=", ":" or any whitespace surrounded by any number of whitespace
69
+ next if key.blank? # the value may be the empty string
70
+ @tmsf.setFeature(key, value)
71
+ end
72
+ end
60
73
  end
61
74
  end
62
75
 
@@ -69,6 +82,14 @@ module RTM
69
82
  features.each do |key, value|
70
83
  @tmsf.setFeature(key, value)
71
84
  end
85
+ elsif features.is_a?(String) && File.exists?(features)
86
+ # in contrast to properties, features values are always boolean
87
+ in_file = File.read_lines(features)
88
+ in_file.each do |line|
89
+ key, value = line.strip.split(/\s*[=:]\s*/, 2)
90
+ next if key.blank? or value.blank?
91
+ @tmsf.setFeature(key, %w[true 1 on enabled ja wahr da oui jo].include?(value)) # multi cultural feature files :)
92
+ end
72
93
  end
73
94
  end
74
95
 
@@ -106,7 +127,7 @@ module RTM
106
127
  else
107
128
  tm = @tms.createTopicMap(base_locator)
108
129
  end
109
- tm.base_iri = base_locator.reference #safe as String
130
+ tm.base_iri ||= base_locator.reference #safe as String
110
131
  tm.engine = self
111
132
  tm.prefixes = {}
112
133
  return tm
@@ -157,9 +178,11 @@ module RTM
157
178
  end
158
179
 
159
180
  # Returns a property in the underlying implementation of TopicMapSystem.
160
- def getProperty(property_name)
161
- @tms.getProperty(property_name)
181
+ def property(property_name)
182
+ @tmsf.getProperty(property_name)
162
183
  end
184
+ alias :get_property :property
185
+ alias :getProperty :property
163
186
 
164
187
  # Returns topic maps stored in this connection.
165
188
  # The optional arguments may specify the iri the topic maps are stored at.
@@ -5,39 +5,39 @@
5
5
 
6
6
  module Java::OrgTmapiCore
7
7
  module TopicMap
8
-
9
8
  alias :tii :type_instance_index
10
9
  alias :li :literal_index
11
10
  alias :si :scoped_index
12
- alias :construct_ii :get_construct_by_item_identifier
13
- alias :c_ii :construct_ii
14
- alias :topic_si :get_topic_by_subject_identifier
15
- alias :t_si :topic_si
16
- alias :topic_sl :get_topic_by_subject_locator
17
- alias :t_sl :topic_sl
18
- alias :create_si :create_topic_by_subject_identifier
19
- alias :create_sl :create_topic_by_subject_locator
20
- alias :create_ii :create_topic_by_item_identifier
11
+
12
+ alias :by_ii :get_construct_by_item_identifier
13
+ alias :by_si :get_topic_by_subject_identifier
14
+ alias :by_sl :get_topic_by_subject_locator
15
+
16
+ alias :t :topics
17
+ alias :a :associations
21
18
  end
22
19
 
23
20
  module Topic
24
21
  alias :o :occurrences
25
22
  alias :n :names
26
- alias :tii :type_instance_index
23
+
27
24
  alias :si :subject_identifiers
28
- alias :si= :add_subject_identifier
29
25
  alias :add_si :add_subject_identifier
30
- alias :sl= :add_subject_locator
31
- alias :add_sl :add_subject_locator
32
26
  alias :remove_si :remove_subject_identifier
27
+
28
+ alias :sl :subject_locators
29
+ alias :add_sl :add_subject_locator
33
30
  alias :remove_sl :remove_subject_locator
34
31
  end
35
32
 
36
33
  module Construct
37
-
38
- alias :item_identifiers= :add_item_identifier
34
+ alias :p :parent
35
+ alias :ii :item_identifiers
39
36
  alias :add_ii :add_item_identifier
40
- alias :ii= :add_item_identifier
41
37
  alias :remove_ii :remove_item_identifier
42
38
  end
39
+
40
+ module Association
41
+ alias :r :roles
42
+ end
43
43
  end
@@ -14,3 +14,4 @@ require 'rtm/javatmapi/core/scoped'
14
14
  require 'rtm/javatmapi/core/locator'
15
15
  require 'rtm/javatmapi/core/variant'
16
16
  require 'rtm/javatmapi/core/typed'
17
+ require 'rtm/javatmapi/core/datatype_aware'
@@ -3,8 +3,8 @@
3
3
 
4
4
  module Java::OrgTmapiCore::Association
5
5
 
6
+ include RTM::Association
6
7
  extend Superiseable
7
- #include RTM::Association
8
8
 
9
9
  superised
10
10
  # Returns the topic map this association belongs to.
@@ -47,7 +47,7 @@ module Java::OrgTmapiCore::Association
47
47
  # The optional identifier specifies the type of
48
48
  # the returned Roles.
49
49
  #
50
- # Identifier may be a Topic or Topic-Reference.
50
+ # Identifier may be a topic reference.
51
51
  #
52
52
  # The result may be empty.
53
53
  #
@@ -3,8 +3,8 @@
3
3
 
4
4
  module Java::OrgTmapiCore::Construct
5
5
 
6
+ include RTM::Construct
6
7
  extend Superiseable
7
- #include RTM::Construct
8
8
 
9
9
  # Returns the TopicMap instance this Construct belongs to.
10
10
  #
@@ -15,12 +15,31 @@ module Java::OrgTmapiCore::Construct
15
15
  getTopicMap
16
16
  end
17
17
 
18
+ # Returns the parent construct this construct belongs to.
19
+ #
20
+ # :call-seq:
21
+ # parent -> a construct (TopicMap, Topic, Association or Name)
22
+ #
23
+ def parent
24
+ getParent
25
+ end
26
+
18
27
  # Returns true if the construct fulfills all constraints defined in the TMDM
19
28
  def valid?
20
29
  # This is defined in RTM::AR and we test it. I guess from the API perspective, in TMAPI the validness of an object is guaranteed.
21
30
  true
22
31
  end unless self.method_defined?(:valid?)
23
32
 
33
+
34
+ # Returns the item identifiers assigned to the current construct.
35
+ #
36
+ # :call-seq:
37
+ # item_identifiers -> Set of Locators
38
+ #
39
+ def item_identifiers
40
+ getItemIdentifiers
41
+ end
42
+
24
43
  superised
25
44
  # Adds an item identifier to this construct.
26
45
  # Identifier may be a String or Locator.
@@ -30,13 +49,18 @@ module Java::OrgTmapiCore::Construct
30
49
  #
31
50
  def add_item_identifier(identifier)
32
51
  # TODO Spec
33
- if identifier.is_a? Java::OrgTmapiCore::Locator
34
- addItemIdentifier(identifier)
35
- else
36
- addItemIdentifier(topic_map.create_locator(identifier))
52
+ begin
53
+ if identifier.is_a? Java::OrgTmapiCore::Locator
54
+ addItemIdentifier(identifier)
55
+ else
56
+ addItemIdentifier(topic_map.create_locator(identifier))
57
+ end
58
+ rescue org.tmapi.core.IdentityConstraintException => ice
59
+ # TODO add condition to switch off automerge
60
+ self.mergeIn(self.topic_map.get(identifier))
37
61
  end
38
62
  end
39
- alias :item_identifiers= :add_item_identifier
63
+ alias :item_identifiers= :add_item_identifier # FIXME ii= is not add! it's set.
40
64
 
41
65
 
42
66
  superised
@@ -0,0 +1,60 @@
1
+ # Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
2
+ # License: Apache License, Version 2.0
3
+
4
+ module Java::OrgTmapiCore::DatatypeAware
5
+
6
+ include RTM::DatatypeAware
7
+ extend Superiseable
8
+
9
+ superised
10
+ # Changes the value (and if given, the datatype) of this Occurrence.
11
+ #
12
+ # The datatype must be a String or Locator.
13
+ #
14
+ # If value is a String, this methods sets the string value and
15
+ # the datatype implicitly to xsd:string.
16
+ #
17
+ # If argument is a Locator, this method sets the IRI value and
18
+ # the datatype implicitly to xsd:anyURI.
19
+ #
20
+ # If argument is a Float, this method sets the Float value and
21
+ # the datatype implicitly to xsd:float.
22
+ #
23
+ # If argument is a Fixnum, this method sets Fixnum value and
24
+ # the datatype implicitly to xsd:long.
25
+ #
26
+ # If argument is a Bignum, this method sets Bignum value and
27
+ # the datatype implicitly to xsd:integer.
28
+ #
29
+ # :call-seq:
30
+ # value = value
31
+ # value = value, datatype
32
+ #
33
+ def value=(args)
34
+ if args.is_a?(Array)
35
+ raise("value=(value) or value=(value,datatype)") unless args.size == 2
36
+ datatype = topic_map.create_locator(args.last)
37
+ value = args.first.to_s
38
+ if datatype.reference == RTM::PSI[:IRI]
39
+ setValue(topic_map.create_locator(value))
40
+ else
41
+ setValue(value, datatype)
42
+ end
43
+ else
44
+ setValue(args)
45
+ end
46
+ end
47
+
48
+ # Sets the datatype of this occurrence without changing its value.
49
+ #
50
+ # The datatype must be a String or Locator.
51
+ #
52
+ # :call-seq:
53
+ # datatype = datatype
54
+ #
55
+ def datatype=(datatype)
56
+ self.value = self.value, datatype
57
+ end
58
+ alias :set_datatype :datatype=
59
+
60
+ end
@@ -2,10 +2,10 @@
2
2
  # License: Apache License, Version 2.0
3
3
 
4
4
  module Java::OrgTmapiCore::Locator
5
-
6
- #include RTM::Locator
5
+
6
+ include RTM::Locator
7
7
  extend Superiseable
8
-
8
+
9
9
  # Returns the external form of the IRI.
10
10
  # Any special character will be escaped using the escaping conventions
11
11
  # of RFC 3987.
@@ -16,9 +16,5 @@ module Java::OrgTmapiCore::Locator
16
16
  def value
17
17
  toExternalForm
18
18
  end
19
-
20
- superised
21
- def to_s
22
- self.value
23
- end
19
+
24
20
  end
@@ -3,9 +3,8 @@
3
3
 
4
4
  module Java::OrgTmapiCore::Name
5
5
 
6
- #include RTM::Name
6
+ include RTM::Name
7
7
  extend Superiseable
8
-
9
8
 
10
9
  # Returns the Topic this Name belongs to.
11
10
  #
@@ -52,50 +51,77 @@ module Java::OrgTmapiCore::Name
52
51
  # Creates a Variant given the value and scope, if value is a Locator or
53
52
  # given value, datatype and scope, if the value is a String.
54
53
  #
55
- # Scope may be an Array of Topic/Topic-References and must not be empty.
54
+ # Scope may be an Array of topic references and must not be empty.
56
55
  #
57
- # Datatype may be a Topic-Reference (String or Locator).
56
+ # Datatype may be a String or Locator.
58
57
  #
59
58
  # :call-seq:
60
59
  # create_variant(value, scope) -> Variant
61
60
  # create_variant(value, datatype, scope) -> Variant
62
61
  #
63
- def create_variant(arg1, arg2, arg3 = :nothing)
64
- if arg1.is_a?(Java::OrgTmapiCore::Locator)
65
- raise("create_variant(value,scope), value: Locator, scope must be an Array") unless arg2.is_a?(Array)
66
- if arg2.empty?
67
- raise("create_variant(value,scope), value: Locator, scope must not be empty")
68
- end
69
- arg2.each do |theme|
70
- raise("create_variant(value,scope), value: Locator, scope must contain Topic/Topic-References") unless (theme.is_a?(Java::OrgTmapiCore::Topic) || theme.is_a?(String) || theme.is_a?(Java::OrgTmapiCore::Locator))
71
- end
72
- return createVariant(arg1, topic_map.get!(arg2).to_java(org.tmapi.core.Topic))
73
- elsif arg1.is_a?(String)
74
- if arg3 == :nothing
75
- raise("create_variant(value,scope), value: String, scope must be an Array") unless arg2.is_a?(Array)
76
- if arg2.empty?
77
- raise("create_variant(value,scope), value: String, scope must not be empty")
78
- end
79
- arg2.each do |theme|
80
- raise("create_variant(value,scope), value: String, scope must contain Topic/Topic-References") unless (theme.is_a?(Java::OrgTmapiCore::Topic) || theme.is_a?(String) || theme.is_a?(Java::OrgTmapiCore::Locator))
81
- end
82
- return createVariant(arg1, topic_map.get!(arg2).to_java(org.tmapi.core.Topic))
83
- elsif (arg2.is_a?(Locator) || arg2.is_a?(String))
84
- if ((arg2 == RTM::PSI[:String]) || (arg2 == topic_map.create_locator(RTM::PSI[:String])))
85
- return createVariant(arg1, topic_map.create_locator(RTM::PSI[:String]), topic_map.get!(arg3))
86
- elsif ((arg2 == RTM::PSI[:IRI]) || (arg2 == topic_map.create_locator(RTM::PSI[:IRI])))
87
- return createVariant(arg1, topic_map.create_locator(RTM::PSI[:IRI]), topic_map.get!(arg3))
88
- elsif ((arg2 == RTM::PSI[:XML]) || (arg2 == topic_map.create_locator(RTM::PSI[:XML])))
89
- return createVariant(arg1, topic_map.create_locator(RTM::PSI[:XML]), topic_map.get!(arg3))
90
- else
91
- raise("create_variant(value,datatype,scope), value: String, unknown datatype")
92
- end
62
+ def create_variant(value, *args)
63
+ # reading scope and datatype, if given
64
+ if args.length == 1
65
+ if args.first.is_a?(Hash)
66
+ # TODO
93
67
  else
94
- raise("create_variant(value,datatype,scope), datatype must be a String (identifier) or Locator")
95
- end
68
+ # scope given
69
+ scope = args.first
70
+ raise("create_variant(value, scope): scope must be an Array") unless scope.is_a?(Array)
71
+ end
72
+ elsif args.length == 2
73
+ # datatype and scope given
74
+ datatype = args.first
75
+ scope = args.last
76
+ raise("create_variant(value, datatype, scope): scope must be an Array") unless scope.is_a?(Array)
96
77
  else
97
- raise "create_variant: value not a Locator or String"
98
- end
78
+ raise("create_variant: arguments don't match - no scope given.")
79
+ end
80
+ # reading the value and setting the datatype dynamicaly if not set before
81
+ unless datatype
82
+ if value.is_a?(Java::OrgTmapiCore::Locator)
83
+ datatype ||= RTM::PSI[:IRI]
84
+ elsif value.is_a?(String)
85
+ datatype ||= RTM::PSI[:String]
86
+ elsif value.is_a?(Float)
87
+ datatype ||= RTM::PSI[:Float]
88
+ elsif value.is_a?(Fixnum)
89
+ datatype ||= RTM::PSI[:Long]
90
+ elsif value.is_a?(Bignum)
91
+ datatype ||= RTM::PSI[:Integer]
92
+ elsif value.is_a?(DateTime) #DateTime is a Time and a Date
93
+ datatype ||= RTM::PSI[:DateTime]
94
+ elsif value.is_a?(Time)
95
+ datatype ||= RTM::PSI[:Time]
96
+ elsif value.is_a?(Date)
97
+ datatype ||= RTM::PSI[:Date]
98
+ else
99
+ raise NoDatatypeHandlerAvailableException.new(value)
100
+ end
101
+ end
102
+ # creating locator for datatype
103
+ datatype = topic_map.create_locator(datatype)
104
+ # value must be a String
105
+ value = value.to_s
106
+ # if datatype is xsd:anyURI -> resolve value against base_iri
107
+ if datatype.reference == RTM::PSI[:IRI]
108
+ value = topic_map.create_locator(value).reference
109
+ end
110
+ # creating an array of topics for the scope
111
+ scope = topic_map.get!(scope).to_java(org.tmapi.core.Topic)
112
+
113
+ begin
114
+ createVariant(value, datatype, scope)
115
+ rescue java.lang.IllegalArgumentException => iae
116
+ raise(iae.message)
117
+ end
118
+ end
119
+
120
+ private
121
+ class NoDatatypeHandlerAvailableException < Exception
122
+ def initialize(value)
123
+ super "Cannot map #{value} to a variant value+datatype. Maybe you need to add a handler for your Ruby object type."
124
+ end
99
125
  end
100
-
126
+
101
127
  end