Class: RDF::TriX::Reader

Inherits:
Reader show all
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-trix-7d61d2cef93b/lib/rdf/trix/reader.rb,
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-trix-7d61d2cef93b/lib/rdf/trix/reader/libxml.rb,
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-trix-7d61d2cef93b/lib/rdf/trix/reader/nokogiri.rb,
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-trix-7d61d2cef93b/lib/rdf/trix/reader/rexml.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

Instance Attribute Summary collapse

Attributes inherited from Reader

#options

Instance Method Summary collapse

Methods inherited from Reader

#base_uri, #canonicalize?, #close, each, #each_statement, #encoding, #fail_object, #fail_predicate, #fail_subject, for, format, #intern?, #lineno, open, options, #prefix, #prefixes, #prefixes=, #read_statement, #read_triple, #rewind, to_sym, #to_sym, #valid?, #validate?

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Enumerable

add_entailment, #dump, #each_graph, #each_object, #each_predicate, #each_statement, #each_subject, #each_term, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph_names, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_term?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #project_graph, #quads, #respond_to_missing?, #statements, #subjects, #supports?, #terms, #to_a, #to_h, #to_set, #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}) (defaults to: {})

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

Options Hash (options):

  • :library (Symbol) — default: :nokogiri, :libxml, or :rexml

Yields:

  • (reader)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored



69
70
71
72
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
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-trix-7d61d2cef93b/lib/rdf/trix/reader.rb', line 69

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(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

#implementationModule (readonly)

Returns the XML implementation module for this reader instance.

Returns:

  • (Module)


57
58
59
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-trix-7d61d2cef93b/lib/rdf/trix/reader.rb', line 57

def implementation
  @implementation
end

Instance Method Details

#parse_element(name, attributes, content) ⇒ RDF::Value

Returns the RDF value of the given TriX element.

Parameters:

Returns:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-trix-7d61d2cef93b/lib/rdf/trix/reader.rb', line 145

def parse_element(name, attributes, content)
  case name.to_sym
    when :id
      RDF::Node.new(content.strip)
    when :uri
      uri = RDF::URI.new(content.strip) # TODO: interned URIs
      uri.validate!     if validate?
      uri.canonicalize! if canonicalize?
      uri
    when :typedLiteral
      literal = RDF::Literal.new(content, :datatype => attributes['datatype'])
      literal.validate!     if validate?
      literal.canonicalize! if canonicalize?
      literal
    when :plainLiteral
      literal = case
        when lang = attributes['xml:lang'] || attributes['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', 'typedLiteral', or 'plainLiteral', but got #{name.inspect}"
  end
end