Skip to content

Commit ea3e888

Browse files
committed
Write VERSION declaration in N-Triples/Quads if version specified, and not canonicalizing.
1 parent a44b432 commit ea3e888

4 files changed

Lines changed: 90 additions & 4 deletions

File tree

lib/rdf/ntriples/writer.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ def initialize(output = $stdout, validate: true, **options, &block)
193193
super
194194
end
195195

196+
##
197+
# Output VERSION directive, if specified and not canonicalizing
198+
# @return [self]
199+
# @abstract
200+
def write_prologue
201+
puts %(VERSION #{version.inspect}) if version && !canonicalize?
202+
@logged_errors_at_prolog = log_statistics[:error].to_i
203+
super
204+
end
205+
196206
##
197207
# Outputs an N-Triples comment line.
198208
#

lib/rdf/writer.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ def self.options
152152
control: :checkbox,
153153
on: ["--unique-bnodes"],
154154
description: "Use unique Node identifiers.") {true},
155+
RDF::CLI::Option.new(
156+
symbol: :version,
157+
control: :select,
158+
datatype: RDF::Format::VERSIONS, # 1.1, 1.2, or 1.2-basic
159+
on: ["--version VERSION"],
160+
description: "RDF Version."),
155161
]
156162
end
157163

@@ -281,13 +287,22 @@ def to_sym
281287
# Use unique {Node} identifiers, defaults to using the identifier which the node was originall initialized with (if any). Implementations should ensure that Nodes are serialized using a unique representation independent of any identifier used when creating the node. See {NTriples::Writer#format_node}
282288
# @option options [Hash{Symbol => String}] :accept_params
283289
# Parameters from ACCEPT header entry for the media-range matching this writer.
290+
# @option options [String] :version
291+
# Parse a specific version of RDF ("1.1', "1.2", or "1.2-basic"")
284292
# @yield [writer] `self`
285293
# @yieldparam [RDF::Writer] writer
286294
# @yieldreturn [void]
287295
def initialize(output = $stdout, **options, &block)
288296
@output, @options = output, options.dup
289297
@nodes, @node_id, @node_id_map = {}, 0, {}
290298

299+
# The rdfstar option implies version 1.2, but can be overridden
300+
@options[:version] ||= "1.2" if @options[:rdfstar]
301+
302+
unless self.version.nil? || RDF::Format::VERSIONS.include?(self.version)
303+
log_warn("Expected version to be one of #{RDF::Format::VERSIONS.join(', ')}, was #{self.version}")
304+
end
305+
291306
if block_given?
292307
write_prologue
293308
case block.arity
@@ -412,6 +427,18 @@ def flush
412427
end
413428
alias_method :flush!, :flush
414429

430+
##
431+
# Returns the RDF version determined by this reader.
432+
#
433+
# @example
434+
# writer.version #=> "1.2"
435+
#
436+
# @return [String]
437+
# @since 3.3.4
438+
def version
439+
@options[:version]
440+
end
441+
415442
##
416443
# @return [self]
417444
# @abstract

spec/nquads_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,30 @@
313313
.to write_each("<http://example/s> <http://example/p> <http://example/o1> .\n",
314314
"<http://example/s> <http://example/p> <http://example/o2> .\n")
315315
end
316+
317+
it "writes version with :version option" do
318+
expect do
319+
described_class.new($stdout, version: "1.2") do |w|
320+
w.insert(graph)
321+
end
322+
end.to write(%(VERSION "1.2"))
323+
end
324+
325+
it "does not write version with :version and :canonicalize options" do
326+
expect do
327+
described_class.new($stdout, version: "1.2", canonicalize: true) do |w|
328+
w.insert(graph)
329+
end
330+
end.not_to write(%(VERSION "1.2"))
331+
end
332+
333+
it "writes version with :rdfstar option" do
334+
expect do
335+
described_class.new($stdout, rdfstar: true) do |w|
336+
w.insert(graph)
337+
end
338+
end.to write(%(VERSION "1.2"))
339+
end
316340
end
317341

318342
context "Writing a Statements" do

spec/ntriples_spec.rb

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -970,14 +970,39 @@
970970
context "Writing a Graph" do
971971
let(:graph) {
972972
g = RDF::Graph.new
973-
g << [RDF::URI('s'), RDF::URI('p'), RDF::URI('o1')]
974-
g << [RDF::URI('s'), RDF::URI('p'), RDF::URI('o2'), RDF::URI('c')]
973+
g << [RDF::URI('http://example/s'), RDF::URI('http://example/p'), RDF::URI('http://example/o1')]
974+
g << [RDF::URI('http://example/s'), RDF::URI('http://example/p'), RDF::URI('http://example/o2'), RDF::URI('c')]
975975
g
976976
}
977977
it "#insert" do
978978
expect do
979-
writer.new($stdout, validate: false).insert(graph)
980-
end.to write_each("<s> <p> <o1> .\n", "<s> <p> <o2> .\n")
979+
writer.new($stdout).insert(graph)
980+
end.to write_each("<http://example/s> <http://example/p> <http://example/o1> .\n",
981+
"<http://example/s> <http://example/p> <http://example/o2> .\n")
982+
end
983+
984+
it "writes version with :version option" do
985+
expect do
986+
writer.new($stdout, version: "1.2") do |w|
987+
w.insert(graph)
988+
end
989+
end.to write(%(VERSION "1.2"))
990+
end
991+
992+
it "does not write version with :version and :canonicalize options" do
993+
expect do
994+
writer.new($stdout, version: "1.2", canonicalize: true) do |w|
995+
w.insert(graph)
996+
end
997+
end.not_to write(%(VERSION "1.2"))
998+
end
999+
1000+
it "writes version with :rdfstar option" do
1001+
expect do
1002+
writer.new($stdout, rdfstar: true) do |w|
1003+
w.insert(graph)
1004+
end
1005+
end.to write(%(VERSION "1.2"))
9811006
end
9821007
end
9831008

0 commit comments

Comments
 (0)