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
-
#options ⇒ Hash
readonly
Any additional options for this reader.
Class Method Summary collapse
-
.each {|klass| ... } ⇒ Enumerator
Enumerates known RDF reader classes.
-
.for(options = {}, &block) ⇒ Class
Finds an RDF reader class based on the given criteria.
-
.format(klass = nil) ⇒ Class
(also: format_class)
Retrieves the RDF serialization format class for this reader class.
-
.open(filename, options = {}) {|reader| ... } ⇒ Object
Parses input from the given file name or URL.
-
.to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Reader.for().
Instance Method Summary collapse
-
#base_uri ⇒ Hash{Symbol => RDF::URI}
Returns the base URI determined by this reader.
-
#close ⇒ void
(also: #close!)
Closes the input stream, after which an ‘IOError` will be raised for further read attempts.
-
#each_statement(&block) ⇒ void
(also: #each)
Iterates the given block for each RDF statement.
-
#each_triple(&block) ⇒ void
Iterates the given block for each RDF triple.
-
#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ Reader
constructor
Initializes the reader.
-
#prefix(name, uri = nil) ⇒ RDF::URI
(also: #prefix!)
Defines the given named URI prefix for this reader.
-
#prefixes ⇒ Hash{Symbol => RDF::URI}
Returns the URI prefixes currently defined for this reader.
-
#prefixes=(prefixes) ⇒ Hash{Symbol => RDF::URI}
Defines the given URI prefixes for this reader.
-
#rewind ⇒ void
(also: #rewind!)
Rewinds the input stream to the beginning of input.
-
#to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Reader.for().
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?, #objects, #predicates, #quads, #statements, #subjects, #supports?, #to_a, #to_hash, #to_set, #triples
Methods included from Countable
Methods included from Readable
Constructor Details
#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ Reader
Initializes the reader.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/rdf/reader.rb', line 185 def initialize(input = $stdin, = {}, &block) @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
#options ⇒ Hash (readonly)
Any additional options for this reader.
210 211 212 |
# File 'lib/rdf/reader.rb', line 210 def @options end |
Class Method Details
.each {|klass| ... } ⇒ Enumerator
Enumerates known RDF reader classes.
51 52 53 |
# File 'lib/rdf/reader.rb', line 51 def self.each(&block) @@subclasses.each(&block) end |
.for(format) ⇒ Class .for(filename) ⇒ Class .for(options = {}) ⇒ Class
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 |
.format(klass = nil) ⇒ Class 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 |
.open(filename, options = {}) {|reader| ... } ⇒ Object
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 |
# 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 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 |
.to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Reader.for()
148 149 150 151 152 153 |
# File 'lib/rdf/reader.rb', line 148 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
#base_uri ⇒ Hash{Symbol => RDF::URI}
Returns the base URI determined by this reader.
220 221 222 |
# File 'lib/rdf/reader.rb', line 220 def base_uri @options[:base_uri] end |
#close ⇒ void 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.
359 360 361 |
# File 'lib/rdf/reader.rb', line 359 def close @input.close unless @input.closed? end |
#each_statement {|statement| ... } ⇒ void #each_statement ⇒ Enumerator 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.
294 295 296 297 298 299 300 301 302 303 |
# File 'lib/rdf/reader.rb', line 294 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| ... } ⇒ void #each_triple ⇒ Enumerator
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.
328 329 330 331 332 333 334 335 336 337 |
# File 'lib/rdf/reader.rb', line 328 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 |
#prefix(name, uri) ⇒ RDF::URI #prefix(name) ⇒ RDF::URI Also known as: prefix!
Defines the given named URI prefix for this reader.
268 269 270 271 |
# File 'lib/rdf/reader.rb', line 268 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 |
#prefixes ⇒ Hash{Symbol => RDF::URI}
Returns the URI prefixes currently defined for this reader.
232 233 234 |
# File 'lib/rdf/reader.rb', line 232 def prefixes @options[:prefixes] ||= {} end |
#prefixes=(prefixes) ⇒ Hash{Symbol => RDF::URI}
Defines the given URI prefixes for this reader.
247 248 249 |
# File 'lib/rdf/reader.rb', line 247 def prefixes=(prefixes) @options[:prefixes] = prefixes end |
#rewind ⇒ void Also known as: rewind!
This method returns an undefined value.
Rewinds the input stream to the beginning of input.
345 346 347 |
# File 'lib/rdf/reader.rb', line 345 def rewind @input.rewind end |
#to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Reader.for()
158 159 160 |
# File 'lib/rdf/reader.rb', line 158 def to_sym self.class.to_sym end |