lib/rdf/writer.rb in rdf-1.0.7 vs lib/rdf/writer.rb in rdf-1.0.8
- old
+ new
@@ -196,11 +196,13 @@
# any additional options
# @option options [Encoding, String, Symbol] :encoding
# the encoding to use on the output stream (Ruby 1.9+).
# Defaults to the format associated with `content_encoding`.
# @option options [Boolean] :canonicalize (false)
- # whether to canonicalize literals when serializing
+ # whether to canonicalize terms when serializing
+ # @option options [Boolean] :validate (false)
+ # whether to validate terms when serializing
# @option options [Hash] :prefixes (Hash.new)
# the prefix mappings to use (not supported by all writers)
# @option options [#to_s] :base_uri (nil)
# the base URI to use when constructing relative URIs (not supported
# by all writers)
@@ -307,10 +309,28 @@
@options[:encoding] ||= Encoding.find(self.class.format.content_encoding.to_s)
end
end
##
+ # Returns `true` if statements and terms should be validated.
+ #
+ # @return [Boolean] `true` or `false`
+ # @since 1.0.8
+ def validate?
+ @options[:validate]
+ end
+
+ ##
+ # Returns `true` if terms should be canonicalized.
+ #
+ # @return [Boolean] `true` or `false`
+ # @since 1.0.8
+ def canonicalize?
+ @options[:canonicalize]
+ end
+
+ ##
# Flushes the underlying output buffer.
#
# @return [void] `self`
def flush
@output.flush if @output.respond_to?(:flush)
@@ -359,19 +379,25 @@
end
##
# @param [RDF::Statement] statement
# @return [void] `self`
+ # @raise [RDF::WriterError] if validating and attempting to write an invalid {RDF::Statement} or if canonicalizing a statement which cannot be canonicalized.
def write_statement(statement)
+ statement = statement.canonicalize! if canonicalize?
+ raise RDF::WriterError, "Statement #{statement.inspect} is invalid" if validate? && statement.invalid?
write_triple(*statement.to_triple)
self
+ rescue ArgumentError => e
+ raise WriterError, e.message
end
alias_method :insert_statement, :write_statement # support the RDF::Writable interface
##
# @param [Array<Array(RDF::Resource, RDF::URI, RDF::Term)>] triples
# @return [void] `self`
+ # @raise [RDF::WriterError] if validating and attempting to write an invalid {RDF::Term}.
def write_triples(*triples)
triples.each { |triple| write_triple(*triple) }
self
end
@@ -379,9 +405,10 @@
# @param [RDF::Resource] subject
# @param [RDF::URI] predicate
# @param [RDF::Term] object
# @return [void] `self`
# @raise [NotImplementedError] unless implemented in subclass
+ # @raise [RDF::WriterError] if validating and attempting to write an invalid {RDF::Term}.
# @abstract
def write_triple(subject, predicate, object)
raise NotImplementedError.new("#{self.class}#write_triple") # override in subclasses
end