rdf-n3 0.3.3.1 → 0.3.4

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.md CHANGED
@@ -1,3 +1,10 @@
1
+ 0.3.4
2
+ -----
3
+ * Reader accepts 1.0E1 in addition to 1.0e1 (case-insensitive match on exponent).
4
+ * Writer was not outputting xsd prefix if it was only used in a literal datatype.
5
+ * Use bare representations of xsd:integer, xsd:boolean, xsd:double, and xsd:decimal.
6
+ * Implement literal canonicalization (on option) in writer.
7
+
1
8
  0.3.3.1
2
9
  -------
3
10
  * Fixed bug in writer when given a base URI.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.3.1
1
+ 0.3.4
@@ -635,6 +635,6 @@ module RDF::N3::Meta
635
635
  :"http://www.w3.org/2000/10/swap/grammar/n3#string" => Regexp.compile("(\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\")"),
636
636
 
637
637
  # Hack to replace integer|double|decimal with numericliteral
638
- :"http://www.w3.org/2000/10/swap/grammar/n3#numericliteral" => Regexp.compile(%(^[-+]?[0-9]+(\\.[0-9]+)?(e[-+]?[0-9]+)?))
638
+ :"http://www.w3.org/2000/10/swap/grammar/n3#numericliteral" => Regexp.compile(%(^[-+]?[0-9]+(\\.[0-9]+)?([eE][-+]?[0-9]+)?))
639
639
  }
640
640
  end
data/lib/rdf/n3/writer.rb CHANGED
@@ -166,18 +166,21 @@ module RDF::N3
166
166
  return nil
167
167
  end
168
168
 
169
+ add_debug "get_qname(#{resource})"
169
170
  qname = case
170
171
  when @uri_to_qname.has_key?(uri)
171
172
  return @uri_to_qname[uri]
172
173
  when u = @uri_to_prefix.keys.detect {|u| uri.index(u.to_s) == 0}
173
174
  # Use a defined prefix
174
175
  prefix = @uri_to_prefix[u]
175
- prefix(prefix, u) # Define for output
176
+ prefix(prefix, u) unless u.to_s.empty? # Define for output
177
+ add_debug "get_qname: add prefix #{prefix.inspect} => #{u}"
176
178
  uri.sub(u.to_s, "#{prefix}:")
177
179
  when @options[:standard_prefixes] && vocab = RDF::Vocabulary.detect {|v| uri.index(v.to_uri.to_s) == 0}
178
180
  prefix = vocab.__name__.to_s.split('::').last.downcase
179
181
  @uri_to_prefix[vocab.to_uri.to_s] = prefix
180
182
  prefix(prefix, vocab.to_uri) # Define for output
183
+ add_debug "get_qname: add standard prefix #{prefix.inspect} => #{vocab.to_uri}"
181
184
  uri.sub(vocab.to_uri.to_s, "#{prefix}:")
182
185
  else
183
186
  nil
@@ -232,14 +235,22 @@ module RDF::N3
232
235
  # @param [Hash{Symbol => Object}] options
233
236
  # @return [String]
234
237
  def format_literal(literal, options = {})
238
+ literal = literal.dup.canonicalize! if @options[:canonicalize]
235
239
  case literal
236
- when RDF::Literal
240
+ when RDF::Literal
241
+ case literal.datatype
242
+ when RDF::XSD.boolean, RDF::XSD.integer, RDF::XSD.decimal
243
+ literal.to_s
244
+ when RDF::XSD.double
245
+ literal.to_s.sub('E', 'e') # Favor lower case exponent
246
+ else
237
247
  text = quoted(literal.value)
238
248
  text << "@#{literal.language}" if literal.has_language?
239
249
  text << "^^#{format_uri(literal.datatype)}" if literal.has_datatype?
240
250
  text
241
- else
242
- quoted(literal.to_s)
251
+ end
252
+ else
253
+ quoted(literal.to_s)
243
254
  end
244
255
  end
245
256
 
@@ -270,7 +281,7 @@ module RDF::N3
270
281
  def start_document
271
282
  @started = true
272
283
 
273
- @output.write("#{indent}@base <#{@base_uri}> .\n") if @base_uri
284
+ @output.write("#{indent}@base <#{@base_uri}> .\n") unless @base_uri.to_s.empty?
274
285
 
275
286
  add_debug("start_document: #{prefixes.inspect}")
276
287
  prefixes.keys.sort_by(&:to_s).each do |prefix|
@@ -353,6 +364,7 @@ module RDF::N3
353
364
  get_qname(statement.subject)
354
365
  get_qname(statement.predicate)
355
366
  get_qname(statement.object)
367
+ get_qname(statement.object.datatype) if statement.object.literal? && statement.object.datatype
356
368
 
357
369
  @references[statement.predicate] = ref_count(statement.predicate) + 1
358
370
  end
