<commit_before>/****************************************************************************
 * Copyright (C) 2012 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (<EMAIL>)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.common;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Properties;


/**
 * Basic Properties class which overrides default values from System properties on its creation.
 * When looking up a key, it is first searched in the system properties, then in the properties structure instantiated
 * in the constructor.
 *
 * @author <NAME> <<EMAIL>>
 */
public class OverridingProperties {

    private final Properties properties;

    /**
     * Loads properties from named resource.
     * The resource must be located in the classpath.
     *
     * @param fName Name of the properties file.
     * @throws IOException If loading of the resource failed.
     */
    public OverridingProperties(String fName) throws IOException {
	InputStream in = this.getClass().getResourceAsStream("/" + fName);
	if (in == null) {
	    in = this.getClass().getResourceAsStream(fName);
	}
	properties = new Properties();
	properties.load(in);
	init();
    }
    /**
     * Load properties from InputStream.
     *
     * @param stream Stream with Java properties format.
     * @throws IOException If loading of the resource failed.
     */
    public OverridingProperties(InputStream stream) throws IOException {
	this(stream, null);
	init();
    }
    /**
     * Load properties from property instance.
     *
     * @param props Properties instance.
     */
    public OverridingProperties(Properties props) {
	properties = new Properties(props);
	init();
    }
    /**
     * Load properties from InputStreams.
     * The second properties stream takes precedence over the first one. This intended as a persistent default
     * mechanism. The first stream may point to a bundled properties file, while the second can point to a file in the
     * applications config directory, so that the user can alter the config values.
     *
     * @param bundledConf Stream to base properties.
     * @param homeConf Stream to properties overriding the base properties. May be null.
     * @throws IOException If loading of the base resource failed.
     */
    public OverridingProperties(InputStream bundledConf, InputStream homeConf) throws IOException {
	Properties baseProps = new Properties();
	baseProps.load(bundledConf);

	try {
	    if (homeConf != null) {
		Properties homeProps = new Properties(baseProps);
		homeProps.load(homeConf);
		baseProps = homeProps;
	    }
	} catch (IOException ex) {
	}

	properties = baseProps;
	init();
    }

    private void init() {
	overrideFromSystem();
    }

    private void overrideFromSystem() {
	for (Object nextKey : properties.keySet()) {
	    if (nextKey instanceof String) {
		String next = (String) nextKey;
		if (System.getProperties().containsKey(next)) {
		    properties.setProperty(next, System.getProperties().getProperty(next));
		}
	    }
	}
    }


    /**
     * Gets URL to resource reachable by the classloader of this class.
     * This convenience method tries the path with / prepended and without.
     *
     * @param fname Path to the file to load.
     * @return URL instance pointing to the resource. A stream can be opened afterwards.
     */
    public URL getDependentResource(String fname) {
	URL url = getClass().getResource("/" + fname);
	if (url == null) {
	    url = getClass().getResource(fname);
	}
	return url;
    }

    /**
     * Gets the value for the given property key.
     *
     * @see Properties#getProperty(java.lang.String)
     * @param key
     * @return The value, or null if none is found.
     */
    public final String getProperty(String key) {
	return properties.getProperty(key);
    }

    /**
     * Sets the value for a property key.
     *
     * @see Properties#setProperty(java.lang.String, java.lang.String)
     * @param key The key to be placed into the property list.
     * @param value The value corresponding to key.
     * @return The previous value in the properties structure, or null if none was set.
     */
    public final Object setProperty(String key, String value) {
	return properties.setProperty(key, value);
    }

    /**
     * Gets a copy of all defined properties.
     *
     * @return Copy of the properties.
     */
    public final Properties properties() {
	return (Properties) properties.clone();
    }

}<commit_msg>Change the way OverridingProperties are loaded<commit_after>/****************************************************************************
 * Copyright (C) 2012-2015 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (<EMAIL>)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.common;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Basic Properties class which overrides default values from System properties on its creation.
 * When looking up a key, it is first searched in the system properties, then in the properties structure instantiated
 * in the constructor.
 *
 * @author <NAME>
 */
public class OverridingProperties {

    private static final Logger logger = LoggerFactory.getLogger(OverridingProperties.class);

