Class: RDF::Raptor::FFI::V1::Parser

Inherits:
FFI::ManagedStruct
  • Object
show all
Includes:
RDF::Raptor::FFI
Defined in:
lib/rdf/raptor/ffi/v1/parser.rb

Overview

This class provides the functionality of turning syntaxes into RDF triples - RDF parsing.

Constant Summary collapse

BASE_URI =

The default base URI

'file:///dev/stdin'
BUFFER_SIZE =

The maximum chunk size for #parse_stream

64 * 1024

Constants included from RDF::Raptor::FFI

ENGINE

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RDF::Raptor::FFI

#version

Constructor Details

#initialize(ptr) ⇒ Parser #initialize(name) ⇒ Parser

Returns a new instance of Parser.

Overloads:

  • #initialize(ptr) ⇒ Parser

    Parameters:

    • ptr (FFI::Pointer)
  • #initialize(name) ⇒ Parser

    Parameters:

    • name (Symbol, String)

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 24

def initialize(ptr_or_name)
  ptr = case ptr_or_name
    when FFI::Pointer then ptr_or_name
    when Symbol       then V1.raptor_new_parser(ptr_or_name.to_s)
    when String       then V1.raptor_new_parser(ptr_or_name)
    else nil
  end
  raise ArgumentError, "invalid argument: #{ptr_or_name.inspect}" if ptr.nil? || ptr.null?
  super(ptr)
end

Class Method Details

.release(ptr)

This method returns an undefined value.

Releases libraptor memory associated with this structure.

Parameters:

  • ptr (FFI::Pointer)


40
41
42
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 40

def self.release(ptr)
  V1.raptor_free_parser(ptr)
end

Instance Method Details

#error_handler=(handler)

This method returns an undefined value.

Parameters:

  • handler (Proc)


47
48
49
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 47

def error_handler=(handler)
  V1.raptor_set_error_handler(self, self, handler)
end

#parse(input, **options) {|parser, statement| ... }

This method returns an undefined value.

Parameters:

  • input (Object)

    the input to parse

  • options (Hash{Symbol => Object})

    any additional options for parsing

Options Hash (**options):

  • :base_uri (String, #to_s) — default: nil

    the base URI to use when resolving relative URIs

Yields:

  • (parser, statement)

    each statement in the input

Yield Parameters:

  • parser (FFI::Pointer)
  • statement (FFI::Pointer)

Yield Returns:

  • (void)

    ignored



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 78

def parse(input, **options, &block)
  case input
    when RDF::URI, %r(^(file|https|http|ftp)://)
      parse_url(input, **options, &block)
    when File, Tempfile
      parse_file(input, **options, &block)
    when IO, StringIO
      parse_stream(input, **options, &block)
    when String
      parse_buffer(input, **options, &block)
    else
      raise ArgumentError, "don't know how to parse #{input.inspect}"
  end
end

#parse_buffer(buffer, base_uri: nil, **options) {|parser, statement| ... }

This method returns an undefined value.

Parameters:

  • buffer (String, #to_str)

    the input buffer to parse

  • options (Hash{Symbol => Object})

    any additional options for parsing (see #parse)

Yields:

  • (parser, statement)

    each statement in the input

Yield Parameters:

  • parser (FFI::Pointer)
  • statement (FFI::Pointer)

Yield Returns:

  • (void)

    ignored



171
172
173
174
175
176
177
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 171

def parse_buffer(buffer, base_uri: nil, **options, &block)
  self.statement_handler = block if block_given?

  parse_start!((base_uri || BASE_URI).to_s)
  parse_chunk(buffer.to_str)
  parse_end!
end

#parse_file(file, base_uri: nil, **options) {|parser, statement| ... }

This method returns an undefined value.

Parameters:

  • file (File, Tempfile, #path)

    the input file to parse

  • options (Hash{Symbol => Object})

    any additional options for parsing (see #parse)

Yields:

  • (parser, statement)

    each statement in the input

Yield Parameters:

  • parser (FFI::Pointer)
  • statement (FFI::Pointer)

Yield Returns:

  • (void)

    ignored



126
127
128
129
130
131
132
133
134
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 126

def parse_file(file, base_uri: nil, **options, &block)
  self.statement_handler = block if block_given?

  data_url = V1::URI.new("file://#{File.expand_path(file.path)}")
  base_uri = base_uri.to_s.empty? ? nil : V1::URI.new(base_uri.to_s)

  result = V1.raptor_parse_file(self, data_url, base_uri)
  # TODO: error handling if result.nonzero?
end

#parse_stream(stream, base_uri: nil, **options) {|parser, statement| ... }

This method returns an undefined value.

Parameters:

  • stream (IO, StringIO, #readpartial)

    the input stream to parse

  • options (Hash{Symbol => Object})

    any additional options for parsing (see #parse)

Yields:

  • (parser, statement)

    each statement in the input

Yield Parameters:

  • parser (FFI::Pointer)
  • statement (FFI::Pointer)

Yield Returns:

  • (void)

    ignored



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 147

def parse_stream(stream, base_uri: nil, **options, &block)
  self.statement_handler = block if block_given?

  begin
    parse_start!((base_uri || BASE_URI).to_s)
    loop do
      parse_chunk(stream.sysread(BUFFER_SIZE))
    end
  rescue EOFError => e
    parse_end!
  end
end

#parse_url(url, base_uri: nil, **options) {|parser, statement| ... } Also known as: parse_uri

This method returns an undefined value.

Parameters:

  • url (RDF::URI, String, #to_s)

    the input URL to parse

  • options (Hash{Symbol => Object})

    any additional options for parsing (see #parse)

Yields:

  • (parser, statement)

    each statement in the input

Yield Parameters:

  • parser (FFI::Pointer)
  • statement (FFI::Pointer)

Yield Returns:

  • (void)

    ignored



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

def parse_url(url, base_uri: nil, **options, &block)
  self.statement_handler = block if block_given?

  data_url = V1::URI.new((url.respond_to?(:to_uri) ? url.to_uri : url).to_s)
  base_uri = base_uri.to_s.empty? ? nil : V1::URI.new(base_uri.to_s)

  result = V1.raptor_parse_uri(self, data_url, base_uri)
  # TODO: error handling if result.nonzero?
end

#statement_handler=(handler)

This method returns an undefined value.

Parameters:

  • handler (Proc)


61
62
63
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 61

def statement_handler=(handler)
  V1.raptor_parser_set_statement_handler(self, self, handler)
end

#warning_handler=(handler)

This method returns an undefined value.

Parameters:

  • handler (Proc)


54
55
56
# File 'lib/rdf/raptor/ffi/v1/parser.rb', line 54

def warning_handler=(handler)
  V1.raptor_set_warning_handler(self, self, handler)
end