Module: JavaProperties::Parsing::Parser

Defined in:
lib/java-properties/parsing/parser.rb

Overview

This module allows parsing of a properties file content string into a JavaProperties::Properties object

Examples:

Parser.parse("item=something \u05d4") => {:item => "something ה"}

Constant Summary collapse

KEY_VALUE_MARKER =

Symbol which separates key from value after normalization

Returns:

  • (String)
'='
KEY_ESCAPE =

Symbol which escapes a KEY_VALUE_MARKER in the key name

Returns:

  • (String)
'\\'
KEY_ONLY_MARKER =

Marker for a line which only consists of an key w/o value

Returns:

  • (Regexp)
/^(\S+)$/

Class Method Summary collapse

Class Method Details

.append_to_properties(properties, key, value) ⇒ Object



56
57
58
59
60
# File 'lib/java-properties/parsing/parser.rb', line 56

def self.append_to_properties(properties, key, value)
  unless key.nil? && value.nil?
    properties[Encoding.decode!(key).to_sym] = Encoding.decode!(value, Encoding::SKIP_SEPARATORS)
  end
end

.extract_key_and_value(line) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/java-properties/parsing/parser.rb', line 39

def self.extract_key_and_value(line)
  # A line must be handled char by char to handled escaped '=' chars in the key name
  key          = StringIO.new
  value        = StringIO.new
  key_complete = false
  last_token   = ''
  line.each_char do |char|
    if !key_complete && char == KEY_VALUE_MARKER && last_token != KEY_ESCAPE
      key_complete = true
    else
      (key_complete ? value : key) << char
    end
    last_token = char
  end
  [key.string, value.string]
end

.parse(text) ⇒ Properties

Parses a string into a JavaProperties::Properties object

Parameters:

  • text (String)

Returns:



27
28
29
30
31
32
33
34
35
# File 'lib/java-properties/parsing/parser.rb', line 27

def self.parse(text)
  properties = Properties.new
  Normalizer.normalize!(text)
  text.each_line do |line|
    key, value = extract_key_and_value(line.chomp)
    append_to_properties(properties, key, value)
  end
  properties
end