Class: RDF::Reader Abstract

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

Overview

This class is abstract.

The base class for RDF parsers.

Examples:

Loading an RDF reader implementation

require 'rdf/ntriples'

Iterating over known RDF reader classes

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

Obtaining an RDF reader class

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

Instantiating an RDF reader class

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

Parsing RDF statements from a file

RDF::Reader.open("etc/doap.nt") do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

Parsing RDF statements from a string

data = StringIO.new(File.read("etc/doap.nt"))
RDF::Reader.for(:ntriples).new(data) do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

See Also:

Direct Known Subclasses

NTriples::Reader

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from Util::Aliasing::LateBound

alias_method

Methods included from Enumerable

#contexts, #dump, #each_context, #each_graph, #each_object, #each_predicate, #each_quad, #each_subject, #enum_context, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_triple, #has_context?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #quads, #statements, #subjects, #supports?, #to_a, #to_hash, #to_set, #triples, #valid?, #validate!

Methods included from Countable

#count, #empty?

Methods included from Readable

#readable?

Constructor Details

- (Reader) initialize(input = $stdin, options = {}) {|reader| ... }

Initializes the reader.

Parameters:

  • input (IO, File, String) (defaults to: $stdin)

    the input stream to read

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

    any additional options

Options Hash (options):

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

    the encoding of the input stream

  • :validate (Boolean) — default: false

    whether to validate the parsed statements and values

  • :canonicalize (Boolean) — default: false

    whether to canonicalize parsed literals

  • :intern (Boolean) — default: true

    whether to intern all parsed URIs

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

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

  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs (not supported by all readers)

Yields:

  • (reader)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rdf/reader.rb', line 195

def initialize(input = $stdin, options = {}, &block)
  @options = options.dup
  @options[:validate]     ||= false
  @options[:canonicalize] ||= false
  @options[:intern]       ||= true
  @options[:prefixes]     ||= Hash.new
  @options[:base_uri]     ||= input.base_uri if input.respond_to?(:base_uri)

  @input = case input
    when String then StringIO.new(input)
    else input
  end

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Enumerable

Instance Attribute Details

- (Hash) options (readonly)

Any additional options for this reader.

Returns:

  • (Hash)

Since:

  • 0.3.0



221
222
223
# File 'lib/rdf/reader.rb', line 221

def options
  @options
end

Class Method Details

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

Enumerates known RDF reader classes.

Yields:

  • (klass)

Yield Parameters:

  • klass (Class)

Returns:



51
52
53
# File 'lib/rdf/reader.rb', line 51

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

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

Finds an RDF reader class based on the given criteria.

If the reader class has a defined format, use that.

