ontopia-topicmaps 0.0.1-java

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README ADDED
@@ -0,0 +1,46 @@
1
+ = ontopia-topicmaps - Query topic maps with Ontopia
2
+
3
+ == VERSION
4
+
5
+ This documentation refers to ontopia-topicmaps version 0.0.1
6
+
7
+
8
+ == DESCRIPTION
9
+
10
+ Query topic maps with Ontopia[http://ontopia.net/]. A working
11
+ installation[http://ontopia.net/doc/current/install.html] of Ontopia needs to
12
+ be present (point to it through the +OTM_BASE+ environment variable in case it
13
+ cannot be found automatically). Note, though, that's it not required to run the
14
+ application server, only the JAR files are needed.
15
+
16
+
17
+ == LINKS
18
+
19
+ <b></b>
20
+ Documentation:: http://blackwinter.github.com/ontopia-topicmaps
21
+ Source code:: http://github.com/blackwinter/ontopia-topicmaps
22
+ RubyGem:: http://rubygems.org/gems/ontopia-topicmaps
23
+ Ontopia:: http://ontopia.net/
24
+
25
+
26
+ == AUTHORS
27
+
28
+ * Jens Wille <mailto:jens.wille@gmail.com>
29
+
30
+
31
+ == LICENSE AND COPYRIGHT
32
+
33
+ Copyright (C) 2013 Jens Wille
34
+
35
+ ontopia-topicmaps is free software: you can redistribute it and/or modify it
36
+ under the terms of the GNU Affero General Public License as published by
37
+ the Free Software Foundation, either version 3 of the License, or (at your
38
+ option) any later version.
39
+
40
+ ontopia-topicmaps is distributed in the hope that it will be useful, but
41
+ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
42
+ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
43
+ License for more details.
44
+
45
+ You should have received a copy of the GNU Affero General Public License
46
+ along with ontopia-topicmaps. If not, see <http://www.gnu.org/licenses/>.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require File.expand_path(%q{../lib/ontopia/topicmaps/version}, __FILE__)
2
+
3
+ begin
4
+ require 'hen'
5
+
6
+ Hen.lay! {{
7
+ :gem => {
8
+ :name => %q{ontopia-topicmaps},
9
+ :version => Ontopia::Topicmaps::VERSION,
10
+ :summary => %q{Query topic maps with Ontopia.},
11
+ :author => %q{Jens Wille},
12
+ :email => %q{jens.wille@gmail.com},
13
+ :license => %q{AGPL},
14
+ :homepage => :blackwinter,
15
+ :platform => 'java',
16
+ :dependencies => %w[]
17
+ }
18
+ }}
19
+ rescue LoadError => err
20
+ warn "Please install the `hen' gem. (#{err})"
21
+ end
@@ -0,0 +1,49 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # ontopia-topicmaps -- Query topic maps with Ontopia. #
5
+ # #
6
+ # Copyright (C) 2013 Jens Wille #
7
+ # #
8
+ # ontopia-topicmaps is free software: you can redistribute it and/or modify #
9
+ # it under the terms of the GNU Affero General Public License as published #
10
+ # by the Free Software Foundation, either version 3 of the License, or (at #
11
+ # your option) any later version. #
12
+ # #
13
+ # ontopia-topicmaps is distributed in the hope that it will be useful, but #
14
+ # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
15
+ # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
16
+ # License for more details. #
17
+ # #
18
+ # You should have received a copy of the GNU Affero General Public License #
19
+ # along with ontopia-topicmaps. If not, see <http://www.gnu.org/licenses/>. #
20
+ # #
21
+ ###############################################################################
22
+ #++
23
+
24
+ module Ontopia
25
+ module Topicmaps
26
+
27
+ module Packages
28
+
29
+ PACKAGES = {
30
+ 'net.ontopia.infoset.impl.basic' => %w[URIFragmentLocator],
31
+ 'net.ontopia.topicmaps.query.utils' => %w[QueryWrapper RowMapperIF],
32
+ 'net.ontopia.topicmaps.utils' => %w[TopicStringifiers],
33
+ 'net.ontopia.topicmaps.utils.ctm' => %w[CTMTopicMapReader],
34
+ 'net.ontopia.topicmaps.utils.jtm' => %w[JTMTopicMapReader],
35
+ 'net.ontopia.topicmaps.utils.ltm' => %w[LTMTopicMapReader],
36
+ 'net.ontopia.topicmaps.utils.rdf' => %w[RDFTopicMapReader],
37
+ 'net.ontopia.topicmaps.utils.xfml' => %w[XFMLTopicMapReader],
38
+ 'net.ontopia.topicmaps.xml' => %w[XTMTopicMapReader],
39
+ 'net.ontopia.utils' => %w[OntopiaRuntimeException StringifierIF]
40
+ }
41
+
42
+ def self.included(base)
43
+ PACKAGES.each_key { |pkg| base.send(:include_package, pkg) }
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,42 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # ontopia-topicmaps -- Query topic maps with Ontopia. #
5
+ # #
6
+ # Copyright (C) 2013 Jens Wille #
7
+ # #
8
+ # ontopia-topicmaps is free software: you can redistribute it and/or modify #
9
+ # it under the terms of the GNU Affero General Public License as published #
10
+ # by the Free Software Foundation, either version 3 of the License, or (at #
11
+ # your option) any later version. #
12
+ # #
13
+ # ontopia-topicmaps is distributed in the hope that it will be useful, but #
14
+ # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
15
+ # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
16
+ # License for more details. #
17
+ # #
18
+ # You should have received a copy of the GNU Affero General Public License #
19
+ # along with ontopia-topicmaps. If not, see <http://www.gnu.org/licenses/>. #
20
+ # #
21
+ ###############################################################################
22
+ #++
23
+
24
+ module Ontopia
25
+ module Topicmaps
26
+
27
+ class IdStringifier
28
+
29
+ NO_VALUE = '[No ID]'.freeze
30
+
31
+ Topicmaps.init(self, :StringifierIF) {
32
+ URIFragmentLocator.field_accessor :fragment
33
+ }
34
+
35
+ def to_string(object)
36
+ (id = object.get_item_identifiers.first) ? id.fragment : NO_VALUE
37
+ end
38
+
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,127 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # ontopia-topicmaps -- Query topic maps with Ontopia. #
5
+ # #
6
+ # Copyright (C) 2013 Jens Wille #
7
+ # #
8
+ # ontopia-topicmaps is free software: you can redistribute it and/or modify #
9
+ # it under the terms of the GNU Affero General Public License as published #
10
+ # by the Free Software Foundation, either version 3 of the License, or (at #
11
+ # your option) any later version. #
12
+ # #
13
+ # ontopia-topicmaps is distributed in the hope that it will be useful, but #
14
+ # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
15
+ # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
16
+ # License for more details. #
17
+ # #
18
+ # You should have received a copy of the GNU Affero General Public License #
19
+ # along with ontopia-topicmaps. If not, see <http://www.gnu.org/licenses/>. #
20
+ # #
21
+ ###############################################################################
22
+ #++
23
+
24
+ module Ontopia
25
+ module Topicmaps
26
+
27
+ class Topicmap
28
+
29
+ Topicmaps.init(self)
30
+
31
+ TOPICMAP_READER = {
32
+ '.ctm' => :CTMTopicMapReader,
33
+ '.jtm' => :JTMTopicMapReader,
34
+ '.ltm' => :LTMTopicMapReader,
35
+ '.rdf' => :RDFTopicMapReader,
36
+ '.xfml' => :XFMLTopicMapReader,
37
+ '.xtm' => :XTMTopicMapReader
38
+ }
39
+
40
+ QUERY = {
41
+ :all_topics => <<-EOT,
42
+ select $TOPIC from
43
+ topic($TOPIC),
44
+ item-identifier($TOPIC, $_)?
45
+ EOT
46
+ :count_all_topics => <<-EOT
47
+ select count($TOPIC) from
48
+ topic($TOPIC),
49
+ item-identifier($TOPIC, $_)?
50
+ EOT
51
+ }
52
+
53
+ def initialize(file)
54
+ Topicmaps.setup_classpath
55
+
56
+ @file = file.include?(File::SEPARATOR) ? file :
57
+ File.join(Topicmaps.base, 'topicmaps', file)
58
+
59
+ @tm = read(@file)
60
+ end
61
+
62
+ attr_reader :tm, :file
63
+
64
+ def count(query = nil, declarations = nil)
65
+ query ||= :count_all_topics
66
+ query_for_list(query, declarations) { |i,| i }.first || 0
67
+ end
68
+
69
+ def query(query = nil, declarations = nil, str = nil)
70
+ str = Topicmaps.stringifier(str || :default)
71
+ res = query_for_list(query, declarations, &block_given? ?
72
+ lambda { |i,| yield(i, str) } : lambda { |i,| str.to_string(i) }).to_a
73
+ end
74
+
75
+ def topics(value_str = nil, key_str = nil)
76
+ hash, key_str = {}, Topicmaps.stringifier(key_str || :id)
77
+
78
+ query(:all_topics, nil, value_str) { |i, str|
79
+ hash[key_str.to_string(i)] = str.to_string(i)
80
+ }
81
+
82
+ hash
83
+ end
84
+
85
+ private
86
+
87
+ def read(file, reader = nil)
88
+ reader ||= TOPICMAP_READER[ext = File.extname(file)]
89
+ raise "Unsupported file format: #{ext}" unless reader
90
+
91
+ self.class.const_get(reader).new(Java::JavaIo::File.new(file)).read
92
+ rescue Java::JavaIo::IOException => err
93
+ raise "Error reading topic map: #{err}"
94
+ end
95
+
96
+ def query_for_list(query, declarations, &block)
97
+ args = [QUERY.has_key?(query ||= :all_topics) ? QUERY[query] :
98
+ query.is_a?(Symbol) ? raise("No such query: #{query}") : query]
99
+
100
+ args << RowMapper.new(&block) if block
101
+
102
+ qw = QueryWrapper.new(tm)
103
+ qw.set_declarations(declarations) if declarations
104
+
105
+ qw.query_for_list(*args)
106
+ rescue OntopiaRuntimeException => err
107
+ raise "Query error: #{err}"
108
+ end
109
+
110
+ class RowMapper
111
+
112
+ Topicmaps.init(self, :RowMapperIF)
113
+
114
+ def initialize(&block)
115
+ @block = block
116
+ end
117
+
118
+ def map_row(result, rowno)
119
+ @block[result.get_value(0), rowno]
120
+ end
121
+
122
+ end
123
+
124
+ end
125
+
126
+ end
127
+ end
@@ -0,0 +1,52 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # ontopia-topicmaps -- Query topic maps with Ontopia. #
5
+ # #
6
+ # Copyright (C) 2013 Jens Wille #
7
+ # #
8
+ # ontopia-topicmaps is free software: you can redistribute it and/or modify #
9
+ # it under the terms of the GNU Affero General Public License as published #
10
+ # by the Free Software Foundation, either version 3 of the License, or (at #
11
+ # your option) any later version. #
12
+ # #
13
+ # ontopia-topicmaps is distributed in the hope that it will be useful, but #
14
+ # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
15
+ # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
16
+ # License for more details. #
17
+ # #
18
+ # You should have received a copy of the GNU Affero General Public License #
19
+ # along with ontopia-topicmaps. If not, see <http://www.gnu.org/licenses/>. #
20
+ # #
21
+ ###############################################################################
22
+ #++
23
+
24
+ module Ontopia
25
+ module Topicmaps
26
+
27
+ module Version
28
+
29
+ MAJOR = 0
30
+ MINOR = 0
31
+ TINY = 1
32
+
33
+ class << self
34
+
35
+ # Returns array representation.
36
+ def to_a
37
+ [MAJOR, MINOR, TINY]
38
+ end
39
+
40
+ # Short-cut for version string.
41
+ def to_s
42
+ to_a.join('.')
43
+ end
44
+
45
+ end
46
+
47
+ end
48
+
49
+ VERSION = Version.to_s
50
+
51
+ end
52
+ end
@@ -0,0 +1,114 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # ontopia-topicmaps -- Query topic maps with Ontopia. #
5
+ # #
6
+ # Copyright (C) 2013 Jens Wille #
7
+ # #
8
+ # ontopia-topicmaps is free software: you can redistribute it and/or modify #
9
+ # it under the terms of the GNU Affero General Public License as published #
10
+ # by the Free Software Foundation, either version 3 of the License, or (at #
11
+ # your option) any later version. #
12
+ # #
13
+ # ontopia-topicmaps is distributed in the hope that it will be useful, but #
14
+ # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
15
+ # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public #
16
+ # License for more details. #
17
+ # #
18
+ # You should have received a copy of the GNU Affero General Public License #
19
+ # along with ontopia-topicmaps. If not, see <http://www.gnu.org/licenses/>. #
20
+ # #
21
+ ###############################################################################
22
+ #++
23
+
24
+ require 'java'
25
+
26
+ require_relative 'topicmaps/version'
27
+ require_relative 'topicmaps/packages'
28
+
29
+ module Ontopia
30
+ module Topicmaps
31
+
32
+ DEFAULT_JARS = ENV['OTM_JARS'] || 'log4j-*:slf4j-log4j*:ontopia-engine-*'
33
+ DEFAULT_BASE = ENV['OTM_BASE'] || '/usr/local/ontopia'
34
+
35
+ @init = {}
36
+
37
+ @base = nil
38
+
39
+ @default_stringifier = nil
40
+
41
+ class << self
42
+
43
+ attr_reader :base
44
+
45
+ def default_stringifier=(str)
46
+ @default_stringifier = stringifier(str)
47
+ end
48
+
49
+ def default_stringifier
50
+ @default_stringifier ||= name_stringifier
51
+ end
52
+
53
+ def name_stringifier
54
+ self::TopicStringifiers.get_default_stringifier
55
+ end
56
+
57
+ def id_stringifier
58
+ IdStringifier.new
59
+ end
60
+
61
+ def stringifier(str)
62
+ str.is_a?(Symbol) ? send("#{str}_stringifier") : str
63
+ end
64
+
65
+ def count(file, *args)
66
+ Topicmap.new(file).count(*args)
67
+ end
68
+
69
+ def query(file, *args, &block)
70
+ Topicmap.new(file).query(*args, &block)
71
+ end
72
+
73
+ def topics(file, *args)
74
+ Topicmap.new(file).topics(*args)
75
+ end
76
+
77
+ def base=(base)
78
+ setup_classpath(base)
79
+ end
80
+
81
+ def setup_classpath(base = nil, jars = nil)
82
+ if @base
83
+ return unless base
84
+ raise "#{self}: $CLASSPATH already set up: #{@base}"
85
+ end
86
+
87
+ @base = base ||= DEFAULT_BASE
88
+
89
+ Array(jars || DEFAULT_JARS).join(':').split(':').each { |jar|
90
+ (path = Dir[File.join(base, 'lib', "#{jar}.jar")].sort.last) ?
91
+ $CLASSPATH << path : raise("JAR not found: #{jar} (#{base})")
92
+ }
93
+
94
+ init.each { |klass, (modules, block)|
95
+ klass.send :include, Packages, *modules.map { |m| const_get(m) }
96
+ klass.class_eval(&block) if block
97
+ }.clear
98
+ end
99
+
100
+ protected
101
+
102
+ def init(klass = nil, *modules, &block)
103
+ klass ? Topicmaps.init[klass] = [modules, block] : @init
104
+ end
105
+
106
+ end
107
+
108
+ init(self)
109
+
110
+ end
111
+ end
112
+
113
+ require_relative 'topicmaps/stringifiers'
114
+ require_relative 'topicmaps/topicmap'
@@ -0,0 +1 @@
1
+ require 'ontopia/topicmaps'