Class: MPXJ::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/mpxj/reader.rb

Overview

Used to read a project plan from a file

Constant Summary collapse

@@max_memory_size =
nil

Class Method Summary collapse

Class Method Details

.jvm_argsObject



47
48
49
50
51
# File 'lib/mpxj/reader.rb', line 47

def self.jvm_args
  args = []
  args << "-Xmx#{@@max_memory_size}" if @@max_memory_size.present? 
  args.join(' ')
end

.max_memory_size=(value) ⇒ Object

Allows the caller to set the maximum memory size used by the JVM when processing a schedule. This is useful when handling large schedules which cause out of memory failures if the JVM’s default maximum memory size is used. The value is either a plain integer number of bytes, or an integer followed by K, M, or G, e.g. ‘MPXJ::Reader.max_memory_size=“500M”`

Parameters:

  • value

    new maximum memory size



42
43
44
# File 'lib/mpxj/reader.rb', line 42

def self.max_memory_size=(value)
  @@max_memory_size = value
end

.path_separatorObject



54
55
56
57
58
59
60
# File 'lib/mpxj/reader.rb', line 54

def self.path_separator
  if windows?
    ";"
  else
    ":"
  end
end

.read(file_name, zone = nil) ⇒ Project

Reads a project plan from a file, and returns a Project instance which provides access to the structure and attributes of the project data. Note that an optional timezone can be supplied to ensue that all date-time values returned are in the specified timezone.

Parameters:

  • file_name (String)

    the name of the file to read

  • zone (ActiveSupport::TimeZone) (defaults to: nil)

    an optional timezone

Returns:

  • (Project)

    new Project instance



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mpxj/reader.rb', line 17

def self.read(file_name, zone = nil)
  project = nil
  json_file = Tempfile.new([File.basename(file_name, ".*"), '.json'])
  tz = zone || Time.zone || ActiveSupport::TimeZone["UTC"]

  begin
    classpath = Dir["#{File.dirname(__FILE__)}/*.jar"].join(path_separator)
    java_output = `java -cp \"#{classpath}\" #{jvm_args} net.sf.mpxj.sample.MpxjConvert \"#{file_name}\" \"#{json_file.path}\"`
    if $?.exitstatus != 0
      report_error(java_output)
    end
    project = Project.new(json_file, tz)
  ensure
    json_file.close
    json_file.unlink
  end
  project
end

.report_error(java_output) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mpxj/reader.rb', line 68

def self.report_error(java_output)
  if java_output.include?('Conversion Error: ')
    message = java_output.split('Conversion Error: ')[1]
    if message.include?('Unsupported file type')
      raise MPXJ::ArgumentError, message
    elsif message.include?('password protected')
      raise MPXJ::PasswordProtected, message
    else
      raise MPXJ::RuntimeError, message
    end
  else        
    raise MPXJ::UnknownError, "Failed to read file: #{java_output}"
  end
end

.windows?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/mpxj/reader.rb', line 63

def self.windows?
  (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
end