Overloads:

  • + (Class) for(format)

    Finds an RDF reader class based on a symbolic name.

    Parameters:

    • format (Symbol)

    Returns:

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

    Finds an RDF reader class based on a file name.

    Parameters:

    • filename (String)

    Returns:

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

    Finds an RDF reader class based on various options.

    Parameters:

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

    Options Hash (options):

    • :file_name (String, #to_s) — default: nil
    • :file_extension (Symbol, #to_sym) — default: nil
    • :content_type (String, #to_s) — default: nil
    • :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)
    • (Class)

Returns:

  • (Class)


89
90
91
92
93
94
# File 'lib/rdf/reader.rb', line 89

def self.for(options = {}, &block)
  options = options.merge(:has_reader => true) if options.is_a?(Hash)
  if format = self.format || Format.for(options, &block)
    format.reader
  end
end

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

Retrieves the RDF serialization format class for this reader class.

Returns:

  • (Class)


100
101
102
103
104
105
106
107
108
109
# File 'lib/rdf/reader.rb', line 100

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

+ (Object) open(filename, options = {}) {|reader| ... }

Note:

A reader returned via this method may not be readable depending on the processing model of the specific reader, as the file is only open during the scope of open. The reader is intended to be accessed through a block.

Parses input from the given file name or URL.

Examples:

Parsing RDF statements from a file

RDF::Reader.open("etc/doap.nt") do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

Parameters:

Options Hash (options):

  • :format (Symbol) — default: :ntriples

Yields:

  • (reader)

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Raises:



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/rdf/reader.rb', line 135

def self.open(filename, options = {}, &block)
  Util::File.open_file(filename, options) do |file|
    format_options = options.dup
    format_options[:content_type] ||= file.content_type if file.respond_to?(:content_type)
    format_options[:file_name] ||= filename
    options[:encoding] ||= file.encoding if file.respond_to?(:encoding)
    reader = self.for(format_options[:format] || format_options) do
      # Return a sample from the input file
      sample = file.read(1000)
      file.rewind
      sample
    end
    if reader
      reader.new(file, options, &block)
    else
      raise FormatError, "unknown RDF format: #{format_options.inspect}\nThis may be resolved with a require of the 'linkeddata' gem."
    end
  end
end

+ (Symbol) to_sym

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

Returns:

  • (Symbol)


158
159
160
161
162
163
# File 'lib/rdf/reader.rb', line 158

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

Instance Method Details

- (RDF::URI) base_uri

Returns the base URI determined by this reader.

Examples:

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

Returns:

Since:

  • 0.3.0



231
232
233
# File 'lib/rdf/reader.rb', line 231

def base_uri
  RDF::URI(@options[:base_uri]) if @options[:base_uri]
end

- (Boolean) canonicalize?

Returns true if parsed values should be canonicalized.

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



464
465
466
# File 'lib/rdf/reader.rb', line 464

def canonicalize?
  @options[:canonicalize]
end

- close Also known as: close!

This method returns an undefined value.

Closes the input stream, after which an IOError will be raised for further read attempts.

If the input stream is already closed, does nothing.



370
371
372
# File 'lib/rdf/reader.rb', line 370

def close
  @input.close unless @input.closed?
end

- each_statement {|statement| ... } - (Enumerator) each_statement Also known as: each

This method returns an undefined value.

Iterates the given block for each RDF statement.

If no block was given, returns an enumerator.

Statements are yielded in the order that they are read from the input stream.

Overloads:

  • - each_statement {|statement| ... }

    This method returns an undefined value.

    Yields:

    • (statement)

      each statement

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • - (Enumerator) each_statement

    Returns:

See Also:



305
306
307
308
309
310
311
312
313
314
# File 'lib/rdf/reader.rb', line 305

def each_statement(&block)
  if block_given?
    begin
      loop { block.call(read_statement) }
    rescue EOFError => e
      rewind rescue nil
    end
  end
  enum_for(:each_statement)
end

- each_triple {|subject, predicate, object| ... } - (Enumerator) each_triple

This method returns an undefined value.

Iterates the given block for each RDF triple.

If no block was given, returns an enumerator.

Triples are yielded in the order that they are read from the input stream.

Overloads:

  • - each_triple {|subject, predicate, object| ... }

    This method returns an undefined value.

    Yields:

    • (subject, predicate, object)

      each triple

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • - (Enumerator) each_triple

    Returns:

See Also:



339
340
341
342
343
344
345
346
347
348
# File 'lib/rdf/reader.rb', line 339

def each_triple(&block)
  if block_given?
    begin
      loop { block.call(*read_triple) }
    rescue EOFError => e
      rewind rescue nil
    end
  end
  enum_for(:each_triple)
end

- (Encoding) encoding

Returns the encoding of the input stream.

Returns:

  • (Encoding)


439
440
441
442
443
444
445
446
447
448
# File 'lib/rdf/reader.rb', line 439

def encoding
  case @options[:encoding]
  when String, Symbol
    Encoding.find(@options[:encoding].to_s)
  when Encoding
    @options[:encoding]
  else
    @options[:encoding] ||= Encoding.find(self.class.format.content_encoding.to_s)
  end
end

- fail_object (protected)

This method returns an undefined value.

Raises an "expected object" parsing error on the current line.

Raises:



429
430
431
432
# File 'lib/rdf/reader.rb', line 429

def fail_object
  raise RDF::ReaderError.new("ERROR [line #{lineno}] Expected object (found: #{current_line.inspect})",
                             lineno: lineno)
end

- fail_predicate (protected)

This method returns an undefined value.

Raises an "expected predicate" parsing error on the current line.

Raises:



419
420
421
422
# File 'lib/rdf/reader.rb', line 419

def fail_predicate
  raise RDF::ReaderError.new("ERROR [line #{lineno}] Expected predicate (found: #{current_line.inspect})",
                             lineno: lineno)
end

- fail_subject (protected)

This method returns an undefined value.

Raises an "expected subject" parsing error on the current line.

Raises:



409
410
411
412
# File 'lib/rdf/reader.rb', line 409

def fail_subject
  raise RDF::ReaderError.new("ERROR [line #{lineno}] Expected subject (found: #{current_line.inspect})",
                             lineno: lineno)
end

- (Boolean) intern?

Returns true if parsed URIs should be interned.

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



473
474
475
# File 'lib/rdf/reader.rb', line 473

def intern?
  @options[:intern]
end

- (Integer) lineno

Current line number being processed. For formats that can associate generated Statement with a particular line number from input, this value reflects that line number.

Returns:

  • (Integer)


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

def lineno
  @input.lineno
end

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

Defines the given named URI prefix for this reader.

Examples:

Defining a URI prefix

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

Returning a URI prefix

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

Overloads:

  • - (RDF::URI) prefix(name, uri)

    Parameters:

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

    Parameters:

    • name (Symbol, #to_s)

Returns:



279
280
281
282
# File 'lib/rdf/reader.rb', line 279

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 reader.

Examples:

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

Returns:

Since:

  • 0.3.0



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

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

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

Defines the given URI prefixes for this reader.

Examples:

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

Parameters:

Returns:

Since:

  • 0.3.0



258
259
260
# File 'lib/rdf/reader.rb', line 258

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

- (RDF::Statement) read_statement (protected)

This method is abstract.

Reads a statement from the input stream.

Returns:

Raises:

  • (NotImplementedError)

    unless implemented in subclass



390
391
392
# File 'lib/rdf/reader.rb', line 390

def read_statement
  Statement.new(*read_triple)
end

- (Array(RDF::Term)) read_triple (protected)

This method is abstract.

Reads a triple from the input stream.

Returns:

Raises:

  • (NotImplementedError)

    unless implemented in subclass



400
401
402
# File 'lib/rdf/reader.rb', line 400

def read_triple
  raise NotImplementedError, "#{self.class}#read_triple" # override in subclasses
end

- rewind Also known as: rewind!

This method returns an undefined value.

Rewinds the input stream to the beginning of input.



356
357
358
# File 'lib/rdf/reader.rb', line 356

def rewind
  @input.rewind
end

- (Symbol) to_sym

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

Returns:

  • (Symbol)


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

def to_sym
  self.class.to_sym
end

- (Boolean) validate?

Returns true if parsed statements and values should be validated.

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



455
456
457
# File 'lib/rdf/reader.rb', line 455

def validate?
  @options[:validate]
end