Class: RDF::TriX::Reader
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trix-4b113f65db0e/lib/rdf/trix/reader.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trix-4b113f65db0e/lib/rdf/trix/reader/rexml.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trix-4b113f65db0e/lib/rdf/trix/reader/libxml.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trix-4b113f65db0e/lib/rdf/trix/reader/nokogiri.rb
Overview
TriX parser.
This class supports REXML, LibXML and Nokogiri for XML
processing, and will automatically select the most performant
implementation (Nokogiri or LibXML) that is available. If need be, you
can explicitly override the used implementation by passing in a
:library
option to Reader.new
or Reader.open
.
Defined Under Namespace
Modules: LibXML, Nokogiri, REXML
Constant Summary
Constants included from Util::Logger
Instance Attribute Summary collapse
-
#base_uri ⇒ RDF::URI
readonly
Returns the Base URI as provided, or found from xml:base.
-
#implementation ⇒ Module
readonly
Returns the XML implementation module for this reader instance.
Attributes inherited from Reader
Attributes included from Enumerable
Instance Method Summary collapse
-
#initialize(input = $stdin, **options) {|reader| ... } ⇒ Reader
constructor
Initializes the TriX reader instance.
-
#parse_element(name, element, content) ⇒ RDF::Value
Returns the RDF value of the given TriX element.
-
#read_statements(graph_element) {|statement| ... } ⇒ Object
Yield each statement from a graph.
-
#read_triple(element, graph_name: nil) ⇒ RDF::Statement
Read a
.
Methods inherited from Reader
#canonicalize?, #close, each, #each_pg_statement, #encoding, #fail_object, #fail_predicate, #fail_subject, for, format, #intern?, #lineno, open, options, #prefix, #prefixes, #prefixes=, #read_statement, #rewind, #to_sym, to_sym, #valid?, #validate?
Methods included from Util::Aliasing::LateBound
Methods included from Enumerable
add_entailment, #canonicalize, #canonicalize!, #dump, #each_object, #each_predicate, #each_subject, #each_term, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph?, #graph_names, #invalid?, #method_missing, #object?, #objects, #predicate?, #predicates, #project_graph, #quad?, #quads, #respond_to_missing?, #statement?, #statements, #subject?, #subjects, #supports?, #term?, #terms, #to_a, #to_h, #to_set, #triple?, #triples, #valid?, #validate!
Methods included from Isomorphic
#bijection_to, #isomorphic_with?
Methods included from Countable
Methods included from Readable
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(input = $stdin, **options) {|reader| ... } ⇒ Reader
Initializes the TriX reader instance.
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 118 119 120 121 122 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trix-4b113f65db0e/lib/rdf/trix/reader.rb', line 79 def initialize(input = $stdin, **, &block) super do @library = case [:library] when nil # Use Nokogiri or LibXML when available, and REXML otherwise: begin require 'nokogiri' :nokogiri rescue LoadError => e begin require 'libxml' :libxml rescue LoadError => e :rexml end end when :nokogiri, :libxml, :rexml [:library] else raise ArgumentError.new("expected :rexml, :libxml or :nokogiri, but got #{[:library].inspect}") end require "rdf/trix/reader/#{@library}" @implementation = case @library when :nokogiri then Nokogiri when :libxml then LibXML when :rexml then REXML end self.extend(@implementation) begin initialize_xml(input, **) rescue log_error("Malformed document: #{$!.}") end if block_given? case block.arity when 0 then instance_eval(&block) else block.call(self) end end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class RDF::Enumerable
Instance Attribute Details
#base_uri ⇒ RDF::URI (readonly)
Returns the Base URI as provided, or found from xml:base
65 66 67 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trix-4b113f65db0e/lib/rdf/trix/reader.rb', line 65 def base_uri @base_uri end |
#implementation ⇒ Module (readonly)
Returns the XML implementation module for this reader instance.
59 60 61 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trix-4b113f65db0e/lib/rdf/trix/reader.rb', line 59 def implementation @implementation end |
Instance Method Details
#parse_element(name, element, content) ⇒ RDF::Value
Returns the RDF value of the given TriX element.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trix-4b113f65db0e/lib/rdf/trix/reader.rb', line 222 def parse_element(name, element, content) case name.to_sym when :id RDF::Node.intern(content.strip) when :uri uri = RDF::URI.new(content.strip) # TODO: interned URIs uri = base_uri.join(uri) if base_uri && uri.relative? uri.validate! if validate? uri.canonicalize! if canonicalize? uri when :triple # RDF-star log_error "expected 'triple' element" unless @options[:rdfstar] read_triple(element) when :typedLiteral content = element.children.c14nxl(library: @library) if element['datatype'] == RDF.XMLLiteral literal = RDF::Literal.new(content, :datatype => RDF::URI(element['datatype'])) literal.validate! if validate? literal.canonicalize! if canonicalize? literal when :plainLiteral literal = case when lang = element['xml:lang'] || element['lang'] RDF::Literal.new(content, :language => lang) else RDF::Literal.new(content) end literal.validate! if validate? literal.canonicalize! if canonicalize? literal else log_error "expected element name to be 'id', 'uri', 'triple', 'typedLiteral', or 'plainLiteral', but got #{name.inspect}" end end |
#read_statements(graph_element) {|statement| ... } ⇒ Object
Yield each statement from a graph
195 196 197 198 199 200 201 202 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trix-4b113f65db0e/lib/rdf/trix/reader.rb', line 195 def read_statements(graph_element, &block) graph_name = read_graph(graph_element) graph_name = base_uri.join(graph_name) if base_uri && graph_name && graph_name.relative? triple_elements(graph_element).each do |triple_element| block.call(read_triple(triple_element, graph_name: graph_name)) end end |
#read_triple(element, graph_name: nil) ⇒ RDF::Statement
Read a
208 209 210 211 212 213 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-trix-4b113f65db0e/lib/rdf/trix/reader.rb', line 208 def read_triple(element, graph_name: nil) terms = element_elements(element)[0..2].map do |element| parse_element(element.name, element, element_content(element)) end RDF::Statement(*terms, graph_name: graph_name) end |