Class: ConfigManager::JavaPropertiesLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/configmanager/loaders.rb

Overview

Loads properties from a Java style property file. The syntax is slightly different from regular property files. Important differences are :

  1. Escape characters are not allowed.

  2. Properties can be assigned a type (int, float, regex, string, bool).

  3. Properties can be collected as arrays.

A property would look like : <key><:optional_type> = <value> For arrays, add a [] to the property : <key><:optional_type>[] = <value separated by ‘,’>

Defined Under Namespace

Classes: SyntaxError

Constant Summary collapse

LINE_REGEX =
/^([\w\.]+)(:(\w+))?(\[\])?\s*([\s=])\s*(.*)$/
COMMAND_REGEX =
/^@(\w+)\s+(.*)$/
TRUE_VALUES =
%w(true yes on)
FALSE_VALUES =
%w(false no off)
TYPE_CONVERTERS =
{"bool" => :type_bool, "int" => :type_int, "float" => :type_float, "regex" => :type_regex,
"string" => :type_string, nil => :type_string}
COMMANDS =
{"import" => :command_import}

Class Method Summary collapse

Class Method Details

.command_import(file_to_import, file_path, line_number, configuration) ⇒ NilClass

Executes the import command.

Parameters:

  • file_to_import (String)

    the file that needs to be imported.

  • file_path (String)

    the file that contained the command.

  • line_number (Integer)

    the line number where the command existed.

  • configuration (ConfigManager::Configuration)

    the configuration to load the properties into.

Returns:

  • (NilClass)


63
64
65
66
67
68
69
# File 'lib/configmanager/loaders.rb', line 63

def self.command_import(file_to_import, file_path, line_number, configuration)
  file_to_import = file_to_import[1..-2]
  file_dir = File.dirname(file_path)
  file_to_import = File.expand_path(file_to_import, file_dir)
  load_properties(file_to_import, configuration)
  nil
end

.load_properties(file_path, configuration) ⇒ NilClass

Loads properties from the specified Java style property file.

Parameters:

  • file_path (String)

    the file to load properties from.

  • configuration (ConfigManager::Configuration)

    the configuration to load the properties into.

Returns:

  • (NilClass)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/configmanager/loaders.rb', line 77

def self.load_properties(file_path, configuration)
  file_path = File.expand_path(file_path)
  lines = File.readlines(file_path)
  lines.each_with_index do |line, line_number|
    line = line.chomp.strip
    # Empty lines, ignore.
    # Comment lines, ignore.
    # Command - Call the command's method.
    next if line.empty?
    next if line.start_with?("#")
    match = line.match(COMMAND_REGEX)
    if match
      command_name = match[1]
      command_arg = match[2]
      raise SyntaxError.new("Unrecognized command '#{command_name}' #(#{file_path}:#{line_number})") unless COMMANDS.has_key?(command_name)
      send(COMMANDS[command_name], command_arg, file_path, line_number, configuration)
      next
    end
    # Other lines, parse as a property.
    # Raises an exception if the line was not valid.
    match = line.match(LINE_REGEX)
    raise SyntaxError.new("Syntax error '#{line}' (#{file_path}:#{line_number})") if match.nil?
    key, type, array, value = match[1], match[3], match[4], match[6]
    # Invalid types raise an exception.
    raise SyntaxError.new("Unrecognized type '#{type}' (#{file_path}:#{line_number})") unless TYPE_CONVERTERS.has_key?(type)
    type_converter = TYPE_CONVERTERS[type]

    # Split into array if required and apply the type converter.
    if array
      value = value.split(",").map { |item| send(type_converter, item.strip, file_path, line_number) }
    else
      value = send(type_converter, value, file_path, line_number)
    end

    # Finally, add to configuration.
    configuration.add_property(key, value)
  end
  nil
end

.type_bool(value, file_path, line_number) ⇒ TrueClass, FalseClass

Converts the specified string value into a boolean (TrueClass, FalseClass).

Parameters:

  • value (String)

    the string value to convert.

  • file_path (String)

    the file that contains the property.

  • line_number (Integer)

    the line that contained this property.

Returns:

  • (TrueClass, FalseClass)

Raises:



124
125
126
127
128
# File 'lib/configmanager/loaders.rb', line 124

def self.type_bool(value, file_path, line_number)
  return true if TRUE_VALUES.include?(value)
  return false if FALSE_VALUES.include?(value)
  raise SyntaxError.new("Invalid boolean '#{value}' (#{file_path}:#{line_number})")
end

.type_float(value, file_path, line_number) ⇒ Float

Converts the specified string value into float. Invalid values will raise an exception.

Parameters:

  • value (String)

    the string value to convert.

  • file_path (String)

    the file that contains the property.

  • line_number (Integer)

    the line that contained this property.

Returns:

  • (Float)


138
139
140
141
142
# File 'lib/configmanager/loaders.rb', line 138

def self.type_float(value, file_path, line_number)
  Float(value)
rescue
  raise SyntaxError.new("Invalid float value '#{value}' (#{file_path}:#{line_number})")
end

.type_int(value, file_path, line_number) ⇒ Integer

Converts the specified string value into a integer. Invalid values will raise an exception

Parameters:

  • value (String)

    the string value to convert.

  • file_path (String)

    the file that contains the property.

  • line_number (Integer)

    the line that contained this property.

Returns:

  • (Integer)


152
153
154
155
156
# File 'lib/configmanager/loaders.rb', line 152

def self.type_int(value, file_path, line_number)
  Integer(value)
rescue
  raise SyntaxError.new("Invalid integer value '#{value}' (#{file_path}:#{line_number})")
end

.type_regex(value, file_path, line_number) ⇒ Regexp

Converts the specified string value into a regexp.

Parameters:

  • value (String)

    the string value to convert.

  • file_path (String)

    the file that contains the property.

  • line_number (Integer)

    the line that contained this property.

Returns:

  • (Regexp)


165
166
167
# File 'lib/configmanager/loaders.rb', line 165

def self.type_regex(value, file_path, line_number)
  Regexp.new(value)
end

.type_string(value, file_path, line_number) ⇒ String

Returns the value as is.

Parameters:

  • value (String)

    the string value to convert.

  • file_path (String)

    the file that contains the property.

  • line_number (Integer)

    the line that contained this property.

Returns:

  • (String)


176
177
178
# File 'lib/configmanager/loaders.rb', line 176

def self.type_string(value, file_path, line_number)
  value
end