Class: RDF::Writer Abstract
- Inherits:
-
Object
- Object
- RDF::Writer
- Extended by:
- Enumerable, Util::Aliasing::LateBound
- Includes:
- Util::Logger, Writable
- Defined in:
- lib/rdf/writer.rb
Overview
The base class for RDF serializers.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#options ⇒ Hash
readonly
Any additional options for this writer.
Class Method Summary collapse
-
.buffer(*args) {|writer| ... } ⇒ String
Buffers output into a string buffer.
- .dump(data, io = nil, options = {}) ⇒ void
-
.each {|klass| ... } ⇒ Enumerator
Enumerates known RDF writer classes.
-
.for(options = {}) ⇒ Class
Finds an RDF writer class based on the given criteria.
-
.format(klass = nil) ⇒ Class
(also: format_class)
Retrieves the RDF serialization format class for this writer class.
-
.open(filename, options = {}, &block) ⇒ RDF::Writer
Writes output to the given ‘filename`.
-
.options ⇒ Array<RDF::CLI::Option>
Options suitable for automatic Writer provisioning.
-
.to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Writer.for().
Instance Method Summary collapse
-
#base_uri ⇒ RDF::URI
Returns the base URI used for this writer.
-
#canonicalize? ⇒ Boolean
Returns ‘true` if terms should be canonicalized.
-
#encoding ⇒ Encoding
Returns the encoding of the output stream.
-
#flush ⇒ self
(also: #flush!)
Flushes the underlying output buffer.
- #format_list(value, options = {}) ⇒ String abstract
- #format_literal(value, options = {}) ⇒ String abstract
- #format_node(value, options = {}) ⇒ String abstract
- #format_term(term, options = {}) ⇒ String
- #format_uri(value, options = {}) ⇒ String abstract
-
#initialize(output = $stdout, options = {}) {|writer| ... } ⇒ Writer
constructor
Initializes the writer.
-
#prefix(name, uri = nil) ⇒ RDF::URI
(also: #prefix!)
Defines the given named URI prefix for this writer.
-
#prefixes ⇒ Hash{Symbol => RDF::URI}
Returns the URI prefixes currently defined for this writer.
-
#prefixes=(prefixes) ⇒ Hash{Symbol => RDF::URI}
Defines the given URI prefixes for this writer.
-
#to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Writer.for().
-
#validate? ⇒ Boolean
Returns ‘true` if statements and terms should be validated.
- #write_comment(text) ⇒ self abstract
- #write_epilogue ⇒ self abstract
- #write_prologue ⇒ self abstract
-
#write_statement(statement) ⇒ self
(also: #insert_statement)
Add a statement to the writer.
- #write_triple(subject, predicate, object) ⇒ self abstract
- #write_triples(*triples) ⇒ self
Methods included from Util::Aliasing::LateBound
Methods included from Writable
Methods included from Util::Logger
#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger
Constructor Details
#initialize(output = $stdout, options = {}) {|writer| ... } ⇒ Writer
Initializes the writer.
259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/rdf/writer.rb', line 259 def initialize(output = $stdout, = {}, &block) @output, = output, .dup @nodes, @node_id, @node_id_map = {}, 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
#options ⇒ Hash (readonly)
Any additional options for this writer.
278 279 280 |
# File 'lib/rdf/writer.rb', line 278 def end |
Class Method Details
.buffer(*args) {|writer| ... } ⇒ String
Buffers output into a string buffer.
192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/rdf/writer.rb', line 192 def self.buffer(*args, &block) = args.last.is_a?(Hash) ? args.last : {} [:encoding] ||= Encoding::UTF_8 if RUBY_PLATFORM == "java" raise ArgumentError, "block expected" unless block_given? StringIO.open do |buffer| buffer.set_encoding([:encoding]) if [:encoding] self.new(buffer, *args) { |writer| block.call(writer) } buffer.string end end |
.dump(data, io = nil, options = {}) ⇒ void
This method returns an undefined value.
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rdf/writer.rb', line 160 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 |
.each {|klass| ... } ⇒ Enumerator
Enumerates known RDF writer classes.
63 64 65 |
# File 'lib/rdf/writer.rb', line 63 def self.each(&block) @@subclasses.each(&block) end |
.for(format) ⇒ Class .for(filename) ⇒ Class .for(options = {}) ⇒ Class
Finds an RDF writer class based on the given criteria.
92 93 94 95 96 97 |
# File 'lib/rdf/writer.rb', line 92 def self.for( = {}) = .merge(has_writer: true) if .is_a?(Hash) if format = self.format || Format.for() format.writer end end |
.format(klass = nil) ⇒ Class Also known as: format_class
Retrieves the RDF serialization format class for this writer class.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rdf/writer.rb', line 103 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 |
.open(filename, options = {}, &block) ⇒ RDF::Writer
Writes output to the given ‘filename`.
212 213 214 215 216 217 218 219 |
# File 'lib/rdf/writer.rb', line 212 def self.open(filename, = {}, &block) File.open(filename, 'wb') do |file| file.set_encoding([:encoding]) if [:encoding] = .dup [:file_name] ||= filename self.for([:format] || ).new(file, , &block) end end |
.options ⇒ Array<RDF::CLI::Option>
Options suitable for automatic Writer provisioning.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/rdf/writer.rb', line 117 def self. [ RDF::CLI::Option.new( symbol: :canonicalize, datatype: TrueClass, on: ["--canonicalize"], description: "Canonicalize input/output.") {true}, RDF::CLI::Option.new( symbol: :encoding, datatype: Encoding, on: ["--encoding ENCODING"], description: "The encoding of the input stream.") {|arg| Encoding.find arg}, RDF::CLI::Option.new( symbol: :prefixes, datatype: Hash, multiple: true, on: ["--prefixes PREFIX,PREFIX"], description: "A comma-separated list of prefix:uri pairs.") do |arg| arg.split(',').inject({}) do |memo, pfxuri| pfx,uri = pfxuri.split(':', 2) memo.merge(pfx.to_sym => RDF::URI(uri)) end end, RDF::CLI::Option.new( symbol: :unique_bnodes, datatype: TrueClass, on: ["--unique-bnodes"], description: "Use unique Node identifiers.") {true}, ] end |
.to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Writer.for()
224 225 226 |
# File 'lib/rdf/writer.rb', line 224 def self.to_sym self.format.to_sym end |
Instance Method Details
#base_uri ⇒ RDF::URI
Returns the base URI used for this writer.
288 289 290 |
# File 'lib/rdf/writer.rb', line 288 def base_uri RDF::URI([:base_uri]) if [:base_uri] end |
#canonicalize? ⇒ Boolean
Returns ‘true` if terms should be canonicalized.
371 372 373 |
# File 'lib/rdf/writer.rb', line 371 def canonicalize? [:canonicalize] end |
#encoding ⇒ Encoding
Returns the encoding of the output stream.
346 347 348 349 350 351 352 353 354 355 |
# File 'lib/rdf/writer.rb', line 346 def encoding case [:encoding] when String, Symbol Encoding.find([:encoding].to_s) when Encoding [:encoding] else [:encoding] ||= Encoding.find(self.class.format.content_encoding.to_s) end end |
#flush ⇒ self Also known as: flush!
Flushes the underlying output buffer.
379 380 381 382 |
# File 'lib/rdf/writer.rb', line 379 def flush @output.flush if @output.respond_to?(:flush) self end |
#format_list(value, options = {}) ⇒ String
532 533 534 |
# File 'lib/rdf/writer.rb', line 532 def format_list(value, = {}) format_term(value.subject, ) end |
#format_literal(value, options = {}) ⇒ String
522 523 524 |
# File 'lib/rdf/writer.rb', line 522 def format_literal(value, = {}) raise NotImplementedError.new("#{self.class}#format_literal") # override in subclasses end |
#format_node(value, options = {}) ⇒ String
502 503 504 |
# File 'lib/rdf/writer.rb', line 502 def format_node(value, = {}) raise NotImplementedError.new("#{self.class}#format_node") # override in subclasses end |
#format_term(term, options = {}) ⇒ String
483 484 485 486 487 488 489 490 491 492 |
# File 'lib/rdf/writer.rb', line 483 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 |
#format_uri(value, options = {}) ⇒ String
512 513 514 |
# File 'lib/rdf/writer.rb', line 512 def format_uri(value, = {}) raise NotImplementedError.new("#{self.class}#format_uri") # override in subclasses end |
#prefix(name, uri) ⇒ RDF::URI #prefix(name) ⇒ RDF::URI Also known as: prefix!
Defines the given named URI prefix for this writer.
336 337 338 339 |
# File 'lib/rdf/writer.rb', line 336 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 writer.
300 301 302 |
# File 'lib/rdf/writer.rb', line 300 def prefixes [:prefixes] ||= {} end |
#prefixes=(prefixes) ⇒ Hash{Symbol => RDF::URI}
Defines the given URI prefixes for this writer.
315 316 317 |
# File 'lib/rdf/writer.rb', line 315 def prefixes=(prefixes) [:prefixes] = prefixes end |
#to_sym ⇒ Symbol
Returns a symbol appropriate to use with RDF::Writer.for()
231 232 233 |
# File 'lib/rdf/writer.rb', line 231 def to_sym self.class.to_sym end |
#validate? ⇒ Boolean
Returns ‘true` if statements and terms should be validated.
362 363 364 |
# File 'lib/rdf/writer.rb', line 362 def validate? [:validate] end |
#write_comment(text) ⇒ self
407 408 409 |
# File 'lib/rdf/writer.rb', line 407 def write_comment(text) self end |
#write_epilogue ⇒ self
396 397 398 399 400 401 |
# File 'lib/rdf/writer.rb', line 396 def write_epilogue if log_statistics[:error] raise RDF::WriterError, "Errors found during processing" end self end |
#write_prologue ⇒ self
388 389 390 |
# File 'lib/rdf/writer.rb', line 388 def write_prologue self end |
#write_statement(statement) ⇒ self Also known as: insert_statement
logs error if attempting to write an invalid Statement or if canonicalizing a statement which cannot be canonicalized.
Add a statement to the writer. This will check to ensure that the statement is complete (no nil terms) and is valid, if the ‘:validation` option is set.
Additionally, it will de-duplicate BNode terms sharing a common identifier.
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 |
# File 'lib/rdf/writer.rb', line 419 def write_statement(statement) statement = statement.canonicalize! if canonicalize? # Make sure BNodes in statement use unique identifiers if statement.node? terms = statement.to_quad.map do |term| if term.is_a?(RDF::Node) term = term.original while term.original @nodes[term] ||= begin # Account for duplicated nodes @node_id_map[term.to_s] ||= term if !@node_id_map[term.to_s].equal?(term) # Rename node term.make_unique! @node_id_map[term.to_s] = term end end else term end end statement = RDF::Statement.from(statement.to_quad) end if statement.incomplete? log_error "Statement #{statement.inspect} is incomplete" elsif validate? && statement.invalid? log_error "Statement #{statement.inspect} is invalid" elsif respond_to?(:write_quad) write_quad(*statement.to_quad) else write_triple(*statement.to_triple) end self rescue ArgumentError => e log_error e. end |
#write_triple(subject, predicate, object) ⇒ self
logs error if attempting to write an invalid Statement or if canonicalizing a statement which cannot be canonicalized.
475 476 477 |
# File 'lib/rdf/writer.rb', line 475 def write_triple(subject, predicate, object) raise NotImplementedError.new("#{self.class}#write_triple") # override in subclasses end |
#write_triples(*triples) ⇒ self
logs error if attempting to write an invalid Statement or if canonicalizing a statement which cannot be canonicalized.
462 463 464 465 |
# File 'lib/rdf/writer.rb', line 462 def write_triples(*triples) triples.each { |triple| write_triple(*triple) } self end |