Class: RDF::Raptor::FFI::Reader

Inherits:
RDF::Reader
  • Object
show all
Includes:
Util::Logger
Defined in:
lib/rdf/raptor/ffi.rb

Overview

FFI reader implementation.

Constant Summary collapse

ERROR_HANDLER =
Proc.new do |user_data, locator, message|
  line = V2.raptor_locator_line(locator)
  log_error(message, lineno: line)
end
WARNING_HANDLER =
Proc.new do |user_data, locator, message|
  line = V2.raptor_locator_line(locator)
  log_warn(message, lineno: line)
end
GENID =
/^genid\d+$/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes the FFI reader instance.

Parameters:

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

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

Options Hash (**options):

  • :base_uri (String, #to_s) — default: "file:///dev/stdin"

Yields:

  • (reader)

    self

Yield Parameters:

  • reader (RDF::Reader)

Yield Returns:

  • (void)

    ignored



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rdf/raptor/ffi.rb', line 43

def initialize(input = $stdin, **options, &block)
  @format = self.class.format.rapper_format
  @parser = V2::Parser.new(@format)
  @parser.error_handler = ERROR_HANDLER
  @parser.warning_handler = WARNING_HANDLER
  
  @parser.namespace_handler = Proc.new do |user_data, raptor_namespace|
    namespace = V2::Namespace.new(raptor_namespace)
    prefix(namespace.prefix, namespace.uri) if namespace.prefix_length > 0
  end

  super
end

Instance Attribute Details

#parserV2::Parser (readonly)

The Raptor parser instance.

Returns:



71
72
73
# File 'lib/rdf/raptor/ffi.rb', line 71

def parser
  @parser
end

Instance Method Details

#create_node(node_id) ⇒ RDF::Node

Parameters:

  • node_id (String)

Returns:

  • (RDF::Node)


139
140
141
142
# File 'lib/rdf/raptor/ffi.rb', line 139

def create_node(node_id)
  @nodes ||= {}
  @nodes[node_id] ||= RDF::Node.new(GENID === node_id ? nil : node_id)
end

#create_uri(uri_str) ⇒ RDF::URI

Parameters:

  • uri_str (String)

Returns:

  • (RDF::URI)


132
133
134
# File 'lib/rdf/raptor/ffi.rb', line 132

def create_uri(uri_str)
  RDF::URI.intern(uri_str)
end

#each_statement(**options) {|statement| ... } ⇒ Object Also known as: each

Yields:

  • (statement)

Yield Parameters:

  • statement (RDF::Statement)

Yield Returns:

  • (void)

    ignored

See Also:

  • RDF::Reader#each_statement


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/rdf/raptor/ffi.rb', line 78

def each_statement(**options, &block)
  if block_given?
    if options[:raw]
      # this is up to an order of magnitude faster...
      parse(@input) do |parser, statement|
        block.call(V2::Statement.new(statement, self))
      end
    else
      parse(@input) do |parser, statement|
        block.call(V2::Statement.new(statement, self).to_rdf)
      end
    end

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

#each_triple {|triple| ... } ⇒ Object

Yields:

  • (triple)

Yield Parameters:

  • triple (Array(RDF::Resource, RDF::URI, RDF::Term))

Yield Returns:

  • (void)

    ignored

See Also:

  • RDF::Reader#each_triple


104
105
106
107
108
109
110
111
# File 'lib/rdf/raptor/ffi.rb', line 104

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