Class: RDF::Writer Abstract
- Inherits:
-
Object
- Object
- RDF::Writer
- Extended by:
- Enumerable, Util::Aliasing::LateBound
- Includes:
- Writable
- Defined in:
- lib/rdf/writer.rb
Overview
The base class for RDF serializers.
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
- (Hash) options
readonly
Any additional options for this writer.
Class Method Summary (collapse)
-
+ (String) buffer(*args) {|writer| ... }
Buffers output into a string buffer.
-
+ dump(data, io = nil, options = {})
-
+ (Enumerator) each {|klass| ... }
Enumerates known RDF writer classes.
-
+ (Class) for(options = {})
Finds an RDF writer class based on the given criteria.
-
+ (Class) format(klass = nil)
(also: format_class)
Retrieves the RDF serialization format class for this writer class.
-
+ (RDF::Writer) open(filename, options = {}, &block)
Writes output to the given
filename. -
+ (Symbol) to_sym
Returns a symbol appropriate to use with RDF::Writer.for().
Instance Method Summary (collapse)
-
- (Hash{Symbol => RDF::URI}) base_uri
Returns the base URI used for this writer.
-
- (Encoding) encoding
Returns the encoding of the output stream.
-
- (String) escaped(string)
protected
-
- flush
(also: #flush!)
Flushes the underlying output buffer.
-
- (String) format_list(value, options = {})
abstract
-
- (String) format_literal(value, options = {})
abstract
-
- (String) format_node(value, options = {})
abstract
-
- (String) format_term(term, options = {})
(also: #format_value)
-
- (String) format_uri(value, options = {})
abstract
-
- (Writer) initialize(output = $stdout, options = {}) {|writer| ... }
constructor
Initializes the writer.
-
- (String) node_id
protected
-
- (RDF::URI) prefix(name, uri = nil)
(also: #prefix!)
Defines the given named URI prefix for this writer.
-
- (Hash{Symbol => RDF::URI}) prefixes
Returns the URI prefixes currently defined for this writer.
-
- (Hash{Symbol => RDF::URI}) prefixes=(prefixes)
Defines the given URI prefixes for this writer.
-
- puts(*args)
protected
-
- (String) quoted(string)
protected
-
- (Symbol) to_sym
Returns a symbol appropriate to use with RDF::Writer.for().
-
- (String) uri_for(uriref)
protected
-
- write_comment(text)
abstract
self. -
- write_epilogue
abstract
self. -
- write_graph(graph)
deprecated
Deprecated.
replace by
RDF::Writable#insert_graph -
- write_prologue
abstract
self. -
- write_statement(statement)
(also: #insert_statement)
self. -
- write_statements(*statements)
deprecated
Deprecated.
replace by
RDF::Writable#insert_statements -
- write_triple(subject, predicate, object)
abstract
self. -
- write_triples(*triples)
self.
Methods included from Util::Aliasing::LateBound
Methods included from Writable
#<<, #insert, #insert_graph, #insert_reader, #insert_statements, #writable?
Constructor Details
- (Writer) initialize(output = $stdout, options = {}) {|writer| ... }
Initializes the writer.
210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/rdf/writer.rb', line 210 def initialize(output = $stdout, = {}, &block) @output, @options = output, .dup @nodes, @node_id = {}, 0 if block_given? write_prologue case block.arity when 1 then block.call(self) else instance_eval(&block) end write_epilogue end end |
Instance Attribute Details
- (Hash) options (readonly)
Any additional options for this writer.
229 230 231 |
# File 'lib/rdf/writer.rb', line 229 def @options end |
Class Method Details
+ (String) buffer(*args) {|writer| ... }
Buffers output into a string buffer.
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/rdf/writer.rb', line 145 def self.buffer(*args, &block) = args.last.is_a?(Hash) ? args.last : {} raise ArgumentError, "block expected" unless block_given? StringIO.open do |buffer| buffer.set_encoding([:encoding]) if buffer.respond_to?(:set_encoding) && [:encoding] self.new(buffer, *args) { |writer| block.call(writer) } buffer.string end end |
+ dump(data, io = nil, options = {})
This method returns an undefined value.
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/rdf/writer.rb', line 113 def self.dump(data, io = nil, = {}) if io.is_a?(String) io = File.open(io, 'w') elsif io.respond_to?(:external_encoding) && io.external_encoding = {:encoding => io.external_encoding}.merge() end io.set_encoding([:encoding]) if io.respond_to?(:set_encoding) && [:encoding] method = data.respond_to?(:each_statement) ? :each_statement : :each if io new(io, ) do |writer| data.send(method) do |statement| writer << statement end writer.flush end else buffer() do |writer| data.send(method) do |statement| writer << statement end end end end |
+ (Enumerator) each {|klass| ... }
Enumerates known RDF writer classes.
50 51 52 |
# File 'lib/rdf/writer.rb', line 50 def self.each(&block) @@subclasses.each(&block) end |
+ (Class) for(format) + (Class) for(filename) + (Class) for(options = {})
Finds an RDF writer class based on the given criteria.
79 80 81 82 83 84 |
# File 'lib/rdf/writer.rb', line 79 def self.for( = {}) = .merge(:has_writer => true) if .is_a?(Hash) if format = self.format || Format.for() format.writer end end |
+ (Class) format(klass = nil) Also known as: format_class
Retrieves the RDF serialization format class for this writer class.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/rdf/writer.rb', line 90 def self.format(klass = nil) if klass.nil? Format.each do |format| if format.writer == self return format end end nil # not found end end |
+ (RDF::Writer) open(filename, options = {}, &block)
Writes output to the given filename.
164 165 166 167 168 169 170 171 |
# File 'lib/rdf/writer.rb', line 164 def self.open(filename, = {}, &block) File.open(filename, 'wb') do |file| file.set_encoding([:encoding]) if file.respond_to?(:set_encoding) && [:encoding] = .dup [:file_name] ||= filename self.for([:format] || ).new(file, , &block) end end |
+ (Symbol) to_sym
Returns a symbol appropriate to use with RDF::Writer.for()
176 177 178 179 180 181 |
# File 'lib/rdf/writer.rb', line 176 def self.to_sym elements = self.to_s.split("::") sym = elements.pop sym = elements.pop if sym == 'Writer' sym.downcase.to_s.to_sym end |
Instance Method Details
- (Hash{Symbol => RDF::URI}) base_uri
Returns the base URI used for this writer.
239 240 241 |
# File 'lib/rdf/writer.rb', line 239 def base_uri @options[:base_uri] end |
- (Encoding) encoding
Returns the encoding of the output stream.
Note: this method requires Ruby 1.9 or newer.
299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/rdf/writer.rb', line 299 def encoding return nil unless "".respond_to?(:force_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 |
- (String) escaped(string) (protected)
476 477 478 479 480 481 482 483 484 |
# File 'lib/rdf/writer.rb', line 476 def escaped(string) string.gsub('\\', '\\\\'). gsub("\b", '\\b'). gsub("\f", '\\f'). gsub("\t", '\\t'). gsub("\n", '\\n'). gsub("\r", '\\r'). gsub('"', '\\"') end |
- flush Also known as: flush!
This method returns an undefined value.
Flushes the underlying output buffer.
315 316 317 318 |
# File 'lib/rdf/writer.rb', line 315 def flush @output.flush if @output.respond_to?(:flush) self end |
- (String) format_list(value, options = {})
441 442 443 |
# File 'lib/rdf/writer.rb', line 441 def format_list(value, = {}) format_term(value.subject, ) end |
- (String) format_literal(value, options = {})
431 432 433 |
# File 'lib/rdf/writer.rb', line 431 def format_literal(value, = {}) raise NotImplementedError.new("#{self.class}#format_literal") # override in subclasses end |
- (String) format_node(value, options = {})
411 412 413 |
# File 'lib/rdf/writer.rb', line 411 def format_node(value, = {}) raise NotImplementedError.new("#{self.class}#format_node") # override in subclasses end |
- (String) format_term(term, options = {}) Also known as: format_value
393 394 395 396 397 398 399 400 401 402 |
# File 'lib/rdf/writer.rb', line 393 def format_term(term, = {}) case term when String then format_literal(RDF::Literal(term, ), ) when RDF::List then format_list(term, ) when RDF::Literal then format_literal(term, ) when RDF::URI then format_uri(term, ) when RDF::Node then format_node(term, ) else nil end end |
- (String) format_uri(value, options = {})
421 422 423 |
# File 'lib/rdf/writer.rb', line 421 def format_uri(value, = {}) raise NotImplementedError.new("#{self.class}#format_uri") # override in subclasses end |
- (String) node_id (protected)
469 470 471 |
# File 'lib/rdf/writer.rb', line 469 def node_id "_:n#{@node_id += 1}" end |
- (RDF::URI) prefix(name, uri) - (RDF::URI) prefix(name) Also known as: prefix!
Defines the given named URI prefix for this writer.
287 288 289 290 |
# File 'lib/rdf/writer.rb', line 287 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 writer.
251 252 253 |
# File 'lib/rdf/writer.rb', line 251 def prefixes @options[:prefixes] ||= {} end |
- (Hash{Symbol => RDF::URI}) prefixes=(prefixes)
Defines the given URI prefixes for this writer.
266 267 268 |
# File 'lib/rdf/writer.rb', line 266 def prefixes=(prefixes) @options[:prefixes] = prefixes end |
- puts(*args) (protected)
This method returns an undefined value.
449 450 451 |
# File 'lib/rdf/writer.rb', line 449 def puts(*args) @output.puts(*args.map {|s| s.respond_to?(:force_encoding) ? s.force_encoding(encoding) : s}) end |
- (String) quoted(string) (protected)
489 490 491 |
# File 'lib/rdf/writer.rb', line 489 def quoted(string) "\"#{string}\"" end |
- (Symbol) to_sym
Returns a symbol appropriate to use with RDF::Writer.for()
186 187 188 |
# File 'lib/rdf/writer.rb', line 186 def to_sym self.class.to_sym end |
- (String) uri_for(uriref) (protected)
456 457 458 459 460 461 462 463 464 465 |
# File 'lib/rdf/writer.rb', line 456 def uri_for(uriref) case when uriref.is_a?(RDF::Node) @nodes[uriref] when uriref.respond_to?(:to_uri) uriref.to_uri.to_s else uriref.to_s end end |
- write_comment(text)
This method returns an undefined value.
self
339 340 341 |
# File 'lib/rdf/writer.rb', line 339 def write_comment(text) self end |
- write_epilogue
This method returns an undefined value.
self
331 332 333 |
# File 'lib/rdf/writer.rb', line 331 def write_epilogue self end |
- write_graph(graph)
replace by RDF::Writable#insert_graph
This method returns an undefined value.
self
347 348 349 350 |
# File 'lib/rdf/writer.rb', line 347 def write_graph(graph) graph.each_triple { |*triple| write_triple(*triple) } self end |
- write_prologue
This method returns an undefined value.
self
324 325 326 |
# File 'lib/rdf/writer.rb', line 324 def write_prologue self end |
- write_statement(statement) Also known as: insert_statement
This method returns an undefined value.
self
364 365 366 367 |
# File 'lib/rdf/writer.rb', line 364 def write_statement(statement) write_triple(*statement.to_triple) self end |
- write_statements(*statements)
replace by RDF::Writable#insert_statements
This method returns an undefined value.
self
356 357 358 359 |
# File 'lib/rdf/writer.rb', line 356 def write_statements(*statements) statements.flatten.each { |statement| write_statement(statement) } self end |
- write_triple(subject, predicate, object)
This method returns an undefined value.
self
385 386 387 |
# File 'lib/rdf/writer.rb', line 385 def write_triple(subject, predicate, object) raise NotImplementedError.new("#{self.class}#write_triple") # override in subclasses end |
- write_triples(*triples)
This method returns an undefined value.
self
373 374 375 376 |
# File 'lib/rdf/writer.rb', line 373 def write_triples(*triples) triples.each { |triple| write_triple(*triple) } self end |