Permalink
Browse files

Added Term#to_base, with implementations for other terms to provide a…

… reasonable N-Triples format for terms; this improves Statement#to_s.
  • Loading branch information...
1 parent 309ae4b commit 85e19ecae20aa737394cedd302b581e900b10dcc @gkellogg gkellogg committed Mar 22, 2013
View
@@ -344,6 +344,17 @@ def canonicalize!
end
##
+ # Returns the base representation of this URI.
+ #
+ # @return [Sring]
+ def to_base
+ text = %("#{escape(value)}")
+ text << "@#{language}" if has_language?
+ text << "^^#{datatype.to_base}" if has_datatype?
+ text
+ end
+
+ ##
# Returns the value as a string.
#
# @return [String]
View
@@ -138,6 +138,14 @@ def ==(other)
alias_method :===, :==
##
+ # Returns the base representation of this URI.
+ #
+ # @return [Sring]
+ def to_base
+ to_s
+ end
+
+ ##
# Returns a string representation of this blank node.
#
# @return [String]
View
@@ -260,25 +260,7 @@ def to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :ob
#
# @return [String]
def to_s
- StringIO.open do |buffer|
- buffer << case subject
- when RDF::Node then subject.to_s
- when RDF::URI then "<#{subject}>"
- else subject.inspect
- end
- buffer << " <#{predicate}> "
- buffer << case object
- when RDF::Literal then object.to_s
- when RDF::Node then object.to_s
- when RDF::URI then "<#{object}>"
- else object.inspect
- end
- buffer << case context
- when nil then " ."
- else " <#{context}> ."
- end
- buffer.string
- end
+ (context ? to_quad : to_triple).map(&:to_base).join(" ") + " ."
end
##
View
@@ -66,11 +66,36 @@ def constant?
end
##
+ # Returns a base representation of `self`.
+ #
+ # @return [RDF::Value]
+ def to_base
+ self
+ end
+
+ ##
# Returns `true` if `self` is a {RDF::Term}.
#
# @return [Boolean]
def term?
true
end
+
+ protected
+ ##
+ # Escape a term using standard character escapes
+ #
+ # @param [String] string
+ # @return [String]
+ def escape(string)
+ string.gsub('\\', '\\\\').
+ gsub("\b", '\\b').
+ gsub("\f", '\\f').
+ gsub("\t", '\\t').
+ gsub("\n", '\\n').
+ gsub("\r", '\\r').
+ gsub('"', '\\"')
+ end
+
end # Term
end # RDF
View
@@ -586,6 +586,14 @@ def to_uri
end
##
+ # Returns the base representation of this URI.
+ #
+ # @return [Sring]
+ def to_base
+ "<#{escape(@uri.to_s)}>"
+ end
+
+ ##
# Returns the string representation of this URI.
#
# @example
@@ -209,7 +209,7 @@ def format_triple(subject, predicate, object)
# @param [Hash{Symbol => Object}] options
# @return [String]
def format_node(node, options = {})
- "_:%s" % node.id
+ node.to_base
end
##
@@ -219,7 +219,7 @@ def format_node(node, options = {})
# @param [Hash{Symbol => Object}] options
# @return [String]
def format_uri(uri, options = {})
- "<%s>" % escaped(uri_for(uri))
+ uri.to_base
end
##
@@ -231,6 +231,7 @@ def format_uri(uri, options = {})
def format_literal(literal, options = {})
case literal
when RDF::Literal
+ # Note, escaping here is more robust than in Term
text = quoted(escaped(literal.value))
text << "@#{literal.language}" if literal.has_language?
text << "^^<#{uri_for(literal.datatype)}>" if literal.has_datatype?
View
@@ -466,8 +466,13 @@ def node_id
# @param [String] string
# @return [String]
def escaped(string)
- string.gsub('\\', '\\\\').gsub("\t", '\\t').
- gsub("\n", '\\n').gsub("\r", '\\r').gsub('"', '\\"')
+ string.gsub('\\', '\\\\').
+ gsub("\b", '\\b').
+ gsub("\f", '\\f').
+ gsub("\t", '\\t').
+ gsub("\n", '\\n').
+ gsub("\r", '\\r').
+ gsub('"', '\\"')
end
##

0 comments on commit 85e19ec

Please sign in to comment.