rdf-raptor 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -6,6 +6,17 @@ This is an [RDF.rb][] plugin that adds support for parsing/serializing
6
6
  library.
7
7
 
8
8
  * <http://github.com/bendiken/rdf-raptor>
9
+ * <http://lists.w3.org/Archives/Public/public-rdf-ruby/2010Apr/0003.html>
10
+
11
+ Features
12
+ --------
13
+
14
+ * Requires the [Raptor][] library and utilities to be available.
15
+ * Based on the [`rapper`][rapper] command-line utility bundled with Raptor.
16
+ * Parses and serializes RDF data from/into the RDF/XML or Turtle formats.
17
+ * Provides serialization format autodetection for RDF/XML and Turtle.
18
+ * Compatible with any operating system supported by Raptor and Ruby.
19
+ * Compatible with MRI 1.8.x, 1.9.x and JRuby (tested with JRuby 1.4).
9
20
 
10
21
  Examples
11
22
  --------
@@ -81,18 +92,31 @@ Documentation
81
92
  Dependencies
82
93
  ------------
83
94
 
84
- * [RDF.rb](http://rubygems.org/gems/rdf) (>= 0.1.3)
85
- * [Raptor](http://librdf.org/raptor/) (>= 1.4.21),
86
- specifically the `rapper` binary
95
+ * [RDF.rb](http://rubygems.org/gems/rdf) (>= 0.1.4)
96
+ * [Raptor][] (>= 1.4.16), specifically the `rapper` binary
87
97
 
88
98
  Installation
89
99
  ------------
90
100
 
91
- The recommended installation method is via RubyGems. To install the latest
92
- official release, do:
101
+ The recommended installation method is via [RubyGems](http://rubygems.org/).
102
+ To install the latest official release of the `RDF::Raptor` gem, do:
93
103
 
94
104
  % [sudo] gem install rdf-raptor
95
105
 
106
+ To install the required [Raptor][] command-line tools themselves, look for a
107
+ `raptor` or `raptor-utils` package in your platform's package management
108
+ system. Here follow installation instructions for the Mac and the most
109
+ common Linux and BSD distributions:
110
+
111
+ % [sudo] port install raptor # Mac OS X with MacPorts
112
+ % [sudo] fink install raptor-bin # Mac OS X with Fink
113
+ % [sudo] aptitude install raptor-utils # Ubuntu / Debian
114
+ % [sudo] yum install raptor # Fedora / CentOS / RHEL
115
+ % [sudo] zypper install raptor # openSUSE
116
+ % [sudo] emerge raptor # Gentoo Linux
117
+ % [sudo] pkg_add -r raptor # FreeBSD
118
+ % [sudo] pkg_add raptor # OpenBSD / NetBSD
119
+
96
120
  Download
97
121
  --------
98
122
 
@@ -120,3 +144,4 @@ information, see <http://unlicense.org/> or the accompanying UNLICENSE file.
120
144
  [RDF/XML]: http://www.w3.org/TR/REC-rdf-syntax/
121
145
  [Turtle]: http://en.wikipedia.org/wiki/Turtle_(syntax)
122
146
  [Raptor]: http://librdf.org/raptor/
147
+ [rapper]: http://librdf.org/raptor/rapper.html
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
data/lib/rdf/raptor.rb CHANGED
@@ -37,12 +37,13 @@ module RDF
37
37
  #
38
38
  # @author [Arto Bendiken](http://ar.to/)
39
39
  module Raptor
40
+ ENGINE = (ENV['RDF_RAPTOR_ENGINE'] || :cli).to_sym unless const_defined?(:ENGINE)
41
+ LIBRAPTOR = ENV['RDF_RAPTOR_LIBPATH'] || 'libraptor' unless const_defined?(:LIBRAPTOR)
42
+ RAPPER = ENV['RDF_RAPTOR_BINPATH'] || 'rapper' unless const_defined?(:RAPPER)
43
+
40
44
  require 'rdf/raptor/version'
41
- require 'rdf/raptor/format'
42
- require 'rdf/raptor/reader'
43
- require 'rdf/raptor/writer'
44
- require 'rdf/raptor/rdfxml'
45
- require 'rdf/raptor/turtle'
45
+ require 'rdf/raptor/cli'
46
+ require 'rdf/raptor/ffi' if ENGINE == :ffi
46
47
 
47
48
  ##
48
49
  # Returns `true` if the `rapper` binary is available.
@@ -64,9 +65,44 @@ module RDF
64
65
  #
65
66
  # @return [String]
66
67
  def self.version
67
- if `rapper --version 2>/dev/null` =~ /^(\d+)\.(\d+)\.(\d+)/
68
+ if `#{RAPPER} --version 2>/dev/null` =~ /^(\d+)\.(\d+)\.(\d+)/
68
69
  [$1, $2, $3].join('.')
69
70
  end
70
71
  end
72
+
73
+ ##
74
+ # Format base class.
75
+ class Format < RDF::Format
76
+ ##
77
+ # @overload rapper_format
78
+ #
79
+ # @overload rapper_format(format)
80
+ # @param [Symbol] format
81
+ #
82
+ # @return [void]
83
+ def self.rapper_format(format = nil)
84
+ unless format
85
+ @rapper_format
86
+ else
87
+ @rapper_format = format
88
+ end
89
+ end
90
+ end
91
+
92
+ ##
93
+ # Reader base class.
94
+ class Reader < RDF::Reader
95
+ include RDF::Raptor::CLI::Reader if ENGINE == :cli
96
+ include RDF::Raptor::FFI::Reader if ENGINE == :ffi
97
+ end
98
+
99
+ ##
100
+ # Writer base class.
101
+ class Writer < RDF::Writer
102
+ include RDF::Raptor::CLI::Writer
103
+ end
104
+
105
+ require 'rdf/raptor/rdfxml'
106
+ require 'rdf/raptor/turtle'
71
107
  end # module Raptor
72
108
  end # module RDF
@@ -0,0 +1,102 @@
1
+ module RDF::Raptor
2
+ ##
3
+ # A command-line interface to Raptor's `rapper` utility.
4
+ module CLI
5
+ ##
6
+ # Reader implementation.
7
+ module Reader
8
+ ##
9
+ # @param [IO, File, RDF::URI, String] input
10
+ # @param [Hash{Symbol => Object}] options
11
+ # @option (options) [String, #to_s] :base_uri ("file:///dev/stdin")
12
+ # @yield [reader]
13
+ # @yieldparam [RDF::Reader] reader
14
+ def initialize(input = $stdin, options = {}, &block)
15
+ raise RDF::ReaderError.new("`rapper` binary not found") unless RDF::Raptor.available?
16
+
17
+ format = self.class.format.rapper_format
18
+ case input
19
+ when RDF::URI, %r(^(file|http|https|ftp)://)
20
+ @command = "#{RAPPER} -q -i #{format} -o ntriples #{input}"
21
+ @command << " #{options[:base_uri]}" if options.has_key?(:base_uri)
22
+ @rapper = IO.popen(@command, 'rb')
23
+ when File
24
+ @command = "#{RAPPER} -q -i #{format} -o ntriples #{File.expand_path(input.path)}"
25
+ @command << " #{options[:base_uri]}" if options.has_key?(:base_uri)
26
+ @rapper = IO.popen(@command, 'rb')
27
+ else # IO, String
28
+ @command = "#{RAPPER} -q -i #{format} -o ntriples file:///dev/stdin"
29
+ @command << " #{options[:base_uri]}" if options.has_key?(:base_uri)
30
+ @rapper = IO.popen(@command, 'rb+')
31
+ @rapper.write(input.respond_to?(:read) ? input.read : input.to_s)
32
+ @rapper.close_write
33
+ end
34
+ @reader = RDF::NTriples::Reader.new(@rapper, options, &block)
35
+ end
36
+
37
+ protected
38
+
39
+ ##
40
+ # @return [Array]
41
+ def read_triple
42
+ @reader.read_triple
43
+ end
44
+ end
45
+
46
+ ##
47
+ # Writer implementation.
48
+ module Writer
49
+ ##
50
+ # @param [IO, File] output
51
+ # @param [Hash{Symbol => Object}] options
52
+ # @yield [writer]
53
+ # @yieldparam [RDF::Writer] writer
54
+ def initialize(output = $stdout, options = {}, &block)
55
+ raise RDF::WriterError.new("`rapper` binary not found") unless RDF::Raptor.available?
56
+
57
+ format = self.class.format.rapper_format
58
+ case output
59
+ when File, IO
60
+ @command = "#{RAPPER} -q -i ntriples -o #{format} file:///dev/stdin"
61
+ @command << " #{options[:base_uri]}" if options.has_key?(:base_uri)
62
+ @rapper = IO.popen(@command, 'rb+')
63
+ else
64
+ raise ArgumentError.new("unsupported output type: #{output.inspect}")
65
+ end
66
+ @writer = RDF::NTriples::Writer.new(@rapper, options)
67
+ super(output, options, &block)
68
+ end
69
+
70
+ protected
71
+
72
+ ##
73
+ # @return [void]
74
+ def write_prologue
75
+ super
76
+ end
77
+
78
+ ##
79
+ # @param [RDF::Resource] subject
80
+ # @param [RDF::URI] predicate
81
+ # @param [RDF::Value] object
82
+ # @return [void]
83
+ def write_triple(subject, predicate, object)
84
+ @writer.write_triple(subject, predicate, object)
85
+ end
86
+
87
+ ##
88
+ # @return [void]
89
+ def write_epilogue
90
+ @rapper.close_write
91
+ begin
92
+ chunk_size = @options[:chunk_size] || 4096 # bytes
93
+ loop do
94
+ @output.write(@rapper.readpartial(chunk_size))
95
+ end
96
+ rescue EOFError => e
97
+ # we're all done
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,281 @@
1
+ require 'ffi'
2
+
3
+ module RDF::Raptor
4
+ ##
5
+ # A foreign-function interface (FFI) to `libraptor`.
6
+ #
7
+ # @see http://librdf.org/raptor/api/
8
+ # @see http://librdf.org/raptor/libraptor.html
9
+ module FFI
10
+ ##
11
+ # Reader implementation.
12
+ module Reader
13
+ ##
14
+ # @param [IO, File, RDF::URI, String] input
15
+ # @param [Hash{Symbol => Object}] options
16
+ # @option (options) [String, #to_s] :base_uri ("file:///dev/stdin")
17
+ # @yield [reader]
18
+ # @yieldparam [RDF::Reader] reader
19
+ def initialize(input = $stdin, options = {}, &block)
20
+ @format = self.class.format.rapper_format
21
+ super
22
+ end
23
+
24
+ ##
25
+ # @yield [statement]
26
+ # @yieldparam [RDF::Statement] statement
27
+ def each_statement(&block)
28
+ each_triple do |triple|
29
+ block.call(RDF::Statement.new(*triple))
30
+ end
31
+ end
32
+
33
+ ##
34
+ # @yield [triple]
35
+ # @yieldparam [Array(RDF::Resource, RDF::URI, RDF::Value)] triple
36
+ def each_triple(&block)
37
+ @parser = Proc.new do |user_data, statement|
38
+ triple = V1_4::Statement.new(statement).to_triple
39
+ block.call(triple)
40
+ end
41
+
42
+ V1_4.with_world do |world|
43
+ V1_4.with_parser(:name => @format) do |parser|
44
+ V1_4.raptor_set_statement_handler(parser, nil, @parser)
45
+ case @input
46
+ when RDF::URI, %r(^(file|http|https|ftp)://)
47
+ begin
48
+ data_url = V1_4.raptor_new_uri(@input.to_s)
49
+ base_uri = @options.has_key?(:base_uri) ? V1_4.raptor_new_uri(@options[:base_uri].to_s) : nil
50
+ unless (result = V1_4.raptor_parse_uri(parser, data_url, base_uri)).zero?
51
+ # TODO: error handling
52
+ end
53
+ ensure
54
+ V1_4.raptor_free_uri(base_uri) if base_uri
55
+ V1_4.raptor_free_uri(data_url) if data_url
56
+ end
57
+
58
+ when File
59
+ begin
60
+ data_url = V1_4.raptor_new_uri("file://#{File.expand_path(@input.path)}")
61
+ base_uri = @options.has_key?(:base_uri) ? V1_4.raptor_new_uri(@options[:base_uri].to_s) : nil
62
+ unless (result = V1_4.raptor_parse_file(parser, data_url, base_uri)).zero?
63
+ # TODO: error handling
64
+ end
65
+ ensure
66
+ V1_4.raptor_free_uri(base_uri) if base_uri
67
+ V1_4.raptor_free_uri(data_url) if data_url
68
+ end
69
+
70
+ else # IO, String
71
+ base_uri = (@options[:base_uri] || 'file:///dev/stdin').to_s
72
+ unless (result = V1_4.raptor_start_parse(parser, base_uri)).zero?
73
+ # TODO: error handling
74
+ end
75
+ # TODO: read in chunks instead of everything in one go:
76
+ unless (result = V1_4.raptor_parse_chunk(parser, buffer = @input.read, buffer.size, 0)).zero?
77
+ # TODO: error handling
78
+ end
79
+ V1_4.raptor_parse_chunk(parser, nil, 0, 1) # EOF
80
+ end
81
+ end
82
+ end
83
+
84
+ @parser = nil
85
+ end
86
+
87
+ alias_method :each, :each_statement
88
+ end
89
+
90
+ ##
91
+ # Helper methods for FFI modules.
92
+ module Base
93
+ def define_pointer(name)
94
+ self.class.send(:define_method, name) { :pointer }
95
+ end
96
+ end
97
+
98
+ ##
99
+ # A foreign-function interface (FFI) to `libraptor` 1.4.x.
100
+ #
101
+ # @see http://librdf.org/raptor/libraptor.html
102
+ module V1_4
103
+ ##
104
+ # @param [Hash{Symbol => Object}] options
105
+ # @option (options) [Boolean] :init (true)
106
+ # @yield [world]
107
+ # @yieldparam [FFI::Pointer] world
108
+ # @return [void]
109
+ def self.with_world(options = {}, &block)
110
+ options = {:init => true}.merge(options)
111
+ begin
112
+ raptor_init if options[:init]
113
+ raptor_world_open(world = raptor_new_world)
114
+ block.call(world)
115
+ ensure
116
+ raptor_free_world(world) if world
117
+ raptor_finish if options[:init]
118
+ end
119
+ end
120
+
121
+ ##
122
+ # @param [Hash{Symbol => Object}] options
123
+ # @option (options) [String, #to_s] :name (:rdfxml)
124
+ # @yield [parser]
125
+ # @yieldparam [FFI::Pointer] parser
126
+ # @return [void]
127
+ def self.with_parser(options = {}, &block)
128
+ begin
129
+ parser = raptor_new_parser((options[:name] || :rdfxml).to_s)
130
+ block.call(parser)
131
+ ensure
132
+ raptor_free_parser(parser) if parser
133
+ end
134
+ end
135
+
136
+ extend Base
137
+ extend ::FFI::Library
138
+ ffi_lib LIBRAPTOR
139
+
140
+ # TODO: Ideally this would be an enum, but the JRuby FFI (as of
141
+ # version 1.4.0) has problems with enums as part of structs:
142
+ # `Unknown field type: #<FFI::Enum> (ArgumentError)`
143
+ RAPTOR_IDENTIFIER_TYPE_RESOURCE = 1
144
+ RAPTOR_IDENTIFIER_TYPE_ANONYMOUS = 2
145
+ RAPTOR_IDENTIFIER_TYPE_LITERAL = 5
146
+
147
+ # @see http://librdf.org/raptor/api/raptor-section-triples.html
148
+ class Statement < ::FFI::Struct
149
+ layout :subject, :pointer,
150
+ :subject_type, :int,
151
+ :predicate, :pointer,
152
+ :predicate_type, :int,
153
+ :object, :pointer,
154
+ :object_type, :int,
155
+ :object_literal_datatype, :pointer,
156
+ :object_literal_language, :string
157
+
158
+ ##
159
+ # @return [RDF::Resource]
160
+ def subject
161
+ @subject ||= case self[:subject_type]
162
+ when RAPTOR_IDENTIFIER_TYPE_RESOURCE
163
+ RDF::URI.new(V1_4.raptor_uri_to_string(self[:subject]))
164
+ when RAPTOR_IDENTIFIER_TYPE_ANONYMOUS
165
+ RDF::Node.new(self[:subject].read_string)
166
+ end
167
+ end
168
+
169
+ ##
170
+ # @return [String]
171
+ def subject_as_string
172
+ V1_4.raptor_statement_part_as_string(
173
+ self[:subject],
174
+ self[:subject_type],
175
+ nil, nil)
176
+ end
177
+
178
+ ##
179
+ # @return [RDF::URI]
180
+ def predicate
181
+ @predicate ||= case self[:predicate_type]
182
+ when RAPTOR_IDENTIFIER_TYPE_RESOURCE
183
+ RDF::URI.new(V1_4.raptor_uri_to_string(self[:predicate]))
184
+ end
185
+ end
186
+
187
+ ##
188
+ # @return [String]
189
+ def predicate_as_string
190
+ V1_4.raptor_statement_part_as_string(
191
+ self[:predicate],
192
+ self[:predicate_type],
193
+ nil, nil)
194
+ end
195
+
196
+ ##
197
+ # @return [RDF::Value]
198
+ def object
199
+ @object ||= case self[:object_type]
200
+ when RAPTOR_IDENTIFIER_TYPE_RESOURCE
201
+ RDF::URI.new(V1_4.raptor_uri_to_string(self[:object]))
202
+ when RAPTOR_IDENTIFIER_TYPE_ANONYMOUS
203
+ RDF::Node.new(self[:object].read_string)
204
+ when RAPTOR_IDENTIFIER_TYPE_LITERAL
205
+ case
206
+ when self[:object_literal_language]
207
+ RDF::Literal.new(self[:object].read_string, :language => self[:object_literal_language])
208
+ when self[:object_literal_datatype] && !self[:object_literal_datatype].null?
209
+ RDF::Literal.new(self[:object].read_string, :datatype => V1_4.raptor_uri_to_string(self[:object_literal_datatype]))
210
+ else
211
+ RDF::Literal.new(self[:object].read_string)
212
+ end
213
+ end
214
+ end
215
+
216
+ ##
217
+ # @return [String]
218
+ def object_as_string
219
+ V1_4.raptor_statement_part_as_string(
220
+ self[:object],
221
+ self[:object_type],
222
+ self[:object_literal_datatype],
223
+ self[:object_literal_language])
224
+ end
225
+
226
+ ##
227
+ # @return [Array(RDF::Resource, RDF::URI, RDF::Value)]
228
+ def to_triple
229
+ [subject, predicate, object]
230
+ end
231
+
232
+ ##
233
+ # @return [Array(RDF::Resource, RDF::URI, RDF::Value, nil)]
234
+ def to_quad
235
+ [subject, predicate, object, nil]
236
+ end
237
+ end
238
+
239
+ # @see http://librdf.org/raptor/api/tutorial-initialising-finishing.html
240
+ attach_function :raptor_init, [], :void
241
+ attach_function :raptor_finish, [], :void
242
+
243
+ # @see http://librdf.org/raptor/api/raptor-section-world.html
244
+ define_pointer :raptor_world
245
+ attach_function :raptor_new_world, [], raptor_world
246
+ attach_function :raptor_world_open, [raptor_world], :int
247
+ attach_function :raptor_free_world, [raptor_world], :void
248
+
249
+ # @see http://librdf.org/raptor/api/raptor-section-uri.html
250
+ define_pointer :raptor_uri
251
+ attach_function :raptor_new_uri, [:string], raptor_uri
252
+ attach_function :raptor_uri_as_string, [raptor_uri], :string
253
+ attach_function :raptor_uri_to_string, [raptor_uri], :string
254
+ attach_function :raptor_uri_print, [raptor_uri, :pointer], :void
255
+ attach_function :raptor_free_uri, [raptor_uri], :void
256
+
257
+ # @see http://librdf.org/raptor/api/raptor-section-triples.html
258
+ define_pointer :raptor_statement
259
+ attach_function :raptor_statement_compare, [raptor_statement, raptor_statement], :int
260
+ attach_function :raptor_print_statement, [raptor_statement, :pointer], :void
261
+ attach_function :raptor_print_statement_as_ntriples, [:pointer, :pointer], :void
262
+ attach_function :raptor_statement_part_as_string, [:pointer, :int, raptor_uri, :string], :string
263
+
264
+ # @see http://librdf.org/raptor/api/raptor-section-parser.html
265
+ callback :raptor_statement_handler, [:pointer, raptor_statement], :void
266
+ define_pointer :raptor_parser
267
+ attach_function :raptor_new_parser, [:string], raptor_parser
268
+ attach_function :raptor_set_statement_handler, [raptor_parser, :pointer, :raptor_statement_handler], :void
269
+ attach_function :raptor_parse_file, [raptor_parser, raptor_uri, raptor_uri], :int
270
+ attach_function :raptor_parse_file_stream, [raptor_parser, :pointer, :string, raptor_uri], :int
271
+ attach_function :raptor_parse_uri, [raptor_parser, raptor_uri, raptor_uri], :int
272
+ attach_function :raptor_start_parse, [raptor_parser, :string], :int
273
+ attach_function :raptor_parse_chunk, [raptor_parser, :string, :size_t, :int], :int
274
+ attach_function :raptor_get_mime_type, [raptor_parser], :string
275
+ attach_function :raptor_set_parser_strict, [raptor_parser, :int], :void
276
+ attach_function :raptor_get_need_base_uri, [raptor_parser], :int
277
+ attach_function :raptor_parser_get_world, [raptor_parser], raptor_world
278
+ attach_function :raptor_free_parser, [raptor_parser], :void
279
+ end
280
+ end
281
+ end
@@ -1,7 +1,7 @@
1
1
  module RDF; module Raptor
2
2
  module VERSION
3
3
  MAJOR = 0
4
- MINOR = 1
4
+ MINOR = 2
5
5
  TINY = 0
6
6
  EXTRA = nil
7
7
 
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
7
+ - 2
8
8
  - 0
9
- version: 0.1.0
9
+ version: 0.2.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Arto Bendiken
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-04-02 00:00:00 +02:00
17
+ date: 2010-04-04 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -27,8 +27,8 @@ dependencies:
27
27
  segments:
28
28
  - 0
29
29
  - 1
30
- - 0
31
- version: 0.1.0
30
+ - 4
31
+ version: 0.1.4
32
32
  type: :development
33
33
  version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
@@ -69,8 +69,8 @@ dependencies:
69
69
  segments:
70
70
  - 0
71
71
  - 1
72
- - 3
73
- version: 0.1.3
72
+ - 4
73
+ version: 0.1.4
74
74
  type: :runtime
75
75
  version_requirements: *id004
76
76
  description: RDF.rb plugin for parsing/serializing RDF/XML and Turtle data using the Raptor RDF Parser library.
@@ -86,12 +86,11 @@ files:
86
86
  - README
87
87
  - UNLICENSE
88
88
  - VERSION
89
- - lib/rdf/raptor/format.rb
89
+ - lib/rdf/raptor/cli.rb
90
+ - lib/rdf/raptor/ffi.rb
90
91
  - lib/rdf/raptor/rdfxml.rb
91
- - lib/rdf/raptor/reader.rb
92
92
  - lib/rdf/raptor/turtle.rb
93
93
  - lib/rdf/raptor/version.rb
94
- - lib/rdf/raptor/writer.rb
95
94
  - lib/rdf/raptor.rb
96
95
  has_rdoc: false
97
96
  homepage: http://rdf.rubyforge.org/
@@ -119,7 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
119
118
  - 0
120
119
  version: "0"
121
120
  requirements:
122
- - Raptor (>= 1.4.21)
121
+ - Raptor (>= 1.4.16)
123
122
  rubyforge_project: rdf
124
123
  rubygems_version: 1.3.6
125
124
  signing_key:
@@ -1,20 +0,0 @@
1
- module RDF::Raptor
2
- ##
3
- # Format base class.
4
- class Format < RDF::Format
5
- ##
6
- # @overload rapper_format
7
- #
8
- # @overload rapper_format(format)
9
- # @param [Symbol] format
10
- #
11
- # @return [void]
12
- def self.rapper_format(format = nil)
13
- unless format
14
- @rapper_format
15
- else
16
- @rapper_format = format
17
- end
18
- end
19
- end
20
- end
@@ -1,38 +0,0 @@
1
- module RDF::Raptor
2
- ##
3
- # Reader base class.
4
- class Reader < RDF::Reader
5
- ##
6
- # @param [IO, File, RDF::URI, String] input
7
- # @param [Hash{Symbol => Object}] options
8
- # @yield [reader]
9
- # @yieldparam [RDF::Reader] reader
10
- def initialize(input = $stdin, options = {}, &block)
11
- raise RDF::ReaderError.new("`rapper` binary not found") unless RDF::Raptor.available?
12
-
13
- format = self.class.format.rapper_format
14
- case input
15
- when RDF::URI, %r(^(file|http|https|ftp)://)
16
- @command = "rapper -q -i #{format} -o ntriples #{input}"
17
- @rapper = IO.popen(@command, 'rb')
18
- when File
19
- @command = "rapper -q -i #{format} -o ntriples #{File.expand_path(input.path)}"
20
- @rapper = IO.popen(@command, 'rb')
21
- else # IO, String
22
- @command = "rapper -q -i #{format} -o ntriples file:///dev/stdin"
23
- @rapper = IO.popen(@command, 'rb+')
24
- @rapper.write(input.respond_to?(:read) ? input.read : input.to_s)
25
- @rapper.close_write
26
- end
27
- @reader = RDF::NTriples::Reader.new(@rapper, options, &block)
28
- end
29
-
30
- protected
31
-
32
- ##
33
- # @return [Array]
34
- def read_triple
35
- @reader.read_triple
36
- end
37
- end
38
- end
@@ -1,69 +0,0 @@
1
- module RDF::Raptor
2
- ##
3
- # Writer base class.
4
- class Writer < RDF::Writer
5
- ##
6
- # @param [IO, File] output
7
- # @param [Hash{Symbol => Object}] options
8
- # @yield [writer]
9
- # @yieldparam [RDF::Writer] writer
10
- def initialize(output = $stdout, options = {}, &block)
11
- raise RDF::WriterError.new("`rapper` binary not found") unless RDF::Raptor.available?
12
-
13
- format = self.class.format.rapper_format
14
- case output
15
- when File, IO
16
- @command = "rapper -q -i ntriples -o #{format} file:///dev/stdin"
17
- @command << " #{options[:base_uri]}" if options.has_key?(:base_uri)
18
- @rapper = IO.popen(@command, 'rb+')
19
- else
20
- raise ArgumentError.new("unsupported output type: #{output.inspect}")
21
- end
22
- @writer = RDF::NTriples::Writer.new(@rapper, options)
23
- super(output, options, &block)
24
- end
25
-
26
- protected
27
-
28
- ##
29
- # @return [void]
30
- def write_prologue
31
- super
32
- end
33
-
34
- ##
35
- # @param [RDF::Resource] subject
36
- # @param [RDF::URI] predicate
37
- # @param [RDF::Value] object
38
- # @return [void]
39
- def write_triple(subject, predicate, object)
40
- @writer.write_triple(subject, predicate, object)
41
- end
42
-
43
- ##
44
- # @return [void]
45
- def write_epilogue
46
- @rapper.close_write
47
- begin
48
- chunk_size = @options[:chunk_size] || 4096 # bytes
49
- loop do
50
- @output.write(@rapper.readpartial(chunk_size))
51
- end
52
- rescue EOFError => e
53
- # we're all done
54
- end
55
- end
56
-
57
- ##
58
- # @todo Remove this once RDF.rb 0.1.4 is released.
59
- # @private
60
- def self.format(klass = nil)
61
- if klass.nil?
62
- Format.each do |format|
63
- return format if format.writer == self
64
- end
65
- return nil # not found
66
- end
67
- end
68
- end
69
- end