Class: RDF::Literal::Base64Binary

Inherits:
RDF::Literal show all
Defined in:
lib/rdf/xsd/binary.rb

Overview

base64Binary represents Base64-encoded arbitrary binary data. The ·value space· of base64Binary is the set of finite-length sequences of binary octets. For base64Binary data the entire binary stream is encoded using the Base64 Alphabet in [RFC 2045].

See Also:

Constant Summary

DATATYPE =
XSD.base64Binary

Instance Method Summary (collapse)

Constructor Details

- (Base64Binary) initialize(value, options = {})

A new instance of Base64Binary

Parameters:

  • value (String, Object)

    If given a string, it will decode it as an object value. Otherwise, it will take the value as the object and encode to retrieve a value

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :lexical (String) — default: nil


43
44
45
46
47
# File 'lib/rdf/xsd/binary.rb', line 43

def initialize(value, options = {})
  super
  @object = value.is_a?(String) ? ::Base64.decode64(value) : value
  canonicalize! unless value.is_a?(String)
end

Instance Method Details

- (RDF::Literal) canonicalize!

Converts this literal into its canonical lexical representation.



54
55
56
57
# File 'lib/rdf/xsd/binary.rb', line 54

def canonicalize!
  @string = ::Base64.encode64(@object)
  self
end

- (Boolean) valid?

Returns true if the value adheres to the defined grammar of the datatype.

Note: depends on implementation of Base64.strict_decode64, which may not be implemented for Ruby 1.8.x.

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
# File 'lib/rdf/xsd/binary.rb', line 67

def valid?
  Base64.respond_to?(:strict_decode64) ?
    Base64.strict_decode64(value.gsub(/\s+/m, '')) :
    Base64.decode64(value.gsub(/\s+/m, ''))
  true
rescue ArgumentError
  STDERR.puts($!.inspect + ": #{value.inspect}")
  false
end