rdf-raptor 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README +49 -5
- data/VERSION +1 -1
- data/lib/rdf/raptor/rdfxml.rb +55 -0
- data/lib/rdf/raptor/reader.rb +8 -0
- data/lib/rdf/raptor/turtle.rb +55 -0
- data/lib/rdf/raptor/version.rb +2 -2
- data/lib/rdf/raptor/writer.rb +63 -1
- data/lib/rdf/raptor.rb +50 -0
- metadata +3 -3
data/README
CHANGED
@@ -12,25 +12,68 @@ Examples
|
|
12
12
|
|
13
13
|
require 'rdf/raptor'
|
14
14
|
|
15
|
-
###
|
15
|
+
### Ensuring Raptor is installed and obtaining the version number
|
16
16
|
|
17
|
-
RDF::
|
17
|
+
RDF::Raptor.available? #=> true
|
18
|
+
RDF::Raptor.version #=> "1.4.21"
|
19
|
+
|
20
|
+
### Parsing RDF statements from an RDF/XML file
|
21
|
+
|
22
|
+
RDF::Reader.open("http://datagraph.org/jhacker/foaf.rdf") do |reader|
|
18
23
|
reader.each_statement do |statement|
|
19
24
|
puts statement.inspect
|
20
25
|
end
|
21
26
|
end
|
22
27
|
|
23
|
-
### Parsing a Turtle file
|
28
|
+
### Parsing RDF statements from a Turtle file
|
24
29
|
|
25
|
-
RDF::Reader.open(
|
30
|
+
RDF::Reader.open("http://datagraph.org/jhacker/foaf.ttl") do |reader|
|
26
31
|
reader.each_statement do |statement|
|
27
32
|
puts statement.inspect
|
28
33
|
end
|
29
34
|
end
|
30
35
|
|
36
|
+
### Serializing RDF statements into an RDF/XML file
|
37
|
+
|
38
|
+
data = RDF::Repository.load("http://datagraph.org/jhacker/foaf.nt")
|
39
|
+
|
40
|
+
RDF::Writer.open("output.rdf") do |writer|
|
41
|
+
data.each_statement do |statement|
|
42
|
+
writer << statement
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
### Serializing RDF statements into a Turtle file
|
47
|
+
|
48
|
+
data = RDF::Repository.load("http://datagraph.org/jhacker/foaf.nt")
|
49
|
+
|
50
|
+
RDF::Writer.open("output.ttl") do |writer|
|
51
|
+
data.each_statement do |statement|
|
52
|
+
writer << statement
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
### Obtaining the RDF/XML format specification class
|
57
|
+
|
58
|
+
RDF::Format.for(:rdfxml) #=> RDF::Raptor::RDFXML::Format
|
59
|
+
RDF::Format.for("input.rdf")
|
60
|
+
RDF::Format.for(:file_name => "input.rdf")
|
61
|
+
RDF::Format.for(:file_extension => "rdf")
|
62
|
+
RDF::Format.for(:content_type => "application/rdf+xml")
|
63
|
+
|
64
|
+
### Obtaining the Turtle format specification class
|
65
|
+
|
66
|
+
RDF::Format.for(:turtle) #=> RDF::Raptor::Turtle::Format
|
67
|
+
RDF::Format.for("input.ttl")
|
68
|
+
RDF::Format.for(:file_name => "input.ttl")
|
69
|
+
RDF::Format.for(:file_extension => "ttl")
|
70
|
+
RDF::Format.for(:content_type => "text/turtle")
|
71
|
+
|
31
72
|
Documentation
|
32
73
|
-------------
|
33
74
|
|
75
|
+
<http://rdf.rubyforge.org/raptor/>
|
76
|
+
|
34
77
|
* {RDF::Raptor}
|
35
78
|
* {RDF::Raptor::RDFXML}
|
36
79
|
* {RDF::Raptor::Turtle}
|
@@ -39,7 +82,8 @@ Dependencies
|
|
39
82
|
------------
|
40
83
|
|
41
84
|
* [RDF.rb](http://rubygems.org/gems/rdf) (>= 0.1.3)
|
42
|
-
* [Raptor](http://librdf.org/raptor/) (>= 1.4.21)
|
85
|
+
* [Raptor](http://librdf.org/raptor/) (>= 1.4.21),
|
86
|
+
specifically the `rapper` binary
|
43
87
|
|
44
88
|
Installation
|
45
89
|
------------
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0
|
1
|
+
0.1.0
|
data/lib/rdf/raptor/rdfxml.rb
CHANGED
@@ -2,10 +2,35 @@ module RDF::Raptor
|
|
2
2
|
##
|
3
3
|
# RDF/XML support.
|
4
4
|
#
|
5
|
+
# @example Requiring the `RDF::Raptor` module
|
6
|
+
# require 'rdf/raptor'
|
7
|
+
#
|
8
|
+
# @example Parsing RDF statements from an RDF/XML file
|
9
|
+
# RDF::Reader.open("input.rdf") do |reader|
|
10
|
+
# reader.each_statement do |statement|
|
11
|
+
# puts statement.inspect
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# @example Serializing RDF statements into an RDF/XML file
|
16
|
+
# RDF::Writer.open("output.rdf") do |writer|
|
17
|
+
# graph.each_statement do |statement|
|
18
|
+
# writer << statement
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
5
22
|
# @see http://www.w3.org/TR/REC-rdf-syntax/
|
6
23
|
module RDFXML
|
7
24
|
##
|
8
25
|
# RDF/XML format specification.
|
26
|
+
#
|
27
|
+
# @example Obtaining an RDF/XML format class
|
28
|
+
# RDF::Format.for(:rdfxml) #=> RDF::Raptor::RDFXML::Format
|
29
|
+
# RDF::Format.for("input.rdf")
|
30
|
+
# RDF::Format.for(:file_name => "input.rdf")
|
31
|
+
# RDF::Format.for(:file_extension => "rdf")
|
32
|
+
# RDF::Format.for(:content_type => "application/rdf+xml")
|
33
|
+
#
|
9
34
|
class Format < RDF::Raptor::Format
|
10
35
|
content_type 'application/rdf+xml', :extension => :rdf
|
11
36
|
content_encoding 'utf-8'
|
@@ -17,12 +42,42 @@ module RDF::Raptor
|
|
17
42
|
|
18
43
|
##
|
19
44
|
# RDF/XML parser.
|
45
|
+
#
|
46
|
+
# @example Obtaining an RDF/XML reader class
|
47
|
+
# RDF::Reader.for(:rdfxml) #=> RDF::Raptor::RDFXML::Reader
|
48
|
+
# RDF::Reader.for("input.rdf")
|
49
|
+
# RDF::Reader.for(:file_name => "input.rdf")
|
50
|
+
# RDF::Reader.for(:file_extension => "rdf")
|
51
|
+
# RDF::Reader.for(:content_type => "application/rdf+xml")
|
52
|
+
#
|
53
|
+
# @example Parsing RDF statements from an RDF/XML file
|
54
|
+
# RDF::Reader.open("input.rdf") do |reader|
|
55
|
+
# reader.each_statement do |statement|
|
56
|
+
# puts statement.inspect
|
57
|
+
# end
|
58
|
+
# end
|
59
|
+
#
|
20
60
|
class Reader < RDF::Raptor::Reader
|
21
61
|
format RDF::Raptor::RDFXML::Format
|
22
62
|
end
|
23
63
|
|
24
64
|
##
|
25
65
|
# RDF/XML serializer.
|
66
|
+
#
|
67
|
+
# @example Obtaining an RDF/XML writer class
|
68
|
+
# RDF::Writer.for(:rdfxml) #=> RDF::Raptor::RDFXML::Writer
|
69
|
+
# RDF::Writer.for("output.rdf")
|
70
|
+
# RDF::Writer.for(:file_name => "output.rdf")
|
71
|
+
# RDF::Writer.for(:file_extension => "rdf")
|
72
|
+
# RDF::Writer.for(:content_type => "application/rdf+xml")
|
73
|
+
#
|
74
|
+
# @example Serializing RDF statements into an RDF/XML file
|
75
|
+
# RDF::Writer.open("output.rdf") do |writer|
|
76
|
+
# graph.each_statement do |statement|
|
77
|
+
# writer << statement
|
78
|
+
# end
|
79
|
+
# end
|
80
|
+
#
|
26
81
|
class Writer < RDF::Raptor::Writer
|
27
82
|
format RDF::Raptor::RDFXML::Format
|
28
83
|
end
|
data/lib/rdf/raptor/reader.rb
CHANGED
@@ -3,7 +3,13 @@ module RDF::Raptor
|
|
3
3
|
# Reader base class.
|
4
4
|
class Reader < RDF::Reader
|
5
5
|
##
|
6
|
+
# @param [IO, File, RDF::URI, String] input
|
7
|
+
# @param [Hash{Symbol => Object}] options
|
8
|
+
# @yield [reader]
|
9
|
+
# @yieldparam [RDF::Reader] reader
|
6
10
|
def initialize(input = $stdin, options = {}, &block)
|
11
|
+
raise RDF::ReaderError.new("`rapper` binary not found") unless RDF::Raptor.available?
|
12
|
+
|
7
13
|
format = self.class.format.rapper_format
|
8
14
|
case input
|
9
15
|
when RDF::URI, %r(^(file|http|https|ftp)://)
|
@@ -21,6 +27,8 @@ module RDF::Raptor
|
|
21
27
|
@reader = RDF::NTriples::Reader.new(@rapper, options, &block)
|
22
28
|
end
|
23
29
|
|
30
|
+
protected
|
31
|
+
|
24
32
|
##
|
25
33
|
# @return [Array]
|
26
34
|
def read_triple
|
data/lib/rdf/raptor/turtle.rb
CHANGED
@@ -2,10 +2,35 @@ module RDF::Raptor
|
|
2
2
|
##
|
3
3
|
# Turtle support.
|
4
4
|
#
|
5
|
+
# @example Requiring the `RDF::Raptor` module
|
6
|
+
# require 'rdf/raptor'
|
7
|
+
#
|
8
|
+
# @example Parsing RDF statements from a Turtle file
|
9
|
+
# RDF::Reader.open("input.ttl") do |reader|
|
10
|
+
# reader.each_statement do |statement|
|
11
|
+
# puts statement.inspect
|
12
|
+
# end
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# @example Serializing RDF statements into a Turtle file
|
16
|
+
# RDF::Writer.open("output.ttl") do |writer|
|
17
|
+
# graph.each_statement do |statement|
|
18
|
+
# writer << statement
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
#
|
5
22
|
# @see http://www.w3.org/TeamSubmission/turtle/
|
6
23
|
module Turtle
|
7
24
|
##
|
8
25
|
# Turtle format specification.
|
26
|
+
#
|
27
|
+
# @example Obtaining a Turtle format class
|
28
|
+
# RDF::Format.for(:turtle) #=> RDF::Raptor::Turtle::Format
|
29
|
+
# RDF::Format.for("input.ttl")
|
30
|
+
# RDF::Format.for(:file_name => "input.ttl")
|
31
|
+
# RDF::Format.for(:file_extension => "ttl")
|
32
|
+
# RDF::Format.for(:content_type => "text/turtle")
|
33
|
+
#
|
9
34
|
class Format < RDF::Raptor::Format
|
10
35
|
content_type 'text/turtle', :extension => :ttl
|
11
36
|
content_encoding 'utf-8'
|
@@ -17,12 +42,42 @@ module RDF::Raptor
|
|
17
42
|
|
18
43
|
##
|
19
44
|
# Turtle parser.
|
45
|
+
#
|
46
|
+
# @example Obtaining a Turtle reader class
|
47
|
+
# RDF::Reader.for(:turtle) #=> RDF::Raptor::Turtle::Reader
|
48
|
+
# RDF::Reader.for("input.ttl")
|
49
|
+
# RDF::Reader.for(:file_name => "input.ttl")
|
50
|
+
# RDF::Reader.for(:file_extension => "ttl")
|
51
|
+
# RDF::Reader.for(:content_type => "text/turtle")
|
52
|
+
#
|
53
|
+
# @example Parsing RDF statements from a Turtle file
|
54
|
+
# RDF::Reader.open("input.ttl") do |reader|
|
55
|
+
# reader.each_statement do |statement|
|
56
|
+
# puts statement.inspect
|
57
|
+
# end
|
58
|
+
# end
|
59
|
+
#
|
20
60
|
class Reader < RDF::Raptor::Reader
|
21
61
|
format RDF::Raptor::Turtle::Format
|
22
62
|
end
|
23
63
|
|
24
64
|
##
|
25
65
|
# Turtle serializer.
|
66
|
+
#
|
67
|
+
# @example Obtaining a Turtle writer class
|
68
|
+
# RDF::Writer.for(:turtle) #=> RDF::Raptor::Turtle::Writer
|
69
|
+
# RDF::Writer.for("output.ttl")
|
70
|
+
# RDF::Writer.for(:file_name => "output.ttl")
|
71
|
+
# RDF::Writer.for(:file_extension => "ttl")
|
72
|
+
# RDF::Writer.for(:content_type => "text/turtle")
|
73
|
+
#
|
74
|
+
# @example Serializing RDF statements into a Turtle file
|
75
|
+
# RDF::Writer.open("output.ttl") do |writer|
|
76
|
+
# graph.each_statement do |statement|
|
77
|
+
# writer << statement
|
78
|
+
# end
|
79
|
+
# end
|
80
|
+
#
|
26
81
|
class Writer < RDF::Raptor::Writer
|
27
82
|
format RDF::Raptor::Turtle::Format
|
28
83
|
end
|
data/lib/rdf/raptor/version.rb
CHANGED
data/lib/rdf/raptor/writer.rb
CHANGED
@@ -2,6 +2,68 @@ module RDF::Raptor
|
|
2
2
|
##
|
3
3
|
# Writer base class.
|
4
4
|
class Writer < RDF::Writer
|
5
|
-
|
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
|
6
68
|
end
|
7
69
|
end
|
data/lib/rdf/raptor.rb
CHANGED
@@ -4,9 +4,34 @@ module RDF
|
|
4
4
|
##
|
5
5
|
# **`RDF::Raptor`** is a Raptor RDF Parser wrapper for RDF.rb.
|
6
6
|
#
|
7
|
+
# * {RDF::Raptor::RDFXML} provides support for the standard
|
8
|
+
# machine-readable RDF/XML format.
|
9
|
+
# * {RDF::Raptor::Turtle} provides support for the popular
|
10
|
+
# human-readable Turtle format.
|
11
|
+
#
|
7
12
|
# @example Requiring the `RDF::Raptor` module
|
8
13
|
# require 'rdf/raptor'
|
9
14
|
#
|
15
|
+
# @example Checking whether Raptor is installed
|
16
|
+
# RDF::Raptor.available? #=> true
|
17
|
+
#
|
18
|
+
# @example Obtaining the Raptor version number
|
19
|
+
# RDF::Raptor.version #=> "1.4.21"
|
20
|
+
#
|
21
|
+
# @example Obtaining an RDF/XML format class
|
22
|
+
# RDF::Format.for(:rdfxml) #=> RDF::Raptor::RDFXML::Format
|
23
|
+
# RDF::Format.for("input.rdf")
|
24
|
+
# RDF::Format.for(:file_name => "input.rdf")
|
25
|
+
# RDF::Format.for(:file_extension => "rdf")
|
26
|
+
# RDF::Format.for(:content_type => "application/rdf+xml")
|
27
|
+
#
|
28
|
+
# @example Obtaining a Turtle format class
|
29
|
+
# RDF::Format.for(:turtle) #=> RDF::Raptor::Turtle::Format
|
30
|
+
# RDF::Format.for("input.ttl")
|
31
|
+
# RDF::Format.for(:file_name => "input.ttl")
|
32
|
+
# RDF::Format.for(:file_extension => "ttl")
|
33
|
+
# RDF::Format.for(:content_type => "text/turtle")
|
34
|
+
#
|
10
35
|
# @see http://rdf.rubyforge.org/
|
11
36
|
# @see http://librdf.org/raptor/
|
12
37
|
#
|
@@ -18,5 +43,30 @@ module RDF
|
|
18
43
|
require 'rdf/raptor/writer'
|
19
44
|
require 'rdf/raptor/rdfxml'
|
20
45
|
require 'rdf/raptor/turtle'
|
46
|
+
|
47
|
+
##
|
48
|
+
# Returns `true` if the `rapper` binary is available.
|
49
|
+
#
|
50
|
+
# @example
|
51
|
+
# RDF::Raptor.available? #=> true
|
52
|
+
#
|
53
|
+
# @return [Boolean]
|
54
|
+
def self.available?
|
55
|
+
!!version
|
56
|
+
end
|
57
|
+
|
58
|
+
##
|
59
|
+
# Returns the installed `rapper` version number, or `nil` if `rapper` is
|
60
|
+
# not available.
|
61
|
+
#
|
62
|
+
# @example
|
63
|
+
# RDF::Raptor.version #=> "1.4.21"
|
64
|
+
#
|
65
|
+
# @return [String]
|
66
|
+
def self.version
|
67
|
+
if `rapper --version 2>/dev/null` =~ /^(\d+)\.(\d+)\.(\d+)/
|
68
|
+
[$1, $2, $3].join('.')
|
69
|
+
end
|
70
|
+
end
|
21
71
|
end # module Raptor
|
22
72
|
end # module RDF
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
- 0
|
8
7
|
- 1
|
9
|
-
|
8
|
+
- 0
|
9
|
+
version: 0.1.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-
|
17
|
+
date: 2010-04-02 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|