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 collapse

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

Instance Attribute Summary

Attributes inherited from Writer

#options

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Writer

#base_uri, buffer, dump, each, #flush, for, format, #format_list, #format_term, #initialize, open, #prefix, #prefixes, #prefixes=, to_sym, #to_sym, #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, #writable?

Constructor Details

This class inherits a constructor from RDF::Writer

Class Method Details

.escape(string) ⇒ 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

.escape_ascii(u) ⇒ String

Parameters:

  • u (Integer, #ord)

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

.escape_unicode(u) ⇒ String

Parameters:

  • u (Integer, #ord)

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

.escape_utf16(u) ⇒ String

Parameters:

  • u (Integer, #ord)

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

.escape_utf32(u) ⇒ String

Parameters:

  • u (Integer, #ord)

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

.serialize(value) ⇒ String

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
133
# File 'lib/rdf/ntriples/writer.rb', line 121

def self.serialize(value)
  writer = self.new
  case value
    when nil then nil
    when FalseClass then value.to_s
    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

#escaped(string) ⇒ Object



219
220
221
# File 'lib/rdf/ntriples/writer.rb', line 219

def escaped(string)
  self.class.escape(string)
end

#format_literal(literal, options = {}) ⇒ String

Returns the N-Triples representation of a literal.

Parameters:

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

Returns:

  • (String)


205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rdf/ntriples/writer.rb', line 205

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

#format_node(node, options = {}) ⇒ String

Returns the N-Triples representation of a blank node.

Parameters:

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

Returns:

  • (String)


185
186
187
# File 'lib/rdf/ntriples/writer.rb', line 185

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

#format_statement(statement) ⇒ String

Returns the N-Triples representation of a statement.

Parameters:

Returns:

  • (String)


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

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

#format_triple(subject, predicate, object) ⇒ String

Returns the N-Triples representation of a triple.

Parameters:

Returns:

  • (String)


171
172
173
174
175
176
177
# File 'lib/rdf/ntriples/writer.rb', line 171

def format_triple(subject, predicate, object)
  if subject.nil? || predicate.nil? || object.nil?
    raise ArgumentError, "Invalid statement [#{subject.inspect}, #{predicate.inspect}, #{object.inspect}]"
  end

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

#format_uri(uri, options = {}) ⇒ String

Returns the N-Triples representation of a URI reference.

Parameters:

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

Returns:

  • (String)


195
196
197
# File 'lib/rdf/ntriples/writer.rb', line 195

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

#write_comment(text) ⇒ void

This method returns an undefined value.

Outputs an N-Triples comment line.

Parameters:

  • text (String)


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

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

#write_triple(subject, predicate, object) ⇒ void

This method returns an undefined value.

Outputs the N-Triples representation of a triple.

Parameters:



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

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