rdf-raptor 0.4.2 → 1.0.0.beta1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +32 -11
- data/VERSION +1 -1
- data/lib/rdf/raptor/cli.rb +0 -1
- data/lib/rdf/raptor/ffi/v1/parser.rb +1 -1
- data/lib/rdf/raptor/ffi/v2/iostream.rb +56 -0
- data/lib/rdf/raptor/ffi/v2/iostream_handler.rb +153 -0
- data/lib/rdf/raptor/ffi/v2/parser.rb +205 -0
- data/lib/rdf/raptor/ffi/v2/serializer.rb +137 -0
- data/lib/rdf/raptor/ffi/v2/statement.rb +143 -0
- data/lib/rdf/raptor/ffi/v2/term.rb +95 -0
- data/lib/rdf/raptor/ffi/v2/uri.rb +111 -0
- data/lib/rdf/raptor/ffi/v2/world.rb +20 -0
- data/lib/rdf/raptor/ffi/v2.rb +156 -0
- data/lib/rdf/raptor/ffi.rb +13 -14
- data/lib/rdf/raptor/format.rb +1 -1
- data/lib/rdf/raptor.rb +1 -1
- metadata +19 -10
@@ -0,0 +1,143 @@
|
|
1
|
+
module RDF::Raptor::FFI::V2
|
2
|
+
##
|
3
|
+
# @see http://librdf.org/raptor/api-1.4/raptor-section-triples.html
|
4
|
+
class Statement < ::FFI::Struct
|
5
|
+
include RDF::Raptor::FFI
|
6
|
+
layout :world, :pointer,
|
7
|
+
:usage, :int,
|
8
|
+
:subject, :pointer,
|
9
|
+
:predicate, :pointer,
|
10
|
+
:object, :pointer,
|
11
|
+
:graph, :pointer
|
12
|
+
|
13
|
+
##
|
14
|
+
# @param [FFI::Pointer] ptr
|
15
|
+
# @param [#create_node] factory
|
16
|
+
def initialize(ptr = nil, factory = nil)
|
17
|
+
super(ptr)
|
18
|
+
@factory = factory if factory
|
19
|
+
end
|
20
|
+
|
21
|
+
##
|
22
|
+
# Releases `libraptor` memory associated with this structure.
|
23
|
+
#
|
24
|
+
# @return [void]
|
25
|
+
def release
|
26
|
+
V2.raptor_free_statement(ptr) unless ptr.null?
|
27
|
+
end
|
28
|
+
|
29
|
+
# @return [Object]
|
30
|
+
attr_accessor :id
|
31
|
+
|
32
|
+
# @return [RDF::Resource]
|
33
|
+
attr_accessor :context
|
34
|
+
|
35
|
+
##
|
36
|
+
# @return [RDF::Resource]
|
37
|
+
def subject
|
38
|
+
@subject ||= V2::Term.new(self[:subject], @factory).value
|
39
|
+
end
|
40
|
+
|
41
|
+
##
|
42
|
+
# Sets the subject term from an `RDF::Resource`.
|
43
|
+
#
|
44
|
+
# @param [RDF::Resource] value
|
45
|
+
# @return [void]
|
46
|
+
def subject=(resource)
|
47
|
+
@subject = nil
|
48
|
+
case resource
|
49
|
+
when RDF::Node
|
50
|
+
self[:subject] = V2.raptor_new_term_from_blank(V2.world, resource.id.to_s)
|
51
|
+
when RDF::URI
|
52
|
+
self[:subject] = V2.raptor_new_term_from_uri_string(V2.world, resource.to_s)
|
53
|
+
else
|
54
|
+
raise ArgumentError, "subject term must be an RDF::Node or RDF::URI"
|
55
|
+
end
|
56
|
+
@subject = resource
|
57
|
+
end
|
58
|
+
|
59
|
+
##
|
60
|
+
# @return [RDF::URI]
|
61
|
+
def predicate
|
62
|
+
@predicate ||= V2::Term.new(self[:predicate], @factory).value
|
63
|
+
end
|
64
|
+
|
65
|
+
##
|
66
|
+
# Sets the predicate term from an `RDF::URI`.
|
67
|
+
#
|
68
|
+
# @param [RDF::URI] value
|
69
|
+
# @return [void]
|
70
|
+
def predicate=(uri)
|
71
|
+
@predicate = nil
|
72
|
+
raise ArgumentError, "predicate term must be an RDF::URI" unless uri.is_a?(RDF::URI)
|
73
|
+
self[:predicate] = V2.raptor_new_term_from_uri_string(V2.world, uri.to_s)
|
74
|
+
@predicate = uri
|
75
|
+
end
|
76
|
+
|
77
|
+
##
|
78
|
+
# @return [RDF::Term]
|
79
|
+
def object
|
80
|
+
@object ||= V2::Term.new(self[:object], @factory).value
|
81
|
+
end
|
82
|
+
|
83
|
+
##
|
84
|
+
# Sets the object term from an `RDF::Term`.
|
85
|
+
#
|
86
|
+
# The value must be one of `RDF::Resource` or `RDF::Literal`.
|
87
|
+
#
|
88
|
+
# @param [RDF::Term] value
|
89
|
+
# @return [void]
|
90
|
+
def object=(value)
|
91
|
+
@object = nil
|
92
|
+
case value
|
93
|
+
when RDF::Node
|
94
|
+
self[:object] = V2.raptor_new_term_from_blank(V2.world, value.id.to_s)
|
95
|
+
when RDF::URI
|
96
|
+
self[:object] = V2.raptor_new_term_from_uri_string(V2.world, value.to_s)
|
97
|
+
when RDF::Literal
|
98
|
+
self[:object] = V2.raptor_new_term_from_literal(V2.world, value.to_s,
|
99
|
+
value.datatype? ? V2.raptor_new_uri(value.datatype.to_s) : nil,
|
100
|
+
value.language? ? V2.raptor_new_string(value.language.to_s) : nil)
|
101
|
+
else
|
102
|
+
raise ArgumentError, "object term must be an RDF::Node, RDF::URI, or RDF::Literal"
|
103
|
+
end
|
104
|
+
@object = value
|
105
|
+
end
|
106
|
+
|
107
|
+
##
|
108
|
+
# @return [Array(RDF::Resource, RDF::URI, RDF::Term)]
|
109
|
+
# @see RDF::Statement#to_triple
|
110
|
+
def to_triple
|
111
|
+
[subject, predicate, object]
|
112
|
+
end
|
113
|
+
|
114
|
+
##
|
115
|
+
# @return [Array(RDF::Resource, RDF::URI, RDF::Term, nil)]
|
116
|
+
# @see RDF::Statement#to_quad
|
117
|
+
def to_quad
|
118
|
+
[subject, predicate, object, context]
|
119
|
+
end
|
120
|
+
|
121
|
+
##
|
122
|
+
# @return [RDF::Statement]
|
123
|
+
def to_rdf
|
124
|
+
RDF::Statement.new(subject, predicate, object, :context => context)
|
125
|
+
end
|
126
|
+
|
127
|
+
##
|
128
|
+
# @return [void]
|
129
|
+
def reset!
|
130
|
+
@subject = @predicate = @object = @context = nil
|
131
|
+
end
|
132
|
+
|
133
|
+
##
|
134
|
+
# Releases `libraptor` memory associated with this structure.
|
135
|
+
#
|
136
|
+
# @return [void]
|
137
|
+
def free
|
138
|
+
V2.raptor_free_statement(self)
|
139
|
+
@subject = @predicate = @object = nil # Allow GC to start
|
140
|
+
end
|
141
|
+
alias_method :release, :free
|
142
|
+
end # Statement
|
143
|
+
end # RDF::Raptor::FFI::V2
|
@@ -0,0 +1,95 @@
|
|
1
|
+
module RDF::Raptor::FFI::V2
|
2
|
+
##
|
3
|
+
# @see http://librdf.org/raptor/api-1.4/raptor-section-triples.html
|
4
|
+
class Term < ::FFI::Struct
|
5
|
+
include RDF::Raptor::FFI
|
6
|
+
|
7
|
+
class LiteralValue < ::FFI::Struct
|
8
|
+
include RDF::Raptor::FFI
|
9
|
+
|
10
|
+
layout :string, :string,
|
11
|
+
:string_len, :int,
|
12
|
+
:datatype, :pointer,
|
13
|
+
:language, :string,
|
14
|
+
:language_len, :char
|
15
|
+
|
16
|
+
def to_str
|
17
|
+
self[:string].unpack('U*').pack('U*')
|
18
|
+
end
|
19
|
+
|
20
|
+
def language
|
21
|
+
unless self[:language].nil? or self[:language].empty?
|
22
|
+
self[:language]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def datatype
|
27
|
+
if self[:datatype] && !self[:datatype].null?
|
28
|
+
RDF::URI.intern(V2.raptor_uri_to_string(self[:datatype]))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def to_rdf
|
33
|
+
str = self.to_str
|
34
|
+
case
|
35
|
+
when language = self.language
|
36
|
+
RDF::Literal.new(str, :language => language)
|
37
|
+
when datatype = self.datatype
|
38
|
+
RDF::Literal.new(str, :datatype => datatype)
|
39
|
+
else
|
40
|
+
RDF::Literal.new(str)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class BlankValue < ::FFI::Struct
|
46
|
+
layout :string, :string,
|
47
|
+
:string_len, :int
|
48
|
+
|
49
|
+
def to_str
|
50
|
+
self[:string]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
class Value < ::FFI::Union
|
55
|
+
include RDF::Raptor::FFI
|
56
|
+
|
57
|
+
layout :uri, :pointer,
|
58
|
+
:literal, LiteralValue,
|
59
|
+
:blank, BlankValue
|
60
|
+
end
|
61
|
+
|
62
|
+
layout :world, :pointer,
|
63
|
+
:usage, :int,
|
64
|
+
:type, :int,
|
65
|
+
:value, Value
|
66
|
+
##
|
67
|
+
# @param [FFI::Pointer] ptr
|
68
|
+
# @param [#create_node] factory
|
69
|
+
def initialize(ptr = nil, factory = nil)
|
70
|
+
super(ptr)
|
71
|
+
@factory = factory if factory
|
72
|
+
end
|
73
|
+
|
74
|
+
def value
|
75
|
+
case self[:type]
|
76
|
+
when RAPTOR_TERM_TYPE_BLANK
|
77
|
+
@factory.create_node(self[:value][:blank].to_str)
|
78
|
+
when RAPTOR_TERM_TYPE_URI
|
79
|
+
@factory.create_uri(V2.raptor_uri_as_string(self[:value][:uri]))
|
80
|
+
when RAPTOR_TERM_TYPE_LITERAL
|
81
|
+
self[:value][:literal].to_rdf
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
##
|
86
|
+
# Releases `libraptor` memory associated with this structure.
|
87
|
+
#
|
88
|
+
# @param [FFI::Pointer] ptr
|
89
|
+
# @return [void]
|
90
|
+
def release
|
91
|
+
V2.raptor_free_term(self) unless ptr.null?
|
92
|
+
end
|
93
|
+
|
94
|
+
end # Term
|
95
|
+
end # RDF::Raptor::FFI::V2
|
@@ -0,0 +1,111 @@
|
|
1
|
+
module RDF::Raptor::FFI::V2
|
2
|
+
##
|
3
|
+
# Raptor has a `raptor_uri` class which must be used for manipulating and
|
4
|
+
# passing URI references. The default internal implementation uses `char*`
|
5
|
+
# strings for URIs, manipulating them and constructing them.
|
6
|
+
#
|
7
|
+
# @see http://librdf.org/raptor/api-1.4/raptor-section-uri.html
|
8
|
+
class URI < ::FFI::ManagedStruct
|
9
|
+
include RDF::Raptor::FFI
|
10
|
+
include RDF::Resource
|
11
|
+
layout :string, [:char, 1] # a safe dummy layout, since it is \0-terminated
|
12
|
+
|
13
|
+
##
|
14
|
+
# @overload initialize(ptr)
|
15
|
+
# @param [FFI::Pointer] ptr
|
16
|
+
#
|
17
|
+
# @overload initialize(name)
|
18
|
+
# @param [RDF::URI, String] name
|
19
|
+
#
|
20
|
+
def initialize(ptr_or_name)
|
21
|
+
ptr = case ptr_or_name
|
22
|
+
when FFI::Pointer then ptr_or_name
|
23
|
+
when RDF::URI then V2.raptor_new_uri(V2.world, ptr_or_name.to_s)
|
24
|
+
when String then V2.raptor_new_uri(V2.world, ptr_or_name)
|
25
|
+
else nil
|
26
|
+
end
|
27
|
+
raise ArgumentError, "invalid argument: #{ptr_or_name.inspect}" if ptr.nil? || ptr.null?
|
28
|
+
super(ptr)
|
29
|
+
end
|
30
|
+
|
31
|
+
##
|
32
|
+
# Releases `libraptor` memory associated with this structure.
|
33
|
+
#
|
34
|
+
# @param [FFI::Pointer] ptr
|
35
|
+
# @return [void]
|
36
|
+
def self.release(ptr)
|
37
|
+
V2.raptor_free_uri(ptr)
|
38
|
+
end
|
39
|
+
|
40
|
+
##
|
41
|
+
# @return [Boolean] `true`
|
42
|
+
def uri?
|
43
|
+
true
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# @return [URI] a copy of `self`
|
48
|
+
def dup
|
49
|
+
copy = self.class.new(V2.raptor_uri_copy(self))
|
50
|
+
copy.taint if tainted?
|
51
|
+
copy
|
52
|
+
end
|
53
|
+
|
54
|
+
##
|
55
|
+
# @return [URI] a copy of `self`
|
56
|
+
def clone
|
57
|
+
copy = self.class.new(V2.raptor_uri_copy(self))
|
58
|
+
copy.taint if tainted?
|
59
|
+
copy.freeze if frozen?
|
60
|
+
copy
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# @return [Integer]
|
65
|
+
def length
|
66
|
+
LibC.strlen(self)
|
67
|
+
end
|
68
|
+
alias_method :size, :length
|
69
|
+
|
70
|
+
##
|
71
|
+
# @return [Boolean] `true` or `false`
|
72
|
+
def ==(other)
|
73
|
+
return true if self.equal?(other)
|
74
|
+
case other
|
75
|
+
when self.class
|
76
|
+
!(V2.raptor_uri_equals(self, other).zero?)
|
77
|
+
when RDF::URI, String
|
78
|
+
to_str == other.to_str
|
79
|
+
else false
|
80
|
+
end
|
81
|
+
end
|
82
|
+
alias_method :===, :==
|
83
|
+
|
84
|
+
##
|
85
|
+
# @return [Boolean] `true` or `false`
|
86
|
+
def eql?(other)
|
87
|
+
return true if self.equal?(other)
|
88
|
+
other.is_a?(self.class) && !(V2.raptor_uri_equals(self, other).zero?)
|
89
|
+
end
|
90
|
+
|
91
|
+
##
|
92
|
+
# @return [Fixnum]
|
93
|
+
def hash
|
94
|
+
to_str.hash
|
95
|
+
end
|
96
|
+
|
97
|
+
##
|
98
|
+
# @return [String] the URI string
|
99
|
+
def to_str
|
100
|
+
V2.raptor_uri_as_string(self)
|
101
|
+
end
|
102
|
+
alias_method :to_s, :to_str
|
103
|
+
|
104
|
+
##
|
105
|
+
# @return [RDF::URI]
|
106
|
+
def to_rdf
|
107
|
+
RDF::URI.intern(to_str)
|
108
|
+
end
|
109
|
+
alias_method :to_uri, :to_rdf
|
110
|
+
end # URI
|
111
|
+
end # RDF::Raptor::FFI::V2
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module RDF::Raptor::FFI::V2
|
2
|
+
class World < ::FFI::AutoPointer
|
3
|
+
include RDF::Raptor::FFI
|
4
|
+
|
5
|
+
def initialize()
|
6
|
+
ptr = V2.raptor_new_world_internal(V2.raptor_version_decimal)
|
7
|
+
super(ptr, self.class.method(:release))
|
8
|
+
end
|
9
|
+
|
10
|
+
##
|
11
|
+
# Releases `libraptor` memory associated with this structure.
|
12
|
+
#
|
13
|
+
# @param [FFI::Pointer] ptr
|
14
|
+
# @return [void]
|
15
|
+
def self.release(ptr)
|
16
|
+
V2.raptor_free_world(ptr)
|
17
|
+
end
|
18
|
+
|
19
|
+
end # World
|
20
|
+
end # RDF::Raptor::FFI::V2
|
@@ -0,0 +1,156 @@
|
|
1
|
+
module RDF::Raptor::FFI
|
2
|
+
##
|
3
|
+
# A foreign-function interface (FFI) to `libraptor` 2.x.
|
4
|
+
#
|
5
|
+
# @see http://librdf.org/raptor/libraptor.html
|
6
|
+
module V2
|
7
|
+
autoload :IOStream, 'rdf/raptor/ffi/v2/iostream'
|
8
|
+
autoload :IOStreamHandler, 'rdf/raptor/ffi/v2/iostream_handler'
|
9
|
+
autoload :Parser, 'rdf/raptor/ffi/v2/parser'
|
10
|
+
autoload :Serializer, 'rdf/raptor/ffi/v2/serializer'
|
11
|
+
autoload :Statement, 'rdf/raptor/ffi/v2/statement'
|
12
|
+
autoload :URI, 'rdf/raptor/ffi/v2/uri'
|
13
|
+
autoload :Term, 'rdf/raptor/ffi/v2/term'
|
14
|
+
autoload :World, 'rdf/raptor/ffi/v2/world'
|
15
|
+
|
16
|
+
extend ::FFI::Library
|
17
|
+
ffi_lib RDF::Raptor::LIBRAPTOR
|
18
|
+
|
19
|
+
# TODO: Ideally this would be an enum, but the JRuby FFI (as of
|
20
|
+
# version 1.4.0) has problems with enums as part of structs:
|
21
|
+
# `Unknown field type: #<FFI::Enum> (ArgumentError)`
|
22
|
+
RAPTOR_TERM_TYPE_UNKNOWN = 0
|
23
|
+
RAPTOR_TERM_TYPE_URI = 1
|
24
|
+
RAPTOR_TERM_TYPE_LITERAL = 2
|
25
|
+
RAPTOR_TERM_TYPE_BLANK = 4
|
26
|
+
|
27
|
+
# @see http://librdf.org/raptor/api/tutorial-initialising-finishing.html
|
28
|
+
typedef :pointer, :raptor_world
|
29
|
+
typedef :int, :raptor_version
|
30
|
+
attach_function :raptor_new_world_internal, [:raptor_version], :raptor_world
|
31
|
+
attach_function :raptor_free_world, [], :void
|
32
|
+
attach_function :raptor_alloc_memory, [:size_t], :pointer
|
33
|
+
attach_function :raptor_calloc_memory, [:size_t, :size_t], :pointer
|
34
|
+
attach_function :raptor_free_memory, [:pointer], :void
|
35
|
+
|
36
|
+
# @see http://librdf.org/raptor/api-1.4/raptor-section-locator.html
|
37
|
+
typedef :pointer, :raptor_locator
|
38
|
+
attach_function :raptor_locator_line, [:raptor_locator], :int
|
39
|
+
attach_function :raptor_locator_column, [:raptor_locator], :int
|
40
|
+
attach_function :raptor_locator_byte, [:raptor_locator], :int
|
41
|
+
|
42
|
+
# @see http://librdf.org/raptor/api/raptor2-section-general.html
|
43
|
+
attach_variable :raptor_version_string, :string
|
44
|
+
attach_variable :raptor_version_major, :int
|
45
|
+
attach_variable :raptor_version_minor, :int
|
46
|
+
attach_variable :raptor_version_release, :int
|
47
|
+
attach_variable :raptor_version_decimal, :int
|
48
|
+
callback :raptor_message_handler, [:pointer, :raptor_locator, :string], :void
|
49
|
+
|
50
|
+
# @see http://librdf.org/raptor/api-1.4/raptor-section-uri.html
|
51
|
+
typedef :pointer, :raptor_uri
|
52
|
+
attach_function :raptor_new_uri, [:raptor_world, :string], :raptor_uri
|
53
|
+
attach_function :raptor_uri_copy, [:raptor_uri], :raptor_uri
|
54
|
+
attach_function :raptor_uri_equals, [:raptor_uri, :raptor_uri], :int
|
55
|
+
attach_function :raptor_uri_as_string, [:raptor_uri], :string
|
56
|
+
attach_function :raptor_uri_to_string, [:raptor_uri], :string
|
57
|
+
attach_function :raptor_uri_print, [:raptor_uri, :pointer], :void
|
58
|
+
attach_function :raptor_free_uri, [:raptor_uri], :void
|
59
|
+
|
60
|
+
# @see http://librdf.org/raptor/api/raptor2-section-triples.html
|
61
|
+
typedef :int, :raptor_identifier_type
|
62
|
+
typedef :pointer, :raptor_identifier
|
63
|
+
typedef :pointer, :raptor_statement
|
64
|
+
attach_function :raptor_statement_compare, [:raptor_statement, :raptor_statement], :int
|
65
|
+
attach_function :raptor_statement_print, [:raptor_statement, :pointer], :void
|
66
|
+
attach_function :raptor_statement_print_as_ntriples, [:pointer, :pointer], :void
|
67
|
+
#attach_function :raptor_statement_part_as_string, [:pointer, :raptor_identifier_type, :raptor_uri, :pointer], :string
|
68
|
+
attach_function :raptor_free_statement, [:raptor_statement], :void
|
69
|
+
typedef :pointer, :raptor_term
|
70
|
+
typedef :string, :literal
|
71
|
+
typedef :pointer, :datatype
|
72
|
+
typedef :string, :language
|
73
|
+
typedef :string, :blank
|
74
|
+
attach_function :raptor_new_term_from_uri, [:raptor_world, :raptor_uri], :raptor_term
|
75
|
+
attach_function :raptor_new_term_from_uri_string, [:raptor_world, :string], :raptor_term
|
76
|
+
attach_function :raptor_new_term_from_literal, [:raptor_world, :literal, :datatype, :language], :raptor_term
|
77
|
+
attach_function :raptor_new_term_from_blank, [:raptor_world, :blank], :raptor_term
|
78
|
+
attach_function :raptor_free_term, [:raptor_term], :void
|
79
|
+
|
80
|
+
# @see http://librdf.org/raptor/api/raptor2-section-parser.html
|
81
|
+
callback :raptor_statement_handler, [:pointer, :raptor_statement], :void
|
82
|
+
typedef :pointer, :raptor_parser
|
83
|
+
typedef :string, :mime_type
|
84
|
+
typedef :string, :buffer
|
85
|
+
attach_function :raptor_new_parser, [:raptor_world, :string], :raptor_parser
|
86
|
+
attach_function :raptor_world_guess_parser_name, [:raptor_world, :raptor_uri, :mime_type, :buffer, :size_t, :string], :string
|
87
|
+
#attach_function :raptor_set_error_handler, [:raptor_parser, :pointer, :raptor_message_handler], :void
|
88
|
+
#attach_function :raptor_set_warning_handler, [:raptor_parser, :pointer, :raptor_message_handler], :void
|
89
|
+
attach_function :raptor_parser_set_statement_handler, [:raptor_parser, :pointer, :raptor_statement_handler], :void
|
90
|
+
attach_function :raptor_parser_parse_file, [:raptor_parser, :raptor_uri, :raptor_uri], :int
|
91
|
+
attach_function :raptor_parser_parse_file_stream, [:raptor_parser, :pointer, :string, :raptor_uri], :int
|
92
|
+
attach_function :raptor_parser_parse_uri, [:raptor_parser, :raptor_uri, :raptor_uri], :int
|
93
|
+
attach_function :raptor_parser_parse_start, [:raptor_parser, :raptor_uri], :int
|
94
|
+
attach_function :raptor_parser_parse_chunk, [:raptor_parser, :string, :size_t, :int], :int
|
95
|
+
#attach_function :raptor_get_mime_type, [:raptor_parser], :string
|
96
|
+
#attach_function :raptor_set_parser_strict, [:raptor_parser, :int], :void
|
97
|
+
#attach_function :raptor_get_need_base_uri, [:raptor_parser], :int
|
98
|
+
attach_function :raptor_parser_parse_abort, [], :void
|
99
|
+
attach_function :raptor_free_parser, [:raptor_parser], :void
|
100
|
+
|
101
|
+
# @see http://librdf.org/raptor/api/raptor2-section-iostream.html
|
102
|
+
typedef :pointer, :raptor_iostream
|
103
|
+
attach_function :raptor_new_iostream_from_handler, [:raptor_world, :pointer, :pointer], :raptor_iostream
|
104
|
+
attach_function :raptor_new_iostream_to_filename, [:raptor_world, :string], :raptor_iostream
|
105
|
+
attach_function :raptor_new_iostream_to_sink, [:raptor_world], :raptor_iostream
|
106
|
+
attach_function :raptor_free_iostream, [:raptor_iostream], :void
|
107
|
+
callback :raptor_iostream_init_func, [:pointer], :int
|
108
|
+
callback :raptor_iostream_finish_func, [:pointer], :void
|
109
|
+
callback :raptor_iostream_write_byte_func, [:pointer, :int], :int
|
110
|
+
callback :raptor_iostream_write_bytes_func, [:pointer, :pointer, :size_t, :size_t], :int
|
111
|
+
callback :raptor_iostream_write_end_func, [:pointer], :void
|
112
|
+
callback :raptor_iostream_read_bytes_func, [:pointer, :pointer, :size_t, :size_t], :int
|
113
|
+
callback :raptor_iostream_read_eof_func, [:pointer], :int
|
114
|
+
|
115
|
+
# @see http://librdf.org/raptor/api-1.4/raptor-section-xml-namespace.html
|
116
|
+
typedef :pointer, :raptor_namespace
|
117
|
+
|
118
|
+
# @see http://librdf.org/raptor/api/raptor2-section-serializer.html
|
119
|
+
typedef :pointer, :raptor_serializer
|
120
|
+
attach_function :raptor_new_serializer, [:raptor_world, :string], :raptor_serializer
|
121
|
+
attach_function :raptor_free_serializer, [:raptor_serializer], :void
|
122
|
+
attach_function :raptor_serializer_start_to_iostream, [:raptor_serializer, :raptor_uri, :raptor_iostream], :int
|
123
|
+
attach_function :raptor_serializer_start_to_filename, [:raptor_serializer, :string], :int
|
124
|
+
attach_function :raptor_serializer_serialize_statement, [:raptor_serializer, :raptor_statement], :int
|
125
|
+
attach_function :raptor_serializer_serialize_end, [:raptor_serializer], :int
|
126
|
+
#attach_function :raptor_serializer_set_error_handler, [:raptor_serializer, :pointer, :raptor_message_handler], :void
|
127
|
+
#attach_function :raptor_serializer_set_warning_handler, [:raptor_serializer, :pointer, :raptor_message_handler], :void
|
128
|
+
|
129
|
+
# Initialize the world.
|
130
|
+
# We do this exactly once and never release because we can't delegate
|
131
|
+
# any memory management to the Ruby GC.
|
132
|
+
# Internally `raptor_init`/`raptor_finish` work with reference counts.
|
133
|
+
@world = nil
|
134
|
+
def self.world
|
135
|
+
@world ||= World.new
|
136
|
+
end
|
137
|
+
|
138
|
+
##
|
139
|
+
# Allocates memory for the string `str` inside `libraptor`, copying the
|
140
|
+
# string into the newly-allocated buffer.
|
141
|
+
#
|
142
|
+
# The buffer should later be deallocated using `raptor_free_string`.
|
143
|
+
#
|
144
|
+
# @return [FFI::Pointer]
|
145
|
+
def raptor_new_string(str)
|
146
|
+
ptr = V2.raptor_alloc_memory(str.bytesize + 1)
|
147
|
+
ptr.put_string(0, str)
|
148
|
+
ptr
|
149
|
+
end
|
150
|
+
module_function :raptor_new_string
|
151
|
+
|
152
|
+
alias_method :raptor_free_string, :raptor_free_memory
|
153
|
+
module_function :raptor_free_string
|
154
|
+
|
155
|
+
end # v2
|
156
|
+
end # RDF::Raptor::FFI
|
data/lib/rdf/raptor/ffi.rb
CHANGED
@@ -9,6 +9,7 @@ module RDF::Raptor
|
|
9
9
|
# @see http://librdf.org/raptor/libraptor.html
|
10
10
|
module FFI
|
11
11
|
autoload :V1, 'rdf/raptor/ffi/v1'
|
12
|
+
autoload :V2, 'rdf/raptor/ffi/v2'
|
12
13
|
|
13
14
|
ENGINE = :ffi
|
14
15
|
|
@@ -21,9 +22,7 @@ module RDF::Raptor
|
|
21
22
|
#
|
22
23
|
# @return [String] an "x.y.z" version string
|
23
24
|
def version
|
24
|
-
|
25
|
-
V1.raptor_version_minor,
|
26
|
-
V1.raptor_version_release].join('.').freeze
|
25
|
+
V2.raptor_version_string.freeze
|
27
26
|
end
|
28
27
|
|
29
28
|
##
|
@@ -41,26 +40,26 @@ module RDF::Raptor
|
|
41
40
|
# @yieldreturn [void] ignored
|
42
41
|
def initialize(input = $stdin, options = {}, &block)
|
43
42
|
@format = self.class.format.rapper_format
|
44
|
-
@parser =
|
43
|
+
@parser = V2::Parser.new(@format)
|
45
44
|
@parser.error_handler = ERROR_HANDLER
|
46
45
|
@parser.warning_handler = WARNING_HANDLER
|
47
46
|
super
|
48
47
|
end
|
49
48
|
|
50
49
|
ERROR_HANDLER = Proc.new do |user_data, locator, message|
|
51
|
-
line =
|
50
|
+
line = V2.raptor_locator_line(locator)
|
52
51
|
raise RDF::ReaderError, line > -1 ? "Line #{line}: #{message}" : message
|
53
52
|
end
|
54
53
|
|
55
54
|
WARNING_HANDLER = Proc.new do |user_data, locator, message|
|
56
|
-
# line =
|
55
|
+
# line = V2.raptor_locator_line(locator)
|
57
56
|
# $stderr.puts line > -1 ? "Line #{line}: #{message}" : message
|
58
57
|
end
|
59
58
|
|
60
59
|
##
|
61
60
|
# The Raptor parser instance.
|
62
61
|
#
|
63
|
-
# @return [
|
62
|
+
# @return [V2::Parser]
|
64
63
|
attr_reader :parser
|
65
64
|
|
66
65
|
##
|
@@ -73,11 +72,11 @@ module RDF::Raptor
|
|
73
72
|
if options[:raw]
|
74
73
|
# this is up to an order of magnitude faster...
|
75
74
|
parse(@input) do |parser, statement|
|
76
|
-
block.call(
|
75
|
+
block.call(V2::Statement.new(statement, self))
|
77
76
|
end
|
78
77
|
else
|
79
78
|
parse(@input) do |parser, statement|
|
80
|
-
block.call(
|
79
|
+
block.call(V2::Statement.new(statement, self).to_rdf)
|
81
80
|
end
|
82
81
|
end
|
83
82
|
end
|
@@ -93,7 +92,7 @@ module RDF::Raptor
|
|
93
92
|
def each_triple(&block)
|
94
93
|
if block_given?
|
95
94
|
parse(@input) do |parser, statement|
|
96
|
-
block.call(
|
95
|
+
block.call(V2::Statement.new(statement, self).to_triple)
|
97
96
|
end
|
98
97
|
end
|
99
98
|
enum_for(:each_triple)
|
@@ -145,9 +144,9 @@ module RDF::Raptor
|
|
145
144
|
# @yieldreturn [void] ignored
|
146
145
|
def initialize(output = $stdout, options = {}, &block)
|
147
146
|
@format = self.class.format.rapper_format
|
148
|
-
@serializer =
|
149
|
-
|
150
|
-
|
147
|
+
@serializer = V2::Serializer.new(@format)
|
148
|
+
#@serializer.error_handler = ERROR_HANDLER
|
149
|
+
#@serializer.warning_handler = WARNING_HANDLER
|
151
150
|
@serializer.start_to(output, options)
|
152
151
|
super
|
153
152
|
end
|
@@ -163,7 +162,7 @@ module RDF::Raptor
|
|
163
162
|
##
|
164
163
|
# The Raptor serializer instance.
|
165
164
|
#
|
166
|
-
# @return [
|
165
|
+
# @return [V2::Serializer]
|
167
166
|
attr_reader :serializer
|
168
167
|
|
169
168
|
##
|
data/lib/rdf/raptor/format.rb
CHANGED
@@ -19,7 +19,7 @@ module RDF
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def detect(sample)
|
22
|
-
parser_name = RDF::Raptor::FFI::
|
22
|
+
parser_name = RDF::Raptor::FFI::V2.raptor_world_guess_parser_name(RDF::Raptor::FFI::V2.world, nil, nil, sample, sample.length, nil)
|
23
23
|
parser_name == rapper_format.to_s
|
24
24
|
end
|
25
25
|
end # Format
|
data/lib/rdf/raptor.rb
CHANGED
@@ -79,7 +79,7 @@ module RDF
|
|
79
79
|
# @author [Arto Bendiken](http://github.com/bendiken)
|
80
80
|
# @author [John Fieber](http://github.com/jfieber)
|
81
81
|
module Raptor
|
82
|
-
LIBRAPTOR = ENV['RDF_RAPTOR_LIBPATH'] || '
|
82
|
+
LIBRAPTOR = ENV['RDF_RAPTOR_LIBPATH'] || 'libraptor2' unless const_defined?(:LIBRAPTOR)
|
83
83
|
RAPPER = ENV['RDF_RAPTOR_BINPATH'] || 'rapper' unless const_defined?(:RAPPER)
|
84
84
|
|
85
85
|
require 'rdf/raptor/version'
|