Class: RDF::RDFXML::Reader

Inherits:
RDF::Reader show all
Includes:
Util::Logger
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-rdfxml-5324e00a23df/lib/rdf/rdfxml/reader.rb,
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-rdfxml-5324e00a23df/lib/rdf/rdfxml/reader/nokogiri.rb,
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-rdfxml-5324e00a23df/lib/rdf/rdfxml/reader/rexml.rb

Overview

An RDF/XML parser in Ruby

Based on RDF/XML Syntax Specification: http://www.w3.org/TR/REC-rdf-syntax/

Author:

Defined Under Namespace

Modules: Nokogiri, REXML

Constant Summary

CORE_SYNTAX_TERMS =
%w(RDF ID about parseType resource nodeID datatype).map {|n| "http://www.w3.org/1999/02/22-rdf-syntax-ns##{n}"}
OLD_TERMS =
%w(aboutEach aboutEachPrefix bagID).map {|n| "http://www.w3.org/1999/02/22-rdf-syntax-ns##{n}"}

Instance Attribute Summary collapse

Attributes inherited from RDF::Reader

#options

Instance Method Summary collapse

Methods included from Util::Logger

#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger

Methods inherited from RDF::Reader

#base_uri, #canonicalize?, each, #encoding, #fail_object, #fail_predicate, #fail_subject, for, format, #intern?, #lineno, open, options, #prefix, #prefixes, #prefixes=, #read_statement, #read_triple, 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_quad, #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 RDF::Readable

#readable?

Constructor Details

#initialize(input = $stdin, options = {}) {|reader| ... } ⇒ reader

Initializes the RDF/XML reader instance.

Parameters:

Options Hash (options):

  • :library (Symbol)

    One of :nokogiri or :rexml. If nil/unspecified uses :nokogiri if available, :rexml otherwise.

  • :encoding (Encoding) — default: Encoding::UTF_8

    the encoding of the input stream (Ruby 1.9+)

  • :validate (Boolean) — default: false

    whether to validate the parsed statements and values

  • :canonicalize (Boolean) — default: false

    whether to canonicalize parsed literals

  • :intern (Boolean) — default: true

    whether to intern all parsed URIs

  • :prefixes (Hash) — default: Hash.new

    the prefix mappings to use (not supported by all readers)

  • :base_uri (#to_s) — default: nil

    the base URI to use when resolving relative URIs

Yields:

  • (reader)

    self

Yield Parameters:

Yield Returns:

  • (void)

    ignored

Raises:

  • (Error)

    Raises RDF::ReaderError if validate



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-rdfxml-5324e00a23df/lib/rdf/rdfxml/reader.rb', line 139

def initialize(input = $stdin, options = {}, &block)
  super do
    @library = case options[:library]
    when nil
      # Use Nokogiri when available, and REXML otherwise:
      defined?(::Nokogiri) ? :nokogiri : :rexml
    when :nokogiri, :rexml
      options[:library]
    else
      log_fatal("expected :rexml or :nokogiri, but got #{options[:library].inspect}", exception: ArgumentError)
    end

    require "rdf/rdfxml/reader/#{@library}"
    @implementation = case @library
      when :nokogiri then Nokogiri
      when :rexml    then REXML
    end
    self.extend(@implementation)

    input.rewind if input.respond_to?(:rewind)
    initialize_xml(input, options) rescue log_fatal($!.message)

    log_error("Empty document") if root.nil?
    log_error("Synax errors") {doc_errors} if !doc_errors.empty?

    block.call(self) if block_given?
  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)


111
112
113
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-rdfxml-5324e00a23df/lib/rdf/rdfxml/reader.rb', line 111

def implementation
  @implementation
end

Instance Method Details

#closeObject

Document closed when read in initialize



172
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-rdfxml-5324e00a23df/lib/rdf/rdfxml/reader.rb', line 172

def close; end

#each_statement {|statement| ... }

This method returns an undefined value.

Iterates the given block for each RDF statement in the input.

Yields:

  • (statement)

Yield Parameters:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-rdfxml-5324e00a23df/lib/rdf/rdfxml/reader.rb', line 180

def each_statement(&block)
  if block_given?
    # Block called from add_statement
    @callback = block
    return unless root

    log_fatal "root must be a proxy not a #{root.class}" unless root.is_a?(@implementation::NodeProxy)

    add_debug(root, "base_uri: #{base_uri.inspect}")
  
    rdf_nodes = root.xpath("//rdf:RDF", "rdf" => RDF.to_uri.to_s)
    if rdf_nodes.size == 0
      # If none found, root element may be processed as an RDF Node

      ec = EvaluationContext.new(base_uri, root, @graph) do |prefix, value|
        prefix(prefix, value)
      end

      nodeElement(root, ec)
    else
      rdf_nodes.each do |node|
        log_fatal "node must be a proxy not a #{node.class}" unless node.is_a?(@implementation::NodeProxy)
        # XXX Skip this element if it's contained within another rdf:RDF element

        # Extract base, lang and namespaces from parents to create proper evaluation context
        ec = EvaluationContext.new(base_uri, nil, @graph)
        ec.extract_from_ancestors(node) do |prefix, value|
          prefix(prefix, value)
        end
        node.children.each {|el|
          next unless el.element?
          log_fatal "el must be a proxy not a #{el.class}" unless el.is_a?(@implementation::NodeProxy)
          new_ec = ec.clone(el) do |prefix, value|
            prefix(prefix, value)
          end
          nodeElement(el, new_ec)
        }
      end
    end

    if validate? && log_statistics[:error]
      raise RDF::ReaderError, "Errors found during processing"
    end
  end
  enum_for(:each_statement)
end

#each_triple {|subject, predicate, object| ... }

This method returns an undefined value.

Iterates the given block for each RDF triple in the input.

Yields:

  • (subject, predicate, object)

Yield Parameters:



235
236
237
238
239
240
241
242
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-rdfxml-5324e00a23df/lib/rdf/rdfxml/reader.rb', line 235

def each_triple(&block)
  if block_given?
    each_statement do |statement|
      block.call(*statement.to_triple)
    end
  end
  enum_for(:each_triple)
end

#rewindObject

No need to rewind, as parsing is done in initialize



169
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-rdfxml-5324e00a23df/lib/rdf/rdfxml/reader.rb', line 169

def rewind; end