rdf-n3 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -5,7 +5,7 @@ require 'rdf/n3'
5
5
  require 'getoptlong'
6
6
 
7
7
  def parse(input, base)
8
- puts RDF::Writer.for($format.to_sym).buffer { |writer|
8
+ puts RDF::Writer.for($format.to_sym).buffer(:base_uri => base) { |writer|
9
9
  RDF::N3::Reader.new(input, :base_uri => base, :strict => true).each do |statement|
10
10
  writer << statement
11
11
  end
@@ -32,7 +32,7 @@ opts.each do |opt, arg|
32
32
  case opt
33
33
  when '--verbose' then $verbose = true
34
34
  when '--quiet' then $quiet = true
35
- when '--debug' then $DEBUG = true
35
+ when '--debug' then ::RDF::N3::debug = true
36
36
  when '--execute' then input = arg
37
37
  when '--format' then $format = arg
38
38
  when '--uri' then base_uri = arg
data/script/tc CHANGED
@@ -30,7 +30,7 @@ opts.each do |opt, arg|
30
30
  case opt
31
31
  when '--verbose' then $verbose = true
32
32
  when '--quiet' then $quiet = true
33
- when '--debug' then $DEBUG = true
33
+ when '--debug' then ::RDF::N3::debug = true
34
34
  when '--format' then $format = arg
35
35
  when '--suite' then suite = arg
36
36
  when '--strict' then $strict = true
@@ -134,8 +134,23 @@ describe "RDF::N3::Reader" do
134
134
  "tab:\t" => ':a :b "tab:\t" .',
135
135
  "é" => ':a :b "\u00E9" .',
136
136
  "€" => ':a :b "\u20AC" .',
137
+ "resumé" => ':a :resume "resum\u00E9" .',
137
138
  }.each_pair do |contents, triple|
138
- specify "test #{contents}" do
139
+ specify "test #{triple}" do
140
+ graph = parse(triple, :base_uri => "http://a/b")
141
+ statement = graph.statements.first
142
+ graph.size.should == 1
143
+ statement.object.value.should == contents
144
+ end
145
+ end
146
+
147
+ {
148
+ 'Dürst' => ':a :b "Dürst" .',
149
+ "é" => ':a :b "é" .',
150
+ "€" => ':a :b "€" .',
151
+ "resumé" => ':a :resume "resumé" .',
152
+ }.each_pair do |contents, triple|
153
+ specify "test #{triple}" do
139
154
  graph = parse(triple, :base_uri => "http://a/b")
140
155
  statement = graph.statements.first
141
156
  graph.size.should == 1
@@ -246,6 +261,12 @@ describe "RDF::N3::Reader" do
246
261
  end
247
262
  end
248
263
 
264
+ it "should allow mixed-case language" do
265
+ n3doc = %(:x2 :p "xyz"@EN .)
266
+ statement = parse(n3doc).statements.first
267
+ statement.object.to_ntriples.should == %("xyz"@en)
268
+ end
269
+
249
270
  it "should create typed literals" do
250
271
  n3doc = "<http://example.org/joe> <http://xmlns.com/foaf/0.1/name> \"Joe\"^^<http://www.w3.org/2001/XMLSchema#string> ."
251
272
  statement = parse(n3doc).statements.first
@@ -271,6 +292,25 @@ describe "RDF::N3::Reader" do
271
292
  end
272
293
  end
273
294
 
295
+ {
296
+ %(<#Dürst> :knows :jane.) => '<http://a/b#D\u00FCrst> <http://a/b#knows> <http://a/b#jane> .',
297
+ %(:Dürst :knows :jane.) => '<http://a/b#D\u00FCrst> <http://a/b#knows> <http://a/b#jane> .',
298
+ %(:bob :resumé "Bob's non-normalized resumé".) => '<http://a/b#bob> <http://a/b#resumé> "Bob\'s non-normalized resumé" .',
299
+ %(:alice :resumé "Alice's normalized resumé".) => '<http://a/b#alice> <http://a/b#resumé> "Alice\'s normalized resumé" .',
300
+ }.each_pair do |n3, nt|
301
+ it "for '#{n3}'" do
302
+ begin
303
+ parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
304
+ rescue
305
+ if defined?(::Encoding)
306
+ raise
307
+ else
308
+ pending("Unicode URIs not supported in Ruby 1.8") { raise }
309
+ end
310
+ end
311
+ end
312
+ end
313
+
274
314
  {
275
315
  %(<#Dürst> a "URI straight in UTF8".) => %(<http://a/b#D\\u00FCrst> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> "URI straight in UTF8" .),
276
316
  #%(:a :related :ひらがな .) => %(<http://a/b#a> <http://a/b#related> <http://a/b#\\u3072\\u3089\\u304C\\u306A> .),
@@ -442,9 +482,13 @@ describe "RDF::N3::Reader" do
442
482
  parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
443
483
  end
444
484
 
445
- it "should do something for @forAll"
485
+ it "should do something for @forAll" do
486
+ pending
487
+ end
446
488
 
447
- it "should do something for @forSome"
489
+ it "should do something for @forSome" do
490
+ pending
491
+ end
448
492
  end
449
493
 
450
494
  describe "namespaces" do
@@ -784,9 +828,13 @@ describe "RDF::N3::Reader" do
784
828
  end
785
829
 
786
830
  describe "formulae" do
787
- it "should require that graph be formula_aware when encountering a formlua"
831
+ it "should require that graph be formula_aware when encountering a formlua" do
832
+ pending
833
+ end
788
834
 
789
- it "should separate triples between specified and quoted graphs"
835
+ it "should separate triples between specified and quoted graphs" do
836
+ pending
837
+ end
790
838
  end
791
839
 
792
840
  describe "object lists" do
@@ -52,24 +52,24 @@ module RdfHelper
52
52
  pred = statement.predicate.to_s.split(/[\#\/]/).last
53
53
  obj = statement.object.is_a?(RDF::Literal) ? statement.object.value : statement.object.to_s
54
54
 
55
- puts "#{pred}: #{obj}" if $DEBUG
55
+ puts "#{pred}: #{obj}" if ::RDF::N3::debug?
56
56
  if statement.predicate == RDF.type
57
57
  self.rdf_type = obj.to_s.split(/[\#\/]/).last
58
58
  #puts statement.subject.to_s
59
59
  elsif pred =~ /Document\Z/i
60
- puts "sub #{uri_prefix} in #{obj} for #{test_dir}" if $DEBUG
60
+ puts "sub #{uri_prefix} in #{obj} for #{test_dir}" if ::RDF::N3::debug?
61
61
  about = obj.dup
62
62
  obj.sub!(uri_prefix, test_dir)
63
- puts " => #{obj}" if $DEBUG
63
+ puts " => #{obj}" if ::RDF::N3::debug?
64
64
  self.send("#{pred}=", obj)
65
65
  if pred == "inputDocument"
66
66
  self.about ||= about
67
67
  self.name ||= statement.subject.to_s.split(/[\#\/]/).last
68
68
  end
69
69
  elsif pred == "referenceOutput"
70
- puts "referenceOutput: #{obj}" if $DEBUG
70
+ puts "referenceOutput: #{obj}" if ::RDF::N3::debug?
71
71
  outputDocument = obj.sub(uri_prefix, test_dir)
72
- puts "referenceOutput: " + self.send(pred) if $DEBUG
72
+ puts "referenceOutput: " + self.send(pred) if ::RDF::N3::debug?
73
73
  elsif self.respond_to?("#{pred}=")
74
74
  self.send("#{pred}=", obj)
75
75
  end
@@ -130,7 +130,7 @@ module RdfHelper
130
130
  @parser.graph.should be_equivalent_graph(self.output, self)
131
131
  else
132
132
  output_graph = RDF::Graph.load(self.outputDocument, :format => detect_format(self.outputDocument))
133
- puts "result: #{CGI.escapeHTML(graph.to_ntriples)}" if $DEBUG
133
+ puts "result: #{CGI.escapeHTML(graph.to_ntriples)}" if ::RDF::N3::debug?
134
134
  graph.should Matchers::be_equivalent_graph(output_graph, self)
135
135
  end
136
136
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdf-n3
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Gregg Kellogg
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-11 00:00:00 -07:00
18
+ date: 2010-11-10 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -56,12 +56,14 @@ dependencies:
56
56
  requirement: &id003 !ruby/object:Gem::Requirement
57
57
  none: false
58
58
  requirements:
59
- - - ">="
59
+ - - "="
60
60
  - !ruby/object:Gem::Version
61
- hash: 3
61
+ hash: 27
62
62
  segments:
63
+ - 1
64
+ - 3
63
65
  - 0
64
- version: "0"
66
+ version: 1.3.0
65
67
  type: :development
66
68
  version_requirements: *id003
67
69
  - !ruby/object:Gem::Dependency
@@ -147,13 +149,13 @@ files:
147
149
  - lib/rdf/n3/format.rb
148
150
  - lib/rdf/n3/patches/array_hacks.rb
149
151
  - lib/rdf/n3/patches/graph_properties.rb
150
- - lib/rdf/n3/patches/literal_normalization.rb
151
152
  - lib/rdf/n3/patches/qname_hacks.rb
152
153
  - lib/rdf/n3/patches/seq.rb
153
- - lib/rdf/n3/patches/uri_hacks.rb
154
154
  - lib/rdf/n3/reader.rb
155
155
  - lib/rdf/n3/reader/n3_grammar.rb
156
156
  - lib/rdf/n3/reader/n3_grammar.treetop
157
+ - lib/rdf/n3/reader/n3_grammar_18.rb
158
+ - lib/rdf/n3/reader/n3_grammar_18.treetop
157
159
  - lib/rdf/n3/version.rb
158
160
  - lib/rdf/n3/vocab.rb
159
161
  - lib/rdf/n3/writer.rb
@@ -1,158 +0,0 @@
1
- # Use Nokogiri or LibXML when available, and REXML otherwise:
2
- begin
3
- require 'nokogiri'
4
- rescue LoadError => e
5
- begin
6
- require 'libxml'
7
- rescue LoadError => e
8
- :rexml
9
- end
10
- end
11
-
12
- module RDF; class Literal
13
- ##
14
- # An XML literal.
15
- #
16
- # @see http://www.w3.org/TR/rdf-concepts/#section-XMLLiteral
17
- # @see http://www.w3.org/TR/rdfa-core/#s_xml_literals
18
- # @since 0.2.1
19
- class XML < Literal
20
- ##
21
- # @param [Object] value
22
- # @option options [String] :lexical (nil)
23
- # @option options [Hash] :namespaces ({}) Use :__default__ or "" to declare default namespace
24
- # @option options [Symbol] :language (nil)
25
- # @option options [Symbol] :library (:nokogiri, :libxml, or :rexml)
26
- def initialize(value, options = {})
27
- options[:namespaces] ||= {}
28
-
29
- @library = case options[:library]
30
- when nil
31
- case
32
- when defined?(::Nokogiri) then :nokogiri
33
- when defined?(::LibXML) then :libxml
34
- else :rexml
35
- end
36
- when :nokogiri, :libxml, :rexml
37
- options[:library]
38
- else
39
- raise ArgumentError.new("expected :rexml, :libxml or :nokogiri, but got #{options[:library].inspect}")
40
- end
41
-
42
- @datatype = options[:datatype] || DATATYPE
43
- @string = options[:lexical] if options.has_key?(:lexical)
44
- @object = parse_value(value, options)
45
- @string = serialize_nodeset(@object)
46
- end
47
-
48
- ##
49
- # Converts the literal into its canonical lexical representation.
50
- #
51
- # @return [Literal]
52
- # @see http://www.w3.org/TR/xml-exc-c14n/
53
- def canonicalize
54
- # This is the opportunity to use exclusive canonicalization library
55
- self
56
- end
57
-
58
- ##
59
- # Returns the value as a string.
60
- #
61
- # @return [String]
62
- def to_s
63
- @string
64
- end
65
-
66
- private
67
-
68
- def parse_value(value, options)
69
- ns_hash = {}
70
- options[:namespaces].each_pair do |prefix, uri|
71
- prefix = prefix == :__default__ ? "" : prefix.to_s
72
- attr = prefix.empty? ? "xmlns" : "xmlns:#{prefix}"
73
- ns_hash[attr] = uri.to_s
74
- end
75
- ns_strs = []
76
- ns_hash.each_pair {|a, u| ns_strs << "#{a}=\"#{u}\""}
77
-
78
- case @library
79
- when :nokogiri then parse_value_nokogiri(value, ns_strs, options[:language])
80
- when :libxml then parse_value_libxml(value, ns_strs, options[:language])
81
- when :rexml then parse_value_rexml(value, ns_strs, options[:language])
82
- else value.to_s
83
- end
84
- end
85
-
86
- def serialize_nodeset(object)
87
- case @library
88
- when :nokogiri then serialize_nodeset_nokogiri(object)
89
- when :libxml then serialize_nodeset_libxml(object)
90
- when :rexml then serialize_nodeset_rexml(object)
91
- else object
92
- end
93
- end
94
-
95
- # Nokogiri implementations
96
- if defined?(::Nokogiri)
97
- # A somewhat half-hearted attempt at C14N.
98
- # Main problem is that it promotes all namespaces to top element, instead of demoting them
99
- # to the required element, and does not properly order either namespaces or attributes.
100
- #
101
- # An open-issue in Nokogiri is to add support for C14N from the underlying libxml2 libraries.
102
- def parse_value_nokogiri(value, ns_strs, language)
103
- elements = if value.is_a?(Nokogiri::XML::NodeSet)
104
- value
105
- else
106
- # Add inherited namespaces to created root element so that they're inherited to sub-elements
107
- Nokogiri::XML::Document.parse("<foo #{ns_strs.join(" ")}>#{value.to_s}</foo>").root.children
108
- end
109
-
110
- elements.map do |c|
111
- if c.is_a?(Nokogiri::XML::Element)
112
- c = Nokogiri::XML.parse(c.dup.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)).root
113
- # Gather namespaces from self and decendant nodes
114
- c.traverse do |n|
115
- ns = n.namespace
116
- next unless ns
117
- prefix = ns.prefix ? "xmlns:#{ns.prefix}" : "xmlns"
118
- c[prefix] = ns.href.to_s unless c.namespaces[prefix]
119
- end
120
-
121
- # Add language
122
- if language && c["lang"].to_s.empty?
123
- c["xml:lang"] = language.to_s
124
- end
125
- end
126
- c
127
- end
128
- end
129
-
130
- def serialize_nodeset_nokogiri(object)
131
- object.map {|c| c.to_xml(:save_with => Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS)}.join("")
132
- end
133
- end # Nokogiri
134
-
135
- if defined?(::LibXML)
136
- # This should use Document#canonicalize if as and when it is available in libxml-ruby
137
- def parse_value_libxml(value, ns_strs, language)
138
- # Fixme
139
- end
140
-
141
- def serialize_nodeset_libxml(object)
142
- # Fixme
143
- end
144
- end # LibXML
145
-
146
- # REXML
147
- # This could make use of the XMLCanonicalizer gem (http://rubygems.org/gems/XMLCanonicalizer)
148
- # But, it hasn't been touched since 2007 and relies on log4r.
149
- def parse_value_rexml(value, ns_strs, language)
150
- # Fixme
151
- end
152
-
153
- def serialize_nodeset_rexml(object)
154
- # Fixme
155
- end
156
-
157
- end # class XML
158
- end; end
@@ -1,29 +0,0 @@
1
- module RDF
2
- class URI
3
- ##
4
- # Joins several URIs together.
5
- #
6
- # @param [Array<String, URI, #to_str>] uris
7
- # @return [URI]
8
- #
9
- # GK -- don't add a "/" at the end of URIs, due to rdfcore/xmlbase/test002.rdf
10
- def join(*uris)
11
- result = @uri
12
- uris.each do |uri|
13
- # result.path += '/' unless result.path.match(/[\#\/]$/) || uri.to_s[0..0] == "#"
14
- result = result.join(uri)
15
- end
16
- self.class.new(result)
17
- end
18
-
19
- # From http://www.w3.org/TR/2004/REC-rdf-concepts-20040210/#section-Graph-URIref
20
- #
21
- # A URI Reference within an RDF graph is a Unicode string that:
22
- # * does not contain any control characters ( #x00 - #x1F, #x7F-#x9F)
23
- # * and would produce a valid URI character sequence (per RFC2396 [URI], sections 2.1) representing an absolute URI with optional fragment identifier when subjected to the encoding described below.
24
- #
25
- # The encoding consists of:
26
- # 1. encoding the Unicode string as UTF-8 [RFC-2279], giving a sequence of octet values.
27
- # 2. %-escaping octets that do not correspond to permitted US-ASCII characters.
28
- end
29
- end