Sha256: 7e51f8c9d7cb237cae988d165abe640ee6a426eb8081f27475b4d2ac43ed91e2

Contents?: true

Size: 1.78 KB

Versions: 8

Compression:

Stored size: 1.78 KB

Contents

module JavaProperties
  module Parsing
    # Module to normalize the content of a properties file
    # 
    # @example Normalizes:
    #   # Comment 1
    #   ! Comment 2
    #   item0
    #   item1 = item1 
    #   item2 : item2 
    #   item3=line 1 \
    #         line 2
    #
    # @example Into:
    #
    #   item0
    #   item1=item1 
    #   item2=item2 
    #   item3=line 1 line 2
    #
    module Normalizer

      # Describes a single normalization rule by replacing content
      class Rule
        # Initializes a new rules base on a matching regexp
        # and a replacement as substitution
        # @param matcher [Regexp]
        # @param replacement [String]
        def initialize(matcher, replacement = '')
          @matcher     = matcher
          @replacement = replacement
        end

        # Apply the substitution to the text in place
        # @param text [string]
        # @return [String]
        def apply!(text)
          text.gsub!(@matcher, @replacement)
        end
      end

      # Collection of ordered rules
      RULES = []

      # Removes comments
      RULES << Rule.new(/^\s*[!\#].*$/)
      
      # Removes leading whitepsace
      RULES << Rule.new(/^\s+/)
      
      # Removes tailing whitepsace
      RULES << Rule.new(/\s+$/)

      # Strings ending with \ are concatenated
      RULES << Rule.new(/\\\s*$[\n\r]+/)

      # Remove whitespace around delimiters and replace with =
      RULES << Rule.new(/^((?:(?:\\[=: \t])|[^=: \t])+)[ \t]*[=: \t][ \t]*/, '\1=')

      RULES.freeze

      # Normalizes the content of a properties file content by applying the RULES
      # @param text [String]
      # @return [String] 
      def self.normalize!(text)
        RULES.each do |rule|
          rule.apply!(text)
        end
        text
      end

    end
  end
end

Version data entries

8 entries across 8 versions & 2 rubygems

Version Path
java-properties-0.3.0 lib/java-properties/parsing/normalizer.rb
java-properties-0.2.1 lib/java-properties/parsing/normalizer.rb
java-properties-0.2.0 lib/java-properties/parsing/normalizer.rb
java-properties-0.1.1 lib/java-properties/parsing/normalizer.rb
java-properties-0.1.0 lib/java-properties/parsing/normalizer.rb
ruby-properties-file-0.0.2 lib/java-properties/parsing/normalizer.rb
java-properties-0.0.2 lib/java-properties/parsing/normalizer.rb
java-properties-0.0.1 lib/java-properties/parsing/normalizer.rb