Class: RDF::Writer Abstract

Inherits:
Object
  • Object
show all
Extended by:
Enumerable, Util::Aliasing::LateBound
Includes:
Writable
Defined in:
lib/rdf/writer.rb

Overview

This class is abstract.

The base class for RDF serializers.

Examples:

Loading an RDF writer implementation

require 'rdf/ntriples'

Iterating over known RDF writer classes

RDF::Writer.each { |klass| puts klass.name }

Obtaining an RDF writer class

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

Instantiating an RDF writer class

RDF::Writer.for(:ntriples).new($stdout) { |writer| ... }

Serializing RDF statements into a file

RDF::Writer.open("spec/data/output.nt") do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

Serializing RDF statements into a string

RDF::Writer.for(:ntriples).buffer do |writer|
  graph.each_statement do |statement|
    writer << statement
  end
end

See Also:

Direct Known Subclasses

NTriples::Writer

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Util::Aliasing::LateBound

alias_method

Methods included from Writable

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

Constructor Details

- (Writer) initialize(output = $stdout, options = {}) {|writer| ... }

Initializes the writer.

Parameters:

  • (IO, File) output (defaults to: $stdout)

    the output stream

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

    any additional options

Options Hash (options):

  • (Encoding) :encoding — default: Encoding::UTF_8

    the encoding to use on the output stream (Ruby 1.9+)

  • (Boolean) :canonicalize — default: false

    whether to canonicalize literals when serializing

  • (Hash) :prefixes — default: Hash.new

    the prefix mappings to use (not supported by all writers)

  • (#to_s) :base_uri — default: nil

    the base URI to use when constructing relative URIs (not supported by all writers)

Yields:

  • (writer)

    self

Yield Parameters:

Yield Returns:

  • (void)


178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/rdf/writer.rb', line 178

def initialize(output = $stdout, options = {}, &block)
  @output, @options = output, options.dup
  @nodes, @node_id  = {}, 0

  if block_given?
    write_prologue
    case block.arity
      when 0 then instance_eval(&block)
      else block.call(self)
    end
    write_epilogue
  end
end

Instance Attribute Details

- (Hash) options (readonly)

Any additional options for this writer.

Returns:

  • (Hash)

Since:

  • 0.2.2



197
198
199
# File 'lib/rdf/writer.rb', line 197

def options
  @options
end

Class Method Details

+ (String) buffer(*args) {|writer| ... }

Buffers output into a string buffer.

Yields:

  • (writer)

Yield Parameters:

Yield Returns:

  • (void)

Returns:

  • (String)


138
139
140
141
142
143
# File 'lib/rdf/writer.rb', line 138

def self.buffer(*args, &block)
  StringIO.open do |buffer|
    self.new(buffer, *args) { |writer| block.call(writer) }
    buffer.string
  end
end

+ dump(data, io = nil, options = {})

This method returns an undefined value.

Parameters:

  • (RDF::Enumerable, #each) data

    the graph or repository to dump

  • (IO, File) io (defaults to: nil)

    the output stream or file to write to

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

    passed to new or buffer



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rdf/writer.rb', line 112

def self.dump(data, io = nil, options = {})
  io = File.open(io, 'w') if io.is_a?(String)
  method = data.respond_to?(:each_statement) ? :each_statement : :each
  if io
    new(io, options) do |writer|
      data.send(method) do |statement|
        writer << statement
      end
      writer.flush
    end
  else
    buffer(options) do |writer|
      data.send(method) do |statement|
        writer << statement
      end
    end
  end
end

+ (Enumerator) each {|klass| ... }

Enumerates known RDF writer classes.

Yields:

  • (klass)

Yield Parameters:

  • (Class) klass

Yield Returns:

  • (void)

    ignored

Returns:



50
51
52
# File 'lib/rdf/writer.rb', line 50

def self.each(&block)
  @@subclasses.each(&block)
end

+ (Class) for(format) + (Class) for(filename) + (Class) for(options = {})

Finds an RDF writer class based on the given criteria.

Overloads:

  • + (Class) for(format)

    Finds an RDF writer class based on a symbolic name.

    Parameters:

    • (Symbol) format

    Returns:

    • (Class)
  • + (Class) for(filename)

    Finds an RDF writer class based on a file name.

    Parameters:

    • (String) filename

    Returns:

    • (Class)
  • + (Class) for(options = {})

    Finds an RDF writer class based on various options.

    Parameters:

    • (Hash{Symbol => Object}) options

    Options Hash (options):

    • (String, #to_s) :file_name — default: nil
    • (Symbol, #to_sym) :file_extension — default: nil
    • (String, #to_s) :content_type — default: nil

    Returns:

    • (Class)

Returns:

  • (Class)


79
80
81
82
83
# File 'lib/rdf/writer.rb', line 79

def self.for(options = {})
  if format = Format.for(options)
    format.writer
  end
end

+ (Class) format(klass = nil) Also known as: format_class

Retrieves the RDF serialization format class for this writer class.

Returns:

  • (Class)


89
90
91
92
93
94
95
96
97
98
# File 'lib/rdf/writer.rb', line 89

def self.format(klass = nil)
  if klass.nil?
    Format.each do |format|
      if format.writer == self
        return format
      end
    end
    nil # not found
  end
end

+ (RDF::Writer) open(filename, options = {}, &block)

Writes output to the given filename.

Parameters:

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

    any additional options (see #initialize)

Options Hash (options):

  • (Symbol) :format — default: nil

Returns:



153
154
155
156
157
# File 'lib/rdf/writer.rb', line 153

def self.open(filename, options = {}, &block)
  File.open(filename, 'wb') do |file|
    self.for(options[:format] || filename).new(file, options, &block)
  end
end

Instance Method Details

- (String) escaped(string) (protected)

Parameters:

  • (String) string

Returns:

  • (String)


414
415
416
417
# File 'lib/rdf/writer.rb', line 414

def escaped(string)
  string.gsub('\\', '\\\\').gsub("\t", '\\t').
    gsub("\n", '\\n').gsub("\r", '\\r').gsub('"', '\\"')
end

- flush Also known as: flush!

This method returns an undefined value.

Flushes the underlying output buffer.



253
254
255
256
# File 'lib/rdf/writer.rb', line 253

def flush
  @output.flush if @output.respond_to?(:flush)
  self
end

- (String) format_list(value, options = {})

This method is abstract.

Parameters:

  • (RDF::List) value
  • (Hash{Symbol => Object}) options (defaults to: {})

Returns:

  • (String)

Since:

  • 0.2.3



379
380
381
# File 'lib/rdf/writer.rb', line 379

def format_list(value, options = {})
  format_term(value.subject, options)
end

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

This method is abstract.

Parameters:

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

Returns:

  • (String)

Raises:

  • (NotImplementedError)

    unless implemented in subclass



369
370
371
# File 'lib/rdf/writer.rb', line 369

def format_literal(value, options = {})
  raise NotImplementedError.new("#{self.class}#format_literal") # override in subclasses
end

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

This method is abstract.

Parameters:

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

Returns:

  • (String)

Raises:

  • (NotImplementedError)

    unless implemented in subclass



349
350
351
# File 'lib/rdf/writer.rb', line 349

def format_node(value, options = {})
  raise NotImplementedError.new("#{self.class}#format_node") # override in subclasses
end

- (String) format_term(term, options = {}) Also known as: format_value

Parameters:

Returns:

  • (String)

Since:

  • 0.3.0



331
332
333
334
335
336
337
338
339
340
# File 'lib/rdf/writer.rb', line 331

def format_term(term, options = {})
  case term
    when String       then format_literal(RDF::Literal(term, options), options)
    when RDF::List    then format_list(term, options)
    when RDF::Literal then format_literal(term, options)
    when RDF::URI     then format_uri(term, options)
    when RDF::Node    then format_node(term, options)
    else nil
  end
end

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

This method is abstract.

Parameters:

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

Returns:

  • (String)

Raises:

  • (NotImplementedError)

    unless implemented in subclass



359
360
361
# File 'lib/rdf/writer.rb', line 359

def format_uri(value, options = {})
  raise NotImplementedError.new("#{self.class}#format_uri") # override in subclasses
end

- (String) node_id (protected)

Returns:

  • (String)


407
408
409
# File 'lib/rdf/writer.rb', line 407

def node_id
  "_:n#{@node_id += 1}"
end

- (Object) prefix(name, uri) - (Object) prefix(name) Also known as: prefix!

Defines the given named URI prefix for this writer.

Examples:

Defining a URI prefix

writer.prefix :dc, RDF::URI('http://purl.org/dc/terms/')

Returning a URI prefix

writer.prefix(:dc)    #=> RDF::URI('http://purl.org/dc/terms/')

Overloads:

  • - (Object) prefix(name, uri)

    Parameters:

    • (Symbol, #to_s) name
    • (RDF::URI, #to_s) uri
  • - (Object) prefix(name)

    Parameters:

    • (Symbol, #to_s) name

Returns:



243
244
245
246
# File 'lib/rdf/writer.rb', line 243

def prefix(name, uri = nil)
  name = name.to_s.empty? ? nil : (name.respond_to?(:to_sym) ? name.to_sym : name.to_s.to_sym)
  uri.nil? ? prefixes[name] : prefixes[name] = uri
end

- (Hash{Symbol => RDF::URI}) prefixes

Returns the URI prefixes currently defined for this writer.

Examples:

writer.prefixes[:dc]  #=> RDF::URI('http://purl.org/dc/terms/')

Returns:

Since:

  • 0.2.2



207
208
209
# File 'lib/rdf/writer.rb', line 207

def prefixes
  @options[:prefixes] ||= {}
end

- (Hash{Symbol => RDF::URI}) prefixes=(prefixes)

Defines the given URI prefixes for this writer.

Examples:

writer.prefixes = {
  :dc => RDF::URI('http://purl.org/dc/terms/'),
}

Parameters:

Returns:

Since:

  • 0.3.0



222
223
224
# File 'lib/rdf/writer.rb', line 222

def prefixes=(prefixes)
  @options[:prefixes] = prefixes
end

- puts(*args) (protected)

This method returns an undefined value.



387
388
389
# File 'lib/rdf/writer.rb', line 387

def puts(*args)
  @output.puts(*args)
end

- (String) quoted(string) (protected)

Parameters:

  • (String) string

Returns:

  • (String)


422
423
424
# File 'lib/rdf/writer.rb', line 422

def quoted(string)
  "\"#{string}\""
end

- (String) uri_for(uriref) (protected)

Parameters:

Returns:

  • (String)


394
395
396
397
398
399
400
401
402
403
# File 'lib/rdf/writer.rb', line 394

def uri_for(uriref)
  case
    when uriref.is_a?(RDF::Node)
      @nodes[uriref]
    when uriref.respond_to?(:to_uri)
      uriref.to_uri.to_s
    else
      uriref.to_s
  end
end

- write_comment(text)

This method is abstract.

This method returns an undefined value.

self

Parameters:

  • (String) text


277
278
279
# File 'lib/rdf/writer.rb', line 277

def write_comment(text)
  self
end

- write_epilogue

This method is abstract.

This method returns an undefined value.

self



269
270
271
# File 'lib/rdf/writer.rb', line 269

def write_epilogue
  self
end

- write_graph(graph)

Deprecated.

replace by RDF::Writable#insert_graph

This method returns an undefined value.

self

Parameters:



285
286
287
288
# File 'lib/rdf/writer.rb', line 285

def write_graph(graph)
  graph.each_triple { |*triple| write_triple(*triple) }
  self
end

- write_prologue

This method is abstract.

This method returns an undefined value.

self



262
263
264
# File 'lib/rdf/writer.rb', line 262

def write_prologue
  self
end

- write_statement(statement) Also known as: insert_statement

This method returns an undefined value.

self

Parameters:



302
303
304
305
# File 'lib/rdf/writer.rb', line 302

def write_statement(statement)
  write_triple(*statement.to_triple)
  self
end

- write_statements(*statements)

Deprecated.

replace by RDF::Writable#insert_statements

This method returns an undefined value.

self

Parameters:



294
295
296
297
# File 'lib/rdf/writer.rb', line 294

def write_statements(*statements)
  statements.flatten.each { |statement| write_statement(statement) }
  self
end

- write_triple(subject, predicate, object)

This method is abstract.

This method returns an undefined value.

self

Parameters:

Raises:

  • (NotImplementedError)

    unless implemented in subclass



323
324
325
# File 'lib/rdf/writer.rb', line 323

def write_triple(subject, predicate, object)
  raise NotImplementedError.new("#{self.class}#write_triple") # override in subclasses
end

- write_triples(*triples)

This method returns an undefined value.

self

Parameters:



311
312
313
314
# File 'lib/rdf/writer.rb', line 311

def write_triples(*triples)
  triples.each { |triple| write_triple(*triple) }
  self
end