Class: RDF::NTriples::Writer

Inherits:
Writer
  • Object
show all
Defined in:
lib/rdf/ntriples/writer.rb

Overview

N-Triples serializer.

Examples:

Obtaining an NTriples writer class

RDF::Writer.for(:ntriples)     #=> RDF::NTriples::Writer
RDF::Writer.for("etc/test.nt")
RDF::Writer.for(:file_name      => "etc/test.nt")
RDF::Writer.for(:file_extension => "nt")
RDF::Writer.for(:content_type   => "text/plain")

Serializing RDF statements into an NTriples file

RDF::NTriples::Writer.open("etc/test.nt") do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

Serializing RDF statements into an NTriples string

RDF::NTriples::Writer.buffer do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

See Also:

Direct Known Subclasses

RDF::NQuads::Writer

Constant Summary

ESCAPE_PLAIN =
/\A[\x20-\x21\x23-\x5B\x5D-\x7E]*\z/m.freeze
ESCAPE_ASCII =
/\A[\x00-\x7F]*\z/m.freeze

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Writer

buffer, dump, each, #flush, for, format, #format_list, #format_term, #initialize, #node_id, open, #prefix, #prefixes, #prefixes=, #puts, #quoted, #uri_for, #write_epilogue, #write_graph, #write_prologue, #write_statement, #write_statements, #write_triples

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Writable

#<<, #insert, #insert_graph, #insert_reader, #insert_statement, #insert_statements, #writable?

Constructor Details

This class inherits a constructor from RDF::Writer

Class Method Details

+ (String) escape(string)

Parameters:

  • (String) string

Returns:

  • (String)

See Also:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rdf/ntriples/writer.rb', line 38

def self.escape(string)
  case
    when string =~ ESCAPE_PLAIN # a shortcut for the simple case
      string
    when string.respond_to?(:ascii_only?) && string.ascii_only?
      StringIO.open do |buffer|
        string.each_byte { |u| buffer << escape_ascii(u) }
        buffer.string
      end
    when string.respond_to?(:each_codepoint)
      StringIO.open do |buffer|
        string.each_codepoint { |u| buffer << escape_unicode(u) }
        buffer.string
      end
    else # works in Ruby 1.8.x, too
      StringIO.open do |buffer|
        string.scan(/./mu) { |c| buffer << escape_unicode(u = c.unpack('U*').first) }
        buffer.string
      end
  end
end

+ (String) escape_ascii(u)

Parameters:

  • (Integer, #ord) u

Returns:

  • (String)

See Also:



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rdf/ntriples/writer.rb', line 81

def self.escape_ascii(u)
  case (u = u.ord)
    when (0x00..0x08) then escape_utf16(u)
    when (0x09)       then "\\t"
    when (0x0A)       then "\\n"
    when (0x0B..0x0C) then escape_utf16(u)
    when (0x0D)       then "\\r"
    when (0x0E..0x1F) then escape_utf16(u)
    when (0x22)       then "\\\""
    when (0x5C)       then "\\\\"
    when (0x7F)       then escape_utf16(u)
    when (0x00..0x7F) then u.chr
    else
      raise ArgumentError.new("expected an ASCII character in (0x00..0x7F), but got 0x#{u.to_s(16)}")
  end
end

+ (String) escape_unicode(u)

Parameters:

  • (Integer, #ord) u

Returns:

  • (String)

See Also:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rdf/ntriples/writer.rb', line 64

def self.escape_unicode(u)
  case (u = u.ord)
    when (0x00..0x7F)        # ASCII 7-bit
      escape_ascii(u)
    when (0x80..0xFFFF)      # Unicode BMP
      escape_utf16(u)
    when (0x10000..0x10FFFF) # Unicode
      escape_utf32(u)
    else
      raise ArgumentError.new("expected a Unicode codepoint in (0x00..0x10FFFF), but got 0x#{u.to_s(16)}")
  end
end

+ (String) escape_utf16(u)

Parameters:

  • (Integer, #ord) u

Returns:

  • (String)

See Also:



102
103
104
# File 'lib/rdf/ntriples/writer.rb', line 102

def self.escape_utf16(u)
  sprintf("\\u%04X", u.ord)
end

+ (String) escape_utf32(u)

Parameters:

  • (Integer, #ord) u

Returns:

  • (String)

See Also:



110
111
112
# File 'lib/rdf/ntriples/writer.rb', line 110

def self.escape_utf32(u)
  sprintf("\\U%08X", u.ord)
end

+ (String) serialize(value)

Returns the serialized N-Triples representation of the given RDF value.

Parameters:

Returns:

  • (String)

Raises:

  • (ArgumentError)

    if value is not an RDF::Statement or RDF::Term



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rdf/ntriples/writer.rb', line 121

def self.serialize(value)
  writer = self.new
  case value
    when nil then nil
    when RDF::Statement
      writer.format_statement(value) + "\n"
    when RDF::Term
      writer.format_term(value)
    else
      raise ArgumentError, "expected an RDF::Statement or RDF::Term, but got #{value.inspect}"
  end
end

Instance Method Details

- (String) format_literal(literal, options = {})

Returns the N-Triples representation of a literal.

Parameters:

  • (RDF::Literal, String, #to_s) literal
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)


200
201
202
203
204
205
206
207
208
209
210
# File 'lib/rdf/ntriples/writer.rb', line 200

def format_literal(literal, options = {})
  case literal
    when RDF::Literal
      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(literal.to_s))
  end
end

- (String) format_node(node, options = {})

Returns the N-Triples representation of a blank node.

Parameters:

  • (RDF::Node) node
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)


180
181
182
# File 'lib/rdf/ntriples/writer.rb', line 180

def format_node(node, options = {})
  "_:%s" % node.id
end

- (String) format_statement(statement)

Returns the N-Triples representation of a statement.

Parameters:

Returns:

  • (String)


159
160
161
# File 'lib/rdf/ntriples/writer.rb', line 159

def format_statement(statement)
  format_triple(*statement.to_triple)
end

- (String) format_triple(subject, predicate, object)

Returns the N-Triples representation of a triple.

Parameters:

Returns:

  • (String)


170
171
172
# File 'lib/rdf/ntriples/writer.rb', line 170

def format_triple(subject, predicate, object)
  "%s %s %s ." % [subject, predicate, object].map { |value| format_term(value) }
end

- (String) format_uri(uri, options = {})

Returns the N-Triples representation of a URI reference.

Parameters:

  • (RDF::URI) literal
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)


190
191
192
# File 'lib/rdf/ntriples/writer.rb', line 190

def format_uri(uri, options = {})
  "<%s>" % escaped(uri_for(uri))
end

- write_comment(text)

This method returns an undefined value.

Outputs an N-Triples comment line.

Parameters:

  • (String) text


139
140
141
# File 'lib/rdf/ntriples/writer.rb', line 139

def write_comment(text)
  puts "# #{text.chomp}" # TODO: correctly output multi-line comments
end

- write_triple(subject, predicate, object)

This method returns an undefined value.

Outputs the N-Triples representation of a triple.

Parameters:



150
151
152
# File 'lib/rdf/ntriples/writer.rb', line 150

def write_triple(subject, predicate, object)
  puts format_triple(subject, predicate, object)
end