rdf-rdfa 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.
@@ -1,23 +1,21 @@
1
- module RDFa
2
- module VERSION
3
- MAJOR = 0
4
- MINOR = 0
5
- TINY = 2
6
- EXTRA = nil
1
+ module RDF::RDFa::VERSION
2
+ MAJOR = 0
3
+ MINOR = 0
4
+ TINY = 3
5
+ EXTRA = nil
7
6
 
8
- STRING = [MAJOR, MINOR, TINY].join('.')
9
- STRING << "-#{EXTRA}" if EXTRA
7
+ STRING = [MAJOR, MINOR, TINY].join('.')
8
+ STRING << "-#{EXTRA}" if EXTRA
10
9
 
11
- ##
12
- # @return [String]
13
- def self.to_s() STRING end
10
+ ##
11
+ # @return [String]
12
+ def self.to_s() STRING end
14
13
 
15
- ##
16
- # @return [String]
17
- def self.to_str() STRING end
14
+ ##
15
+ # @return [String]
16
+ def self.to_str() STRING end
18
17
 
19
- ##
20
- # @return [Array(Integer, Integer, Integer)]
21
- def self.to_a() [MAJOR, MINOR, TINY] end
22
- end
18
+ ##
19
+ # @return [Array(Integer, Integer, Integer)]
20
+ def self.to_a() [MAJOR, MINOR, TINY] end
23
21
  end
@@ -3,5 +3,4 @@ module RDF
3
3
  class XHV < Vocabulary("http://www.w3.org/1999/xhtml/vocab#"); end
4
4
  class XML < Vocabulary("http://www.w3.org/XML/1998/namespace"); end
5
5
  class XSI < Vocabulary("http://www.w3.org/2001/XMLSchema-instance"); end
6
- class OWL < Vocabulary("http://www.w3.org/2002/07/owl#"); end
7
6
  end
