<commit_before>/*
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 org.hatemile.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * The Configure class contains the configuration of HaTeMiLe.
 */
public class Configure {

    /**
     * The parameters of configuration of HaTeMiLe.
     */
    protected final Map<String, String> parameters;

    /**
     * Initializes a new object that contains the configuration of HaTeMiLe.
     */
    public Configure() {
        this("hatemile-configure.xml");
    }

    /**
     * Initializes a new object that contains the configuration of HaTeMiLe.
     * @param fileName The full path of file.
     */
    public Configure(final String fileName) {
        parameters = new HashMap<String, String>();
        InputStream inputStream = File.class
                .getResourceAsStream("/" + fileName);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {
            DocumentBuilder documentBuilder = factory.newDocumentBuilder();
            Document document = documentBuilder.parse(inputStream);
            Element rootElement = document.getDocumentElement();

            if (rootElement.getTagName().equalsIgnoreCase("PARAMETERS")) {
                NodeList nodeListParameters = rootElement
                        .getElementsByTagName("parameter");

                for (int i = 0, length = nodeListParameters.getLength();
                        i < length; i++) {
                    Element parameterElement = (Element) nodeListParameters
                            .item(i);
                    if (parameterElement.hasAttribute("name")) {
                        parameters.put(parameterElement.getAttribute("name"),
                                parameterElement.getTextContent());
                    }
                }
            }
        } catch (Exception exception) {
            throw new RuntimeException(exception);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ex) {
                    Logger.getLogger(Configure.class.getName())
                            .log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    /**
     * Returns the parameters of configuration.
     * @return The parameters of configuration.
     */
    public Map<String, String> getParameters() {
        return new HashMap<String, String>(parameters);
    }

    /**
     * Returns the value of a parameter of configuration.
     * @param parameter The parameter.
     * @return The value of the parameter.
     */
    public String getParameter(final String parameter) {
        if (!parameters.containsKey(parameter)) {
            throw new IllegalArgumentException();
        }<commit_msg>:lock: Returns unmodifiable maps from public methods<commit_after>/*
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 org.hatemile.util;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * The Configure class contains the configuration of HaTeMiLe.
 */
public class Configure {

    /**
     * The parameters of configuration of HaTeMiLe.
     */
    protected final Map<String, String> parameters;

    /**
     * Initializes a new object that contains the configuration of HaTeMiLe.
     */
    public Configure() {
        this("hatemile-configure.xml");
    }

    /**
     * Initializes a new object that contains the configuration of HaTeMiLe.
     * @param fileName The full path of file.
     */
    public Configure(final String fileName) {
        parameters = new HashMap<String, String>();
        InputStream inputStream = File.class
                .getResourceAsStream("/" + fileName);

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {
            DocumentBuilder documentBuilder = factory.newDocumentBuilder();
            Document document = documentBuilder.parse(inputStream);
            Element rootElement = document.getDocumentElement();

            if (rootElement.getTagName().equalsIgnoreCase("PARAMETERS")) {
                NodeList nodeListParameters = rootElement
                        .getElementsByTagName("parameter");

                for (int i = 0, length = nodeListParameters.getLength();
                        i < length; i++) {
                    Element parameterElement = (Element) nodeListParameters
                            .item(i);
                    if (parameterElement.hasAttribute("name")) {
                        parameters.put(parameterElement.getAttribute("name"),
                                parameterElement.getTextContent());
                    }
                }
            }
        } catch (Exception exception) {
            throw new RuntimeException(exception);
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ex) {
                    Logger.getLogger(Configure.class.getName())
                            .log(Level.SEVERE, null, ex);
                }
            }
        }
    }

    /**
     * Returns the parameters of configuration.
     * @return The parameters of configuration.
     */
    public Map<String, String> getParameters() {
        return Collections.unmodifiableMap(parameters);
    }

    /**
     * Returns the value of a parameter of configuration.
     * @param parameter The parameter.
     * @return The value of the parameter.
     */
    public String getParameter(final String parameter) {
        if (!parameters.containsKey(parameter)) {
            throw new IllegalArgumentException();
        }
{'fraction_non_alphanumeric': 0.08717530259757922, 'fraction_numerical': 0.002175982592139263, 'mean_word_length': 2.2085514834205933, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 6}, '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': '2971618', 'n_tokens_mistral': 1802, 'n_tokens_neox': 1738, 'n_words': 550}