class RDF::Raptor::FFI::V2::Parser
This class provides the functionality of turning syntaxes into RDF
triples - RDF
parsing.
Constants
- BASE_URI
The default base
URI
- BUFFER_SIZE
The maximum chunk size for ‘#parse_stream`
Public Class Methods
@overload initialize(ptr)
@param [FFI::Pointer] ptr
@overload initialize(name)
@param [Symbol, String] name
# 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
Releases ‘libraptor` memory associated with this structure.
@param [FFI::Pointer] ptr @return [void]
# File lib/rdf/raptor/ffi/v2/parser.rb, line 40 def self.release(ptr) V2.raptor_free_parser(ptr) end
Public Instance Methods
@param [Proc] handler @return [void]
# File lib/rdf/raptor/ffi/v2/parser.rb, line 47 def error_handler=(handler) #V2.raptor_set_error_handler(self, self, handler) end
@param [Proc] handler @return [void]
# File lib/rdf/raptor/ffi/v2/parser.rb, line 68 def namespace_handler=(handler) V2.raptor_parser_set_namespace_handler(self, self, handler) end
@param [Object] input
the input to parse
@param [Hash{Symbol => Object}] options
any additional options for parsing
@option options [String, to_s] :base_uri (nil)
the base URI to use when resolving relative URIs
@yield [parser, statement]
each statement in the input
@yieldparam [FFI::Pointer] parser @yieldparam [FFI::Pointer] statement @yieldreturn [void] ignored @return [void]
# 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
@param [String, to_str] buffer
the input buffer to parse
@param [Hash{Symbol => Object}] options
any additional options for parsing (see {#parse})
@yield [parser, statement]
each statement in the input
@yieldparam [FFI::Pointer] parser @yieldparam [FFI::Pointer] statement @yieldreturn [void] ignored @return [void]
# 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
@private @param [String] buffer
the input chunk to parse
@return [void]
# File lib/rdf/raptor/ffi/v2/parser.rb, line 200 def parse_chunk(buffer) result = V2.raptor_parser_parse_chunk(self, buffer, buffer.bytesize, 0) # TODO: error handling if result.nonzero? end
@private @return [void]
# File lib/rdf/raptor/ffi/v2/parser.rb, line 208 def parse_end! result = V2.raptor_parser_parse_chunk(self, nil, 0, 1) # EOF end
@param [File, Tempfile, path] file
the input file to parse
@param [Hash{Symbol => Object}] options
any additional options for parsing (see {#parse})
@yield [parser, statement]
each statement in the input
@yieldparam [FFI::Pointer] parser @yieldparam [FFI::Pointer] statement @yieldreturn [void] ignored @return [void]
# 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
@private @param [String] base_uri @return [void]
# File lib/rdf/raptor/ffi/v2/parser.rb, line 190 def parse_start!(base_uri = BASE_URI) result = V2.raptor_parser_parse_start(self, V2::URI.new(base_uri)) # TODO: error handling if result.nonzero? end
@param [IO, StringIO, readpartial] stream
the input stream to parse
@param [Hash{Symbol => Object}] options
any additional options for parsing (see {#parse})
@yield [parser, statement]
each statement in the input
@yieldparam [FFI::Pointer] parser @yieldparam [FFI::Pointer] statement @yieldreturn [void] ignored @return [void]
# 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
@param [RDF::URI, String, to_s] url
the input URL to parse
@param [Hash{Symbol => Object}] options
any additional options for parsing (see {#parse})
@yield [parser, statement]
each statement in the input
@yieldparam [FFI::Pointer] parser @yieldparam [FFI::Pointer] statement @yieldreturn [void] ignored @return [void]
# 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
@param [Proc] handler @return [void]
# File lib/rdf/raptor/ffi/v2/parser.rb, line 61 def statement_handler=(handler) V2.raptor_parser_set_statement_handler(self, self, handler) end
@param [Proc] handler @return [void]
# File lib/rdf/raptor/ffi/v2/parser.rb, line 54 def warning_handler=(handler) #V2.raptor_set_warning_handler(self, self, handler) end