lib/sysmodb/extractor.rb in simple-spreadsheet-extractor-0.18.0 vs lib/sysmodb/extractor.rb in simple-spreadsheet-extractor-0.18.1

- old
+ new

@@ -4,11 +4,11 @@ # Exception that is thrown when a problem occurs during the extraction class SpreadsheetExtractionException < Exception; end # handles the delegation to java class Extractor - JAR_VERSION = '0.18.0'.freeze + JAR_VERSION = '0.18.1'.freeze DEFAULT_PATH = File.dirname(__FILE__) + "/../../jars/simple-spreadsheet-extractor-#{JAR_VERSION}.jar" def initialize(memory_allocation) @memory_allocation = memory_allocation raise Exception, 'Windows is not currently supported' if is_windows? @@ -36,28 +36,32 @@ elsif spreadsheet_data.is_a?(String) execute_command_line spreadsheet_data, format, sheet, trim end end - def spreadsheet_extractor_command(filepath, format = 'xml', sheet = nil, trim = false) - command = "java -Xmx#{@memory_allocation} -jar #{(defined? SPREADSHEET_EXTRACTOR_JAR_PATH) ? SPREADSHEET_EXTRACTOR_JAR_PATH : DEFAULT_PATH}" - command += " -o #{format}" - command += " -s #{sheet}" if sheet - command += ' -t' if trim - command += " < #{filepath}" - command + def spreadsheet_extractor_command_line(filepath, format = 'xml', sheet = false, trim = false) + + args = "-Xmx#{@memory_allocation} -jar #{(defined? SPREADSHEET_EXTRACTOR_JAR_PATH) ? SPREADSHEET_EXTRACTOR_JAR_PATH : DEFAULT_PATH}" + args += " -o :format" + args += " -s :sheet" if sheet + args += ' -t' if trim + args += " < :filepath" + args += " 2>:error_log_path" + Terrapin::CommandLine.new("java", args) end def is_windows? !(RUBY_PLATFORM =~ /mswin32/ || RUBY_PLATFORM =~ /mingw32/).nil? end def execute_command_line(filepath, format = 'xml', sheet = nil, trim = false) - command = spreadsheet_extractor_command filepath, format, sheet, trim - begin - Terrapin::CommandLine.new(command).run.strip - rescue Terrapin::ExitStatusError, Terrapin::CommandNotFoundError => e - raise SpreadsheetExtractionException, e.message + command_line = spreadsheet_extractor_command_line filepath, format, !!sheet, trim + Tempfile.create('spreadsheet-extraction-error-log') do |f| + begin + command_line.run(format: format, sheet: sheet, filepath: filepath, error_log_path: f.path).strip + rescue Terrapin::ExitStatusError, Terrapin::CommandNotFoundError + raise SpreadsheetExtractionException, f.read + end end end end end