class OoxmlParser::OOXMLDocumentObject

Basic class for any OOXML Document Object

Attributes

path_to_folder[RW]

@return [String] path to root folder

root_subfolder[RW]

@return [String] path to root subfolder

theme[RW]

@return [PresentationTheme] list of themes

xmls_stack[RW]

@return [Array<String>] stack of xmls

parent[RW]

@return [OOXMLDocumentObject] object which hold current object

Public Class Methods

add_to_xmls_stack(path) click to toggle source

Add file to parsing stack @param path [String] path of file to add to stack @return [void]

# File lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb, line 122
def add_to_xmls_stack(path)
  OOXMLDocumentObject.xmls_stack << if path.include?('..')
                                      "#{File.dirname(OOXMLDocumentObject.xmls_stack.last)}/#{path}"
                                    elsif path.start_with?(OOXMLDocumentObject.root_subfolder)
                                      path
                                    else
                                      OOXMLDocumentObject.root_subfolder + path
                                    end
end
copy_file_and_rename_to_zip(path) click to toggle source

Copy this file and rename to zip @param path [String] path to file @return [String] path to result zip

# File lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb, line 80
def copy_file_and_rename_to_zip(path)
  file_name = File.basename(path)
  tmp_folder = Dir.mktmpdir('ruby-ooxml-parser')
  file_path = "#{tmp_folder}/#{file_name}"
  FileUtils.rm_rf(tmp_folder) if File.directory?(tmp_folder)
  FileUtils.mkdir_p(tmp_folder)
  raise "Cannot find file by path #{path}" unless File.exist?(path)

  FileUtils.cp path, tmp_folder
  file_path
end
current_xml() click to toggle source

@return [String] path to current xml file

# File lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb, line 115
def current_xml
  OOXMLDocumentObject.path_to_folder + OOXMLDocumentObject.xmls_stack.last
end
dir() click to toggle source

@return [String] dir to base of file

# File lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb, line 110
def dir
  "#{OOXMLDocumentObject.path_to_folder}#{File.dirname(OOXMLDocumentObject.xmls_stack.last)}/"
end
encrypted_file?(path_to_file, ignore_system: false) click to toggle source

@param path_to_file [String] file @param ignore_system [True, False] should host system be ignored, since

this method is OS-dependent

@return [True, False] Check if file is protected by password on open

# File lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb, line 59
def encrypted_file?(path_to_file, ignore_system: false)
  if Gem.win_platform? || ignore_system
    warn 'FileMagic and checking file for encryption is not supported on Windows'
    return false
  end
  file_result = FileMagic.new(:mime).file(path_to_file)
  # Support of Encrtypted status in `file` util was introduced in file v5.20
  # but LTS version of ubuntu before 16.04 uses older `file` and it return `Composite Document`
  # https://github.com/file/file/blob/master/ChangeLog#L217
  if file_result.include?('encrypted') ||
     file_result.include?('Composite Document File V2 Document, No summary info') ||
     file_result.include?('application/CDFV2-corrupt')
    warn("File #{path_to_file} is encrypted. Can't parse it")
    return true
  end
  false
end
new(parent: nil) click to toggle source
# File lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb, line 18
def initialize(parent: nil)
  @parent = parent
end
unzip_file(path_to_file, destination) click to toggle source

Unzip specified file @param path_to_file [String] path to zip file @param destination [String] folder to extract @return [void]

# File lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb, line 96
def unzip_file(path_to_file, destination)
  Zip.warn_invalid_date = false
  Zip::File.open(path_to_file) do |zip_file|
    raise LoadError, "There is no files in zip #{path_to_file}" if zip_file.entries.empty?

    zip_file.each do |file|
      file_path = File.join(destination, file.name)
      FileUtils.mkdir_p(File.dirname(file_path))
      zip_file.extract(file, file_path) unless File.exist?(file_path)
    end
  end
end

Public Instance Methods

==(other) click to toggle source

Compare this object to other @param other [Object] any other object @return [True, False] result of comparision

# File lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb, line 25
def ==(other)
  return false if self.class != other.class

  instance_variables.each do |current_attribute|
    next if current_attribute == :@parent
    return false unless instance_variable_get(current_attribute) == other.instance_variable_get(current_attribute)
  end
  true
end
parse_xml(xml_path) click to toggle source

@return [Nokogiri::XML::Document] result of parsing xml via nokogiri

# File lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb, line 41
def parse_xml(xml_path)
  Nokogiri::XML(File.open(xml_path), &:strict)
end
with_data?() click to toggle source

@return [True, false] if structure contain any user data

# File lib/ooxml_parser/common_parser/common_data/ooxml_document_object.rb, line 36
def with_data?
  true
end