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?, #objects, #predicates, #quads, #statements, #subjects, #to_a, #to_hash, #to_set, #triples

Methods included from Countable

#count, #empty?

Methods included from Readable

#readable?

Constructor Details

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

Initializes the reader.

Parameters:

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

    the input stream to read

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

    any additional options

Options Hash (options):

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

    the encoding of the input stream (Ruby 1.9+)

  • (Boolean) :validate — default: false

    whether to validate the parsed statements and values

  • (Boolean) :canonicalize — default: false

    whether to canonicalize parsed literals

  • (Boolean) :intern — default: true

    whether to intern all parsed URIs

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

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

  • (#to_s) :base_uri — 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



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rdf/reader.rb', line 153

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

  @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

Instance Attribute Details

- (Hash) options (readonly)

Any additional options for this reader.

Returns:

  • (Hash)

Since:

  • 0.3.0



178
179
180
# File 'lib/rdf/reader.rb', line 178

def options
  @options
end

Class Method Details

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

Enumerates known RDF reader classes.

Yields:

  • (klass)

Yield Parameters:

  • (Class) klass

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.

Overloads:

  • + (Class) for(format)

    Finds an RDF reader class based on a symbolic name.

    Parameters:

    • (Symbol) format

    Returns:

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

    Finds an RDF reader class based on a file name.

    Parameters:

    • (String) filename

    Returns:

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

    Finds an RDF reader 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)


80
81
82
83
84
# File 'lib/rdf/reader.rb', line 80

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

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

Retrieves the RDF serialization format class for this writer class.

Returns:

  • (Class)


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

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

Parses input from the given file name or URL.

Parameters:

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

    any additional options (see #initialize and Format.for)

Options Hash (options):

  • (Symbol) :format — default: :ntriples

Yields:

  • (reader)

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Raises:



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rdf/reader.rb', line 116

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
    reader = self.for(format_options)
    if reader
      reader.new(file, options, &block)
    else
      raise FormatError, "unknown RDF format: #{options[:format] || {:file_name => filename, :content_type => content_type}.inspect}"
    end
  end
end

Instance Method Details

- (Boolean) canonicalize? (protected)

Returns true if parsed values should be canonicalized.

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



393
394
395
# File 'lib/rdf/reader.rb', line 393

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.



315
316
317
# File 'lib/rdf/reader.rb', line 315

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:



250
251
252
253
254
255
256
257
258
259
# File 'lib/rdf/reader.rb', line 250

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:



284
285
286
287
288
289
290
291
292
293
# File 'lib/rdf/reader.rb', line 284

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

Returns the encoding of the input stream.

Note: this method requires Ruby 1.9 or newer.

Returns:

  • (Encoding)


375
376
377
# File 'lib/rdf/reader.rb', line 375

def encoding
  @options[:encoding] ||= Encoding::UTF_8
end

- fail_object (protected)

This method returns an undefined value.

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

Raises:



365
366
367
# File 'lib/rdf/reader.rb', line 365

def fail_object
  raise RDF::ReaderError, "expected object in #{@input.inspect} line #{lineno}"
end

- fail_predicate (protected)

This method returns an undefined value.

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

Raises:



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

def fail_predicate
  raise RDF::ReaderError, "expected predicate in #{@input.inspect} line #{lineno}"
end

- fail_subject (protected)

This method returns an undefined value.

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

Raises:



347
348
349
# File 'lib/rdf/reader.rb', line 347

def fail_subject
  raise RDF::ReaderError, "expected subject in #{@input.inspect} line #{lineno}"
end

- (Boolean) intern? (protected)

Returns true if parsed URIs should be interned.

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



402
403
404
# File 'lib/rdf/reader.rb', line 402

def intern?
  @options[:intern]
end

- (Object) prefix(name, uri) - (Object) 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:

  • - (Object) prefix(name, uri)

    Parameters:

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

    Parameters:

    • (Symbol, #to_s) name

Returns:



224
225
226
227
# File 'lib/rdf/reader.rb', line 224

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



188
189
190
# File 'lib/rdf/reader.rb', line 188

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



203
204
205
# File 'lib/rdf/reader.rb', line 203

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



328
329
330
# File 'lib/rdf/reader.rb', line 328

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



338
339
340
# File 'lib/rdf/reader.rb', line 338

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.



301
302
303
# File 'lib/rdf/reader.rb', line 301

def rewind
  @input.rewind
end

- (Boolean) validate? (protected)

Returns true if parsed statements and values should be validated.

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.0



384
385
386
# File 'lib/rdf/reader.rb', line 384

def validate?
  @options[:validate]
end