rdf 0.1.4 → 0.1.5
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 +3 -1
- data/VERSION +1 -1
- data/lib/rdf.rb +57 -1
- data/lib/rdf/model/resource.rb +8 -0
- data/lib/rdf/ntriples/reader.rb +98 -31
- data/lib/rdf/version.rb +1 -1
- metadata +3 -4
- data/lib/rdf/spec.rb +0 -49
data/README
CHANGED
@@ -10,13 +10,15 @@ This is a pure-Ruby library for working with [Resource Description Framework
|
|
10
10
|
Features
|
11
11
|
--------
|
12
12
|
|
13
|
-
* 100% pure Ruby with minimal dependencies and no bloat
|
13
|
+
* 100% pure Ruby with minimal dependencies and no bloat.
|
14
14
|
* 100% free and unencumbered [public domain](http://unlicense.org/) software.
|
15
15
|
* Provides a clean, well-designed RDF object model and related APIs.
|
16
16
|
* Supports parsing and serializing N-Triples out of the box, with more
|
17
17
|
serialization format support available through add-on plugins.
|
18
18
|
* Plays nice with others: entirely contained in the `RDF` module, and does
|
19
19
|
not modify any of Ruby's core classes or standard library.
|
20
|
+
* Based entirely on Ruby's autoloading, meaning that you can generally make
|
21
|
+
use of any one part of the library without needing to load up the rest.
|
20
22
|
* Compatible with Ruby 1.8.x, Ruby 1.9.x, and JRuby (tested with JRuby 1.4).
|
21
23
|
|
22
24
|
Examples
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.5
|
data/lib/rdf.rb
CHANGED
@@ -56,7 +56,63 @@ module RDF
|
|
56
56
|
autoload :XSD, 'rdf/vocab/xsd'
|
57
57
|
|
58
58
|
##
|
59
|
-
#
|
59
|
+
# Alias for `RDF::Resource.new`.
|
60
|
+
#
|
61
|
+
# @return [RDF::Resource]
|
62
|
+
def self.Resource(*args, &block)
|
63
|
+
Resource.new(*args, &block)
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
# Alias for `RDF::Node.new`.
|
68
|
+
#
|
69
|
+
# @return [RDF::Node]
|
70
|
+
def self.Node(*args, &block)
|
71
|
+
Node.new(*args, &block)
|
72
|
+
end
|
73
|
+
|
74
|
+
##
|
75
|
+
# Alias for `RDF::URI.new`.
|
76
|
+
#
|
77
|
+
# @overload URI(uri)
|
78
|
+
# @param [URI, String, #to_s] uri
|
79
|
+
#
|
80
|
+
# @overload URI(options = {})
|
81
|
+
# @param [Hash{Symbol => Object} options
|
82
|
+
#
|
83
|
+
# @return [RDF::URI]
|
84
|
+
def self.URI(*args, &block)
|
85
|
+
URI.new(*args, &block)
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# Alias for `RDF::Literal.new`.
|
90
|
+
#
|
91
|
+
# @return [RDF::Literal]
|
92
|
+
def self.Literal(*args, &block)
|
93
|
+
Literal.new(*args, &block)
|
94
|
+
end
|
95
|
+
|
96
|
+
##
|
97
|
+
# Alias for `RDF::Graph.new`.
|
98
|
+
#
|
99
|
+
# @return [RDF::Graph]
|
100
|
+
def self.Graph(*args, &block)
|
101
|
+
Graph.new(*args, &block)
|
102
|
+
end
|
103
|
+
|
104
|
+
##
|
105
|
+
# Alias for `RDF::Statement.new`.
|
106
|
+
#
|
107
|
+
# @return [RDF::Statement]
|
108
|
+
def self.Statement(*args, &block)
|
109
|
+
Statement.new(*args, &block)
|
110
|
+
end
|
111
|
+
|
112
|
+
##
|
113
|
+
# Alias for `RDF::Vocabulary.create`.
|
114
|
+
#
|
115
|
+
# @param [String] uri
|
60
116
|
# @return [Class]
|
61
117
|
def self.Vocabulary(uri)
|
62
118
|
Vocabulary.create(uri)
|
data/lib/rdf/model/resource.rb
CHANGED
data/lib/rdf/ntriples/reader.rb
CHANGED
@@ -28,26 +28,106 @@ module RDF::NTriples
|
|
28
28
|
class Reader < RDF::Reader
|
29
29
|
format RDF::NTriples::Format
|
30
30
|
|
31
|
+
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar
|
32
|
+
COMMENT = /^#\s*(.*)$/.freeze
|
33
|
+
NODEID = /^_:([A-Za-z][A-Za-z0-9]*)/.freeze
|
34
|
+
URIREF = /^<([^>]+)>/.freeze
|
35
|
+
LITERAL_PLAIN = /^"((?:\\"|[^"])*)"/.freeze
|
36
|
+
LITERAL_WITH_LANGUAGE = /^"((?:\\"|[^"])*)"@([a-z]+[\-a-z0-9]*)/.freeze
|
37
|
+
LITERAL_WITH_DATATYPE = /^"((?:\\"|[^"])*)"\^\^<([^>]+)>/.freeze
|
38
|
+
LANGUAGE_TAG = /^@([a-z]+[\-a-z0-9]*)/.freeze
|
39
|
+
DATATYPE_URI = /^\^\^<([^>]+)>/.freeze
|
40
|
+
LITERAL = Regexp.union(LITERAL_WITH_LANGUAGE, LITERAL_WITH_DATATYPE, LITERAL_PLAIN).freeze
|
41
|
+
SUBJECT = Regexp.union(URIREF, NODEID).freeze
|
42
|
+
PREDICATE = Regexp.union(URIREF).freeze
|
43
|
+
OBJECT = Regexp.union(URIREF, NODEID, LITERAL).freeze
|
44
|
+
|
31
45
|
##
|
32
46
|
# Reconstructs an RDF value from its serialized N-Triples
|
33
47
|
# representation.
|
34
48
|
#
|
35
|
-
# @param [String]
|
49
|
+
# @param [String] input
|
36
50
|
# @return [RDF::Value]
|
37
|
-
def self.unserialize(
|
38
|
-
case
|
51
|
+
def self.unserialize(input)
|
52
|
+
case input
|
39
53
|
when nil then nil
|
40
|
-
else self.new(
|
54
|
+
else self.new(input).read_value
|
41
55
|
end
|
42
56
|
end
|
43
57
|
|
58
|
+
##
|
59
|
+
# @param [String] input
|
60
|
+
# @return [RDF::Resource]
|
61
|
+
def self.parse_subject(input)
|
62
|
+
parse_uri(input) || parse_node(input)
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# @param [String] input
|
67
|
+
# @return [RDF::URI]
|
68
|
+
def self.parse_predicate(input)
|
69
|
+
parse_uri(input)
|
70
|
+
end
|
71
|
+
|
72
|
+
##
|
73
|
+
# @param [String] input
|
74
|
+
# @return [RDF::Value]
|
75
|
+
def self.parse_object(input)
|
76
|
+
parse_uri(input) || parse_node(input) || parse_literal(input)
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# @param [String] input
|
81
|
+
# @return [RDF::Node]
|
82
|
+
def self.parse_node(input)
|
83
|
+
if input =~ NODEID
|
84
|
+
RDF::Node.new($1)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
##
|
89
|
+
# @param [String] input
|
90
|
+
# @return [RDF::URI]
|
91
|
+
def self.parse_uri(input)
|
92
|
+
if input =~ URIREF
|
93
|
+
RDF::URI.new($1)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
##
|
98
|
+
# @param [String] input
|
99
|
+
# @return [RDF::Literal]
|
100
|
+
def self.parse_literal(input)
|
101
|
+
case input
|
102
|
+
when LITERAL_WITH_LANGUAGE
|
103
|
+
RDF::Literal.new(unescape($1), :language => $2)
|
104
|
+
when LITERAL_WITH_DATATYPE
|
105
|
+
RDF::Literal.new(unescape($1), :datatype => $2)
|
106
|
+
when LITERAL_PLAIN
|
107
|
+
RDF::Literal.new(unescape($1))
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
##
|
112
|
+
# @param [String] string
|
113
|
+
# @return [String]
|
114
|
+
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
|
115
|
+
def self.unescape(string)
|
116
|
+
["\t", "\n", "\r", "\"", "\\"].each do |escape|
|
117
|
+
string.gsub!(escape.inspect[1...-1], escape)
|
118
|
+
end
|
119
|
+
string.gsub!(/\\u([0-9A-Fa-f]{4,4})/u) { [$1.hex].pack('U*') }
|
120
|
+
string.gsub!(/\\U([0-9A-Fa-f]{8,8})/u) { [$1.hex].pack('U*') }
|
121
|
+
string
|
122
|
+
end
|
123
|
+
|
44
124
|
##
|
45
125
|
# @return [RDF::Value]
|
46
126
|
def read_value
|
47
127
|
begin
|
48
128
|
read_statement
|
49
129
|
rescue RDF::ReaderError => e
|
50
|
-
read_uriref ||
|
130
|
+
read_uriref || read_node || read_literal
|
51
131
|
end
|
52
132
|
end
|
53
133
|
|
@@ -61,9 +141,9 @@ module RDF::NTriples
|
|
61
141
|
|
62
142
|
begin
|
63
143
|
unless blank? || read_comment
|
64
|
-
subject = read_uriref ||
|
144
|
+
subject = read_uriref || read_node || fail_subject
|
65
145
|
predicate = read_uriref || fail_predicate
|
66
|
-
object = read_uriref ||
|
146
|
+
object = read_uriref || read_node || read_literal || fail_object
|
67
147
|
return [subject, predicate, object]
|
68
148
|
end
|
69
149
|
rescue RDF::ReaderError => e
|
@@ -77,55 +157,42 @@ module RDF::NTriples
|
|
77
157
|
# @return [Boolean]
|
78
158
|
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar (comment)
|
79
159
|
def read_comment
|
80
|
-
match(
|
160
|
+
match(COMMENT)
|
81
161
|
end
|
82
162
|
|
83
163
|
##
|
84
|
-
# @return [URI]
|
164
|
+
# @return [RDF::URI]
|
85
165
|
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar (uriref)
|
86
166
|
def read_uriref
|
87
|
-
if uri = match(
|
167
|
+
if uri = match(URIREF)
|
88
168
|
RDF::URI.new(uri)
|
89
169
|
end
|
90
170
|
end
|
91
171
|
|
92
172
|
##
|
93
|
-
# @return [Node]
|
173
|
+
# @return [RDF::Node]
|
94
174
|
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar (nodeID)
|
95
|
-
def
|
96
|
-
if node_id = match(
|
175
|
+
def read_node
|
176
|
+
if node_id = match(NODEID)
|
97
177
|
@nodes[node_id] ||= RDF::Node.new(node_id)
|
98
178
|
end
|
99
179
|
end
|
100
180
|
|
101
181
|
##
|
102
|
-
# @return [Literal]
|
182
|
+
# @return [RDF::Literal]
|
103
183
|
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar (literal)
|
104
184
|
def read_literal
|
105
|
-
if literal = match(
|
106
|
-
literal =
|
185
|
+
if literal = match(LITERAL_PLAIN)
|
186
|
+
literal = self.class.unescape(literal)
|
107
187
|
|
108
|
-
if language = match(
|
188
|
+
if language = match(LANGUAGE_TAG)
|
109
189
|
RDF::Literal.new(literal, :language => language)
|
110
|
-
elsif datatype = match(/^(\^\^)/)
|
190
|
+
elsif datatype = match(/^(\^\^)/) # FIXME
|
111
191
|
RDF::Literal.new(literal, :datatype => read_uriref || fail_object)
|
112
192
|
else
|
113
193
|
RDF::Literal.new(literal) # plain string literal
|
114
194
|
end
|
115
195
|
end
|
116
196
|
end
|
117
|
-
|
118
|
-
##
|
119
|
-
# @param [String] string
|
120
|
-
# @return [String]
|
121
|
-
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
|
122
|
-
def unescaped(string)
|
123
|
-
["\t", "\n", "\r", "\"", "\\"].each do |escape|
|
124
|
-
string.gsub!(escape.inspect[1...-1], escape)
|
125
|
-
end
|
126
|
-
string.gsub!(/\\u([0-9A-Fa-f]{4,4})/u) { [$1.hex].pack('U*') }
|
127
|
-
string.gsub!(/\\U([0-9A-Fa-f]{8,8})/u) { [$1.hex].pack('U*') }
|
128
|
-
string
|
129
|
-
end
|
130
197
|
end
|
131
198
|
end
|
data/lib/rdf/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 5
|
9
|
+
version: 0.1.5
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Arto Bendiken
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-04-
|
18
|
+
date: 2010-04-06 00:00:00 +02:00
|
19
19
|
default_executable: rdf
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -117,7 +117,6 @@ files:
|
|
117
117
|
- lib/rdf/query.rb
|
118
118
|
- lib/rdf/reader.rb
|
119
119
|
- lib/rdf/repository.rb
|
120
|
-
- lib/rdf/spec.rb
|
121
120
|
- lib/rdf/version.rb
|
122
121
|
- lib/rdf/vocab/cc.rb
|
123
122
|
- lib/rdf/vocab/dc.rb
|
data/lib/rdf/spec.rb
DELETED
@@ -1,49 +0,0 @@
|
|
1
|
-
require 'rdf'
|
2
|
-
|
3
|
-
module RDF
|
4
|
-
##
|
5
|
-
# **`RDF::Spec`** provides RSpec extensions for RDF.rb.
|
6
|
-
#
|
7
|
-
# @example Requiring the `RDF::Spec` module
|
8
|
-
# require 'rdf/spec'
|
9
|
-
#
|
10
|
-
# @example Including the matchers in `spec/spec_helper.rb`
|
11
|
-
# require 'rdf/spec'
|
12
|
-
#
|
13
|
-
# Spec::Runner.configure do |config|
|
14
|
-
# config.include(RDF::Spec::Matchers)
|
15
|
-
# end
|
16
|
-
#
|
17
|
-
# @example Using the shared examples for `RDF::Enumerable`
|
18
|
-
# require 'rdf/spec/enumerable'
|
19
|
-
#
|
20
|
-
# describe RDF::Enumerable do
|
21
|
-
# before :each do
|
22
|
-
# @statements = RDF::NTriples::Reader.new(File.open("etc/doap.nt")).to_a
|
23
|
-
# @enumerable = @statements.dup.extend(RDF::Enumerable)
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# it_should_behave_like RDF_Enumerable
|
27
|
-
# end
|
28
|
-
#
|
29
|
-
# @example Using the shared examples for `RDF::Repository`
|
30
|
-
# require 'rdf/spec/repository'
|
31
|
-
#
|
32
|
-
# describe RDF::Repository do
|
33
|
-
# before :each do
|
34
|
-
# @repository = RDF::Repository.new
|
35
|
-
# end
|
36
|
-
#
|
37
|
-
# it_should_behave_like RDF_Repository
|
38
|
-
# end
|
39
|
-
#
|
40
|
-
# @see http://rdf.rubyforge.org/
|
41
|
-
# @see http://rspec.info/
|
42
|
-
#
|
43
|
-
# @author [Arto Bendiken](http://ar.to/)
|
44
|
-
# @author [Ben Lavender](http://bhuga.net/)
|
45
|
-
module Spec
|
46
|
-
autoload :Matchers, 'rdf/spec/matchers'
|
47
|
-
autoload :VERSION, 'rdf/spec/version'
|
48
|
-
end # module Spec
|
49
|
-
end # module RDF
|