rdf 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/rdf.rb +1 -0
- data/lib/rdf/nquads.rb +7 -0
- data/lib/rdf/ntriples/reader.rb +40 -14
- data/lib/rdf/ntriples/writer.rb +16 -0
- data/lib/rdf/reader.rb +12 -5
- data/lib/rdf/version.rb +1 -1
- metadata +4 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.3
|
data/lib/rdf.rb
CHANGED
data/lib/rdf/nquads.rb
ADDED
data/lib/rdf/ntriples/reader.rb
CHANGED
@@ -28,41 +28,67 @@ module RDF::NTriples
|
|
28
28
|
class Reader < RDF::Reader
|
29
29
|
format RDF::NTriples::Format
|
30
30
|
|
31
|
+
##
|
32
|
+
# Reconstructs an RDF value from its serialized N-Triples
|
33
|
+
# representation.
|
34
|
+
#
|
35
|
+
# @param [String] data
|
36
|
+
# @return [RDF::Value]
|
37
|
+
def self.unserialize(data)
|
38
|
+
self.new(data).read_value
|
39
|
+
end
|
40
|
+
|
41
|
+
##
|
42
|
+
# @return [RDF::Value]
|
43
|
+
def read_value
|
44
|
+
begin
|
45
|
+
read_statement
|
46
|
+
rescue RDF::ReaderError => e
|
47
|
+
read_uriref || read_bnode || read_literal
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
31
51
|
##
|
32
52
|
# @return [Array]
|
33
53
|
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar
|
34
54
|
def read_triple
|
35
55
|
loop do
|
36
56
|
readline.strip! # EOFError thrown on end of input
|
57
|
+
line = @line # for backtracking input in case of parse error
|
37
58
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
59
|
+
begin
|
60
|
+
unless blank? || read_comment
|
61
|
+
subject = read_uriref || read_bnode || fail_subject
|
62
|
+
predicate = read_uriref || fail_predicate
|
63
|
+
object = read_uriref || read_bnode || read_literal || fail_object
|
64
|
+
return [subject, predicate, object]
|
65
|
+
end
|
66
|
+
rescue RDF::ReaderError => e
|
67
|
+
@line = line # this allows #read_value to work
|
68
|
+
raise e
|
43
69
|
end
|
44
70
|
end
|
45
71
|
end
|
46
72
|
|
47
73
|
##
|
48
74
|
# @return [Boolean]
|
49
|
-
# @see
|
75
|
+
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar (comment)
|
50
76
|
def read_comment
|
51
77
|
match(/^#\s*(.*)$/)
|
52
78
|
end
|
53
79
|
|
54
80
|
##
|
55
|
-
# @return [URI
|
56
|
-
# @see
|
81
|
+
# @return [URI]
|
82
|
+
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar (uriref)
|
57
83
|
def read_uriref
|
58
84
|
if uri = match(/^<([^>]+)>/)
|
59
|
-
RDF::URI.
|
85
|
+
RDF::URI.new(uri)
|
60
86
|
end
|
61
87
|
end
|
62
88
|
|
63
89
|
##
|
64
|
-
# @return [Node
|
65
|
-
# @see
|
90
|
+
# @return [Node]
|
91
|
+
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar (nodeID)
|
66
92
|
def read_bnode
|
67
93
|
if node_id = match(/^_:([A-Za-z][A-Za-z0-9]*)/)
|
68
94
|
@nodes[node_id] ||= RDF::Node.new(node_id)
|
@@ -70,8 +96,8 @@ module RDF::NTriples
|
|
70
96
|
end
|
71
97
|
|
72
98
|
##
|
73
|
-
# @return [
|
74
|
-
# @see
|
99
|
+
# @return [Literal]
|
100
|
+
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_grammar (literal)
|
75
101
|
def read_literal
|
76
102
|
if literal = match(/^"((?:\\"|[^"])*)"/)
|
77
103
|
literal = unescaped(literal)
|
@@ -89,7 +115,7 @@ module RDF::NTriples
|
|
89
115
|
##
|
90
116
|
# @param [String] string
|
91
117
|
# @return [String]
|
92
|
-
# @see
|
118
|
+
# @see http://www.w3.org/TR/rdf-testcases/#ntrip_strings
|
93
119
|
def unescaped(string)
|
94
120
|
["\t", "\n", "\r", "\"", "\\"].each do |escape|
|
95
121
|
string.gsub!(escape.inspect[1...-1], escape)
|
data/lib/rdf/ntriples/writer.rb
CHANGED
@@ -27,6 +27,22 @@ module RDF::NTriples
|
|
27
27
|
class Writer < RDF::Writer
|
28
28
|
format RDF::NTriples::Format
|
29
29
|
|
30
|
+
##
|
31
|
+
# Returns the serialized N-Triples representation of the given RDF
|
32
|
+
# value.
|
33
|
+
#
|
34
|
+
# @param [RDF::Value] value
|
35
|
+
# @return [String]
|
36
|
+
def self.serialize(value)
|
37
|
+
writer = self.new
|
38
|
+
case value
|
39
|
+
when RDF::Statement
|
40
|
+
writer.format_statement(value) + "\n"
|
41
|
+
else
|
42
|
+
writer.format_value(value)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
30
46
|
##
|
31
47
|
# Outputs an N-Triples comment line.
|
32
48
|
#
|
data/lib/rdf/reader.rb
CHANGED
@@ -138,26 +138,33 @@ module RDF
|
|
138
138
|
##
|
139
139
|
# @yield [statement]
|
140
140
|
# @yieldparam [Statement]
|
141
|
-
# @return [
|
141
|
+
# @return [Enumerator]
|
142
142
|
def each_statement(&block)
|
143
|
-
|
144
|
-
|
143
|
+
begin
|
144
|
+
loop { block.call(read_statement) }
|
145
|
+
rescue EOFError => e
|
146
|
+
end
|
145
147
|
end
|
146
148
|
|
147
149
|
##
|
148
150
|
# @yield [triple]
|
149
151
|
# @yieldparam [Array(Value)]
|
150
|
-
# @return [
|
152
|
+
# @return [Enumerator]
|
151
153
|
def each_triple(&block)
|
152
154
|
begin
|
153
155
|
loop { block.call(*read_triple) }
|
154
156
|
rescue EOFError => e
|
155
157
|
end
|
156
|
-
self
|
157
158
|
end
|
158
159
|
|
159
160
|
protected
|
160
161
|
|
162
|
+
##
|
163
|
+
# @raise [NotImplementedError] unless implemented in subclass
|
164
|
+
def read_statement
|
165
|
+
Statement.new(*read_triple)
|
166
|
+
end
|
167
|
+
|
161
168
|
##
|
162
169
|
# @raise [NotImplementedError] unless implemented in subclass
|
163
170
|
def read_triple
|
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
|
+
- 3
|
9
|
+
version: 0.1.3
|
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-03-
|
18
|
+
date: 2010-03-30 00:00:00 +02:00
|
19
19
|
default_executable: rdf
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -106,6 +106,7 @@ files:
|
|
106
106
|
- lib/rdf/model/statement.rb
|
107
107
|
- lib/rdf/model/uri.rb
|
108
108
|
- lib/rdf/model/value.rb
|
109
|
+
- lib/rdf/nquads.rb
|
109
110
|
- lib/rdf/ntriples/format.rb
|
110
111
|
- lib/rdf/ntriples/reader.rb
|
111
112
|
- lib/rdf/ntriples/writer.rb
|