class SysMODB::Extractor

handles the delegation to java, and executes the extraction passing the input file through STDIN, and reading the results through STDOUT.

Constants

BUFFER_SIZE
DEFAULT_PATH
JAR_VERSION

Public Class Methods

new(memory_allocation) click to toggle source
# File lib/sysmodb/extractor.rb, line 14
def initialize(memory_allocation)
  @memory_allocation = memory_allocation
  if is_windows?
    raise Exception.new("Windows is not currently supported")
  end
end

Public Instance Methods

spreadsheet_to_csv(spreadsheet_data,sheet=1,trim=false) click to toggle source
# File lib/sysmodb/extractor.rb, line 25
def spreadsheet_to_csv(spreadsheet_data,sheet=1,trim=false)
  read_with_open4 spreadsheet_data,"csv",sheet,trim
end
spreadsheet_to_xml(spreadsheet_data) click to toggle source
# File lib/sysmodb/extractor.rb, line 21
def spreadsheet_to_xml(spreadsheet_data)
  read_with_open4 spreadsheet_data,"xml"
end

Private Instance Methods

is_windows?() click to toggle source
# File lib/sysmodb/extractor.rb, line 39
def is_windows?
  !(RUBY_PLATFORM =~ /mswin32/ || RUBY_PLATFORM =~ /mingw32/).nil?
end
read_with_open4(spreadsheet_data,format="xml",sheet=nil,trim=false) click to toggle source
# File lib/sysmodb/extractor.rb, line 43
def read_with_open4(spreadsheet_data,format="xml",sheet=nil,trim=false)
  output = ""
  err_message = ""
  command = spreadsheet_extractor_command format,sheet,trim
  status = Open4.popen4(command) do |_pid, stdin, stdout, stderr|
    while ((line = spreadsheet_data.gets(BUFFER_SIZE)) != nil) do
      stdin << line
    end
    stdin.close

    while ((line = stdout.gets(BUFFER_SIZE)) != nil) do
      output << line
    end
    stdout.close

    until ((line=stderr.gets((BUFFER_SIZE))).nil?) do
      err_message << line
    end
    stderr.close
  end

  if status.to_i != 0
    raise SpreadsheetExtractionException.new(err_message)
  end

  output.strip
end
spreadsheet_extractor_command(format="xml",sheet=nil,trim=false) click to toggle source
# File lib/sysmodb/extractor.rb, line 31
def spreadsheet_extractor_command(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
end