Skip to content

Commit

Permalink
Improved RDF::NTriples::Writer and fixed a regression bug in #format_…
Browse files Browse the repository at this point in the history
…node.
  • Loading branch information
artob committed Mar 14, 2010
1 parent 4c1901d commit 4a77f24
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions lib/rdf/ntriples/writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Writer < RDF::Writer
# @param [String] text
# @return [void]
def write_comment(text)
puts "# #{text}"
puts "# #{text.chomp}" # TODO: correctly output multi-line comments
end

##
Expand All @@ -23,44 +23,64 @@ def write_comment(text)
# @param [RDF::Value] object
# @return [void]
def write_triple(subject, predicate, object)
puts "%s %s %s ." % [subject, predicate, object].map { |value| format_value(value) }
puts format_triple(subject, predicate, object)
end

##
# Returns the N-Triples representation of a statement.
#
# @param [RDF::Statement] statement
# @return [String]
def format_statement(statement)
format_triple(*statement.to_triple)
end

##
# Returns the N-Triples representation of a triple.
#
# @param [RDF::Resource] subject
# @param [RDF::URI] predicate
# @param [RDF::Value] object
# @return [String]
def format_triple(subject, predicate, object)
"%s %s %s ." % [subject, predicate, object].map { |value| format_value(value) }
end

##
# Returns the N-Triples representation of a blank node.
#
# @param [RDF::Node] value
# @param [RDF::Node] node
# @param [Hash{Symbol => Object}] options
# @return [String]
def format_node(value, options = {})
def format_node(node, options = {})
"_:%s" % node.id
end

##
# Returns the N-Triples representation of a URI reference.
#
# @param [RDF::URI] value
# @param [RDF::URI] literal
# @param [Hash{Symbol => Object}] options
# @return [String]
def format_uri(value, options = {})
"<%s>" % uri_for(value)
def format_uri(uri, options = {})
"<%s>" % uri_for(uri)
end

##
# Returns the N-Triples representation of a literal.
#
# @param [RDF::Literal, String, #to_s] value
# @param [RDF::Literal, String, #to_s] literal
# @param [Hash{Symbol => Object}] options
# @return [String]
def format_literal(value, options = {})
case value
def format_literal(literal, options = {})
case literal
when RDF::Literal
text = quoted(escaped(value.value))
text << "@#{value.language}" if value.language
text << "^^<#{uri_for(value.datatype)}>" if value.datatype
text = quoted(escaped(literal.value))
text << "@#{literal.language}" if literal.has_language?
text << "^^<#{uri_for(literal.datatype)}>" if literal.has_datatype?
text
else
quoted(escaped(value.to_s))
quoted(escaped(literal.to_s))
end
end
end
Expand Down

0 comments on commit 4a77f24

Please sign in to comment.