Class: RDF::Raptor::FFI::V2::Parser
- Inherits:
-
FFI::ManagedStruct
- Object
- FFI::ManagedStruct
- RDF::Raptor::FFI::V2::Parser
- 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
Class Method Summary collapse
-
.release(ptr)
Releases
libraptor
memory associated with this structure.
Instance Method Summary collapse
- #error_handler=(handler)
-
#initialize(ptr_or_name) ⇒ Parser
constructor
A new instance of Parser.
- #namespace_handler=(handler)
- #parse(input, **options) {|parser, statement| ... }
- #parse_buffer(buffer, **options) {|parser, statement| ... }
- #parse_file(file, **options) {|parser, statement| ... }
- #parse_stream(stream, **options) {|parser, statement| ... }
- #parse_url(url, **options) {|parser, statement| ... } (also: #parse_uri)
- #statement_handler=(handler)
- #warning_handler=(handler)
Methods included from RDF::Raptor::FFI
Constructor Details
#initialize(ptr) ⇒ Parser #initialize(name) ⇒ Parser
Returns a new instance of Parser.
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.
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.
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.
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.
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, **, &block) case input when RDF::URI, URI, %r(^(file|https|http|ftp)://) parse_url(input, **, &block) when File, Tempfile parse_file(input, **, &block) when IO, StringIO parse_stream(input, **, &block) when String parse_buffer(input, **, &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.
178 179 180 181 182 183 184 |
# File 'lib/rdf/raptor/ffi/v2/parser.rb', line 178 def parse_buffer(buffer, **, &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, **options) {|parser, statement| ... }
This method returns an undefined value.
133 134 135 136 137 138 139 140 141 |
# File 'lib/rdf/raptor/ffi/v2/parser.rb', line 133 def parse_file(file, **, &block) self.statement_handler = block if block_given? data_url = V2::URI.new("file://#{File.(file.path)}") base_uri = [:base_uri].to_s.empty? ? nil : V2::URI.new([: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.
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, **, &block) self.statement_handler = block if block_given? begin parse_start!([: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.
111 112 113 114 115 116 117 118 119 |
# File 'lib/rdf/raptor/ffi/v2/parser.rb', line 111 def parse_url(url, **, &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 = [:base_uri].to_s.empty? ? nil : V2::URI.new([: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.
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.
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 |