Class: RDF::Format Abstract

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/rdf/format.rb

Overview

This class is abstract.

The base class for RDF serialization formats.

Examples:

Loading an RDF serialization format implementation

require 'rdf/ntriples'

Iterating over known RDF serialization formats

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

Getting a serialization format class

RDF::Format.for(:ntriples)     #=> RDF::NTriples::Format
RDF::Format.for("etc/doap.nt")
RDF::Format.for(file_name: "etc/doap.nt")
RDF::Format.for(file_extension: "nt")
RDF::Format.for(content_type: "application/n-triples")

Obtaining serialization format MIME types

RDF::Format.content_types      #=> {"application/n-triples" => [RDF::NTriples::Format]}

Obtaining serialization format file extension mappings

RDF::Format.file_extensions    #=> {nt: [RDF::NTriples::Format]}

Defining a new RDF serialization format class

class RDF::NTriples::Format < RDF::Format
  content_type     'application/n-triples', extension: :nt
  content_encoding 'utf-8'

  reader RDF::NTriples::Reader
  writer RDF::NTriples::Writer
end

Instantiating an RDF reader or writer class (1)

RDF::Format.for(:ntriples).reader.new($stdin)  { |reader| ... }
RDF::Format.for(:ntriples).writer.new($stdout) { |writer| ... }

Instantiating an RDF reader or writer class (2)

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

See Also:

Class Method Summary collapse

Class Method Details

.accept_typeArray<String>

Returns an array of values appropriate for an Accept header. Same as self.content_type, if no parameter is given when defined.

Returns:

  • (Array<String>)


474
475
476
# File 'lib/rdf/format.rb', line 474

def self.accept_type
  @@accept_types.map {|t, formats| t if formats.include?(self)}.compact
end

.accept_typesArray<String>

Returns the set of content types with quality for available RDF::Reader subclasses.

Examples:


accept_types = RDF::Format.accept_types
# => %w(text/html;q=0.5 text/turtle ...)

Returns:

  • (Array<String>)


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

def self.accept_types
  reader_symbols.flat_map {|s| RDF::Format.for(s).accept_type}.uniq
end

.cli_commandsHash{Symbol => {description: String, lambda: Lambda(Array, Hash)}}

Hash of CLI commands appropriate for this format

Returns:

  • (Hash{Symbol => {description: String, lambda: Lambda(Array, Hash)}})


378
379
380
# File 'lib/rdf/format.rb', line 378

def self.cli_commands
  {}
end

.content_encoding(encoding = nil) (protected)

This method returns an undefined value.

Defines the content encoding for this RDF serialization format.

When called without an encoding, it returns the currently defined content encoding for this format

