<commit_before>/*
 * Copyright 2013 StuxCrystal
 *
 * 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 net.stuxcrystal.configuration;

import net.stuxcrystal.configuration.exceptions.ConfigurationException;
import net.stuxcrystal.configuration.exceptions.FileException;
import net.stuxcrystal.configuration.exceptions.ReflectionException;
import net.stuxcrystal.configuration.exceptions.ValueException;
import net.stuxcrystal.configuration.logging.ErrorStreamBinding;
import net.stuxcrystal.configuration.node.Node;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * A singleton to load the configurations.
 *
 * @author StuxCrystal
 */
public class ConfigurationLoader {

    /**
     * A list of all {@link NodeTreeGenerator}s.
     */
    private final List<NodeTreeGenerator> generators = new ArrayList<>();

    /**
     * A list of all known {@link ValueType}s.
     */
    final List<ValueType<?>> types = new ArrayList<>();

    /**
     * The logging interface.
     */
    private LoggingInterface logger = new ErrorStreamBinding();

    /**
     * Basic Configuration Loader.
     */
    public ConfigurationLoader() {
        this(new BaseConstructor());
    }

    /**
     * Constructs the configuration loader with a Constructor object that loads the types and generators needed.
     *
     * @param constructor
     */
    public ConfigurationLoader(Constructor constructor) {
        constructor.loadGenerators(this);
        constructor.loadValueTypes(this);
    }

    /**
     * Adds a tree-generator.
     *
     * @param generator The generator to be added.
     */
    public void addTreeGenerator(NodeTreeGenerator generator) {
        this.generators.add(generator);
    }

    /**
     * Removes a tree-generator.
     *
     * @param generator The generator to be removed.
     */
    public void removeTreeGenerator(NodeTreeGenerator generator) {
        this.generators.remove(generator);
    }

    /**
     * Adds a type.
     *
     * @param type The type to be added.
     */
    public void addType(ValueType<?> type) {
        this.types.add(type);
    }

    /**
     * Removes a type.
     *
     * @param type The type to be removed.
     */
    public void removeType(ValueType<?> type) {
        this.types.remove(type);
    }

    public void setLoggingInterface(LoggingInterface logger) {
        this.logger = logger;
    }

    /**
     * Parses a file.
     *
     * @param file The file to be parsed.
     * @param cls  The class to be instantiated.
     * @return An instance with the specified values.
     * @throws net.stuxcrystal.configuration.exceptions.ConfigurationException
     *          If something fails.
     */
    @SuppressWarnings("unchecked")
    public <T> T parseFile(File file, Class<? extends T> cls) throws ConfigurationException {
        Node<?> nodes = null;
        for (NodeTreeGenerator generator : this.generators) {
            try {
                if (generator.isValidFile(file)) {
                    nodes = generator.parseFile(file, this);
                    break;
                }
            } catch (IOException e) {
                throw new FileException(e);
            }
        }

        if (nodes == null)
            throw new ValueException("File type not supported.");

        return parseNodeTree(nodes, cls);
    }

    /**
     * Parses the contents of a stream.
     *
     * @param is        The stream to parse.
     * @param generator The generator to be used.
     * @param cls       The class of the configuration object.
     * @param <T>       The type.
     * @return An configuration object.
     * @throws ConfigurationException
     * @throws IOException
     */
    public <T> T parseStream(InputStream is, NodeTreeGenerator generator, Class<? extends T> cls) throws ConfigurationException, IOException {
        Node<?> nodes = generator.parse(is, this);

        if (nodes == null)
            throw new ValueException("Failed to parse stream content.");

        return parseNodeTree(nodes, cls);
    }

