Class: RDF::TriX::Writer
- Defined in:
- vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb,
vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer/rexml.rb,
vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer/nokogiri.rb
Overview
TriX serializer.
This class supports both REXML and Nokogiri for XML processing,
and will automatically select the most performant implementation
(Nokogiri) when it is available. If need be, you can explicitly
override the used implementation by passing in a :library
option to
Writer.new
or Writer.open
.
Defined Under Namespace
Constant Summary
Constants included from Util::Logger
Instance Attribute Summary collapse
-
#graph_name ⇒ RDF::Resource
readonly
Returns the current graph_name, if any.
-
#implementation ⇒ Module
readonly
Returns the XML implementation module for this writer instance.
Attributes inherited from Writer
Instance Method Summary collapse
-
#format_literal(value, **options) ⇒ Element
Returns the TriX representation of a literal.
-
#format_node(value, **options) ⇒ Element
Returns the TriX representation of a blank node.
-
#format_triple(subject, predicate, object, **options) ⇒ Element
Returns the TriX representation of a triple.
-
#format_uri(value, **options) ⇒ Element
Returns the TriX representation of a URI reference.
-
#graph(name = nil) {|writer| ... }
Defines a named graph.
-
#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer
constructor
Initializes the TriX writer instance.
-
#nested? ⇒ Boolean
protected
Returns
true
if we are currently in awriter.graph { ... }
block. -
#write_comment(text)
Generates an XML comment.
-
#write_quad(subject, predicate, object, graph_name)
Generates the TriX representation of a quad.
-
#write_triple(subject, predicate, object)
Generates the TriX representation of a triple.
Methods inherited from Writer
accept?, #base_uri, buffer, #canonicalize?, dump, each, #encoding, #escaped, #flush, for, format, #format_embTriple, #format_list, #format_term, #node_id, open, options, #prefix, #prefixes, #prefixes=, #puts, #quoted, #to_sym, to_sym, #uri_for, #validate?, #write_epilogue, #write_prologue, #write_statement, #write_triples
Methods included from Util::Aliasing::LateBound
Methods included from Writable
#<<, #insert, #insert_graph, #insert_reader, #insert_statement, #insert_statements, #writable?
Methods included from Util::Coercions
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 TriX writer instance.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 73 def initialize(output = $stdout, **, &block) @graph_name = nil @nesting = 0 @library = case [:library] when nil # Use Nokogiri or LibXML when available, and REXML otherwise: begin require 'nokogiri' :nokogiri rescue LoadError => e begin require 'libxml' :rexml # FIXME: no LibXML support implemented yet rescue LoadError => e :rexml end end when :libxml then :rexml # FIXME when :nokogiri, :libxml, :rexml [:library] else raise ArgumentError.new("expected :rexml, :libxml or :nokogiri, but got #{[:library].inspect}") end require "rdf/trix/writer/#{@library}" @implementation = case @library when :nokogiri then Nokogiri when :libxml then LibXML # TODO when :rexml then REXML end self.extend(@implementation) @encoding = ([:encoding] || 'utf-8').to_s initialize_xml(**) super do if block_given? case block.arity when 0 then instance_eval(&block) else block.call(self) end end end end |
Instance Attribute Details
#graph_name ⇒ RDF::Resource (readonly)
Returns the current graph_name, if any.
59 60 61 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 59 def graph_name @graph_name end |
#implementation ⇒ Module (readonly)
Returns the XML implementation module for this writer instance.
53 54 55 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 53 def implementation @implementation end |
Instance Method Details
#format_literal(value, **options) ⇒ Element
Returns the TriX representation of a literal.
230 231 232 233 234 235 236 237 238 239 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 230 def format_literal(value, **) case when value.has_datatype? create_element(:typedLiteral, value.value.to_s, 'datatype' => value.datatype.to_s) when value.has_language? create_element(:plainLiteral, value.value.to_s, 'xml:lang' => value.language.to_s) else create_element(:plainLiteral, value.value.to_s) end end |
#format_node(value, **options) ⇒ Element
Returns the TriX representation of a blank node.
210 211 212 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 210 def format_node(value, **) create_element(:id, value.id.to_s) end |
#format_triple(subject, predicate, object, **options) ⇒ Element
Returns the TriX representation of a triple.
196 197 198 199 200 201 202 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 196 def format_triple(subject, predicate, object, **) create_element(:triple) do |triple| triple << format_term(subject, **) triple << format_term(predicate, **) triple << format_term(object, **) end end |
#format_uri(value, **options) ⇒ Element
Returns the TriX representation of a URI reference.
220 221 222 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 220 def format_uri(value, **) create_element(:uri, value.to_s) end |
#graph(name = nil) {|writer| ... }
This method returns an undefined value.
Defines a named graph.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 126 def graph(name = nil, &block) @nesting += 1 @graph = create_graph(@graph_name = name) if block_given? case block.arity when 1 then block.call(self) else instance_eval(&block) end end @graph = nil @nesting -= 1 end |
#nested? ⇒ Boolean (protected)
Returns true
if we are currently in a writer.graph { ... }
block.
143 144 145 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 143 def nested? @nesting > 0 end |
#write_comment(text)
This method returns an undefined value.
Generates an XML comment.
155 156 157 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 155 def write_comment(text) (@graph || @trix) << create_comment(text) end |
#write_quad(subject, predicate, object, graph_name)
This method returns an undefined value.
Generates the TriX representation of a quad.
167 168 169 170 171 172 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 167 def write_quad(subject, predicate, object, graph_name) unless nested? || graph_name.to_s == @graph_name.to_s @graph = create_graph(@graph_name = graph_name) end write_triple(subject, predicate, object) end |
#write_triple(subject, predicate, object)
This method returns an undefined value.
Generates the TriX representation of a triple.
181 182 183 184 185 186 |
# File 'vendor/bundler/ruby/2.7.0/bundler/gems/rdf-trix-7b1c1d429760/lib/rdf/trix/writer.rb', line 181 def write_triple(subject, predicate, object) @graph = create_graph unless @graph @graph << format_triple(subject, predicate, object) rescue ArgumentError => e log_error(subject, predicate, object, e.) end |