Sha256: 5a9a5d04bcd6ead9c10f3a340984c253fbd0d596e1da0607eb7de906dc194ac0

Contents?: true

Size: 1.46 KB

Versions: 10

Compression:

Stored size: 1.46 KB

Contents

require 'nokogiri'

module BerkeleyLibrary
  module Util
    module ODS
      module XML
        class DocumentNode

          ENCODING = 'UTF-8'.freeze

          attr_reader :path

          # Initializes a new DocumentNode
          # @param path [String] the path to this document in the container
          def initialize(path)
            @path = path
          end

          def to_xml(out = nil, compact: true)
            return write_xml_to_string(compact: compact) unless out
            return write_xml_to_stream(out, compact: compact) if out.respond_to?(:write)

            write_xml_to_file(out, compact: compact)
          end

          def doc
            @doc ||= Nokogiri::XML::Document.new.tap do |doc|
              doc.encoding = ENCODING
            end
          end

          private

          def write_xml_to_stream(out, compact:)
            doc.root ||= root_element_node.element
            if compact
              doc.write_to(out, encoding: ENCODING, save_with: 0)
            else
              doc.write_to(out, encoding: ENCODING)
            end
          end

          def write_xml_to_string(compact:)
            StringIO.new.tap do |out|
              out.binmode
              write_xml_to_stream(out, compact: compact)
            end.string
          end

          def write_xml_to_file(path, compact:)
            File.open(path, 'wb') { |f| write_xml_to_stream(f, compact: compact) }
          end
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
berkeley_library-tind-0.7.2 lib/berkeley_library/util/ods/xml/document_node.rb
berkeley_library-tind-0.7.1 lib/berkeley_library/util/ods/xml/document_node.rb
berkeley_library-tind-0.7.0 lib/berkeley_library/util/ods/xml/document_node.rb
berkeley_library-tind-0.6.0 lib/berkeley_library/util/ods/xml/document_node.rb
berkeley_library-tind-0.5.1 lib/berkeley_library/util/ods/xml/document_node.rb
berkeley_library-tind-0.5.0 lib/berkeley_library/util/ods/xml/document_node.rb
berkeley_library-tind-0.4.3 lib/berkeley_library/util/ods/xml/document_node.rb
berkeley_library-tind-0.4.2 lib/berkeley_library/util/ods/xml/document_node.rb
berkeley_library-tind-0.4.1 lib/berkeley_library/util/ods/xml/document_node.rb
berkeley_library-tind-0.4.0 lib/berkeley_library/util/ods/xml/document_node.rb