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 => "text/plain")

Obtaining serialization format MIME types

RDF::Format.content_types      #=> {"text/plain" => [RDF::NTriples::Format]}

Obtaining serialization format file extension mappings

RDF::Format.file_extensions    #=> {:nt => "text/plain"}

Defining a new RDF serialization format class

class RDF::NTriples::Format < RDF::Format
  content_type     'text/plain', :extension => :nt
  content_encoding 'ascii'

  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:

Direct Known Subclasses

NQuads::Format, NTriples::Format

Constant Summary

@@requires =
{}
@@file_extensions =
{}
@@content_type =
{}
@@content_types =
{}
@@content_encoding =
{}
@@readers =
{}
@@writers =
{}

Class Method Summary (collapse)

Class Method Details

+ content_encoding(encoding) (protected)

This method returns an undefined value.

Defines the content encoding for this RDF serialization format.

Parameters:



348
349
350
# File 'lib/rdf/format.rb', line 348

def self.content_encoding(encoding)
  @@content_encoding[self] = encoding.to_sym
end

+ content_type(type, options) + (Array<String>) content_type

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.

    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
  • + (Array<String>) content_type

    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>)


310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/rdf/format.rb', line 310

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
    @@content_type[self] = type
    (@@content_types[type] ||= []) << self

    if extensions = (options[:extension] || options[:extensions])
      extensions = [extensions].flatten.map(&:to_sym)
      extensions.each { |ext| @@file_extensions[ext] = type }
    end
    if aliases = (options[:alias] || options[:aliases])
      aliases = [aliases].flatten.each { |a| (@@content_types[a] ||= []) << self }
    end
  end
end

+ (Hash{String => Array<Class>}) content_types

Returns MIME content types for known RDF serialization formats.

Returns:

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


152
153
154
# File 'lib/rdf/format.rb', line 152

def self.content_types
  @@content_types
end

+ (Boolean) detect(sample)

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)


274
275
276
# File 'lib/rdf/format.rb', line 274

def self.detect(sample)
  false
end

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

Enumerates known RDF serialization format classes.

Yields:

  • (klass)

Yield Parameters:

  • (Class)

Returns:



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

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

+ (Hash{Symbol => String}) file_extensions

Returns file extensions for known RDF serialization formats.

Returns:

  • (Hash{Symbol => String})


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

def self.file_extensions
  @@file_extensions
end

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

Finds an RDF serialization format class based on the given criteria.

Overloads:

  • + (Class) for(format)

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

    Parameters:

    • format (Symbol)

    Returns:

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

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

    Parameters:

    • filename (String)

    Returns:

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

    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.

    • :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 first one we find

    Yield Returns:

    • (String)

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

    Returns:

    • (Class)

Returns:

  • (Class)


90
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
# File 'lib/rdf/format.rb', line 90

def self.for(options = {})
  format = case options
    when String
      # Find a format based on the file name
      self.for(:file_name => options)

    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 if mime_type.include?(?;) # remove any media type parameters
          content_types[mime_type]
        # Find a format based on the file name:
        when file_name = options[:file_name]
          self.for(:file_extension => File.extname(file_name.to_s)[1..-1])
        # Find a format based on the file extension:
        when file_ext  = options[:file_extension]
          if file_extensions.has_key?(file_ext = file_ext.to_sym)
            self.for(:content_type => file_extensions[file_ext])
          end
      end

    when Symbol
      case format = options
        # Special case, since we want this to work despite autoloading
        when :ntriples
          RDF::NTriples::Format
        # For anything else, find a match based on the full class name
        else
          @@subclasses.each do |klass|
            if klass.to_sym == format ||
               klass.name.to_s.split('::').map(&:downcase).include?(format.to_s.downcase)
              return klass
            end
          end
          nil # not found
      end
  end
  
  if format.is_a?(Array)
    return format.first if format.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?)
    # Given a sample, perform format detection across the appropriate formats, choosing
    # the first that matches
    format ||= @@subclasses

    format.detect {|f| f.detect(sample)}
  end
end

+ reader(klass) + reader { ... } + (Class) reader 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

  • + (Class) reader

    Retrieves the reader class for this RDF serialization format.

    Returns:

    • (Class)


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

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

+ 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)


339
340
341
# File 'lib/rdf/format.rb', line 339

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

+ (Symbol) to_sym

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

Returns:

  • (Symbol)


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

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

+ writer(klass) + writer { ... } + (Class) writer 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

  • + (Class) writer

    Retrieves the writer class for this RDF serialization format.

    Returns:

    • (Class)


247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/rdf/format.rb', line 247

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