Class: RDF::Raptor::FFI::Writer

Inherits:
Writer
  • Object
show all
Includes:
Util::Logger
Defined in:
lib/rdf/raptor/ffi.rb

Overview

FFI writer implementation.

Constant Summary collapse

ERROR_HANDLER =
Proc.new do |user_data, locator, message|
  log_error(message)
end
WARNING_HANDLER =
Proc.new do |user_data, locator, message|
  log_warn(message)
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout, **options) {|writer| ... } ⇒ Writer

Initializes the FFI writer instance.

Parameters:

  • output (IO, File) (defaults to: $stdout)
  • options (Hash{Symbol => Object})

    any additional options (see RDF::Writer#initialize)

Yields:

  • (writer)

    self

Yield Parameters:

  • writer (RDF::Writer)

Yield Returns:

  • (void)

    ignored



159
160
161
162
163
164
165
166
# File 'lib/rdf/raptor/ffi.rb', line 159

def initialize(output = $stdout, **options, &block)
  @format = self.class.format.rapper_format
  @serializer = V2::Serializer.new(@format)
  @serializer.error_handler   = ERROR_HANDLER
  @serializer.warning_handler = WARNING_HANDLER
  @serializer.start_to(output, **options)
  super
end

Instance Attribute Details

#serializerV2::Serializer (readonly)

The Raptor serializer instance.

Returns:



180
181
182
# File 'lib/rdf/raptor/ffi.rb', line 180

def serializer
  @serializer
end

Class Method Details

.serialize(value) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/rdf/raptor/ffi.rb', line 182

def self.serialize(value)
  output = StringIO.new
  writer = new(output)
  case value
  when nil then nil
  when FalseClass then value.to_s
  when RDF::Statement
    writer.write_triple(statement.subject, statement.predicate, statement.object)
  when RDF::Term
    writer.write_term(value)
  else
    raise ArgumentError, "expected an RDF::Statement or RDF::Term, but got #{value.inspect}"
  end

  output.to_s
end

Instance Method Details

#write_epilogue

This method returns an undefined value.

See Also:

  • Writer#write_epilogue


224
225
226
227
# File 'lib/rdf/raptor/ffi.rb', line 224

def write_epilogue
  @serializer.finish
  super
end

#write_prologue

This method returns an undefined value.

See Also:

  • Writer#write_prologue


216
217
218
219
# File 'lib/rdf/raptor/ffi.rb', line 216

def write_prologue
  @serializer.prefix(self.prefixes)
  super
end

#write_term(value) ⇒ Object

Raises:

  • (NotImplementedError)


209
210
211
# File 'lib/rdf/raptor/ffi.rb', line 209

def write_term(value)
  raise NotImplementedError
end

#write_triple(subject, predicate, object)

This method returns an undefined value.

Parameters:

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

See Also:

  • Writer#write_triple


205
206
207
# File 'lib/rdf/raptor/ffi.rb', line 205

def write_triple(subject, predicate, object)
  @serializer.serialize_triple(subject, predicate, object)
end