    /**
     * Writes the configuration into a file.
     *
     * @param file          The file to save into.
     * @param configuration The configuration.
     * @throws ConfigurationException
     */
    public void dumpFile(File file, Object configuration) throws ConfigurationException {
        Node<?> nodes = dumpConfiguration(configuration);

        boolean found = false;
        for (NodeTreeGenerator generator : this.generators) {
            try {
                if (generator.isValidFile(file)) {
                    generator.dumpFile(file, nodes, this);
                    found = true;
                    break;
                }
            } catch (IOException e) {
                throw new FileException(e);
            }
        }<commit_msg>Added File-Creation for dumpFile.<commit_after>/*
 * Copyright 2013 StuxCrystal
 *
 * 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 net.stuxcrystal.configuration;

import net.stuxcrystal.configuration.exceptions.ConfigurationException;
import net.stuxcrystal.configuration.exceptions.FileException;
import net.stuxcrystal.configuration.exceptions.ReflectionException;
import net.stuxcrystal.configuration.exceptions.ValueException;
import net.stuxcrystal.configuration.logging.ErrorStreamBinding;
import net.stuxcrystal.configuration.node.Node;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * A singleton to load the configurations.
 *
 * @author StuxCrystal
 */
public class ConfigurationLoader {

    /**
     * A list of all {@link NodeTreeGenerator}s.
     */
    private final List<NodeTreeGenerator> generators = new ArrayList<>();

    /**
     * A list of all known {@link ValueType}s.
     */
    final List<ValueType<?>> types = new ArrayList<>();

    /**
     * The logging interface.
     */
    private LoggingInterface logger = new ErrorStreamBinding();

    /**
     * Basic Configuration Loader.
     */
    public ConfigurationLoader() {
        this(new BaseConstructor());
    }

    /**
     * Constructs the configuration loader with a Constructor object that loads the types and generators needed.
     *
     * @param constructor
     */
    public ConfigurationLoader(Constructor constructor) {
        constructor.loadGenerators(this);
        constructor.loadValueTypes(this);
    }

    /**
     * Adds a tree-generator.
     *
     * @param generator The generator to be added.
     */
    public void addTreeGenerator(NodeTreeGenerator generator) {
        this.generators.add(generator);
    }

    /**
     * Removes a tree-generator.
     *
     * @param generator The generator to be removed.
     */
    public void removeTreeGenerator(NodeTreeGenerator generator) {
        this.generators.remove(generator);
    }

    /**
     * Adds a type.
     *
     * @param type The type to be added.
     */
    public void addType(ValueType<?> type) {
        this.types.add(type);
    }

    /**
     * Removes a type.
     *
     * @param type The type to be removed.
     */
    public void removeType(ValueType<?> type) {
        this.types.remove(type);
    }

    public void setLoggingInterface(LoggingInterface logger) {
        this.logger = logger;
    }

    /**
     * Parses a file.
     *
     * @param file The file to be parsed.
     * @param cls  The class to be instantiated.
     * @return An instance with the specified values.
     * @throws net.stuxcrystal.configuration.exceptions.ConfigurationException
     *          If something fails.
     */
    @SuppressWarnings("unchecked")
    public <T> T parseFile(File file, Class<? extends T> cls) throws ConfigurationException {
        Node<?> nodes = null;
        for (NodeTreeGenerator generator : this.generators) {
            try {
                if (generator.isValidFile(file)) {
                    nodes = generator.parseFile(file, this);
                    break;
                }
            } catch (IOException e) {
                throw new FileException(e);
            }
        }

        if (nodes == null)
            throw new ValueException("File type not supported.");

        return parseNodeTree(nodes, cls);
    }

    /**
     * Parses the contents of a stream.
     *
     * @param is        The stream to parse.
     * @param generator The generator to be used.
     * @param cls       The class of the configuration object.
     * @param <T>       The type.
     * @return An configuration object.
     * @throws ConfigurationException
     * @throws IOException
     */
    public <T> T parseStream(InputStream is, NodeTreeGenerator generator, Class<? extends T> cls) throws ConfigurationException, IOException {
        Node<?> nodes = generator.parse(is, this);

        if (nodes == null)
            throw new ValueException("Failed to parse stream content.");

        return parseNodeTree(nodes, cls);
    }

    /**
     * Writes the configuration into a file.<p />
     *
     * Creates the file (and the parent-directories) if needed.
     *
     * @param file          The file to save into.
     * @param configuration The configuration.
     * @throws ConfigurationException
     */
    public void dumpFile(File file, Object configuration) throws ConfigurationException {
        if (!file.exists()) {
            if (!file.getParentFile().exists()) {
                file.getParentFile().mkdirs();
            }

            try {
                file.createNewFile();
            } catch (IOException e) {
                throw new FileException("Failed to create file", e);
            }
        }

        Node<?> nodes = dumpConfiguration(configuration);

        boolean found = false;
        for (NodeTreeGenerator generator : this.generators) {
            try {
                if (generator.isValidFile(file)) {
                    generator.dumpFile(file, nodes, this);
                    found = true;
                    break;
                }
            } catch (IOException e) {
                throw new FileException(e);
            }
        }
{'fraction_non_alphanumeric': 0.09343389529724934, 'fraction_numerical': 0.001419698314108252, 'mean_word_length': 2.2584562012142237, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4741211', 'n_tokens_mistral': 2930, 'n_tokens_neox': 2856, 'n_words': 910}