Class: RDF::Raptor::FFI::V2::Serializer

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

Overview

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

Constant Summary

Constants included from RDF::Raptor::FFI

ENGINE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RDF::Raptor::FFI

#version

Constructor Details

#initialize(ptr) ⇒ Serializer #initialize(name) ⇒ Serializer

Returns a new instance of Serializer.

Overloads:

  • #initialize(ptr) ⇒ Serializer

    Parameters:

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

    Parameters:

    • name (Symbol, String)

Raises:

  • (ArgumentError)


30
31
32
33
34
35
36
37
38
39
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 30

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

Instance Attribute Details

#iostreamObject (readonly)

Returns the value of attribute iostream.



10
11
12
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 10

def iostream
  @iostream
end

Class Method Details

.release(ptr)

This method returns an undefined value.

Releases libraptor memory associated with this structure.

Parameters:

  • ptr (FFI::Pointer)


46
47
48
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 46

def self.release(ptr)
  V2.raptor_free_serializer(ptr)
end

Instance Method Details

#error_handler=(handler)

This method returns an undefined value.

Parameters:

  • handler (Proc)


53
54
55
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 53

def error_handler=(handler)
  V2.raptor_serializer_set_error_handler(self, self, handler) if V2.respond_to?(:raptor_serializer_set_error_handler)
end

#finish

This method returns an undefined value.



116
117
118
119
120
121
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 116

def finish
  if V2.raptor_serializer_serialize_end(self).nonzero?
    raise RDF::WriterError, "raptor_serialize_end() failed"
  end
  @iostream = @base_uri = nil # allows GC
end

#prefix(prefixes)

This method returns an undefined value.

Parameters:

  • prefixes (Hash{namespace => uri})


106
107
108
109
110
111
112
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 106

def prefix(prefixes)
  prefixes.each do |name, uri|
    if V2.raptor_serializer_set_namespace(self, V2::URI.new(uri.to_s), name.to_s).nonzero?
      raise RDF::WriterError, "raptor_serializer_set_namespace() failed"
    end
  end
end

#serialize_raw_statement(statement)

This method returns an undefined value.

Parameters:



144
145
146
147
148
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 144

def serialize_raw_statement(statement)
  if V2.raptor_serializer_serialize_statement(self, statement).nonzero?
    raise RDF::WriterError, "raptor_serialize_statement() failed"
  end
end

#serialize_triple(subject, predicate, object)

This method returns an undefined value.

Parameters:

  • subject (RDF::Resource)
  • predicate (RDF::URI)
  • object (RDF::Term)


128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 128

def serialize_triple(subject, predicate, object)
  raptor_statement = V2::Statement.new
  raptor_statement.subject   = subject
  raptor_statement.predicate = predicate
  raptor_statement.object    = object
  begin
    serialize_raw_statement(raptor_statement)
  ensure
    raptor_statement.release
    raptor_statement = nil
  end
end

#start_to(output, **options)

This method returns an undefined value.

Parameters:

  • output (Object)

    where output should be written to

  • options (Hash{Symbol => Object})

    any additional options for serializing

Options Hash (**options):

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

    the base URI to use when resolving relative URIs



72
73
74
75
76
77
78
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 72

def start_to(output, **options)
  if output.respond_to?(:write)
    start_to_stream(output, **options)
   else
    raise ArgumentError, "don't know how to serialize to #{output.inspect}"
  end
end

#start_to_iostream(iostream, base_uri: nil, **options)

This method returns an undefined value.

Parameters:

  • iostream (V2::IOStream)
  • options (Hash{Symbol => Object})

    any additional options for serializing (see #start_to)



95
96
97
98
99
100
101
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 95

def start_to_iostream(iostream, base_uri: nil, **options)
  @iostream = iostream # prevents premature GC
  @base_uri = base_uri.to_s.empty? ? nil : V2::URI.new(base_uri.to_s)
  if V2.raptor_serializer_start_to_iostream(self, @base_uri, @iostream).nonzero?
    raise RDF::WriterError, "raptor_serialize_start_to_iostream() failed"
  end
end

#start_to_stream(stream, **options)

This method returns an undefined value.

Parameters:

  • stream (IO, StringIO)
  • options (Hash{Symbol => Object})

    any additional options for serializing (see #start_to)



85
86
87
88
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 85

def start_to_stream(stream, **options)
  iostream = V2::IOStream.new(V2::IOStreamHandler.new(stream), free_iostream: self[:free_iostream_on_end])
  start_to_iostream(iostream, **options)
end

#warning_handler=(handler)

This method returns an undefined value.

Parameters:

  • handler (Proc)


60
61
62
# File 'lib/rdf/raptor/ffi/v2/serializer.rb', line 60

def warning_handler=(handler)
  V2.raptor_serializer_set_warning_handler(self, self, handler) if V2.respond_to?(:raptor_serializer_set_warning_handler)
end