Class: RDF::Reader Abstract
- Inherits:
-
Object
- Object
- RDF::Reader
- Extended by:
- Enumerable, Util::Aliasing::LateBound
- Includes:
- Enumerable, Readable
- Defined in:
- lib/rdf/reader.rb
Overview
The base class for RDF parsers.
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Hash) options
readonly
Any additional options for this reader.
Class Method Summary (collapse)
-
+ (Enumerator) each {|klass| ... }
Enumerates known RDF reader classes.
-
+ (Class) for(options = {}, &block)
Finds an RDF reader class based on the given criteria.
-
+ (Class) format(klass = nil)
(also: format_class)
Retrieves the RDF serialization format class for this reader class.
-
+ (Object) open(filename, options = {}) {|reader| ... }
Parses input from the given file name or URL.
-
+ (Symbol) to_sym
Returns a symbol appropriate to use with RDF::Reader.for().
Instance Method Summary (collapse)
-
- (Hash{Symbol => RDF::URI}) base_uri
Returns the base URI determined by this reader.
-
- (Boolean) canonicalize?
protected
Returns
trueif parsed values should be canonicalized. -
- close
(also: #close!)
Closes the input stream, after which an
IOErrorwill be raised for further read attempts. -
- each_statement(&block)
(also: #each)
Iterates the given block for each RDF statement.
-
- each_triple(&block)
Iterates the given block for each RDF triple.
-
- (Encoding) encoding
protected
Returns the encoding of the input stream.
-
- fail_object
protected
Raises an "expected object" parsing error on the current line.
-
- fail_predicate
protected
Raises an "expected predicate" parsing error on the current line.
-
- fail_subject
protected
Raises an "expected subject" parsing error on the current line.
-
- (Reader) initialize(input = $stdin, options = {}) {|reader| ... }
constructor
Initializes the reader.
-
- (Boolean) intern?
protected
Returns
trueif parsed URIs should be interned. -
- (Integer) lineno
Current line number being processed.
-
- (RDF::URI) prefix(name, uri = nil)
(also: #prefix!)
Defines the given named URI prefix for this reader.
-
- (Hash{Symbol => RDF::URI}) prefixes
Returns the URI prefixes currently defined for this reader.
-
- (Hash{Symbol => RDF::URI}) prefixes=(prefixes)
Defines the given URI prefixes for this reader.
-
- (RDF::Statement) read_statement
protected
abstract
Reads a statement from the input stream.
-
- (Array(RDF::Term)) read_triple
protected
abstract
Reads a triple from the input stream.
-
- rewind
(also: #rewind!)
Rewinds the input stream to the beginning of input.
-
- (Symbol) to_sym
Returns a symbol appropriate to use with RDF::Reader.for().
-
- (Boolean) validate?
protected
Returns
trueif parsed statements and values should be validated.
Methods included from Util::Aliasing::LateBound
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
Methods included from Readable
Constructor Details
- (Reader) initialize(input = $stdin, options = {}) {|reader| ... }
Initializes the reader.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/rdf/reader.rb', line 186 def initialize(input = $stdin, = {}, &block) @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.
212 213 214 |
# File 'lib/rdf/reader.rb', line 212 def @options end |
Class Method Details
+ (Enumerator) each {|klass| ... }
Enumerates known RDF reader classes.
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.
89 90 91 92 93 94 |
# File 'lib/rdf/reader.rb', line 89 def self.for( = {}, &block) = .merge(:has_reader => true) if .is_a?(Hash) if format = self.format || Format.for(, &block) format.reader end end |
+ (Class) format(klass = nil) Also known as: format_class
Retrieves the RDF serialization format class for this reader 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| ... }
Parses input from the given file name or URL.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/rdf/reader.rb', line 126 def self.open(filename, = {}, &block) Util::File.open_file(filename, ) do |file| = .dup [:content_type] ||= file.content_type if file.respond_to?(:content_type) [:file_name] ||= filename [:encoding] ||= file.encoding if file.respond_to?(:encoding) reader = self.for([:format] || ) do # Return a sample from the input file sample = file.read(1000) file.rewind sample end if reader reader.new(file, , &block) else raise FormatError, "unknown RDF format: #{.inspect}" end end end |
+ (Symbol) to_sym
Returns a symbol appropriate to use with RDF::Reader.for()
149 150 151 152 153 154 |
# File 'lib/rdf/reader.rb', line 149 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
- (Hash{Symbol => RDF::URI}) base_uri
Returns the base URI determined by this reader.
222 223 224 |
# File 'lib/rdf/reader.rb', line 222 def base_uri @options[:base_uri] end |
- (Boolean) canonicalize? (protected)
Returns true if parsed values should be canonicalized.
453 454 455 |
# File 'lib/rdf/reader.rb', line 453 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.
361 362 363 |
# File 'lib/rdf/reader.rb', line 361 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.
296 297 298 299 300 301 302 303 304 305 |
# File 'lib/rdf/reader.rb', line 296 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.
330 331 332 333 334 335 336 337 338 339 |
# File 'lib/rdf/reader.rb', line 330 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.
428 429 430 431 432 433 434 435 436 437 |
# File 'lib/rdf/reader.rb', line 428 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.
418 419 420 |
# File 'lib/rdf/reader.rb', line 418 def fail_object raise RDF::ReaderError, "expected object in line #{lineno}: #{current_line.inspect}" end |
- fail_predicate (protected)
This method returns an undefined value.
Raises an "expected predicate" parsing error on the current line.
409 410 411 |
# File 'lib/rdf/reader.rb', line 409 def fail_predicate raise RDF::ReaderError, "expected predicate in line #{lineno}: #{current_line.inspect}" end |
- fail_subject (protected)
This method returns an undefined value.
Raises an "expected subject" parsing error on the current line.
400 401 402 |
# File 'lib/rdf/reader.rb', line 400 def fail_subject raise RDF::ReaderError, "expected subject in line #{lineno}: #{current_line.inspect}" end |
- (Boolean) intern? (protected)
Returns true if parsed URIs should be interned.
462 463 464 |
# File 'lib/rdf/reader.rb', line 462 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.
369 370 371 |
# File 'lib/rdf/reader.rb', line 369 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.
270 271 272 273 |
# File 'lib/rdf/reader.rb', line 270 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.
234 235 236 |
# File 'lib/rdf/reader.rb', line 234 def prefixes @options[:prefixes] ||= {} end |
- (Hash{Symbol => RDF::URI}) prefixes=(prefixes)
Defines the given URI prefixes for this reader.
249 250 251 |
# File 'lib/rdf/reader.rb', line 249 def prefixes=(prefixes) @options[:prefixes] = prefixes end |
- (RDF::Statement) read_statement (protected)
Reads a statement from the input stream.
381 382 383 |
# File 'lib/rdf/reader.rb', line 381 def read_statement Statement.new(*read_triple) end |
- (Array(RDF::Term)) read_triple (protected)
Reads a triple from the input stream.
391 392 393 |
# File 'lib/rdf/reader.rb', line 391 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.
347 348 349 |
# File 'lib/rdf/reader.rb', line 347 def rewind @input.rewind end |
- (Symbol) to_sym
Returns a symbol appropriate to use with RDF::Reader.for()
159 160 161 |
# File 'lib/rdf/reader.rb', line 159 def to_sym self.class.to_sym end |
- (Boolean) validate? (protected)
Returns true if parsed statements and values should be validated.
444 445 446 |
# File 'lib/rdf/reader.rb', line 444 def validate? @options[:validate] end |