Module: Leptris::XML::Serialization

Defined in:
lib/leptris/xml/serialization.rb

Overview

Owns the SerializeOptions lifecycle and the owned-string read for document and subtree serialization/canonicalization. Document and Element expose one-line methods over this module — the options struct, the encoding anchor, the C call, and the read-and-free all live here.

Class Method Summary collapse

Class Method Details

.build_options(indent: 0, no_decl: false, encoding: nil) ⇒ Object

Builds a SerializeOptions struct. Returns [opts, encoding_anchor]; the anchor must stay referenced while opts is in an FFI call.



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/leptris/xml/serialization.rb', line 30

def self.build_options(indent: 0, no_decl: false, encoding: nil)
  opts = Leptris::XML::FFI::SerializeOptions.new
  opts[:indent] = indent.to_i
  opts[:xml_declaration] = no_decl ? 0 : 1
  encoding_anchor = nil
  if encoding
    encoding_anchor = ::FFI::MemoryPointer.from_string(encoding.to_s)
    opts[:encoding] = encoding_anchor
  end
  [opts, encoding_anchor]
end

.canonicalize(ffi_function, c_ptr, version:, mode:, inclusive_namespaces: nil, with_comments: false) ⇒ Object

ffi_function is a bound FFI function taking (c_ptr, version, mode, ns_ptr, flags): leptris_c14n_canonicalize_ex or leptris_c14n_canonicalize_subtree_ex.



21
22
23
24
25
26
# File 'lib/leptris/xml/serialization.rb', line 21

def self.canonicalize(ffi_function, c_ptr, version:, mode:,
                      inclusive_namespaces: nil, with_comments: false)
  ns_ptr, _ns_anchor = Leptris::XML::CStringArray.to_c(inclusive_namespaces)
  str_ptr = ffi_function.call(c_ptr, version, mode, ns_ptr, with_comments ? 1 : 0)
  Leptris::XML::FFI.read_owned_string(str_ptr)
end

.to_xml(ffi_function, c_ptr, indent: 0, no_decl: false, encoding: nil) ⇒ Object

ffi_function is a bound FFI function taking (c_ptr, opts_ptr): leptris_document_serialize or leptris_element_serialize.



11
12
13
14
15
16
# File 'lib/leptris/xml/serialization.rb', line 11

def self.to_xml(ffi_function, c_ptr, indent: 0, no_decl: false, encoding: nil)
  opts, _encoding_anchor = build_options(
    indent: indent, no_decl: no_decl, encoding: encoding)
  str_ptr = ffi_function.call(c_ptr, opts.pointer)
  Leptris::XML::FFI.read_owned_string(str_ptr)
end