data/rdf-rdfa.gemspec CHANGED
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rdf-rdfa}
8
- s.version = "0.0.2"
8
+ s.version = "0.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Gregg Kellogg", "Nicholas Humfrey"]
12
- s.date = %q{2010-06-03}
13
- s.description = %q{ RDF::RDFa is an RDFa parser for Ruby using the RDF.rb library suite.
11
+ s.authors = ["Gregg Kellogg"]
12
+ s.date = %q{2010-06-10}
13
+ s.description = %q{ RDF::RDFa is an RDFa reader for Ruby using the RDF.rb library suite.
14
14
  }
15
15
  s.email = %q{gregg@kellogg-assoc.com}
16
16
  s.extra_rdoc_files = [
@@ -19,23 +19,27 @@ Gem::Specification.new do |s|
19
19
  "README.rdoc"
20
20
  ]
21
21
  s.files = [
22
- "AUTHORS",
22
+ ".gitignore",
23
+ ".yardopts",
24
+ "AUTHORS",
25
+ "CONTRIBUTORS",
23
26
  "History.txt",
24
27
  "README.rdoc",
25
28
  "Rakefile",
26
29
  "VERSION",
30
+ "etc/basic.html",
27
31
  "etc/foaf.html",
28
32
  "etc/xhv.html",
29
33
  "example.rb",
30
34
  "lib/rdf/rdfa.rb",
31
35
  "lib/rdf/rdfa/format.rb",
32
36
  "lib/rdf/rdfa/reader.rb",
33
- "lib/rdf/rdfa/reader/exceptions.rb",
34
37
  "lib/rdf/rdfa/version.rb",
35
38
  "lib/rdf/rdfa/vocab.rb",
36
39
  "pkg/.gitignore",
37
40
  "rdf-rdfa.gemspec",
38
41
  "script/console",
42
+ "spec/matchers.rb",
39
43
  "spec/rdfa-triples/0001.nt",
40
44
  "spec/rdfa-triples/0006.nt",
41
45
  "spec/rdfa-triples/0007.nt",
@@ -157,9 +161,10 @@ Gem::Specification.new do |s|
157
161
  s.rdoc_options = ["--charset=UTF-8"]
158
162
  s.require_paths = ["lib"]
159
163
  s.rubygems_version = %q{1.3.7}
160
- s.summary = %q{RDFa parser for RDF.rb.}
164
+ s.summary = %q{RDFa reader for RDF.rb.}
161
165
  s.test_files = [
162
- "spec/rdfa_helper.rb",
166
+ "spec/matchers.rb",
167
+ "spec/rdfa_helper.rb",
163
168
  "spec/rdfa_reader_spec.rb",
164
169
  "spec/spec_helper.rb"
165
170
  ]
@@ -171,14 +176,12 @@ Gem::Specification.new do |s|
171
176
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
172
177
  s.add_runtime_dependency(%q<rdf>, [">= 0.1.6"])
173
178
  s.add_runtime_dependency(%q<nokogiri>, [">= 1.3.3"])
174
- s.add_runtime_dependency(%q<patron>, [">= 0.4.6"])
175
179
  s.add_development_dependency(%q<rspec>, [">= 0"])
176
180
  s.add_development_dependency(%q<rdf-spec>, [">= 0"])
177
181
  s.add_development_dependency(%q<activesupport>, [">= 2.3.0"])
178
182
  else
179
183
  s.add_dependency(%q<rdf>, [">= 0.1.6"])
180
184
  s.add_dependency(%q<nokogiri>, [">= 1.3.3"])
181
- s.add_dependency(%q<patron>, [">= 0.4.6"])
182
185
  s.add_dependency(%q<rspec>, [">= 0"])
183
186
  s.add_dependency(%q<rdf-spec>, [">= 0"])
184
187
  s.add_dependency(%q<activesupport>, [">= 2.3.0"])
@@ -186,7 +189,6 @@ Gem::Specification.new do |s|
186
189
  else
187
190
  s.add_dependency(%q<rdf>, [">= 0.1.6"])
188
191
  s.add_dependency(%q<nokogiri>, [">= 1.3.3"])
189
- s.add_dependency(%q<patron>, [">= 0.4.6"])
190
192
  s.add_dependency(%q<rspec>, [">= 0"])
191
193
  s.add_dependency(%q<rdf-spec>, [">= 0"])
192
194
  s.add_dependency(%q<activesupport>, [">= 2.3.0"])
data/spec/matchers.rb ADDED
@@ -0,0 +1,205 @@
1
+ module Matchers
2
+ class BeEquivalentGraph
3
+ Info = Struct.new(:about, :information, :trace, :compare, :inputDocument, :outputDocument)
4
+ def normalize(graph)
5
+ case @info.compare
6
+ when :array
7
+ array = case graph
8
+ when Graph, Parser
9
+ graph = graph.graph if graph.respond_to?(:graph)
10
+ anon = "a"
11
+ anon_ctx = {}
12
+ graph.triples.collect {|triple| triple.to_ntriples }.each do |t|
13
+ t.gsub(/_:nbn\d+[a-z]+N/, "_:").
14
+ gsub!(/_:bn\d+[a-z]+/) do |bn|
15
+ # Normalize anon BNodes
16
+ if anon_ctx[bn]
17
+ anon_ctx[bn]
18
+ else
19
+ anon_ctx[bn] = anon
20
+ anon = anon.succ
21
+ end
22
+ "_:#{anon_ctx[bn]}"
23
+ end
24
+ end.sort
25
+ when Array
26
+ graph.sort
27
+ else
28
+ graph.to_s.split("\n").
29
+ map {|t| t.gsub(/^\s*(.*)\s*$/, '\1')}.
30
+ reject {|t2| t2.match(/^\s*$/)}.
31
+ compact.
32
+ sort.
33
+ uniq
34
+ end
35
+
36
+ # Implement to_ntriples on array, to simplify logic later
37
+ def array.to_ntriples; self.join("\n") + "\n"; end
38
+ array
39
+ else
40
+ case graph
41
+ when Graph then graph
42
+ when Parser then graph.graph
43
+ when IO, StringIO
44
+ Parser.parse(graph, @info.about)
45
+ else
46
+ parser = Parser.new(:struct => true)
47
+ fmt = parser.detect_format(graph.to_s)
48
+ parser.parse(graph.to_s, @info.about, :type => fmt)
49
+ end
50
+ end
51
+ end
52
+
53
+ def initialize(expected, info)
54
+ @info = if info.respond_to?(:about)
55
+ info
56
+ elsif info.is_a?(Hash)
57
+ identifier = info[:identifier] || expected.is_a?(Graph) ? expected.identifier : info[:about]
58
+ Info.new(identifier, info[:information] || "", info[:trace], info[:compare])
59
+ else
60
+ Info.new(expected.is_a?(Graph) ? expected.identifier : info, info.to_s)
61
+ end
62
+ @expected = normalize(expected)
63
+ end
64
+
65
+ def matches?(actual)
66
+ @actual = normalize(actual)
67
+ @actual == @expected
68
+ end
69
+
70
+ def failure_message_for_should
71
+ info = @info.respond_to?(:information) ? @info.information : ""
72
+ if @expected.is_a?(Graph) && @actual.size != @expected.size
73
+ "Graph entry count differs:\nexpected: #{@expected.size}\nactual: #{@actual.size}"
74
+ elsif @expected.is_a?(Array) && @actual.size != @expected.length
75
+ "Graph entry count differs:\nexpected: #{@expected.length}\nactual: #{@actual.size}"
76
+ elsif @expected.is_a?(Graph) && @actual.identifier != @expected.identifier
77
+ "Graph identifiers differ:\nexpected: #{@expected.identifier}\nactual: #{@actual.identifier}"
78
+ else
79
+ "Graph differs#{@info.compare == :array ? '(array)' : ''}\n"
80
+ end +
81
+ "\n#{info + "\n" unless info.empty?}" +
82
+ (@info.inputDocument ? "Input file: #{@info.inputDocument}\n" : "") +
83
+ (@info.outputDocument ? "Output file: #{@info.outputDocument}\n" : "") +
84
+ "Unsorted Expected:\n#{@expected.to_ntriples}" +
85
+ "Unsorted Results:\n#{@actual.to_ntriples}" +
86
+ (@info.trace ? "\nDebug:\n#{@info.trace}" : "")
87
+ end
88
+ def negative_failure_message
89
+ "Graphs do not differ\n"
90
+ end
91
+ end
92
+
93
+ def be_equivalent_graph(expected, info = nil)
94
+ BeEquivalentGraph.new(expected, info)
95
+ end
96
+
97
+ # Run expected SPARQL query against actual
98
+ if $redland_enabled
99
+ class PassQuery
100
+ def initialize(expected, info)
101
+ @expected = expected
102
+ @query = Redland::Query.new(expected)
103
+ @info = info
104
+ end
105
+ def matches?(actual)
106
+ @actual = actual
107
+ @expected_results = @info.respond_to?(:expectedResults) ? @info.expectedResults : true
108
+ model = Redland::Model.new
109
+ ntriples_parser = Redland::Parser.ntriples
110
+ ntriples_parser.parse_string_into_model(model, actual.to_ntriples, "http://www.w3.org/2006/07/SWD/RDFa/testsuite/xhtml1-testcases/")
111
+
112
+ @results = @query.execute(model)
113
+ #puts "Redland query results: #{@results.inspect}"
114
+ if @expected_results
115
+ @results.is_boolean? && @results.get_boolean?
116
+ else
117
+ @results.nil? || @results.is_boolean? && !@results.get_boolean?
118
+ end
119
+ end
120
+ def failure_message_for_should
121
+ info = @info.respond_to?(:information) ? @info.information : ""
122
+ "#{info + "\n" unless info.empty?}" +
123
+ if @results.nil?
124
+ "Query failed to return results"
125
+ elsif !@results.is_boolean?
126
+ "Query returned non-boolean results"
127
+ elsif @expected_results
128
+ "Query returned false"
129
+ else
130
+ "Query returned true (expected false)"
131
+ end +
132
+ "\n#{@expected}" +
133
+ "\n#{@info.input}" +
134
+ "\nResults:\n#{@actual.to_ntriples}" +
135
+ "\nDebug:\n#{@info.trace}"
136
+ end
137
+ end
138
+
139
+ def pass_query(expected, info = "")
140
+ PassQuery.new(expected, info)
141
+ end
142
+ else
143
+ def pass_query(expect, info = ""); false; end
144
+ end
145
+
146
+ class BeValidXML
147
+ def initialize(info)
148
+ @info = info
149
+ end
150
+ def matches?(actual)
151
+ @actual = actual
152
+ @doc = Nokogiri::XML.parse(actual)
153
+ @results = @doc.validate
154
+ @results.nil?
155
+ rescue
156
+ false
157
+ end
158
+ def failure_message_for_should
159
+ "#{@info + "\n" unless @info.empty?}" +
160
+ if @doc.nil?
161
+ "did not parse"
162
+ else
163
+ "\n#{@results}" +
164
+ "\nParsed:\n#{@doc}"
165
+ end +
166
+ "\nActual:\n#{@actual}"
167
+ end
168
+ end
169
+
170
+ def be_valid_xml(info = "")
171
+ BeValidXML.new(info)
172
+ end
173
+
174
+ class BeEquivalentXML
175
+ def initialize(expected, info)
176
+ @expected = expected
177
+ @info = info
178
+ end
179
+
180
+ def matches?(actual)
181
+ @actual = actual
182
+
183
+ a = @actual.index("<") == 0 ? @actual : "<foo>#{@actual}</foo>"
184
+ e = @expected.index("<") == 0 ? @expected : "<foo>#{@expected}</foo>"
185
+ a_hash = ActiveSupport::XmlMini.parse(a)
186
+ e_hash = ActiveSupport::XmlMini.parse(e)
187
+ a_hash == e_hash
188
+ rescue
189
+ puts $!
190
+ @fault = $!.message
191
+ false
192
+ end
193
+
194
+ def failure_message_for_should
195
+ "#{@info + "\n" unless @info.empty?}" +
196
+ "Fault: #{@fault + "\n" if @fault}" +
197
+ "Expected:#{@expected}\n" +
198
+ "Actual:#{@actual}"
199
+ end
200
+ end
201
+
202
+ def be_equivalent_xml(expected, info = "")
203
+ BeEquivalentXML.new(expected, info)
204
+ end
205
+ end
@@ -16,158 +16,467 @@ describe RDF::RDFa::Format do
16
16
  end
17
17
 
18
18
  describe "RDF::RDFa::Reader" do
19
- it "should be discoverable" do
20
- readers = [
21
- RDF::Reader.for(:rdfa),
22
- RDF::Reader.for("etc/foaf.html"),
23
- RDF::Reader.for(:file_name => "etc/foaf.html"),
24
- RDF::Reader.for(:file_extension => "html"),
25
- RDF::Reader.for(:file_extension => "xhtml"),
26
- RDF::Reader.for(:content_type => "text/html"),
27
- RDF::Reader.for(:content_type => "application/xhtml+xml"),
28
- ]
29
- readers.each { |reader| reader.should == RDF::RDFa::Reader }
19
+ context "discovery" do
20
+ {
21
+ "html" => RDF::Reader.for(:rdfa),
22
+ "etc/foaf.html" => RDF::Reader.for("etc/foaf.html"),
23
+ "foaf.html" => RDF::Reader.for(:file_name => "foaf.html"),
24
+ ".html" => RDF::Reader.for(:file_extension => "html"),
25
+ "application/xhtml+xml" => RDF::Reader.for(:content_type => "application/xhtml+xml"),
26
+ }.each_pair do |label, format|
27
+ it "should discover '#{label}'" do
28
+ format.should == RDF::RDFa::Reader
29
+ end
30
+ end
31
+ end
32
+
33
+ context :interface do
34
+ before(:each) do
35
+ @sampledoc = <<-EOF;
36
+ <?xml version="1.0" encoding="UTF-8"?>
37
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
38
+ <html xmlns="http://www.w3.org/1999/xhtml"
39
+ xmlns:dc="http://purl.org/dc/elements/1.1/">
40
+ <head>
41
+ <title>Test 0001</title>
42
+ </head>
43
+ <body>
44
+ <p>This photo was taken by <span class="author" about="photo1.jpg" property="dc:creator">Mark Birbeck</span>.</p>
45
+ </body>
46
+ </html>
47
+ EOF
48
+ end
49
+
50
+ it "should yield reader" do
51
+ inner = mock("inner")
52
+ inner.should_receive(:called).with(RDF::RDFa::Reader)
53
+ RDF::RDFa::Reader.new(@sampledoc) do |reader|
54
+ inner.called(reader.class)
55
+ end
56
+ end
57
+
58
+ it "should return reader" do
59
+ RDF::RDFa::Reader.new(@sampledoc).should be_a(RDF::RDFa::Reader)
60
+ end
61
+
62
+ it "should yield statements" do
63
+ inner = mock("inner")
64
+ inner.should_receive(:called).with(RDF::Statement)
65
+ RDF::RDFa::Reader.new(@sampledoc).each_statement do |statement|
66
+ inner.called(statement.class)
67
+ end
68
+ end
69
+
70
+ it "should yield triples" do
71
+ inner = mock("inner")
72
+ inner.should_receive(:called).with(RDF::URI, RDF::URI, RDF::Literal)
73
+ RDF::RDFa::Reader.new(@sampledoc).each_triple do |subject, predicate, object|
74
+ inner.called(subject.class, predicate.class, object.class)
75
+ end
76
+ end
77
+ end
78
+
79
+ context "paring a simple doc" do
80
+ before :each do
81
+ sampledoc = <<-EOF;
82
+ <?xml version="1.0" encoding="UTF-8"?>
83
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
84
+ <html xmlns="http://www.w3.org/1999/xhtml"
85
+ xmlns:dc="http://purl.org/dc/elements/1.1/">
86
+ <head>
87
+ <title>Test 0001</title>
88
+ </head>
89
+ <body>
90
+ <p>This photo was taken by <span class="author" about="photo1.jpg" property="dc:creator">Mark Birbeck</span>.</p>
91
+ </body>
92
+ </html>
93
+ EOF
94
+
95
+ @graph = parse(sampledoc, :base_uri => "http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/0001.xhtml", :strict => true)
96
+ @statement = @graph.statements.first
97
+ end
98
+
99
+ it "should return 1 triple" do
100
+ @graph.size.should == 1
101
+ end
102
+
103
+ it "should have a subject with an expanded URI" do
104
+ @statement.subject.should == RDF::URI('http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/photo1.jpg')
105
+ end
106
+
107
+ it "should have a predicate of dc:creator" do
108
+ @statement.predicate.should == RDF::DC11.creator
109
+ end
110
+
111
+ it "should have an object of type literal and value 'Mark Birkbeck'" do
112
+ @statement.object.should == RDF::Literal("Mark Birbeck")
113
+ end
114
+ end
115
+
116
+ context "parsing a simple doc without a base URI" do
117
+ before :each do
118
+ sampledoc = <<-EOF;
119
+ <?xml version="1.0" encoding="UTF-8"?>
120
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
121
+ <html xmlns="http://www.w3.org/1999/xhtml"
122
+ xmlns:dc="http://purl.org/dc/elements/1.1/">
123
+ <body>
124
+ <p>This photo was taken by <span class="author" about="_:photo" property="dc:creator">Mark Birbeck</span>.</p>
125
+ </body>
126
+ </html>
127
+ EOF
128
+
129
+ @graph = parse(sampledoc, :strict => true)
130
+ @statement = @graph.statements.first
131
+ end
132
+
133
+ it "should return 1 triple" do
134
+ @graph.size.should == 1
135
+ end
136
+
137
+ it "should have a Blank Node named 'photo' as the subject of the triple" do
138
+ @statement.subject.should == RDF::Node('photo')
139
+ end
140
+
141
+ it "should have a predicate of dc:creator" do
142
+ @statement.predicate.should == RDF::DC11.creator
143
+ end
144
+
145
+ it "should have an object of type literal and value 'Mark Birkbeck'" do
146
+ @statement.object.should == RDF::Literal("Mark Birbeck")
147
+ end
148
+ end
149
+
150
+ context "parsing a document containing an XML Literal" do
151
+ before :each do
152
+ sampledoc = <<-EOF
153
+ <?xml version="1.0" encoding="UTF-8"?>
154
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
155
+ <html xmlns="http://www.w3.org/1999/xhtml"
156
+ xmlns:dc="http://purl.org/dc/elements/1.1/">
157
+ <head>
158
+ <title>Test 0011</title>
159
+ </head>
160
+ <body>
161
+ <div about="">
162
+ Author: <span property="dc:creator">Albert Einstein</span>
163
+ <h2 property="dc:title">E = mc<sup>2</sup>: The Most Urgent Problem of Our Time</h2>
164
+ </div>
165
+ </body>
166
+ </html>
167
+ EOF
168
+
169
+ @graph = parse(sampledoc, :base_uri => "http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/0011.xhtml", :strict => true)
170
+ end
171
+
172
+ it "should return 2 triples" do
173
+ @graph.size.should == 2
174
+ end
175
+
176
+ it "should have a triple for the dc:creator of the document" do
177
+ @graph.should have_triple([
178
+ RDF::URI('http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/0011.xhtml'),
179
+ RDF::DC11.creator,
180
+ "Albert Einstein"
181
+ ])
182
+ end
183
+
184
+ it "should have an XML Literal for the dc:title of the document" do
185
+ @graph.should have_triple([
186
+ RDF::URI('http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/0011.xhtml'),
187
+ RDF::DC11.title,
188
+ RDF::Literal("E = mc<sup>2</sup>: The Most Urgent Problem of Our Time", :datatype => RDF.XMLLiteral)
189
+ ])
190
+ end
191
+ end
192
+
193
+ context "parsing a document containing sereral bnodes" do
194
+ before :each do
195
+ sampledoc = <<-EOF
196
+ <?xml version="1.0" encoding="UTF-8"?>
197
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
198
+ <html xmlns="http://www.w3.org/1999/xhtml" version="XHTML+RDFa 1.0"
199
+ xmlns:foaf="http://xmlns.com/foaf/0.1/">
200
+ <head>
201
+ <title>Test 0017</title>
202
+ </head>
203
+ <body>
204
+ <p>
205
+ <span about="[_:a]" property="foaf:name">Manu Sporny</span>
206
+ <span about="[_:a]" rel="foaf:knows" resource="[_:b]">knows</span>
207
+ <span about="[_:b]" property="foaf:name">Ralph Swick</span>.
208
+ </p>
209
+ </body>
210
+ </html>
211
+ EOF
212
+
213
+ @graph = parse(sampledoc, :base_uri => "http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/0017.xhtml", :strict => true)
214
+ end
215
+
216
+ it "should return 3 triples" do
217
+ @graph.size.should == 3
218
+ end
219
+
220
+ it "should have a triple for the foaf:name of BNode A" do
221
+ @graph.should have_triple([
222
+ RDF::Node('a'),
223
+ RDF::FOAF.name,
224
+ "Manu Sporny"
225
+ ])
226
+ end
227
+
228
+ it "should have a triple for the foaf:name of BNode B" do
229
+ @graph.should have_triple([
230
+ RDF::Node('b'),
231
+ RDF::FOAF.name,
232
+ "Ralph Swick"
233
+ ])
234
+ end
235
+
236
+ it "should have a triple for BNode A knows BNode B" do
237
+ @graph.should have_triple([
238
+ RDF::Node('a'),
239
+ RDF::FOAF.knows,
240
+ RDF::Node('b'),
241
+ ])
242
+ end
30
243
  end
31
244
 
32
- it "should parse simple doc" do
33
- sampledoc = <<-EOF;
34
- <?xml version="1.0" encoding="UTF-8"?>
35
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
36
- <html xmlns="http://www.w3.org/1999/xhtml"
37
- xmlns:dc="http://purl.org/dc/elements/1.1/">
38
- <head>
39
- <title>Test 0001</title>
40
- </head>
41
- <body>
42
- <p>This photo was taken by <span class="author" about="photo1.jpg" property="dc:creator">Mark Birbeck</span>.</p>
43
- </body>
44
- </html>
45
- EOF
46
-
47
- reader = RDF::RDFa::Reader.new(sampledoc, :base_uri => "http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/0001.xhtml", :strict => true)
48
- reader.graph.size.should == 1
49
- reader.graph.should have_object("Mark Birbeck")
245
+ context "parsing a document that uses the typeof attribute" do
246
+ before :each do
247
+ sampledoc = <<-EOF
248
+ <?xml version="1.0" encoding="UTF-8"?>
249
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
250
+ <html xmlns="http://www.w3.org/1999/xhtml" version="XHTML+RDFa 1.0"
251
+ xmlns:foaf="http://xmlns.com/foaf/0.1/">
252
+ <head>
253
+ <title>Test 0049</title>
254
+ </head>
255
+ <body>
256
+ <div about="http://www.example.org/#me" typeof="foaf:Person">
257
+ <p property="foaf:name">John Doe</p>
258
+ </div>
259
+ </body>
260
+ </html>
261
+ EOF
262
+
263
+ @graph = parse(sampledoc, :base_uri => "http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/0049.xhtml", :strict => true)
264
+ end
265
+
266
+ it "should return 2 triples" do
267
+ @graph.size.should == 2
268
+ end
269
+
270
+ it "should have a triple stating that #me is of type foaf:Person" do
271
+ @graph.should have_triple([
272
+ RDF::URI('http://www.example.org/#me'),
273
+ RDF.type,
274
+ RDF::FOAF.Person
275
+ ])
276
+ end
277
+
278
+ it "should have a triple stating that #me has name 'John Doe'" do
279
+ @graph.should have_triple([
280
+ RDF::URI('http://www.example.org/#me'),
281
+ RDF::FOAF.name,
282
+ RDF::Literal("John Doe")
283
+ ])
284
+ end
50
285
  end
51
286
 
52
- it "should parse simple doc without a base URI" do
53
- sampledoc = <<-EOF;
54
- <?xml version="1.0" encoding="UTF-8"?>
55
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
56
- <html xmlns="http://www.w3.org/1999/xhtml"
57
- xmlns:dc="http://purl.org/dc/elements/1.1/">
58
- <body>
59
- <p>This photo was taken by <span class="author" about="_:photo" property="dc:creator">Mark Birbeck</span>.</p>
60
- </body>
61
- </html>
62
- EOF
63
-
64
- reader = RDF::RDFa::Reader.new(sampledoc, :strict => true)
65
- reader.graph.size.should == 1
66
- reader.graph.should have_triple([RDF::Node('photo'), RDF::DC11.creator, "Mark Birbeck"])
287
+ context "parsing a document with a <base> tag in the <head>" do
288
+ before :each do
289
+ sampledoc = <<-EOF
290
+ <?xml version="1.0" encoding="UTF-8"?>
291
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
292
+ <html xmlns="http://www.w3.org/1999/xhtml" version="XHTML+RDFa 1.0"
293
+ xmlns:dc="http://purl.org/dc/elements/1.1/">
294
+ <head>
295
+ <base href="http://www.example.org/"></base>
296
+ <title>Test 0072</title>
297
+ </head>
298
+ <body>
299
+ <p about="faq">
300
+ Learn more by reading the example.org
301
+ <span property="dc:title">Example FAQ</span>.
302
+ </p>
303
+ </body>
304
+ </html>
305
+ EOF
306
+
307
+ @graph = parse(sampledoc, :base_uri => "http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/0072.xhtml", :strict => true)
308
+ end
309
+
310
+ it "should return 1 triple" do
311
+ @graph.size.should == 1
312
+ end
313
+
314
+ it "should have the subject of the triple relative to the URI in base" do
315
+ @graph.should have_subject RDF::URI('http://www.example.org/faq')
316
+ end
67
317
  end
68
318
 
69
- it "should parse an XML Literal" do
70
- sampledoc = <<-EOF
71
- <?xml version="1.0" encoding="UTF-8"?>
72
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
73
- <html xmlns="http://www.w3.org/1999/xhtml"
74
- xmlns:dc="http://purl.org/dc/elements/1.1/">
75
- <head>
76
- <title>Test 0011</title>
77
- </head>
78
- <body>
79
- <div about="">
80
- Author: <span property="dc:creator">Albert Einstein</span>
81
- <h2 property="dc:title">E = mc<sup>2</sup>: The Most Urgent Problem of Our Time</h2>
82
- </div>
83
- </body>
84
- </html>
85
- EOF
86
-
87
- reader = RDF::RDFa::Reader.new(sampledoc, :base_uri => "http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/0011.xhtml", :strict => true)
88
- reader.graph.size.should == 2
89
-
90
- reader.graph.should have_object("Albert Einstein")
91
- reader.graph.should have_object(
92
- RDF::Literal("E = mc<sup>2</sup>: The Most Urgent Problem of Our Time",
93
- :datatype => RDF.XMLLiteral)
94
- )
319
+ context "parsing a document with a profile containing a prefix mapping" do
320
+ before :each do
321
+ sampledoc = <<-EOF
322
+ <?xml version="1.0" encoding="UTF-8"?>
323
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
324
+ <html xmlns="http://www.w3.org/1999/xhtml" version="XHTML+RDFa 1.0">
325
+ <head>
326
+ <title>Test</title>
327
+ <base href="http://example.org/"/>
328
+ </head>
329
+ <body profile="http://www.w3.org/2007/08/pyRdfa/profiles/basic">
330
+ <div about="#me">
331
+ <p>
332
+ <span property="foaf:name">Ivan Herman</span>
333
+ </p>
334
+ </div>
335
+ </body>
336
+ </html>
337
+ EOF
338
+
339
+ basic = File.open(File.join(File.dirname(__FILE__), "..", "etc", "basic.html"))
340
+ basic_graph = RDF::Graph.new
341
+ RDF::RDFa::Reader.new(basic,
342
+ :base_uri => "http://www.w3.org/2007/08/pyRdfa/profiles/basic").each do |statement|
343
+ basic_graph << statement
344
+ end
345
+
346
+ RDF::Graph.stub!(:load).with("http://www.w3.org/2007/08/pyRdfa/profiles/basic",
347
+ :base_uri => "http://www.w3.org/2007/08/pyRdfa/profiles/basic",
348
+ :format => :rdfa).and_return(basic_graph)
349
+
350
+ @graph = parse(sampledoc, :strict => true)
351
+ @statement = @graph.statements.first
352
+ end
353
+
354
+ it "should return 1 triple" do
355
+ @graph.size.should == 1
356
+ end
357
+
358
+ it "should have a subject of http://example.org/#me" do
359
+ @statement.subject.should == RDF::URI('http://example.org/#me')
360
+ end
361
+
362
+ it "should have a predicate of foaf:name" do
363
+ @statement.predicate.should == RDF::FOAF.name
364
+ end
365
+
366
+ it "should have an object with the literal 'Ivan Herman'" do
367
+ @statement.object.should == RDF::Literal('Ivan Herman')
368
+ end
95
369
  end
96
370
 
371
+ context "parsing a document with a profile containing a term mapping" do
372
+ before :each do
373
+ sampledoc = <<-EOF
374
+ <?xml version="1.0" encoding="UTF-8"?>
375
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
376
+ <html xmlns="http://www.w3.org/1999/xhtml" version="XHTML+RDFa 1.0">
377
+ <head>
378
+ <title>Test</title>
379
+ <base href="http://example.org/"/>
380
+ </head>
381
+ <body profile="http://www.w3.org/2007/08/pyRdfa/profiles/foaf">
382
+ <div about="#me">
383
+ <p>
384
+ <span property="name">Ivan Herman</span>
385
+ </p>
386
+ </div>
387
+ </body>
388
+ </html>
389
+ EOF
390
+
391
+ foaf = File.open(File.join(File.dirname(__FILE__), "..", "etc", "foaf.html"))
392
+ foaf_graph = RDF::Graph.new
393
+ RDF::RDFa::Reader.new(foaf,
394
+ :base_uri => "http://www.w3.org/2007/08/pyRdfa/profiles/foaf").each do |statement|
395
+ foaf_graph << statement
396
+ end
397
+
398
+ RDF::Graph.stub!(:load).with("http://www.w3.org/2007/08/pyRdfa/profiles/foaf",
399
+ :base_uri => "http://www.w3.org/2007/08/pyRdfa/profiles/foaf",
400
+ :format => :rdfa).and_return(foaf_graph)
401
+
402
+ @graph = parse(sampledoc, :strict => true)
403
+ @statement = @graph.statements.first
404
+ end
405
+
406
+ it "should return 1 triple" do
407
+ @graph.size.should == 1
408
+ end
409
+
410
+ it "should have a subject of http://example.org/#me" do
411
+ @statement.subject.should == RDF::URI('http://example.org/#me')
412
+ end
413
+
414
+ it "should have a predicate of foaf:name" do
415
+ @statement.predicate.should == RDF::FOAF.name
416
+ end
97
417
 
98
- it "should parse BNodes" do
99
- sampledoc = <<-EOF
100
- <?xml version="1.0" encoding="UTF-8"?>
101
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
102
- <html xmlns="http://www.w3.org/1999/xhtml"
103
- xmlns:foaf="http://xmlns.com/foaf/0.1/">
104
- <head>
105
- <title>Test 0017</title>
106
- </head>
107
- <body>
108
- <p>
109
- <span about="[_:a]" property="foaf:name">Manu Sporny</span>
110
- <span about="[_:a]" rel="foaf:knows" resource="[_:b]">knows</span>
111
- <span about="[_:b]" property="foaf:name">Ralph Swick</span>.
112
- </p>
113
- </body>
114
- </html>
115
- EOF
116
-
117
- reader = RDF::RDFa::Reader.new(sampledoc, :base_uri => "http://rdfa.digitalbazaar.com/test-suite/test-cases/xhtml1/0011.xhtml", :strict => true)
118
-
119
- reader.graph.size.should == 3
120
- reader.graph.should have_object("Ralph Swick")
121
- reader.graph.should have_object("Manu Sporny")
418
+ it "should have an object with the literal 'Ivan Herman'" do
419
+ @statement.object.should == RDF::Literal('Ivan Herman')
420
+ end
122
421
  end
123
-
124
- def self.test_cases(suite)
422
+
423
+ def self.test_cases(suite)
125
424
  [] #RdfaHelper::TestCase.test_cases(suite)
425
+ end
426
+
427
+ # W3C Test suite from http://www.w3.org/2006/07/SWD/RDFa/testsuite/
428
+ %w(xhtml html4 html5).each do |suite|
429
+ describe "w3c #{suite} testcases" do
430
+ describe "that are approved" do
431
+ test_cases(suite).each do |t|
432
+ puts t.inspect
433
+ next unless t.status == "approved"
434
+ #next unless t.name =~ /0140/
435
+ specify "test #{t.name}: #{t.title}#{", (negative test)" unless t.expectedResults}" do
436
+ #puts t.input
437
+ #puts t.results
438
+ begin
439
+ t.run_test do |rdfa_string, rdfa_parser|
440
+ rdfa_parser.parse(rdfa_string, t.informationResourceInput, :debug => [])
441
+ end
442
+ rescue SparqlException => e
443
+ pending(e.message) { raise }
444
+ end
445
+ end
446
+ end
447
+ end
448
+ describe "that are unreviewed" do
449
+ test_cases(suite).each do |t|
450
+ next unless t.status == "unreviewed"
451
+ #next unless t.name =~ /0185/
452
+ #puts t.inspect
453
+ specify "test #{t.name}: #{t.title}#{", (negative test)" unless t.expectedResults}" do
454
+ begin
455
+ t.run_test do |rdfa_string, rdfa_parser|
456
+ rdfa_parser.parse(rdfa_string, t.informationResourceInput, :debug => [])
457
+ end
458
+ rescue SparqlException => e
459
+ pending(e.message) { raise }
460
+ rescue Spec::Expectations::ExpectationNotMetError => e
461
+ if t.name =~ /01[789]\d/
462
+ raise
463
+ else
464
+ pending() { raise }
465
+ end
466
+ end
467
+ end
468
+ end
469
+ end
126
470
  end
471
+ end
127
472
 
128
- # W3C Test suite from http://www.w3.org/2006/07/SWD/RDFa/testsuite/
129
- %w(xhtml html4 html5).each do |suite|
130
- describe "w3c #{suite} testcases" do
131
- describe "that are approved" do
132
- test_cases(suite).each do |t|
133
- puts t.inspect
134
- next unless t.status == "approved"
135
- #next unless t.name =~ /0140/
136
- specify "test #{t.name}: #{t.title}#{", (negative test)" unless t.expectedResults}" do
137
- #puts t.input
138
- #puts t.results
139
- begin
140
- t.run_test do |rdfa_string, rdfa_parser|
141
- rdfa_parser.parse(rdfa_string, t.informationResourceInput, :debug => [])
142
- end
143
- rescue SparqlException => e
144
- pending(e.message) { raise }
145
- end
146
- end
147
- end
148
- end
149
- describe "that are unreviewed" do
150
- test_cases(suite).each do |t|
151
- next unless t.status == "unreviewed"
152
- #next unless t.name =~ /0185/
153
- #puts t.inspect
154
- specify "test #{t.name}: #{t.title}#{", (negative test)" unless t.expectedResults}" do
155
- begin
156
- t.run_test do |rdfa_string, rdfa_parser|
157
- rdfa_parser.parse(rdfa_string, t.informationResourceInput, :debug => [])
158
- end
159
- rescue SparqlException => e
160
- pending(e.message) { raise }
161
- rescue Spec::Expectations::ExpectationNotMetError => e
162
- if t.name =~ /01[789]\d/
163
- raise
164
- else
165
- pending() { raise }
166
- end
167
- end
168
- end
169
- end
170
- end
473
+ def parse(input, options)
474
+ @debug = []
475
+ graph = RDF::Graph.new
476
+ RDF::RDFa::Reader.new(input, options.merge(:debug => @debug)).each do |statement|
477
+ graph << statement
171
478
  end
479
+ graph
172
480
  end
173
- end
481
+
482
+ end