    private final Properties properties;

    /**
     * Loads properties from named resource.
     * The resource must be located in the classpath.
     *
     * @param fName Name of the properties file.
     * @throws IOException If loading of the resource failed.
     */
    public OverridingProperties(String fName) throws IOException {
	this(getfileStream(OverridingProperties.class, fName));
    }

    /**
     * Loads properties from named resource.
     * The resource must be located in the classpath.
     *
     * @param clazz Class used as reference to load the resource.
     * @param fName Name of the properties file.
     * @throws IOException If loading of the resource failed.
     */
    public OverridingProperties(Class clazz, String fName) throws IOException {
	this(getfileStream(clazz, fName));
    }

    /**
     * Load properties from InputStream.
     *
     * @param stream Stream with Java properties format.
     * @throws IOException If loading of the resource failed.
     */
    public OverridingProperties(InputStream stream) throws IOException {
	this(stream, null);
    }

    /**
     * Load properties from property instance.
     *
     * @param props Properties instance.
     * @throws IOException If the merge of the properties with the system defaults failed.
     */
    public OverridingProperties(Properties props) throws IOException {
	properties = mergeWithOverrides(props);
    }

    /**
     * Load properties from InputStreams.
     * The second properties stream takes precedence over the first one. This intended as a persistent default
     * mechanism. The first stream may point to a bundled properties file, while the second can point to a file in the
     * applications config directory, so that the user can alter the config values.
     *
     * @param bundledConf Stream to base properties.
     * @param homeConf Stream to properties overriding the base properties. May be null.
     * @throws IOException If loading of the base resource failed.
     */
    public OverridingProperties(InputStream bundledConf, InputStream homeConf) throws IOException {
	Properties baseProps = new Properties();
	baseProps.load(bundledConf);
	bundledConf.close();

	try {
	    if (homeConf != null) {
		Properties homeProps = new Properties(baseProps);
		homeProps.load(homeConf);
		homeConf.close();
		baseProps = homeProps;
	    }
	} catch (IOException ex) {
	    logger.error("Failed to load given properties stream.", ex);
	}

	properties = mergeWithOverrides(baseProps);
    }


    private static InputStream getfileStream(Class clazz, String fName) {
	InputStream in = clazz.getResourceAsStream("/" + fName);
	if (in == null) {
	    in = clazz.getResourceAsStream(fName);
	}
	return in;
    }

    private static Properties mergeWithOverrides(Properties reference) throws IOException {
	Properties result = new Properties(reference);
	result.load(propsToStream(getOverrides(reference)));
	return result;
    }

    private static Reader propsToStream(Properties properties) throws IOException {
	StringWriter w = new StringWriter();
	properties.store(w, null);
	String propsStr = w.toString();
	return new StringReader(propsStr);
    }

    private static Properties getOverrides(Properties reference) {
	Properties overrides = new Properties();
	for (String nextKey : reference.stringPropertyNames()) {
	    if (System.getProperties().containsKey(nextKey)) {
		overrides.setProperty(nextKey, System.getProperties().getProperty(nextKey));
	    }
	}
	return overrides;
    }


    /**
     * Gets the value for the given property key.
     *
     * @see Properties#getProperty(java.lang.String)
     * @param key
     * @return The value, or null if none is found.
     */
    public final String getProperty(String key) {
	return properties.getProperty(key);
    }

    /**
     * Sets the value for a property key.
     *
     * @see Properties#setProperty(java.lang.String, java.lang.String)
     * @param key The key to be placed into the property list.
     * @param value The value corresponding to key.
     * @return The previous value in the properties structure, or null if none was set.
     */
    public final Object setProperty(String key, String value) {
	return properties.setProperty(key, value);
    }

    /**
     * Gets a copy of all defined properties.
     * The values in the copy are all handled as defaults.
     *
     * @return Copy of the properties.
     */
    public final Properties properties() {
	return new Properties(properties);
    }

}
{'fraction_non_alphanumeric': 0.10724025688396235, 'fraction_numerical': 0.001935427113574382, 'mean_word_length': 3.329017517136329, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '7062657', 'n_tokens_mistral': 2995, 'n_tokens_neox': 2882, 'n_words': 1155}