package com.github.nodevops.confd.maven.plugin.utils;

import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by pseillier on 28/12/2015.
 */
public class DictionaryUtil {
    public static Map<String, String> readDictionaryAsEnvVariables(File dictionary, String encoding) throws DictionaryException {

        try {
            List<String> lines = FileUtils.loadFile(dictionary);
            // the file must contain at least one key/value pair
            if (lines.isEmpty()) {
                throw new DictionaryException("dictionary " + dictionary + " does not contain key/value pair");
            }
            Map<String, String> env = new HashMap<String, String>();
            for (String line : lines) {
                String[] split = StringUtils.split(line, "=", 2);
                if (split.length < 2) {
                    throw new DictionaryException("dictionary " + dictionary + " : bad key/value format for " + line);
                }
                env.put(normalizeKey(split[0]), split[1]);

            }
            return env;
        } catch (IOException e) {
            throw new DictionaryException("unable to load " + dictionary, e);
        } catch (InvalidKeyFormatException e) {
            throw new DictionaryException("unable to load " + dictionary, e);
        }

    }

    /**
     * Convert a key from confd format "/part1/part2/...." to  shell variable format "PART1_PART2_....".
     * The valid format for the input key is :
     * <code>(/[a-zA-Z0-9]+)+</code>
     *
     * @param key the key to convert
     * @return a new key in shell variable format
     * @throws DictionaryException - If the key format is not valid
     */
    private static String normalizeKey(String key) throws InvalidKeyFormatException {

        // check the key format
        if (!Pattern.matches("(/[A-Za-z0-9]+)+", key)) {
            throw new InvalidKeyFormatException("the key " + key + " format is not valid. Valid format is (/[a-zA-Z0-9]+)+");
        }
        // convert confd key format /part1/part2/.../... to shell variable PART1_PART2_..._...
        String upperCaseKey = StringUtils.stripStart(key, "/").toUpperCase();

        return StringUtils.stripStart(key, "/").toUpperCase().replace('/', '_');

    }

}

@@ -302,76 +302,1298 @@
  * C
-reated by pseillier on 28/12/2015.%0A */%0Apublic class DictionaryUtil %7B
+ontains some helper methods to handle dictionaries. A dictionary is a file containing key/value pairs.%0A * The file may contain comments which begin with a '#'.%0A * The key/value separator is the '=' character.%0A * The key format is :%0A * - a sequence of parts%0A * - a part begin with a %22/%22 character followed by one or more uppercase letter%0A * and/or lowercase letter and/or digit and or underscore character %22_%22%0A * %3Cp%3E%0A * Example of valid key/value pairs :%0A * %3Cp%3E%0A * %3Cpre%3E%0A *%0A * /web/media_server/address = media.server.com%0A * /web/home_page = http://www.home.com/%0A * /proxy/http = server.proxy.int%0A * /proxy/https = server.proxy.int%0A * /users/user01 = user1%0A * /user/use02 = user2%0A *%0A * %3C/pre%3E%0A */%0Apublic class DictionaryUtil %7B%0A    /**%0A     * Load the %3Ccode%3Edictionary%3C/code%3E file into a %3Ccode%3EMap%3C/code%3E according to the %3Ccode%3Eencoding%3C/code%3E.%0A     * %3Cp%3E%0A     * The key names are converted to shell environment variable format.%0A     * For instance /part1/part2 is converted to PART1_PART2 (the first '/' is removed).%0A     *%0A     * @param dictionary the file path to load%0A     * @param encoding   the encoding of the file to load%0A     * @return a %3Ccode%3EMap%3C/code%3E representation of the file%0A     * @throws DictionaryException if the file cannot be load or if a key/value is not valid.%0A     */
 %0A   
@@ -2171,41 +2171,37 @@
 t = 
-StringUtils.split(line, %22=
+line.split(%22%5C%5Cs*=%5C%5Cs*
 %22, 2);%0A
+%0A
     
@@ -2233,11 +2233,202 @@
 gth 
-%3C 2
+!= 2) %7B%0A                    throw new DictionaryException(%22dictionary %22 + dictionary + %22 : bad key/value format for %22 + line);%0A                %7D%0A                if (StringUtils.isEmpty(split%5B1%5D)
 ) %7B%0A
@@ -3093,27 +3093,18 @@
 %3Ccode%3E(/
-%5Ba-zA-Z0-9%5D
+%5Cw
 +)+%3C/cod
@@ -3431,19 +3431,11 @@
 (%22(/
-%5BA-Za-z0-9%5D
+%5C%5Cw
 +)+%22

Fix : replace FileUtils.split with String.split to handle correctly blank characters around the '=' separator. Optimize regular expression