Class: RDF::Raptor::FFI::V2::Parser

Inherits:
FFI::ManagedStruct
  • Object
show all
Includes:
RDF::Raptor::FFI
Defined in:
lib/rdf/raptor/ffi/v2/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/v2/parser.rb', line 24

def initialize(ptr_or_name)
  ptr = case ptr_or_name
    when FFI::Pointer then ptr_or_name
    when Symbol       then V2.raptor_new_parser(V2.world, ptr_or_name.to_s)
    when String       then V2.raptor_new_parser(V2.world, 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/v2/parser.rb', line 40

def self.release(ptr)
  V2.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/v2/parser.rb', line 47

def error_handler=(handler)
  #V2.raptor_set_error_handler(self, self, handler)
end

#namespace_handler=(handler)

This method returns an undefined value.

Parameters:

  • handler (Proc)


68
69
70
# File 'lib/rdf/raptor/ffi/v2/parser.rb', line 68

def namespace_handler=(handler)
  V2.raptor_parser_set_namespace_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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rdf/raptor/ffi/v2/parser.rb', line 85

def parse(input, **options, &block)
  case input
    when RDF::URI, 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, **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



178
179
180
181
182
183
184
# File 'lib/rdf/raptor/ffi/v2/parser.rb', line 178

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

  parse_start!((options[:base_uri] || BASE_URI).to_s)
  parse_chunk(buffer.to_str)
  parse_end!
end

#parse_file(file, **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



133
134
135
136
137
138
139
140
141
# File 'lib/rdf/raptor/ffi/v2/parser.rb', line 133

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

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

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

#parse_stream(stream, **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



154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/rdf/raptor/ffi/v2/parser.rb', line 154

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

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

#parse_url(url, **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



111
112
113
114
115
116
117
118
119
# File 'lib/rdf/raptor/ffi/v2/parser.rb', line 111

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

  data_url = V2::URI.new((url.respond_to?(:to_uri) ? url.to_uri : url).to_s)
  base_uri = options[:base_uri].to_s.empty? ? nil : V2::URI.new(options[:base_uri].to_s)

  result = V2.raptor_parser_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/v2/parser.rb', line 61

def statement_handler=(handler)
  V2.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/v2/parser.rb', line 54

def warning_handler=(handler)
  #V2.raptor_set_warning_handler(self, self, handler)
end