Skip to content

Commit

Permalink
Lazily load the OpenOffice doc file. As with Excelx, this means the O…
Browse files Browse the repository at this point in the history
…S or user will be responsible for cleaning up the temporary files.

Inline OpenOffice#extract_content.
Drop Base#load_xml as it is no longer used.
  • Loading branch information
Empact committed Nov 24, 2014
1 parent fc7d2f2 commit a74157a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Accept the tmpdir_root option in Roo::Excelx
* Change the tmpdir prefix from oo_ to roo_
* In Excelx, load styles, shared strings and the workbook lazily. Leave the tmpdir open so that reading may take place after initialize. The OS will be responsible for cleaning it up.
* In OpenOffice / LibreOffice, load the content xml lazily. Leave the tmpdir open so that reading may take place after initialize. The OS will be responsible for cleaning it up.
* Lazily initialize @default_sheet, to avoid reading the sheets earlier than necessary. Use the #default_sheet accessor instead.

* bugfixes
Expand Down
32 changes: 15 additions & 17 deletions lib/roo/open_office.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@

class Roo::OpenOffice < Roo::Base
class << self
def extract_content(tmpdir, filename)
Roo::ZipFile.open(filename) do |zip|
process_zipfile(tmpdir, zip)
end
end

def process_zipfile(tmpdir, zip, path='')
if zip.file.file? path
if path == "content.xml"
Expand All @@ -33,11 +27,11 @@ def initialize(filename, options={})
file_warning = options[:file_warning] || :error

file_type_check(filename,'.ods','an Roo::OpenOffice', file_warning, packed)
make_tmpdir(nil, options[:tmpdir_root]) do |tmpdir|
@filename = local_filename(filename, tmpdir, packed)
#TODO: @cells_read[:default] = false
self.class.extract_content(tmpdir, @filename)
@doc = ::Roo::Utils.load_xml(File.join(tmpdir, "roo_content.xml"))
@tmpdir = make_tmpdir(filename.split('/').last, options[:tmpdir_root])
@filename = local_filename(filename, @tmpdir, packed)
#TODO: @cells_read[:default] = false
Roo::ZipFile.open(@filename) do |zip|
self.class.process_zipfile(@tmpdir, zip)
end
super(filename, options)
@formula = Hash.new
Expand Down Expand Up @@ -145,7 +139,7 @@ def celltype(row,col,sheet=nil)
end

def sheets
@doc.xpath("//*[local-name()='table']").map do |sheet|
doc.xpath("//*[local-name()='table']").map do |sheet|
sheet.attributes["name"].value
end
end
Expand Down Expand Up @@ -220,9 +214,13 @@ def comments(sheet=nil)

private

def doc
@doc ||= ::Roo::Utils.load_xml(File.join(@tmpdir, "roo_content.xml"))
end

# read the version of the OO-Version
def oo_version
@doc.xpath("//*[local-name()='document-content']").each do |office|
doc.xpath("//*[local-name()='document-content']").each do |office|
@officeversion = attr(office,'version')
end
end
Expand Down Expand Up @@ -280,7 +278,7 @@ def read_cells(sheet=nil)
return if @cells_read[sheet]

sheet_found = false
@doc.xpath("//*[local-name()='table']").each do |ws|
doc.xpath("//*[local-name()='table']").each do |ws|
if sheet == attr(ws,'name')
sheet_found = true
col = 1
Expand Down Expand Up @@ -375,7 +373,7 @@ def read_cells(sheet=nil)
end
end
end
@doc.xpath("//*[local-name()='automatic-styles']").each do |style|
doc.xpath("//*[local-name()='automatic-styles']").each do |style|
read_styles(style)
end
if !sheet_found
Expand All @@ -392,7 +390,7 @@ def read_comments(sheet=nil)
end

def read_labels
@label ||= Hash[@doc.xpath("//table:named-range").map do |ne|
@label ||= Hash[doc.xpath("//table:named-range").map do |ne|
#-
# $Sheet1.$C$5
#+
Expand Down Expand Up @@ -459,4 +457,4 @@ def attr(node, attr_name)
node.attributes[attr_name].value
end
end
end # class
end

0 comments on commit a74157a

Please sign in to comment.