<commit_before>package com.github.nodevops.confd.maven.plugin.processors.impl;

import com.github.nodevops.confd.maven.plugin.processors.Processor;
import com.github.nodevops.confd.maven.plugin.processors.ProcessorContext;
import com.github.nodevops.confd.maven.plugin.processors.ProcessorExecutionException;
import com.github.nodevops.confd.maven.plugin.templating.Parser;
import com.github.nodevops.confd.maven.plugin.utils.DictionaryException;
import com.github.nodevops.confd.maven.plugin.utils.DictionaryUtil;
import com.moandjiezana.toml.Toml;
import org.codehaus.plexus.util.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by pseillier on 22/12/2015.
 */
public class JavaProcessorImpl implements Processor {

    @Override
    public void process(ProcessorContext context) throws ProcessorExecutionException {
        // the toml files are in the conf.d sub directory
        File tomlDirectory = new File(context.getWorkingDirectory(), "conf.d");
        File templatesDirectory = new File(context.getWorkingDirectory(), "templates");
        try {
            // load dictionary
            Map<String, String> env = DictionaryUtil.readDictionaryAsProperties(context.getDictionaryPath(), context.getEncoding());
            // browse the .toml files
            List<File> tomlFiles = FileUtils.getFiles(tomlDirectory, "*.toml", null);
            for (File tomlFile : tomlFiles) {
                // process each toml file
                try {
                    processToml(tomlFile, templatesDirectory, env, context.getEncoding());
                } catch (IOException e) {
                    throw new ProcessorExecutionException("unable to process toml file  " + tomlFile, e);
                }
            }
        } catch (DictionaryException e) {
            throw new ProcessorExecutionException("unable to load dictionary " + context.getDictionaryPath(), e);
        } catch (IllegalStateException e) {
            throw new ProcessorExecutionException("unable to process toml files from directory " + tomlDirectory, e);
        } catch (IOException e) {
            throw new ProcessorExecutionException("unable to process toml files from directory " + tomlDirectory, e);
        }
    }

    private void processToml(File tomlFile, File templatesDirectory, Map<String, String> env, String encoding) throws IOException {
        Toml toml = new Toml().read(tomlFile);
        String src = toml.getString("template.src");
        String dest = toml.getString("template.dest");
        List<String> keys = toml.getList("template.keys");

        File templateFile = new File(templatesDirectory, src);
        File destFile = new File(dest);


        Parser parser = new Parser(templateFile, encoding);

        String parsedTemplate = parser.parse(env);
        FileUtils.fileWrite(destFile, encoding, parsedTemplate);
    }
}<commit_msg>Fix : filter the dictionary entries according to the keys defined in the toml file<commit_after>package com.github.nodevops.confd.maven.plugin.processors.impl;

import com.github.nodevops.confd.maven.plugin.processors.Processor;
import com.github.nodevops.confd.maven.plugin.processors.ProcessorContext;
import com.github.nodevops.confd.maven.plugin.processors.ProcessorExecutionException;
import com.github.nodevops.confd.maven.plugin.templating.Parser;
import com.github.nodevops.confd.maven.plugin.utils.DictionaryException;
import com.github.nodevops.confd.maven.plugin.utils.DictionaryUtil;
import com.moandjiezana.toml.Toml;
import org.codehaus.plexus.util.FileUtils;

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

/**
 * Created by pseillier on 22/12/2015.
 */
public class JavaProcessorImpl implements Processor {

    @Override
    public void process(ProcessorContext context) throws ProcessorExecutionException {
        // the toml files are in the conf.d sub directory
        File tomlDirectory = new File(context.getWorkingDirectory(), "conf.d");
        File templatesDirectory = new File(context.getWorkingDirectory(), "templates");
        try {
            // load dictionary
            Map<String, String> env = DictionaryUtil.readDictionaryAsProperties(context.getDictionaryPath(), context.getEncoding());
            // browse the .toml files
            List<File> tomlFiles = FileUtils.getFiles(tomlDirectory, "*.toml", null);
            for (File tomlFile : tomlFiles) {
                // process each toml file
                try {
                    processToml(tomlFile, templatesDirectory, env, context.getEncoding());
                } catch (IOException e) {
                    throw new ProcessorExecutionException("unable to process toml file  " + tomlFile, e);
                }
            }
        } catch (DictionaryException e) {
            throw new ProcessorExecutionException("unable to load dictionary " + context.getDictionaryPath(), e);
        } catch (IllegalStateException e) {
            throw new ProcessorExecutionException("unable to process toml files from directory " + tomlDirectory, e);
        } catch (IOException e) {
            throw new ProcessorExecutionException("unable to process toml files from directory " + tomlDirectory, e);
        }
    }

    private void processToml(File tomlFile, File templatesDirectory, Map<String, String> env, String encoding) throws IOException {
        Toml toml = new Toml().read(tomlFile);
        String src = toml.getString("template.src");
        String dest = toml.getString("template.dest");
        List<String> keys = toml.getList("template.keys");

        if(keys==null || keys.size()==0) {
            throw new IOException("toml file " + tomlFile + " keys section must exist and must contain at least one key");
        }

        // filter the env map according to the keys defined in the toml
        HashMap<String,String> filteredEnv=new HashMap<String, String>();
        for(Map.Entry<String,String> entry : env.entrySet()) {
            for(String key : keys) {
                if(entry.getKey().startsWith(key)) {
                    filteredEnv.put(entry.getKey(),entry.getValue());
                }
            }
        }
        File templateFile = new File(templatesDirectory, src);
        File destFile = new File(dest);


        Parser parser = new Parser(templateFile, encoding);

        String parsedTemplate = parser.parse(filteredEnv);
        FileUtils.fileWrite(destFile, encoding, parsedTemplate);
    }
}
{'fraction_non_alphanumeric': 0.09934481182386104, 'fraction_numerical': 0.002590278835898217, 'mean_word_length': 2.973365617433414, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2566607', 'n_tokens_mistral': 1783, 'n_tokens_neox': 1725, 'n_words': 435}