Sha256: cbc1eb62f05a0b8704b34d4441308284eb58b11e6818083dbd8d37f971ddd63c

Contents?: true

Size: 1.21 KB

Versions: 2

Compression:

Stored size: 1.21 KB

Contents

module RDF class Writer
  ##
  # An N-Triples serializer.
  #
  # @see <http://www.w3.org/TR/rdf-testcases/#ntriples>
  class NTriples < Writer
    content_type 'text/plain', :extension => :nt
    content_encoding 'ascii'

    ##
    # @param  [String] text
    # @return [void]
    def write_comment(text)
      puts "# #{text}"
    end

    ##
    # @param  [Resource] subject
    # @param  [URI]      predicate
    # @param  [Value]    object
    # @return [void]
    def write_triple(subject, predicate, object)
      s = format_uri(subject)
      p = format_uri(predicate)
      o = object.kind_of?(RDF::URI) ? format_uri(object) : format_literal(object)
      puts "%s %s %s ." % [s, p, o]
    end

    ##
    # @param  [node] Resource
    # @return [void]
    def format_uri(node)
      "<%s>" % uri_for(node)
    end

    ##
    # @param  [String, Literal] literal
    # @return [void]
    def format_literal(literal) # TODO
      #if literal.kind_of?(RDF::Literal)
      #  text = quoted(escaped(literal.value))
      #  text << "@#{literal.language}" if literal.language
      #  text << "^^<#{uri_for(literal.type)}>" if literal.type
      #  text
      #else
        quoted(escaped(literal.to_s))
      #end
    end
  end
end end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rdf-0.0.5 lib/rdf/writer/ntriples.rb
rdf-0.0.4 lib/rdf/writer/ntriples.rb