Class: RDF::TriX::Reader

Inherits:
Reader show all
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.

Examples:

Loading TriX parsing support

require 'rdf/trix'

Obtaining a TriX reader class

RDF::Reader.for(:trix)         #=> RDF::TriX::Reader
RDF::Reader.for("etc/doap.xml")
RDF::Reader.for(:file_name      => "etc/doap.xml")
RDF::Reader.for(:file_extension => "xml")
RDF::Reader.for(:content_type   => "application/trix")

Instantiating a Nokogiri-based reader

RDF::TriX::Reader.new(input, :library => :nokogiri)

Instantiating a LibXML-based reader

RDF::TriX::Reader.new(input, :library => :libxml)

Instantiating a REXML-based reader

RDF::TriX::Reader.new(input, :library => :rexml)

Parsing RDF statements from a TriX file

RDF::TriX::Reader.open("etc/doap.xml") do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

Parsing RDF statements from a TriX string

data = StringIO.new(File.read("etc/doap.xml"))
RDF::TriX::Reader.new(data) do |reader|
  reader.each_statement do |statement|
    puts statement.inspect
  end
end

See Also:

Defined Under Namespace

Modules: LibXML, Nokogiri, REXML

Constant Summary

Constants included from Util::Logger

Util::Logger::IOWrapper

Instance Attribute Summary collapse

Attributes inherited from Reader

#options

Attributes included from Enumerable

#existentials, #universals

Instance Method Summary collapse

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

#alias_method

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

#count, #empty?

Methods included from Readable

#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.

Parameters:

  • input (IO, File, String) (defaults to: $stdin)
  • options (Hash{Symbol => Object})

    any additional options (see RDF::Reader#initialize)

Options Hash (**options):

  • :library (Symbol) — default: :nokogiri, :libxml, or :rexml
  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs

Yields:

  • (reader)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored



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, **options, &block)
  super do
    @library = case options[: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
        options[:library]
      else
        raise ArgumentError.new("expected :rexml, :libxml or :nokogiri, but got #{options[: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, **options)
    rescue
      log_error("Malformed document: #{$!.message}")
    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_uriRDF::URI (readonly)

Returns the Base URI as provided, or found from xml:base

Returns:



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

#implementationModule (readonly)

Returns the XML implementation module for this reader instance.

Returns:

  • (Module)


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.

Parameters:

Returns:



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

Parameters:

Yields:

  • statement

Yield Parameters:



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

Parameters:

Returns:



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