Parameters:

  • encoding (#to_sym) (defaults to: nil)


512
513
514
515
# File 'lib/rdf/format.rb', line 512

def self.content_encoding(encoding = nil)
  @@content_encoding[self] = encoding.to_sym if encoding
  @@content_encoding[self] || "utf-8"
end

.content_type(type, options) .content_typeArray<String>

Retrieves or defines MIME content types for this RDF serialization format.

Overloads:

  • .content_type(type, options)

    This method returns an undefined value.

    Retrieves or defines the MIME content type for this RDF serialization format.

    Optionally also defines alias MIME content types for this RDF serialization format.

    Optionally also defines a file extension, or a list of file extensions, that should be mapped to the given MIME type and handled by this class.

    Optionally, both type, alias, and aliases, may be parameterized for expressing quality.

    content_type "text/html;q=0.4"
    

    Parameters:

    • type (String)
    • options (Hash{Symbol => Object})

    Options Hash (options):

    • :alias (String) — default: nil
    • :aliases (Array<String>) — default: nil
    • :extension (Symbol) — default: nil
    • :extensions (Array<Symbol>) — default: nil
  • .content_typeArray<String>

    Retrieves the MIME content types for this RDF serialization format.

    The return is an array where the first element is the cannonical MIME type for the format and following elements are alias MIME types.

    Returns:

    • (Array<String>)


436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/rdf/format.rb', line 436

def self.content_type(type = nil, options = {})
  if type.nil?
    [@@content_type[self], @@content_types.map {
      |ct, cl| (cl.include?(self) && ct != @@content_type[self]) ?  ct : nil }].flatten.compact
  else
    accept_type, type = type, type.split(';').first
    @@content_type[self] = type
    @@content_types[type] ||= []
    @@content_types[type] << self unless @@content_types[type].include?(self)

    @@accept_types[accept_type] ||= []
    @@accept_types[accept_type] << self unless @@accept_types[accept_type].include?(self)

    if extensions = (options[:extension] || options[:extensions])
      extensions = Array(extensions).map(&:to_sym)
      extensions.each do |ext|
        @@file_extensions[ext] ||= []
        @@file_extensions[ext] << self unless @@file_extensions[ext].include?(self)
      end
    end
    if aliases = (options[:alias] || options[:aliases])
      aliases = Array(aliases).each do |a|
        aa = a.split(';').first
        @@accept_types[a] ||= []
        @@accept_types[a] << self unless @@accept_types[a].include?(self)

        @@content_types[aa] ||= []
        @@content_types[aa] << self unless @@content_types[aa].include?(self)
      end
    end
  end
end

.content_typesHash{String => Array<Class>}

Returns MIME content types for known RDF serialization formats.

Examples:

retrieving a list of supported Mime types


RDF::Format.content_types.keys

Returns:

  • (Hash{String => Array<Class>})


168
169
170
# File 'lib/rdf/format.rb', line 168

def self.content_types
  @@content_types
end

.detect(sample) ⇒ Boolean

Use a text sample to detect the format of an input file. Sub-classes implement a matcher sufficient to detect probably format matches, including disambiguating between other similar formats.

Used to determine format class from loaded formats by for when a match cannot be unambigiously found otherwise.

Examples:

RDF::NTriples::Format.detect("<a> <b> <c> .") #=> true

Parameters:

  • sample (String)

    Beginning several bytes (~ 1K) of input.

Returns:

  • (Boolean)


395
396
397
# File 'lib/rdf/format.rb', line 395

def self.detect(sample)
  false
end

.each {|klass| ... } ⇒ Enumerator

Enumerates known RDF serialization format classes.

Yields:

  • (klass)

Yield Parameters:

  • (Class)

Returns:

  • (Enumerator)


54
55
56
# File 'lib/rdf/format.rb', line 54

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

.file_extensionArray<String>

Retrieves or defines file extensions for this RDF serialization format.

The return is an array where the first element is the cannonical file extension for the format and following elements are alias file extensions.

Returns:

  • (Array<String>)


485
486
487
# File 'lib/rdf/format.rb', line 485

def self.file_extension
  @@file_extensions.map {|ext, formats| ext if formats.include?(self)}.compact
end

.file_extensionsHash{Symbol => Array<Class>}

Returns file extensions for known RDF serialization formats.

Examples:

retrieving a list of supported file extensions


RDF::Format.file_extensions.keys

Returns:

  • (Hash{Symbol => Array<Class>})


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

def self.file_extensions
  @@file_extensions
end

.for(format) ⇒ Class .for(filename) ⇒ Class .for(**options) ⇒ Class

Finds an RDF serialization format class based on the given criteria. If multiple formats are identified, the last one found is returned; this allows descrimination of equivalent formats based on load order.

Overloads:

  • .for(format) ⇒ Class

    Finds an RDF serialization format class based on a symbolic name.

    Parameters:

    • format (Symbol)

    Returns:

    • (Class)
  • .for(filename) ⇒ Class

    Finds an RDF serialization format class based on a file name.

    Parameters:

    Returns:

    • (Class)
  • .for(**options) ⇒ Class

    Finds an RDF serialization format class based on various options.

    Parameters:

    • options (Hash{Symbol => Object})

    Options Hash (**options):

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

      Note that content_type will be taken from a URL opened using Util::File.open_file.

    • :has_reader (Boolean) — default: false

      Only return a format having a reader.

    • :has_writer (Boolean) — default: false

      Only return a format having a writer.

    • :sample (String) — default: nil

      A sample of input used for performing format detection. If we find no formats, or we find more than one, and we have a sample, we can perform format detection to find a specific format to use, in which case we pick the last one we find

    Yield Returns:

    • (String)

      another way to provide a sample, allows lazy for retrieving the sample.

    Returns:

    • (Class)

Returns:

  • (Class)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rdf/format.rb', line 91

def self.for(options = {})
  format = case options
    when String, RDF::URI
      # Find a format based on the file name
      fn, options = options, {}
      self.for(file_name: fn) { yield if block_given? }

    when Hash
      case
        # Find a format based on the MIME content type:
        when mime_type = options[:content_type]
          # @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
          # @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7
          mime_type = mime_type.to_s
          mime_type = mime_type.split(';').first # remove any media type parameters

          # Ignore text/plain, a historical encoding for N-Triples, which is
          # problematic in format detection, as many web servers will serve
          # content by default text/plain.
          content_types[mime_type] unless mime_type == 'text/plain' && (options[:sample] || block_given?)
        # Find a format based on the file name:
        when file_name = options[:file_name]
          self.for(file_extension: File.extname(RDF::URI(file_name).path.to_s)[1..-1]) { yield if block_given? }
        # Find a format based on the file extension:
        when file_ext  = options[:file_extension]
          file_extensions[file_ext.to_sym]
      end

    when Symbol
      # Try to find a match based on the full class name
      # We want this to work even if autoloading fails
      fmt, options = options, {}
      classes = @@subclasses.select { |klass| klass.symbols.include?(fmt) }
      if classes.empty?
        classes = case fmt
        when :ntriples then [RDF::NTriples::Format]
        when :nquads   then [RDF::NQuads::Format]
        else                []
        end
      end
      classes
  end

  if format.is_a?(Array)
    format = format.select {|f| f.reader} if options[:has_reader]
    format = format.select {|f| f.writer} if options[:has_writer]
    
    return format.last if format.uniq.length == 1
  elsif !format.nil?
    return format
  end

  # If we have a sample, use that for format detection
  if sample = (options[:sample] if options.is_a?(Hash)) || (yield if block_given?)
    sample = sample.dup.to_s
    sample.force_encoding(Encoding::ASCII_8BIT) if sample.respond_to?(:force_encoding)
    # Given a sample, perform format detection across the appropriate formats, choosing the last that matches
    format ||= @@subclasses

    # Return last format that has a positive detection
    format.reverse.detect {|f| f.detect(sample)} || format.last
  elsif format.is_a?(Array)
    # Otherwise, just return the last matching format
    format.last
  else
    nil
  end
end

.nameSymbol

Returns a human-readable name for the format. Subclasses should override this to use something difererent than the Class name.

Examples:


RDF::NTriples::Format.name => "N-Triples"

Returns:

  • (Symbol)


282
283
284
285
286
287
# File 'lib/rdf/format.rb', line 282

def self.name
  elements = self.to_s.split("::")
  name = elements.pop
  name = elements.pop if name == 'Format'
  name.to_s
end

.reader(klass) .reader { ... } .readerClass Also known as: reader_class

This method returns an undefined value.

Retrieves or defines the reader class for this RDF serialization format.

Overloads:

  • .reader(klass)

    This method returns an undefined value.

    Defines the reader class for this RDF serialization format.

    The class should be a subclass of Reader, or implement the same interface.

    Parameters:

    • klass (Class)
  • .reader { ... }

    This method returns an undefined value.

    Defines the reader class for this RDF serialization format.

    The block should return a subclass of Reader, or a class that implements the same interface. The block won't be invoked until the reader class is first needed.

    Yields:

    Yield Returns:

    • (Class)

      klass

  • .readerClass

    Retrieves the reader class for this RDF serialization format.

    Returns:

    • (Class)


319
320
321
322
323
324
325
326
327
328
329
330
# File 'lib/rdf/format.rb', line 319

def self.reader(klass = nil, &block)
  case
    when klass
      @@readers[self] = klass
    when block_given?
      @@readers[self] = block
    else
      klass = @@readers[self]
      klass = @@readers[self] = klass.call if klass.is_a?(Proc)
      klass
  end
end

.reader_symbolsArray<Symbol>

Returns the set of format symbols for available RDF::Reader subclasses.

Examples:


symbols = RDF::Format.reader_symbols
format = RDF::Format.for(symbols.first)

Returns:

  • (Array<Symbol>)


193
194
195
# File 'lib/rdf/format.rb', line 193

def self.reader_symbols
  @@readers.keys.map(&:symbols).flatten.uniq
end

.reader_typesArray<String>

Returns the set of content types for available RDF::Reader subclasses.

Examples:


content_types = RDF::Format.reader_types
format = RDF::Format.for(content_type: content_types.first)

Returns:

  • (Array<String>)


206
207
208
# File 'lib/rdf/format.rb', line 206

def self.reader_types
  reader_symbols.flat_map {|s| RDF::Format.for(s).content_type}.uniq
end

.require(library) (protected)

This method returns an undefined value.

Defines a required Ruby library for this RDF serialization format.

The given library will be required lazily, i.e. only when it is actually first needed, such as when instantiating a reader or parser instance for this format.

Parameters:

  • library (String, #to_s)


500
501
502
# File 'lib/rdf/format.rb', line 500

def self.require(library)
  (@@requires[self] ||= []) << library.to_s
end

.symbolsArray<Symbol>

Note:

Individual formats can override this to provide an array of symbols; otherwise, it uses self.to_sym

Returns the set of symbols for a writer appropriate for use with with RDF::Format.for()

Returns:

  • (Array<Symbol>)

See Also:

Since:

  • 2.0



268
269
270
# File 'lib/rdf/format.rb', line 268

def self.symbols
  [self.to_sym]
end

.to_symSymbol

Note:

Defaults to the last element of the class name before Format downcased and made a symbol. Individual formats can override this.

Returns a symbol appropriate to use with RDF::Format.for()

Returns:

  • (Symbol)


254
255
256
257
258
259
# File 'lib/rdf/format.rb', line 254

def self.to_sym
  elements = self.to_s.split("::")
  sym = elements.pop
  sym = elements.pop if sym == 'Format'
  sym.downcase.to_s.to_sym if sym.is_a?(String)
end

.writer(klass) .writer { ... } .writerClass Also known as: writer_class

This method returns an undefined value.

Retrieves or defines the writer class for this RDF serialization format.

Overloads:

  • .writer(klass)

    This method returns an undefined value.

    Defines the writer class for this RDF serialization format.

    The class should be a subclass of Writer, or implement the same interface.

    Parameters:

    • klass (Class)
  • .writer { ... }

    This method returns an undefined value.

    Defines the writer class for this RDF serialization format.

    The block should return a subclass of Writer, or a class that implements the same interface. The block won't be invoked until the writer class is first needed.

    Yields:

    Yield Returns:

    • (Class)

      klass

  • .writerClass

    Retrieves the writer class for this RDF serialization format.

    Returns:

    • (Class)


362
363
364
365
366
367
368
369
370
371
372
373
# File 'lib/rdf/format.rb', line 362

def self.writer(klass = nil, &block)
  case
    when klass
      @@writers[self] = klass
    when block_given?
      @@writers[self] = block
    else
      klass = @@writers[self]
      klass = @@writers[self] = klass.call if klass.is_a?(Proc)
      klass
  end
end

.writer_symbolsArray<Symbol>

Returns the set of format symbols for available RDF::Writer subclasses.

Examples:


symbols = RDF::Format.writer_symbols
format = RDF::Format.for(symbols.first)

Returns:

  • (Array<Symbol>)


232
233
234
# File 'lib/rdf/format.rb', line 232

def self.writer_symbols
  @@writers.keys.map(&:symbols).flatten.uniq
end

.writer_typesArray<String>

Returns the set of content types for available RDF::Writer subclasses.

Examples:


content_types = RDF::Format.writer_types
format = RDF::Format.for(content_type: content_types.first)

Returns:

  • (Array<String>)


245
246
247
# File 'lib/rdf/format.rb', line 245

def self.writer_types
  writer_symbols.flat_map {|s| RDF::Format.for(s).content_type}.uniq
end