<commit_before>/**
 * Copyright 2012 <NAME>
 * http://www.ripariandata.com
 * <EMAIL>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ripariandata.timberwolf.conf4j;

import java.io.File;

import java.lang.reflect.Field;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

/**
 * Inspired by the CmdLineParser of args4j, this takes a class with ConfigEntry
 * annotations and a configuration file and sets the fields so annotated with
 * the values from the config file.
 * <p>
 * Since our needs for configuration handling are pretty simple, this does a lot
 * less than args4j.  It only handles fields, not methods, and doesn't do anything
 * other than match names to values.
 */
public class ConfigFileParser
{
    private Map<String, FieldSetter> fields = new HashMap<String, FieldSetter>();

    public ConfigFileParser(final Object bean)
    {
        for (Class c = bean.getClass(); c != null; c = c.getSuperclass())
        {
            for (Field f : c.getDeclaredFields())
            {
                ConfigEntry entry = f.getAnnotation(ConfigEntry.class);
                if (entry != null)
                {
                    fields.put(entry.name(), new FieldSetter(bean, f));
                }
            }
        }
    }

    /**
     * Takes the values from the named configuration file and apply them to the
     * target class.  Assumes that the file is a java properties file, readable
     * by <a href="http://commons.apache.org/configuration/apidocs/org/apache/commons/configuration/PropertiesConfiguration.html">
     * org.apache.commons.configuration.PropertiesConfiguration</a>.
     *
     * @throws ConfigFileMissingException If the named configuration file does not exist.
     * @throws ConfigFileException If there was any other problem reading the configuration file.
     */
    public void parseConfigFile(final String configFile) throws ConfigFileException
    {
        File f = new File(configFile);
        if (!f.exists())
        {
            throw new ConfigFileMissingException(configFile, this);
        }

        Configuration config;
        try
        {
            config = new PropertiesConfiguration(configFile);
        }
        catch (ConfigurationException e)
        {
            throw new ConfigFileException("There was an error loading the configuration file at " + configFile, e, this);
        }
        parseConfiguration(config);
    }

    /**
     * Takes the values from the given Configuration instance and applies them to
     * the target class.  Fields in the target class marked with ConfigEntry
     * annotations that don't correspond to any properties in the given configuration
     * are not modified.  Properties in the configuration that don't match any
     * fields marked as ConfigEntry are ignored.
     */
    public void parseConfiguration(final Configuration config)
    {
        Iterator<String> keys = config.getKeys();
        while (keys.hasNext())
        {
            String key = keys.next();<commit_msg>Wrapping 121-character line.<commit_after>/**
 * Copyright 2012 <NAME>
 * http://www.ripariandata.com
 * <EMAIL>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ripariandata.timberwolf.conf4j;

import java.io.File;

import java.lang.reflect.Field;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

/**
 * Inspired by the CmdLineParser of args4j, this takes a class with ConfigEntry
 * annotations and a configuration file and sets the fields so annotated with
 * the values from the config file.
 * <p>
 * Since our needs for configuration handling are pretty simple, this does a lot
 * less than args4j.  It only handles fields, not methods, and doesn't do anything
 * other than match names to values.
 */
public class ConfigFileParser
{
    private Map<String, FieldSetter> fields = new HashMap<String, FieldSetter>();

    public ConfigFileParser(final Object bean)
    {
        for (Class c = bean.getClass(); c != null; c = c.getSuperclass())
        {
            for (Field f : c.getDeclaredFields())
            {
                ConfigEntry entry = f.getAnnotation(ConfigEntry.class);
                if (entry != null)
                {
                    fields.put(entry.name(), new FieldSetter(bean, f));
                }
            }
        }
    }

    /**
     * Takes the values from the named configuration file and apply them to the
     * target class.  Assumes that the file is a java properties file, readable
     * by <a href="http://commons.apache.org/configuration/apidocs/org/apache/commons/configuration/PropertiesConfiguration.html">
     * org.apache.commons.configuration.PropertiesConfiguration</a>.
     *
     * @throws ConfigFileMissingException If the named configuration file does not exist.
     * @throws ConfigFileException If there was any other problem reading the configuration file.
     */
    public void parseConfigFile(final String configFile) throws ConfigFileException
    {
        File f = new File(configFile);
        if (!f.exists())
        {
            throw new ConfigFileMissingException(configFile, this);
        }

        Configuration config;
        try
        {
            config = new PropertiesConfiguration(configFile);
        }
        catch (ConfigurationException e)
        {
            throw new ConfigFileException("There was an error loading the configuration file at " + configFile,
                                          e, this);
        }
        parseConfiguration(config);
    }

    /**
     * Takes the values from the given Configuration instance and applies them to
     * the target class.  Fields in the target class marked with ConfigEntry
     * annotations that don't correspond to any properties in the given configuration
     * are not modified.  Properties in the configuration that don't match any
     * fields marked as ConfigEntry are ignored.
     */
    public void parseConfiguration(final Configuration config)
    {
        Iterator<String> keys = config.getKeys();
        while (keys.hasNext())
        {
            String key = keys.next();
{'fraction_non_alphanumeric': 0.07901365504441203, 'fraction_numerical': 0.0033143311679703037, 'mean_word_length': 2.7439205955334987, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3836455', 'n_tokens_mistral': 1890, 'n_tokens_neox': 1831, 'n_words': 715}