class SpreadBase::Codecs::OpenDocument12

Interface for OpenDocument 1. encoding/decoding.

Public Instance Methods

decode_archive(zip_buffer, options={}) click to toggle source

Decode an OpenDocument archive.

params:

zip_buffer

archive as binary string. if it's been read from the disk, don't forget to read in binary fmode.

options:

floats_as_bigdecimal

(false) decode floats as BigDecimal instead of Float

returns the SpreadBase::Document instance.

# File lib/spreadbase/codecs/open_document_12.rb, line 61
def decode_archive(zip_buffer, options={})
  io = StringIO.new(zip_buffer)
  content_xml_data = Zip::File.new(io, false, true).read('content.xml')

  decode_content_xml(content_xml_data, options)
end
decode_content_xml(content_xml_data, options={}) click to toggle source

Utility method; decode the content.xml belonging to an OpenDocument archive.

options:

floats_as_bigdecimal

(false) decode floats as BigDecimal instead of Float

returns the SpreadBase::Document instance.

# File lib/spreadbase/codecs/open_document_12.rb, line 101
def decode_content_xml(content_xml_data, options={})
  root_node = REXML::Document.new(content_xml_data)

  decode_document_node(root_node, options)
end
encode_to_archive(el_document, options={}) click to toggle source

Encode a Document to an OpenDocument archive.

The generated archive contains the strictly necessary data required to have a consistent archive.

params:

el_document

SpreadBase::Document instance

options:

prettify

(false )prettifies the content.xml to be human readable.

returns the archive as binary string.

# File lib/spreadbase/codecs/open_document_12.rb, line 36
def encode_to_archive(el_document, options={})
  document_buffer = encode_to_content_xml(el_document, options)
  zip_buffer      = ''

  Zip::File.open_buffer(zip_buffer) do | zip_file |
    zip_file.get_output_stream('META-INF/manifest.xml') { |f| f << MANIFEST_XML }
    zip_file.get_output_stream('content.xml') { |f| f << document_buffer }
  end

  zip_buffer
end
encode_to_content_xml(el_document, options={}) click to toggle source

Utility method; encodes the Document to the content.xml format.

params:

el_document

SpreadBase::Document instance

options:

prettify

(false ) prettifies the content.xml to be human readable.

returns content.xml as string.

# File lib/spreadbase/codecs/open_document_12.rb, line 82
def encode_to_content_xml(el_document, options={})
  prettify = options[:prettify]

  document_xml_root = encode_to_document_node(el_document)
  document_buffer   = prettify ? pretty_xml(document_xml_root) : document_xml_root.to_s

  document_buffer
end

Private Instance Methods

pretty_xml(document) click to toggle source
# File lib/spreadbase/codecs/open_document_12.rb, line 109
def pretty_xml(document)
  buffer = ""

  xml_formatter = REXML::Formatters::Pretty.new
  xml_formatter.compact = true
  xml_formatter.write(document, buffer)

  buffer
end