rdf-rdfxml 0.0.2 → 0.0.3
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 +20 -0
- data/VERSION +1 -1
- data/lib/rdf/rdfxml/format.rb +2 -2
- data/lib/rdf/rdfxml/patches/graph_properties.rb +34 -0
- data/lib/rdf/rdfxml/patches/literal_hacks.rb +66 -0
- data/lib/rdf/rdfxml/patches/qname_hacks.rb +57 -0
- data/lib/rdf/rdfxml/patches/seq.rb +34 -0
- data/lib/rdf/rdfxml/patches/uri_hacks.rb +19 -0
- data/lib/rdf/rdfxml/reader.rb +10 -7
- data/lib/rdf/rdfxml/writer.rb +215 -114
- data/lib/rdf/rdfxml.rb +4 -0
- data/rdf-rdfxml.gemspec +17 -4
- data/spec/format_spec.rb +1 -1
- data/spec/graph_spec.rb +74 -0
- data/spec/literal_spec.rb +86 -0
- data/spec/rdf_escape_spec.rb +36 -0
- data/spec/rdf_helper.rb +2 -0
- data/spec/reader_spec.rb +7 -3
- data/spec/uri_spec.rb +61 -0
- data/spec/{xml_serializer_spec.rb → writer_spec.rb} +114 -110
- metadata +19 -6
data/spec/graph_spec.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
require 'rdf/rdfxml/patches/graph_properties'
|
3
|
+
require 'rdf/rdfxml/patches/seq'
|
4
|
+
|
5
|
+
class EX < RDF::Vocabulary("http://example.com/"); end
|
6
|
+
|
7
|
+
describe RDF::Graph do
|
8
|
+
describe "properties" do
|
9
|
+
subject { RDF::Graph.new }
|
10
|
+
|
11
|
+
it "should get asserted properties" do
|
12
|
+
subject << [EX.a, EX.b, EX.c]
|
13
|
+
subject.properties(EX.a).should be_a(Hash)
|
14
|
+
subject.properties(EX.a).size.should == 1
|
15
|
+
subject.properties(EX.a).has_key?(EX.b.to_s).should be_true
|
16
|
+
subject.properties(EX.a)[EX.b.to_s].should == [EX.c]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should get asserted properties with 2 properties" do
|
20
|
+
subject << [EX.a, EX.b, EX.c]
|
21
|
+
subject << [EX.a, EX.b, EX.d]
|
22
|
+
subject.properties(EX.a).should be_a(Hash)
|
23
|
+
subject.properties(EX.a).size.should == 1
|
24
|
+
subject.properties(EX.a).has_key?(EX.b.to_s).should be_true
|
25
|
+
subject.properties(EX.a)[EX.b.to_s].should include(EX.c, EX.d)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should get asserted properties with 3 properties" do
|
29
|
+
subject << [EX.a, EX.b, EX.c]
|
30
|
+
subject << [EX.a, EX.b, EX.d]
|
31
|
+
subject << [EX.a, EX.b, EX.e]
|
32
|
+
subject.properties(EX.a).should be_a(Hash)
|
33
|
+
subject.properties(EX.a).size.should == 1
|
34
|
+
subject.properties(EX.a).has_key?(EX.b.to_s).should be_true
|
35
|
+
subject.properties(EX.a)[EX.b.to_s].should include(EX.c, EX.d, EX.e)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should get asserted properties for a RDF::Node" do
|
39
|
+
bn = RDF::Node.new
|
40
|
+
subject << [bn, EX.b, EX.c]
|
41
|
+
subject.properties(bn).should be_a(Hash)
|
42
|
+
subject.properties(bn).size.should == 1
|
43
|
+
subject.properties(bn).has_key?(EX.b.to_s).should be_true
|
44
|
+
subject.properties(bn)[EX.b.to_s].should == [EX.c]
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should get asserted type with single type" do
|
48
|
+
subject << [EX.a, RDF.type, EX.Audio]
|
49
|
+
subject.properties(EX.a)[RDF.type.to_s].should == [EX.Audio]
|
50
|
+
subject.type_of(EX.a).should == [EX.Audio]
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should get nil with no type" do
|
54
|
+
subject << [EX.a, EX.b, EX.c]
|
55
|
+
subject.properties(EX.a)[RDF.type.to_s].should == nil
|
56
|
+
subject.type_of(EX.a).should == []
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe "rdf:_n sequences" do
|
61
|
+
subject {
|
62
|
+
g = RDF::Graph.new
|
63
|
+
g << [EX.Seq, RDF.type, RDF.Seq]
|
64
|
+
g << [EX.Seq, RDF._1, EX.john]
|
65
|
+
g << [EX.Seq, RDF._2, EX.jane]
|
66
|
+
g << [EX.Seq, RDF._3, EX.rick]
|
67
|
+
g
|
68
|
+
}
|
69
|
+
|
70
|
+
it "should return object list" do
|
71
|
+
subject.seq(EX.Seq).should == [EX.john, EX.jane, EX.rick]
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
|
3
|
+
describe RDF::Literal do
|
4
|
+
describe "XML Literal" do
|
5
|
+
describe "with no namespace" do
|
6
|
+
subject { RDF::Literal.new("foo <sup>bar</sup> baz!", :datatype => RDF["XMLLiteral"]) }
|
7
|
+
it "should indicate xmlliteral?" do
|
8
|
+
subject.xmlliteral?.should == true
|
9
|
+
end
|
10
|
+
|
11
|
+
it "should return normalized literal" do subject.value.should == "foo <sup>bar</sup> baz!" end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "with a namespace" do
|
15
|
+
subject {
|
16
|
+
RDF::Literal.xmlliteral("foo <sup>bar</sup> baz!", :namespaces => {"dc" => "http://purl.org/dc/terms/"})
|
17
|
+
}
|
18
|
+
|
19
|
+
it "should return normalized literal" do subject.value.should == "foo <sup>bar</sup> baz!" end
|
20
|
+
|
21
|
+
describe "and language" do
|
22
|
+
subject {
|
23
|
+
RDF::Literal.xmlliteral("foo <sup>bar</sup> baz!", :namespaces => {"dc" => "http://purl.org/dc/terms/"}, :language => "fr")
|
24
|
+
}
|
25
|
+
|
26
|
+
it "should return normalized literal" do subject.value.should == "foo <sup xml:lang=\"fr\">bar</sup> baz!" end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "and language with an existing language embedded" do
|
30
|
+
subject {
|
31
|
+
RDF::Literal.xmlliteral("foo <sup>bar</sup><sub xml:lang=\"en\">baz</sub>", :namespaces => {}, :language => "fr")
|
32
|
+
}
|
33
|
+
|
34
|
+
it "should return normalized literal" do subject.value.should == "foo <sup xml:lang=\"fr\">bar</sup><sub xml:lang=\"en\">baz</sub>" end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "and namespaced element" do
|
38
|
+
subject {
|
39
|
+
root = Nokogiri::XML.parse(%(
|
40
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
41
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
|
42
|
+
<html xmlns="http://www.w3.org/1999/xhtml"
|
43
|
+
xmlns:dc="http://purl.org/dc/terms/"
|
44
|
+
xmlns:ex="http://example.org/rdf/"
|
45
|
+
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
|
46
|
+
xmlns:svg="http://www.w3.org/2000/svg">
|
47
|
+
<head profile="http://www.w3.org/1999/xhtml/vocab http://www.w3.org/2005/10/profile">
|
48
|
+
<title>Test 0100</title>
|
49
|
+
</head>
|
50
|
+
<body>
|
51
|
+
<div about="http://www.example.org">
|
52
|
+
<h2 property="ex:example" datatype="rdf:XMLLiteral"><svg:svg/></h2>
|
53
|
+
</div>
|
54
|
+
</body>
|
55
|
+
</html>
|
56
|
+
), nil, nil, Nokogiri::XML::ParseOptions::DEFAULT_XML).root
|
57
|
+
content = root.css("h2").children
|
58
|
+
RDF::Literal.xmlliteral(content, :namespaces => {:svg => "http://www.w3.org/2000/svg", :dc => "http://purl.org/dc/terms"})
|
59
|
+
}
|
60
|
+
it "should add namespace" do subject.value.should == "<svg:svg xmlns:svg=\"http://www.w3.org/2000/svg\"></svg:svg>" end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe "and existing namespace definition" do
|
64
|
+
subject {
|
65
|
+
RDF::Literal.xmlliteral("<svg:svg xmlns:svg=\"http://www.w3.org/2000/svg\"/>", :namespaces => {"svg" => "http://www.w3.org/2000/svg"})
|
66
|
+
}
|
67
|
+
it "should add namespace" do subject.value.should == "<svg:svg xmlns:svg=\"http://www.w3.org/2000/svg\"></svg:svg>" end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
describe "with a default namespace" do
|
72
|
+
subject {
|
73
|
+
RDF::Literal.xmlliteral("foo <sup>bar</sup> baz!", :namespaces => {"" => "http://purl.org/dc/terms/"})
|
74
|
+
}
|
75
|
+
|
76
|
+
it "should return normalized literal foo" do subject.value.should == "foo <sup xmlns=\"http://purl.org/dc/terms/\">bar</sup> baz!" end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "with <br/>" do
|
80
|
+
subject {
|
81
|
+
RDF::Literal.xmlliteral("<br/>")
|
82
|
+
}
|
83
|
+
it "should add namespace" do subject.value.should == "<br></br>" end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
+
|
4
|
+
describe "String RDF encoding" do
|
5
|
+
{
|
6
|
+
"Gregg" => 'Gregg',
|
7
|
+
"Dürst" => 'D\u00FCrst',
|
8
|
+
"simple literal" => 'simple literal',
|
9
|
+
"backslash:\\" => 'backslash:\\\\',
|
10
|
+
"dquote:\"" => 'dquote:\\"',
|
11
|
+
"newline:\n" => 'newline:\\n',
|
12
|
+
"return:\r" => 'return:\\r',
|
13
|
+
"tab:\t" => 'tab:\\t',
|
14
|
+
}.each_pair do |raw, encoded|
|
15
|
+
specify "'#{raw}' should escape to '#{encoded}'" do
|
16
|
+
raw.rdf_escape.should == encoded
|
17
|
+
end
|
18
|
+
|
19
|
+
specify "'#{encoded}' should unescape to '#{raw}'" do
|
20
|
+
encoded.rdf_unescape.should == raw
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# 16-bit string encodings
|
25
|
+
{
|
26
|
+
"16-bit:\u{15678}another" => '16-bit:\\U00015678another',
|
27
|
+
}.each_pair do |raw, encoded|
|
28
|
+
specify "'#{raw}' should escape to '#{encoded}'" do
|
29
|
+
raw.rdf_escape.should == encoded
|
30
|
+
end
|
31
|
+
|
32
|
+
specify "'#{encoded}' should unescape to '#{raw}'" do
|
33
|
+
encoded.rdf_unescape.should == raw
|
34
|
+
end
|
35
|
+
end if defined?(::Encoding)
|
36
|
+
end
|
data/spec/rdf_helper.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
autoload :YAML, "yaml"
|
2
|
+
autoload :CGI, 'cgi'
|
2
3
|
|
3
4
|
RDFCORE_DIR = File.join(File.dirname(__FILE__), 'rdfcore')
|
4
5
|
RDFCORE_TEST = "http://www.w3.org/2000/10/rdf-tests/rdfcore/Manifest.rdf"
|
@@ -95,6 +96,7 @@ module RdfHelper
|
|
95
96
|
return unless output
|
96
97
|
|
97
98
|
output_graph = RDF::Graph.load(self.outputDocument)
|
99
|
+
puts "result: #{CGI.escapeHTML(graph.to_ntriples)}" if $DEBUG
|
98
100
|
graph.should Matchers::be_equivalent_graph(output_graph, self)
|
99
101
|
end
|
100
102
|
|
data/spec/reader_spec.rb
CHANGED
@@ -7,11 +7,11 @@ describe "RDF::RDFXML::Reader" do
|
|
7
7
|
context "discovery" do
|
8
8
|
{
|
9
9
|
"rdf" => RDF::Reader.for(:rdf),
|
10
|
-
"
|
10
|
+
"rdfxml" => RDF::Reader.for(:rdfxml),
|
11
11
|
"etc/foaf.xml" => RDF::Reader.for("etc/foaf.xml"),
|
12
12
|
"etc/foaf.rdf" => RDF::Reader.for("etc/foaf.rdf"),
|
13
13
|
"foaf.xml" => RDF::Reader.for(:file_name => "foaf.xml"),
|
14
|
-
"foaf.rdf" => RDF::Reader.for(:file_name => "foaf.
|
14
|
+
"foaf.rdf" => RDF::Reader.for(:file_name => "foaf.rdf"),
|
15
15
|
".xml" => RDF::Reader.for(:file_extension => "xml"),
|
16
16
|
".rdf" => RDF::Reader.for(:file_extension => "rdf"),
|
17
17
|
"application/xml" => RDF::Reader.for(:content_type => "application/xml"),
|
@@ -312,7 +312,11 @@ EOF
|
|
312
312
|
|
313
313
|
it "should parse xml literal test" do
|
314
314
|
file = File.join(@rdf_dir, "xml-literal-mixed.rdf")
|
315
|
-
|
315
|
+
begin
|
316
|
+
test_file(file, "http://www.example.com/books#book12345")
|
317
|
+
rescue
|
318
|
+
pending("NTriples support for multi-line quites") { raise }
|
319
|
+
end
|
316
320
|
end
|
317
321
|
end
|
318
322
|
|
data/spec/uri_spec.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
3
|
+
|
4
|
+
describe RDF::URI do
|
5
|
+
subject { RDF::URI.new("http://example.org")}
|
6
|
+
|
7
|
+
context "join" do
|
8
|
+
it "should append fragment to uri" do
|
9
|
+
subject.join("foo").to_s.should == "http://example.org/foo"
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should append another fragment" do
|
13
|
+
subject.join("foo#bar").to_s.should == "http://example.org/foo#bar"
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should append another URI" do
|
17
|
+
subject.join(RDF::URI.new("foo#bar")).to_s.should == "http://example.org/foo#bar"
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "normalization" do
|
21
|
+
{
|
22
|
+
%w(http://foo ) => "http://foo/",
|
23
|
+
%w(http://foo a) => "http://foo/a",
|
24
|
+
%w(http://foo /a) => "http://foo/a",
|
25
|
+
%w(http://foo #a) => "http://foo/#a",
|
26
|
+
|
27
|
+
%w(http://foo/ ) => "http://foo/",
|
28
|
+
%w(http://foo/ a) => "http://foo/a",
|
29
|
+
%w(http://foo/ /a) => "http://foo/a",
|
30
|
+
%w(http://foo/ #a) => "http://foo/#a",
|
31
|
+
|
32
|
+
%w(http://foo# ) => "http://foo/", # Special case for Addressable
|
33
|
+
%w(http://foo# a) => "http://foo/a",
|
34
|
+
%w(http://foo# /a) => "http://foo/a",
|
35
|
+
%w(http://foo# #a) => "http://foo/#a",
|
36
|
+
|
37
|
+
%w(http://foo/bar ) => "http://foo/bar",
|
38
|
+
%w(http://foo/bar a) => "http://foo/a",
|
39
|
+
%w(http://foo/bar /a) => "http://foo/a",
|
40
|
+
%w(http://foo/bar #a) => "http://foo/bar#a",
|
41
|
+
|
42
|
+
%w(http://foo/bar/ ) => "http://foo/bar/",
|
43
|
+
%w(http://foo/bar/ a) => "http://foo/bar/a",
|
44
|
+
%w(http://foo/bar/ /a) => "http://foo/a",
|
45
|
+
%w(http://foo/bar/ #a) => "http://foo/bar/#a",
|
46
|
+
|
47
|
+
%w(http://foo/bar# ) => "http://foo/bar",
|
48
|
+
%w(http://foo/bar# a) => "http://foo/a",
|
49
|
+
%w(http://foo/bar# /a) => "http://foo/a",
|
50
|
+
%w(http://foo/bar# #a) => "http://foo/bar#a",
|
51
|
+
|
52
|
+
%w(http://foo/bar# #D%C3%BCrst) => "http://foo/bar#D%C3%BCrst",
|
53
|
+
%w(http://foo/bar# #Dürst) => "http://foo/bar#D%C3%BCrst",
|
54
|
+
}.each_pair do |input, result|
|
55
|
+
it "should create <#{result}> from <#{input[0]}> and '#{input[1]}'" do
|
56
|
+
RDF::URI.new(input[0]).join(input[1].to_s).normalize.to_s.should == result
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|