Sha256: 098f70d935daa36ab643ff4fcd3557a77c5ec2f6e6ccf6a00badb0291fc4b696

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

module RDF::NTriples
  ##
  # N-Triples serializer.
  #
  # @see http://www.w3.org/TR/rdf-testcases/#ntriples
  class Writer < RDF::Writer
    format RDF::NTriples::Format

    ##
    # Outputs an N-Triples comment line.
    #
    # @param  [String] text
    # @return [void]
    def write_comment(text)
      puts "# #{text}"
    end

    ##
    # Outputs the N-Triples representation of a triple.
    #
    # @param  [RDF::Resource] subject
    # @param  [RDF::URI]      predicate
    # @param  [RDF::Value]    object
    # @return [void]
    def write_triple(subject, predicate, object)
      puts "%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  [Hash{Symbol => Object}] options
    # @return [String]
    def format_node(value, options = {})
      "_:%s" % node.id
    end

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

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rdf-0.1.0 lib/rdf/ntriples/writer.rb