data/rdf-n3.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{rdf-n3}
8
- s.version = "0.3.3.1"
8
+ s.version = "0.3.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Gregg Kellogg"]
12
- s.date = %q{2011-04-26}
12
+ s.date = %q{2011-07-10}
13
13
  s.description = %q{RDF::N3 is an Notation-3 and Turtle reader/writer for the RDF.rb library suite.}
14
14
  s.email = %q{gregg@kellogg-assoc.com}
15
15
  s.extra_rdoc_files = [
@@ -73,19 +73,6 @@ Gem::Specification.new do |s|
73
73
  s.require_paths = ["lib"]
74
74
  s.rubygems_version = %q{1.5.0}
75
75
  s.summary = %q{N3/Turtle reader/writer for RDF.rb.}
76
- s.test_files = [
77
- "spec/cwm_spec.rb",
78
- "spec/cwm_test.rb",
79
- "spec/format_spec.rb",
80
- "spec/matchers.rb",
81
- "spec/reader_spec.rb",
82
- "spec/spec_helper.rb",
83
- "spec/swap_spec.rb",
84
- "spec/swap_test.rb",
85
- "spec/turtle_spec.rb",
86
- "spec/turtle_test.rb",
87
- "spec/writer_spec.rb"
88
- ]
89
76
 
90
77
  if s.respond_to? :specification_version then
91
78
  s.specification_version = 3
data/spec/reader_spec.rb CHANGED
@@ -446,6 +446,7 @@ describe "RDF::N3::Reader" do
446
446
  %(:a :b 1.0e1) => %(<http://a/b#a> <http://a/b#b> "1.0e1"^^<http://www.w3.org/2001/XMLSchema#double> .),
447
447
  %(:a :b 1.0e-1) => %(<http://a/b#a> <http://a/b#b> "1.0e-1"^^<http://www.w3.org/2001/XMLSchema#double> .),
448
448
  %(:a :b 1.0e+1) => %(<http://a/b#a> <http://a/b#b> "1.0e+1"^^<http://www.w3.org/2001/XMLSchema#double> .),
449
+ %(:a :b 1.0E1) => %(<http://a/b#a> <http://a/b#b> "1.0e1"^^<http://www.w3.org/2001/XMLSchema#double> .),
449
450
  }.each_pair do |n3, nt|
450
451
  it "should create typed literal for '#{n3}'" do
451
452
  parse(n3, :base_uri => "http://a/b").should be_equivalent_graph(nt, :about => "http://a/b", :trace => @debug)
data/spec/writer_spec.rb CHANGED
@@ -223,17 +223,130 @@ describe RDF::N3::Writer do
223
223
  #$verbose = false
224
224
  end
225
225
  end
226
-
227
- describe "strings" do
228
- it "encodes embedded \"\"\"" do
229
- n3 = %(:a :b """testing string parsing in N3.
230
- """ .)
231
- serialize(n3, nil, [/testing string parsing in N3.\n/])
226
+
227
+ describe "literals" do
228
+ describe "plain" do
229
+ it "encodes embedded \"\"\"" do
230
+ n3 = %(:a :b """testing string parsing in N3.
231
+ """ .)
232
+ serialize(n3, nil, [/testing string parsing in N3.\n/])
233
+ end
234
+
235
+ it "encodes embedded \"" do
236
+ n3 = %(:a :b """string with " escaped quote marks""" .)
237
+ serialize(n3, nil, [/string with \\" escaped quote mark/])
238
+ end
239
+ end
240
+
241
+ describe "with language" do
242
+ it "specifies language for literal with language" do
243
+ ttl = %q(:a :b "string"@en .)
244
+ serialize(ttl, nil, [%r("string"@en)])
245
+ end
246
+ end
247
+
248
+ describe "xsd:anyURI" do
249
+ it "uses xsd namespace for datatype" do
250
+ ttl = %q(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b "http://foo/"^^xsd:anyURI .)
251
+ serialize(ttl, nil, [
252
+ %r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
253
+ %r("http://foo/"\^\^xsd:anyURI \.),
254
+ ])
255
+ end
256
+ end
257
+
258
+ describe "xsd:boolean" do
259
+ [
260
+ [%q("true"^^xsd:boolean), /true ./],
261
+ [%q("TrUe"^^xsd:boolean), /true ./],
262
+ [%q("1"^^xsd:boolean), /true ./],
263
+ [%q(true), /true ./],
264
+ [%q("false"^^xsd:boolean), /false ./],
265
+ [%q("FaLsE"^^xsd:boolean), /false ./],
266
+ [%q("0"^^xsd:boolean), /false ./],
267
+ [%q(false), /false ./],
268
+ ].each do |(l,r)|
269
+ it "uses token for #{l.inspect}" do
270
+ ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{l} .)
271
+ serialize(ttl, nil, [
272
+ %r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
273
+ r,
274
+ ], :canonicalize => true)
275
+ end
276
+ end
277
+ end
278
+
279
+ describe "xsd:integer" do
280
+ [
281
+ [%q("1"^^xsd:integer), /1 ./],
282
+ [%q(1), /1 ./],
283
+ [%q("0"^^xsd:integer), /0 ./],
284
+ [%q(0), /0 ./],
285
+ [%q("10"^^xsd:integer), /10 ./],
286
+ [%q(10), /10 ./],
287
+ ].each do |(l,r)|
288
+ it "uses token for #{l.inspect}" do
289
+ ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{l} .)
290
+ serialize(ttl, nil, [
291
+ %r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
292
+ r,
293
+ ], :canonicalize => true)
294
+ end
295
+ end
296
+ end
297
+
298
+ describe "xsd:int" do
299
+ [
300
+ [%q("1"^^xsd:int), /"1"\^\^xsd:int ./],
301
+ [%q("0"^^xsd:int), /"0"\^\^xsd:int ./],
302
+ [%q("10"^^xsd:int), /"10"\^\^xsd:int ./],
303
+ ].each do |(l,r)|
304
+ it "uses token for #{l.inspect}" do
305
+ ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{l} .)
306
+ serialize(ttl, nil, [
307
+ %r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
308
+ r,
309
+ ], :canonicalize => true)
310
+ end
311
+ end
232
312
  end
233
313
 
234
- it "encodes embedded \"" do
235
- n3 = %(:a :b """string with " escaped quote marks""" .)
236
- serialize(n3, nil, [/string with \\" escaped quote mark/])
314
+ describe "xsd:decimal" do
315
+ [
316
+ [%q("1.0"^^xsd:decimal), /1.0 ./],
317
+ [%q(1.0), /1.0 ./],
318
+ [%q("0.1"^^xsd:decimal), /0.1 ./],
319
+ [%q(0.1), /0.1 ./],
320
+ [%q("10.02"^^xsd:decimal), /10.02 ./],
321
+ [%q(10.02), /10.02 ./],
322
+ ].each do |(l,r)|
323
+ it "uses token for #{l.inspect}" do
324
+ ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{l} .)
325
+ serialize(ttl, nil, [
326
+ %r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
327
+ r,
328
+ ], :canonicalize => true)
329
+ end
330
+ end
331
+ end
332
+
333
+ describe "xsd:double" do
334
+ [
335
+ [%q("1.0e1"^^xsd:double), /1.0e1 ./],
336
+ [%q(1.0e1), /1.0e1 ./],
337
+ [%q("0.1e1"^^xsd:double), /1.0e0 ./],
338
+ [%q(0.1e1), /1.0e0 ./],
339
+ [%q("10.02e1"^^xsd:double), /1.002e2 ./],
340
+ [%q(10.02e1), /1.002e2 ./],
341
+ ].each do |(l,r)|
342
+ it "uses token for #{l.inspect}" do
343
+ ttl = %(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . :a :b #{l} .)
344
+ serialize(ttl, nil, [
345
+ %r(@prefix xsd: <http://www.w3.org/2001/XMLSchema#> \.),
346
+ r,
347
+ ], :canonicalize => true)
348
+ end
349
+ end
237
350
  end
238
351
  end
239
352
 
@@ -266,9 +379,10 @@ describe RDF::N3::Writer do
266
379
 
267
380
  # Serialize ntstr to a string and compare against regexps
268
381
  def serialize(ntstr, base = nil, regexps = [], options = {})
269
- g = parse(ntstr, :base_uri => base)
382
+ prefixes = options[:prefixes] || {}
383
+ g = parse(ntstr, :base_uri => base, :prefixes => prefixes)
270
384
  @debug = []
271
- result = RDF::N3::Writer.buffer(options.merge(:debug => @debug, :base_uri => base)) do |writer|
385
+ result = RDF::N3::Writer.buffer(options.merge(:debug => @debug, :base_uri => base, :prefixes => prefixes)) do |writer|
272
386
  writer << g
273
387
  end
274
388
  if $verbose
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rdf-n3
3
3
  version: !ruby/object:Gem::Version
4
- hash: 89
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 3
10
- - 1
11
- version: 0.3.3.1
9
+ - 4
10
+ version: 0.3.4
12
11
  platform: ruby
13
12
  authors:
14
13
  - Gregg Kellogg
@@ -16,7 +15,7 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2011-04-26 00:00:00 -07:00
18
+ date: 2011-07-10 00:00:00 -07:00
20
19
  default_executable:
21
20
  dependencies:
22
21
  - !ruby/object:Gem::Dependency
@@ -238,15 +237,5 @@ rubygems_version: 1.5.0
238
237
  signing_key:
239
238
  specification_version: 3
240
239
  summary: N3/Turtle reader/writer for RDF.rb.
241
- test_files:
242
- - spec/cwm_spec.rb
243
- - spec/cwm_test.rb
244
- - spec/format_spec.rb
245
- - spec/matchers.rb
246
- - spec/reader_spec.rb
247
- - spec/spec_helper.rb
248
- - spec/swap_spec.rb
249
- - spec/swap_test.rb
250
- - spec/turtle_spec.rb
251
- - spec/turtle_test.rb
252
- - spec/writer_spec.rb
240
+ test_files: []
241
+