Class: RDF::TriX::Reader
- Inherits:
-
Reader
- Object
- Reader
- RDF::TriX::Reader
- Defined in:
- lib/rdf/trix/reader.rb,
lib/rdf/trix/reader/rexml.rb,
lib/rdf/trix/reader/libxml.rb,
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
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.
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 <triple>.
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 '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 |
Instance Attribute Details
#base_uri ⇒ RDF::URI (readonly)
Returns the Base URI as provided, or found from xml:base
65 66 67 |
# File '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 '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 '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 '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 <triple>
208 209 210 211 212 213 |
# File '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 |