rdf_context 0.5.3 → 0.5.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/History.txt +12 -0
- data/VERSION +1 -1
- data/bin/rdf_context +48 -3
- data/lib/rdf_context.rb +4 -1
- data/lib/rdf_context/exceptions.rb +1 -0
- data/lib/rdf_context/graph.rb +6 -1
- data/lib/rdf_context/namespace.rb +11 -15
- data/lib/rdf_context/parser.rb +1 -1
- data/lib/rdf_context/rdfaparser.rb +385 -131
- data/lib/rdf_context/serializer/recursive_serializer.rb +3 -7
- data/lib/rdf_context/serializer/xml_serializer.rb +21 -8
- data/lib/rdf_context/store/abstract_store.rb +0 -1
- data/lib/rdf_context/uriref.rb +25 -19
- data/spec/graph_spec.rb +16 -7
- data/spec/literal_spec.rb +10 -10
- data/spec/matchers.rb +1 -0
- data/spec/n3parser_spec.rb +2 -2
- data/spec/namespaces_spec.rb +16 -2
- data/spec/rdfa_helper.rb +8 -13
- data/spec/rdfa_parser_spec.rb +50 -6
- data/spec/spec_helper.rb +5 -0
- data/spec/turtle_serializer_spec.rb +6 -7
- data/spec/uriref_spec.rb +11 -27
- metadata +3 -5
- data/cmx.db +0 -0
- data/ep.nt +0 -194
@@ -66,16 +66,12 @@ module RdfContext
|
|
66
66
|
# Return a QName for the URI, or nil. Adds namespace of QName to defined namespaces
|
67
67
|
def get_qname(uri)
|
68
68
|
if uri.is_a?(URIRef)
|
69
|
-
|
70
|
-
qn = @graph.qname(uri)
|
71
|
-
rescue RdfException
|
72
|
-
return false # no namespace
|
73
|
-
end
|
69
|
+
qn = @graph.qname(uri)
|
74
70
|
# Local parts with . will mess up serialization
|
75
|
-
return false if qn.index('.')
|
71
|
+
return false if qn.nil? || qn.index('.')
|
76
72
|
|
77
73
|
add_namespace(uri.namespace)
|
78
|
-
|
74
|
+
qn
|
79
75
|
end
|
80
76
|
end
|
81
77
|
|
@@ -32,6 +32,8 @@ module RdfContext
|
|
32
32
|
|
33
33
|
doc = Nokogiri::XML::Document.new
|
34
34
|
|
35
|
+
puts "\nserialize: graph namespaces: #{@graph.nsbinding.inspect}" if $DEBUG
|
36
|
+
|
35
37
|
preprocess
|
36
38
|
|
37
39
|
predicates = @graph.predicates.uniq
|
@@ -40,13 +42,12 @@ module RdfContext
|
|
40
42
|
required_namespaces = {}
|
41
43
|
possible.each do |res|
|
42
44
|
next unless res.is_a?(URIRef)
|
43
|
-
if
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
required_namespaces[res.base] = true if !get_qname(res) && predicates.include?(res)
|
48
|
-
end
|
45
|
+
if res.namespace
|
46
|
+
add_namespace(res.namespace)
|
47
|
+
else
|
48
|
+
required_namespaces[res.base] = true
|
49
49
|
end
|
50
|
+
#puts "possible namespace for #{res}: #{res.namespace || %(<#{res.base}>)}"
|
50
51
|
end
|
51
52
|
add_namespace(RDF_NS)
|
52
53
|
add_namespace(XML_NS) if @base || @lang
|
@@ -64,7 +65,7 @@ module RdfContext
|
|
64
65
|
# Add bindings for predicates not already having bindings
|
65
66
|
tmp_ns = "ns0"
|
66
67
|
required_namespaces.keys.each do |uri|
|
67
|
-
puts "
|
68
|
+
puts "create namespace definition for #{uri}" if $DEBUG
|
68
69
|
add_namespace(Namespace.new(uri, tmp_ns))
|
69
70
|
tmp_ns = tmp_ns.succ
|
70
71
|
end
|
@@ -103,7 +104,8 @@ module RdfContext
|
|
103
104
|
if rdf_type.is_a?(URIRef)
|
104
105
|
element = get_qname(rdf_type)
|
105
106
|
properties[RDF_TYPE.to_s] = rest
|
106
|
-
|
107
|
+
type_ns = rdf_type.namespace rescue nil
|
108
|
+
if type_ns && @default_ns && type_ns.uri == @default_ns.uri
|
107
109
|
properties[RDF_TYPE.to_s] = rest
|
108
110
|
element = rdf_type.short_name
|
109
111
|
end
|
@@ -143,6 +145,7 @@ module RdfContext
|
|
143
145
|
# If _is_unique_ is true, this predicate may be able to be serialized as an attribute
|
144
146
|
def predicate(prop, object, node, is_unique)
|
145
147
|
qname = prop.to_qname(uri_binding)
|
148
|
+
raise RdfException, "No qname generated for <#{prop}>" unless qname
|
146
149
|
|
147
150
|
# See if we can serialize as attribute.
|
148
151
|
# * untyped attributes that aren't duplicated where xml:lang == @lang
|
@@ -233,5 +236,15 @@ module RdfContext
|
|
233
236
|
end
|
234
237
|
node.add_child(pred_node) if pred_node
|
235
238
|
end
|
239
|
+
|
240
|
+
def preprocess_triple(triple)
|
241
|
+
super
|
242
|
+
|
243
|
+
# Pre-fetch qnames, to fill namespaces
|
244
|
+
get_qname(triple.predicate)
|
245
|
+
get_qname(triple.object) if triple.predicate == RDF_TYPE
|
246
|
+
|
247
|
+
@references[triple.predicate] = ref_count(triple.predicate) + 1
|
248
|
+
end
|
236
249
|
end
|
237
250
|
end
|
data/lib/rdf_context/uriref.rb
CHANGED
@@ -47,14 +47,17 @@ module RdfContext
|
|
47
47
|
# short_name of URI for creating QNames.
|
48
48
|
# "#{base]{#short_name}}" == uri
|
49
49
|
def short_name
|
50
|
-
@short_name ||=
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
50
|
+
@short_name ||= begin
|
51
|
+
path = @uri.path.split("/")
|
52
|
+
if @namespace
|
53
|
+
self.to_s.sub(@namespace.uri.to_s, "")
|
54
|
+
elsif @uri.fragment
|
55
|
+
@uri.fragment
|
56
|
+
elsif path && path.length > 1 && path.last.class == String && path.last.length > 0 && path.last.index("/") != 0
|
57
|
+
path.last
|
58
|
+
else
|
59
|
+
false
|
60
|
+
end
|
58
61
|
end
|
59
62
|
end
|
60
63
|
|
@@ -63,7 +66,7 @@ module RdfContext
|
|
63
66
|
def base
|
64
67
|
@base ||= begin
|
65
68
|
uri_base = self.to_s
|
66
|
-
sn = short_name.to_s
|
69
|
+
sn = short_name ? short_name.to_s : ""
|
67
70
|
uri_base[0, uri_base.length - sn.length]
|
68
71
|
end
|
69
72
|
end
|
@@ -90,21 +93,24 @@ module RdfContext
|
|
90
93
|
alias_method :to_ntriples, :to_n3
|
91
94
|
|
92
95
|
# Output URI as QName using URI binding
|
93
|
-
def to_qname(uri_binding =
|
94
|
-
|
95
|
-
|
96
|
+
def to_qname(uri_binding = [])
|
97
|
+
namespaces = case uri_binding
|
98
|
+
when Hash then uri_binding.values
|
99
|
+
when Array then uri_binding
|
100
|
+
else []
|
101
|
+
end
|
102
|
+
ns = namespace(namespaces)
|
103
|
+
"#{ns.prefix}:#{short_name}" if ns
|
96
104
|
end
|
97
105
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
uri_binding[uri]
|
103
|
-
end
|
106
|
+
# Look at namespaces and find first that matches this URI, ordering by longest URI first
|
107
|
+
def namespace(namespaces = [])
|
108
|
+
@namespace ||=
|
109
|
+
namespaces.sort_by {|ns| -ns.uri.to_s.length}.detect {|ns| self.to_s.index(ns.uri.to_s) == 0}
|
104
110
|
end
|
105
111
|
|
106
112
|
def inspect
|
107
|
-
"#{self.class}[#{self.to_n3}]"
|
113
|
+
"#{self.class}[#{self.to_n3}, ns=#{namespace.inspect}]"
|
108
114
|
end
|
109
115
|
|
110
116
|
# Output URI as resource reference for RDF/XML
|
data/spec/graph_spec.rb
CHANGED
@@ -120,13 +120,13 @@ describe "Graphs" do
|
|
120
120
|
|
121
121
|
describe "with XML Literal objects" do
|
122
122
|
subject {
|
123
|
-
dc = Namespace.new("http://purl.org/dc/
|
123
|
+
dc = Namespace.new("http://purl.org/dc/terms/", "dc")
|
124
124
|
xhtml = Namespace.new("http://www.w3.org/1999/xhtml", "")
|
125
125
|
g = Graph.new(:store => ListStore.new)
|
126
126
|
g << Triple.new(
|
127
127
|
URIRef.new("http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/0011.xhtml"),
|
128
|
-
URIRef.new("http://purl.org/dc/
|
129
|
-
Literal.typed("E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/
|
128
|
+
URIRef.new("http://purl.org/dc/terms/title"),
|
129
|
+
Literal.typed("E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/terms/\">2</sup>: The Most Urgent Problem of Our Time",
|
130
130
|
"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",
|
131
131
|
g.nsbinding)
|
132
132
|
)
|
@@ -136,20 +136,20 @@ describe "Graphs" do
|
|
136
136
|
}
|
137
137
|
|
138
138
|
it "should output NTriple" do
|
139
|
-
nt = '<http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/0011.xhtml> <http://purl.org/dc/
|
139
|
+
nt = '<http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/0011.xhtml> <http://purl.org/dc/terms/title> "E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/terms/\">2</sup>: The Most Urgent Problem of Our Time"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral> .' + "\n"
|
140
140
|
subject.to_ntriples.should == nt
|
141
141
|
end
|
142
142
|
|
143
143
|
it "should output RDF/XML" do
|
144
144
|
rdfxml = <<-HERE
|
145
145
|
<?xml version="1.0" encoding="UTF-8"?>
|
146
|
-
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:dc=\"http://purl.org/dc/
|
146
|
+
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:xml=\"http://www.w3.org/XML/1998/namespace\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:dc=\"http://purl.org/dc/terms/\" xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:xhv=\"http://www.w3.org/1999/xhtml/vocab#\">
|
147
147
|
<rdf:Description rdf:about="http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/0011.xhtml">
|
148
|
-
<dc:title rdf:parseType="Literal">E = mc<sup xmlns="http://www.w3.org/1999/xhtml" xmlns:dc="http://purl.org/dc/
|
148
|
+
<dc:title rdf:parseType="Literal">E = mc<sup xmlns="http://www.w3.org/1999/xhtml" xmlns:dc="http://purl.org/dc/terms/">2>/sup>: The Most Urgent Problem of Our Time</dc:title>
|
149
149
|
</rdf:Description>
|
150
150
|
</rdf:RDF>
|
151
151
|
HERE
|
152
|
-
subject.to_rdfxml.should include("E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/
|
152
|
+
subject.to_rdfxml.should include("E = mc<sup xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:dc=\"http://purl.org/dc/terms/\">2</sup>: The Most Urgent Problem of Our Time")
|
153
153
|
end
|
154
154
|
end
|
155
155
|
|
@@ -328,6 +328,15 @@ HERE
|
|
328
328
|
subject.properties(@ex.a).has_key?(@ex.b.to_s).should be_true
|
329
329
|
subject.properties(@ex.a)[@ex.b.to_s].should include(@ex.c, @ex.d, @ex.e)
|
330
330
|
end
|
331
|
+
|
332
|
+
it "should get asserted properties for a BNode" do
|
333
|
+
bn = BNode.new
|
334
|
+
subject.add_triple(bn, @ex.b, @ex.c)
|
335
|
+
subject.properties(bn).should be_a(Hash)
|
336
|
+
subject.properties(bn).size.should == 1
|
337
|
+
subject.properties(bn).has_key?(@ex.b.to_s).should be_true
|
338
|
+
subject.properties(bn)[@ex.b.to_s].should == [@ex.c]
|
339
|
+
end
|
331
340
|
|
332
341
|
it "should get asserted type with single type" do
|
333
342
|
subject.add_triple(@ex.a, RDF_TYPE, @ex.Audio)
|
data/spec/literal_spec.rb
CHANGED
@@ -269,7 +269,7 @@ describe "Literals: " do
|
|
269
269
|
describe "with a namespace" do
|
270
270
|
subject {
|
271
271
|
Literal.typed("foo <sup>bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",
|
272
|
-
:namespaces => {"dc" => Namespace.new("http://purl.org/dc/
|
272
|
+
:namespaces => {"dc" => Namespace.new("http://purl.org/dc/terms/", "dc")})
|
273
273
|
}
|
274
274
|
|
275
275
|
describe "encodings" do
|
@@ -282,7 +282,7 @@ describe "Literals: " do
|
|
282
282
|
describe "and language" do
|
283
283
|
subject {
|
284
284
|
Literal.typed("foo <sup>bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",
|
285
|
-
:namespaces => {"dc" => Namespace.new("http://purl.org/dc/
|
285
|
+
:namespaces => {"dc" => Namespace.new("http://purl.org/dc/terms/", "dc")},
|
286
286
|
:language => "fr")
|
287
287
|
}
|
288
288
|
|
@@ -315,7 +315,7 @@ describe "Literals: " do
|
|
315
315
|
<?xml version="1.0" encoding="UTF-8"?>
|
316
316
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
|
317
317
|
<html xmlns="http://www.w3.org/1999/xhtml"
|
318
|
-
xmlns:dc="http://purl.org/dc/
|
318
|
+
xmlns:dc="http://purl.org/dc/terms/"
|
319
319
|
xmlns:ex="http://example.org/rdf/"
|
320
320
|
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
321
321
|
xmlns:svg="http://www.w3.org/2000/svg">
|
@@ -334,7 +334,7 @@ describe "Literals: " do
|
|
334
334
|
"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",
|
335
335
|
:namespaces => {
|
336
336
|
"svg" => Namespace.new("http://www.w3.org/2000/svg", "svg"),
|
337
|
-
"dc" => Namespace.new("http://purl.org/dc/
|
337
|
+
"dc" => Namespace.new("http://purl.org/dc/terms/", "dc")
|
338
338
|
})
|
339
339
|
}
|
340
340
|
it "should return xml_args" do subject.xml_args.should == ["<svg:svg xmlns:svg=\"http://www.w3.org/2000/svg\"></svg:svg>", {"rdf:parseType" => "Literal"}] end
|
@@ -353,23 +353,23 @@ describe "Literals: " do
|
|
353
353
|
describe "with a default namespace" do
|
354
354
|
subject {
|
355
355
|
Literal.typed("foo <sup>bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral",
|
356
|
-
:namespaces => {"" => Namespace.new("http://purl.org/dc/
|
356
|
+
:namespaces => {"" => Namespace.new("http://purl.org/dc/terms/", "")})
|
357
357
|
}
|
358
358
|
|
359
359
|
describe "encodings" do
|
360
|
-
it "should return n3" do subject.to_n3.should == "\"foo <sup xmlns=\\\"http://purl.org/dc/
|
360
|
+
it "should return n3" do subject.to_n3.should == "\"foo <sup xmlns=\\\"http://purl.org/dc/terms/\\\">bar</sup> baz!\"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral>" end
|
361
361
|
it "should return ntriples" do subject.to_ntriples.should == subject.to_n3 end
|
362
|
-
it "should return xml_args" do subject.xml_args.should == ["foo <sup xmlns=\"http://purl.org/dc/
|
363
|
-
it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral\">foo <sup xmlns=\"http://purl.org/dc/
|
362
|
+
it "should return xml_args" do subject.xml_args.should == ["foo <sup xmlns=\"http://purl.org/dc/terms/\">bar</sup> baz!", {"rdf:parseType" => "Literal"}] end
|
363
|
+
it "should return TriX" do subject.to_trix.should == "<typedLiteral datatype=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral\">foo <sup xmlns=\"http://purl.org/dc/terms/\">bar</sup> baz!</typedLiteral>" end
|
364
364
|
end
|
365
365
|
end
|
366
366
|
|
367
367
|
describe "with multiple namespaces" do
|
368
368
|
subject {
|
369
|
-
Literal.typed("foo <sup <sup xmlns:dc=\"http://purl.org/dc/
|
369
|
+
Literal.typed("foo <sup <sup xmlns:dc=\"http://purl.org/dc/terms/\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
|
370
370
|
}
|
371
371
|
it "should ignore namespace order" do
|
372
|
-
g = Literal.typed("foo <sup <sup xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:dc=\"http://purl.org/dc/
|
372
|
+
g = Literal.typed("foo <sup <sup xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:dc=\"http://purl.org/dc/terms/\">bar</sup> baz!", "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")
|
373
373
|
should == g
|
374
374
|
end
|
375
375
|
end
|
data/spec/matchers.rb
CHANGED
@@ -110,6 +110,7 @@ module Matchers
|
|
110
110
|
ntriples_parser.parse_string_into_model(model, actual.to_ntriples, "http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/")
|
111
111
|
|
112
112
|
@results = @query.execute(model)
|
113
|
+
#puts "Redland query results: #{@results.inspect}"
|
113
114
|
if @expected_results
|
114
115
|
@results.is_boolean? && @results.get_boolean?
|
115
116
|
else
|
data/spec/n3parser_spec.rb
CHANGED
@@ -72,7 +72,7 @@ describe "N3 parser" do
|
|
72
72
|
|
73
73
|
it "should parse multi-line literal" do
|
74
74
|
@parser.parse(%(
|
75
|
-
<http://www.example.com/books#book12345> <http://purl.org/dc/
|
75
|
+
<http://www.example.com/books#book12345> <http://purl.org/dc/terms/title> """
|
76
76
|
Foo
|
77
77
|
<html:b xmlns:html="http://www.w3.org/1999/xhtml" html:a="b">bar<rdf:Thing xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><a:b xmlns:a="foo:"></a:b>here<a:c xmlns:a="foo:"></a:c></rd
|
78
78
|
f:Thing></html:b>
|
@@ -261,7 +261,7 @@ describe "N3 parser" do
|
|
261
261
|
it "should map <> to document uri" do
|
262
262
|
n3doc = "@prefix : <> ."
|
263
263
|
@parser.parse(n3doc, "http://the.document.itself")
|
264
|
-
@parser.graph.nsbinding.should == {"" => Namespace.new("http://the.document.itself", ""
|
264
|
+
@parser.graph.nsbinding.should == {"" => Namespace.new("http://the.document.itself#", "")}
|
265
265
|
end
|
266
266
|
|
267
267
|
it "should use <> as a prefix and as a triple node" do
|
data/spec/namespaces_spec.rb
CHANGED
@@ -9,8 +9,8 @@ describe "Namespace" do
|
|
9
9
|
end
|
10
10
|
|
11
11
|
it "should create URIRef for frag" do
|
12
|
-
foaf_frag = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf"
|
13
|
-
foaf_frag.knows.to_s.should == "http://xmlns.com/foaf/0.1
|
12
|
+
foaf_frag = Namespace.new("http://xmlns.com/foaf/0.1/", "foaf")
|
13
|
+
foaf_frag.knows.to_s.should == "http://xmlns.com/foaf/0.1/knows"
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -50,6 +50,20 @@ describe "Namespace" do
|
|
50
50
|
subject.foo.should == "http://xmlns.com/foaf/0.1/foo"
|
51
51
|
end
|
52
52
|
|
53
|
+
describe "no URI normalization" do
|
54
|
+
specify {Namespace.new("http://foo", "foo").uri.should == "http://foo"}
|
55
|
+
specify {Namespace.new("http://foo#", "foo").uri.should == "http://foo#"}
|
56
|
+
specify {Namespace.new("http://foo/", "foo").uri.should == "http://foo/"}
|
57
|
+
specify {Namespace.new("xyz:foo", "foo").uri.should == "xyz:foo"}
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should not normalize URI" do
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "serialization" do
|
64
|
+
specify {subject.to_s.should == "foaf: http://xmlns.com/foaf/0.1/"}
|
65
|
+
end
|
66
|
+
|
53
67
|
describe '#+' do
|
54
68
|
it "should construct URI with +" do
|
55
69
|
(subject + "foo").class.should == URIRef
|
data/spec/rdfa_helper.rb
CHANGED
@@ -3,11 +3,6 @@ module RdfaHelper
|
|
3
3
|
class TestCase
|
4
4
|
include Matchers
|
5
5
|
|
6
|
-
TEST_DIR = File.join(File.dirname(__FILE__), 'rdfa-test-suite')
|
7
|
-
NT_DIR = File.join(File.dirname(__FILE__), 'rdfa-triples')
|
8
|
-
BASE_MANIFEST_URL = "http://rdfa.digitalbazaar.com/test-suite/"
|
9
|
-
BASE_TEST_CASE_URL = "#{BASE_MANIFEST_URL}test-cases/"
|
10
|
-
|
11
6
|
HTMLRE = Regexp.new('([0-9]{4,4})\.xhtml')
|
12
7
|
TCPATHRE = Regexp.compile('\$TCPATH')
|
13
8
|
|
@@ -34,7 +29,7 @@ module RdfaHelper
|
|
34
29
|
#next unless statement.subject.uri.to_s.match(/0001/)
|
35
30
|
unless self.about
|
36
31
|
self.about = Addressable::URI.parse(statement.subject.uri.to_s)
|
37
|
-
self.name = statement.subject.short_name
|
32
|
+
self.name = statement.subject.short_name || self.about
|
38
33
|
end
|
39
34
|
|
40
35
|
if statement.predicate.short_name == "expectedResults"
|
@@ -74,7 +69,7 @@ module RdfaHelper
|
|
74
69
|
end
|
75
70
|
|
76
71
|
def tcpath
|
77
|
-
|
72
|
+
RDFA_TEST_CASE_URL + (suite == "xhtml" ? "xhtml1" : suite)
|
78
73
|
end
|
79
74
|
|
80
75
|
# Read in file, and apply modifications to create a properly formatted HTML
|
@@ -82,7 +77,7 @@ module RdfaHelper
|
|
82
77
|
f = self.inputDocument
|
83
78
|
found_head = false
|
84
79
|
namespaces = ""
|
85
|
-
body = File.readlines(File.join(
|
80
|
+
body = File.readlines(File.join(RDFA_DIR, "tests", f)).map do |line|
|
86
81
|
found_head ||= line.match(/<head/)
|
87
82
|
if found_head
|
88
83
|
line.chop
|
@@ -118,14 +113,14 @@ module RdfaHelper
|
|
118
113
|
# Read in file, and apply modifications reference either .html or .xhtml
|
119
114
|
def results
|
120
115
|
f = self.name + ".sparql"
|
121
|
-
body = File.read(File.join(
|
116
|
+
body = File.read(File.join(RDFA_DIR, "tests", f)).gsub(TCPATHRE, tcpath)
|
122
117
|
|
123
118
|
suite == "xhtml" ? body : body.gsub(HTMLRE, '\1.html')
|
124
119
|
end
|
125
120
|
|
126
121
|
def triples
|
127
122
|
f = self.name + ".nt"
|
128
|
-
body = File.read(File.join(
|
123
|
+
body = File.read(File.join(RDFA_NT_DIR, f)).gsub(TCPATHRE, tcpath)
|
129
124
|
suite == "xhtml" ? body : body.gsub(HTMLRE, '\1.html')
|
130
125
|
end
|
131
126
|
|
@@ -151,7 +146,7 @@ module RdfaHelper
|
|
151
146
|
# Run SPARQL query
|
152
147
|
@parser.graph.should pass_query(query_string, self)
|
153
148
|
else
|
154
|
-
|
149
|
+
raise SparqlException, "Query skipped, Redland not installed"
|
155
150
|
end
|
156
151
|
|
157
152
|
@parser.graph.to_rdfxml.should be_valid_xml
|
@@ -166,9 +161,9 @@ module RdfaHelper
|
|
166
161
|
return @test_cases unless @test_cases.empty?
|
167
162
|
|
168
163
|
@suite = suite # Process the given test suite
|
169
|
-
@manifest_url = "#{
|
164
|
+
@manifest_url = "#{RDFA_MANIFEST_URL}#{suite}-manifest.rdf"
|
170
165
|
|
171
|
-
manifest_str = File.read(File.join(
|
166
|
+
manifest_str = File.read(File.join(RDFA_DIR, "#{suite}-manifest.rdf"))
|
172
167
|
parser = RdfXmlParser.new
|
173
168
|
|
174
169
|
begin
|
data/spec/rdfa_parser_spec.rb
CHANGED
@@ -1,13 +1,47 @@
|
|
1
1
|
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
2
|
|
3
3
|
require 'rdfa_helper'
|
4
|
+
require 'patron'
|
4
5
|
|
5
6
|
# Time to add your specs!
|
6
7
|
# http://rspec.info/
|
7
8
|
describe "RDFa parser" do
|
8
|
-
before(:each)
|
9
|
+
before(:each) do
|
10
|
+
@parser = RdfaParser.new
|
11
|
+
|
12
|
+
# Don't load external profiles when testing
|
13
|
+
basic_resp = mock("basic_resp")
|
14
|
+
basic_resp.stub(:status).and_return(200)
|
15
|
+
basic_resp.stub(:body).and_return(File.read(File.join(RDFA_DIR, "profiles", "basic.html")))
|
16
|
+
|
17
|
+
foaf_resp = mock("foaf_resp")
|
18
|
+
foaf_resp.stub(:status).and_return(200)
|
19
|
+
foaf_resp.stub(:body).and_return(File.read(File.join(RDFA_DIR, "profiles", "foaf.html")))
|
20
|
+
|
21
|
+
hcard_resp = mock("hcard_resp")
|
22
|
+
hcard_resp.stub(:status).and_return(200)
|
23
|
+
hcard_resp.stub(:body).and_return("HCARD")
|
24
|
+
|
25
|
+
profile_resp = mock("profile_resp")
|
26
|
+
profile_resp.stub(:status).and_return(200)
|
27
|
+
profile_resp.stub(:body).and_return("PROFILE")
|
28
|
+
|
29
|
+
xhv_resp = mock("xhv_resp")
|
30
|
+
xhv_resp.stub(:status).and_return(200)
|
31
|
+
xhv_resp.stub(:body).and_return(File.read(File.join(RDFA_DIR, "profiles", "xhv.html")))
|
32
|
+
|
33
|
+
sess = mock("session")
|
34
|
+
sess.stub(:base_url=)
|
35
|
+
sess.stub(:timeout=)
|
36
|
+
sess.stub(:get).with("http://www.w3.org/2007/08/pyRdfa/profiles/foaf").and_return(foaf_resp)
|
37
|
+
sess.stub(:get).with("http://www.w3.org/2007/08/pyRdfa/profiles/basic").and_return(basic_resp)
|
38
|
+
sess.stub(:get).with("http://www.w3.org/1999/xhtml/vocab").and_return(xhv_resp)
|
39
|
+
sess.stub(:get).with("http://microformats.org/profiles/hcard").and_return(hcard_resp)
|
40
|
+
sess.stub(:get).with("http://www.w3.org/2005/10/profile").and_return(profile_resp)
|
41
|
+
Patron::Session.stub!(:new).and_return(sess)
|
42
|
+
end
|
9
43
|
|
10
|
-
|
44
|
+
it "should parse simple doc" do
|
11
45
|
sampledoc = <<-EOF;
|
12
46
|
<?xml version="1.0" encoding="UTF-8"?>
|
13
47
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
|
@@ -119,8 +153,12 @@ describe "RDFa parser" do
|
|
119
153
|
specify "test #{t.name}: #{t.title}#{", (negative test)" unless t.expectedResults}" do
|
120
154
|
#puts t.input
|
121
155
|
#puts t.results
|
122
|
-
|
123
|
-
|
156
|
+
begin
|
157
|
+
t.run_test do |rdfa_string, rdfa_parser|
|
158
|
+
rdfa_parser.parse(rdfa_string, t.informationResourceInput, :debug => [])
|
159
|
+
end
|
160
|
+
rescue SparqlException => e
|
161
|
+
pending(e.message) { raise }
|
124
162
|
end
|
125
163
|
end
|
126
164
|
end
|
@@ -128,15 +166,21 @@ describe "RDFa parser" do
|
|
128
166
|
describe "that are unreviewed" do
|
129
167
|
test_cases(suite).each do |t|
|
130
168
|
next unless t.status == "unreviewed"
|
131
|
-
#next unless t.name =~ /
|
169
|
+
#next unless t.name =~ /0185/
|
132
170
|
#puts t.inspect
|
133
171
|
specify "test #{t.name}: #{t.title}#{", (negative test)" unless t.expectedResults}" do
|
134
172
|
begin
|
135
173
|
t.run_test do |rdfa_string, rdfa_parser|
|
136
174
|
rdfa_parser.parse(rdfa_string, t.informationResourceInput, :debug => [])
|
137
175
|
end
|
176
|
+
rescue SparqlException => e
|
177
|
+
pending(e.message) { raise }
|
138
178
|
rescue Spec::Expectations::ExpectationNotMetError => e
|
139
|
-
|
179
|
+
if t.name =~ /01[789]\d/
|
180
|
+
raise
|
181
|
+
else
|
182
|
+
pending() { raise }
|
183
|
+
end
|
140
184
|
end
|
141
185
|
end
|
142
186
|
end
|