<commit_before>/*
 * Copyright 2011 <NAME>
 * 
 * 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 de.martido.genny.provider;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import de.martido.genny.Field;
import de.martido.genny.FieldFilter;
import de.martido.genny.FieldProvider;
import de.martido.genny.util.Logger;

/**
 * A default {@link FieldProvider} that works with regular Java property files like
 * {@code java.util.Properties} and {@code java.util.PropertyResourceBundle}. Therefore,
 * the name and value of a provided {@link Field} will be the property's key.
 * <p>
 * The following defaults apply if not otherwise specified:
 * <ul>
 * <li>Property files are loaded from the classpath.</li>
 * <li>Property files are encoded using the platform default.</li>
 * </ul>
 * 
 * @author <NAME>
 */
public class PropertyFileProvider implements FieldProvider {

  /**
   * Should resources be loaded from the classpath.
   * <p>
   * By default set to {@code true}.
   */
  private final boolean loadFromClasspath;

  /**
   * The property file's charset.
   * <p>
   * By default set to the platform's default character set.
   */
  private final Charset charset;

  /**
   * A list of property file names.
   */
  private final List<String> propertyFileNames;

  private PropertyFileProvider(Map<String, Object> context) {

    @SuppressWarnings("unchecked")
    List<String> propertyFileNames = (List<String>) context.get("propertyFileNames");
    this.propertyFileNames = propertyFileNames;

    Boolean loadFromClasspath = (Boolean) context.get("loadFromClasspath");
    this.loadFromClasspath = loadFromClasspath != null ? loadFromClasspath : true;

    Charset charset = (Charset) context.get("charset");
    this.charset = charset != null ? charset : Charset.forName(System.getProperty("file.encoding"));
  }

  public static AwaitingLoadBehaviour forFile(String propertyFileName) {
    return forFiles(propertyFileName);
  }

  /**
   * Specifies the list of property files from which to generate the class. The files will be loaded
   * in the order given, i.e. properties defined in files may overwrite previously defined
   * properties.
   * 
   * @param propertyFileNames
   * @return
   */
  public static AwaitingLoadBehaviour forFiles(String... propertyFileNames) {
    String[] copy = new String[propertyFileNames.length];
    System.arraycopy(propertyFileNames, 0, copy, 0, propertyFileNames.length);<commit_msg>Changed default encoding of property files from the platform default to UTF-8.<commit_after>/*
 * Copyright 2011 <NAME>
 * 
 * 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 de.martido.genny.provider;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import de.martido.genny.Field;
import de.martido.genny.FieldFilter;
import de.martido.genny.FieldProvider;
import de.martido.genny.util.Logger;

/**
 * A default {@link FieldProvider} that works with regular Java property files like
 * {@code java.util.Properties} and {@code java.util.PropertyResourceBundle}. Therefore,
 * the name and value of a provided {@link Field} will be the property's key.
 * <p>
 * The following defaults apply if not otherwise specified:
 * <ul>
 * <li>Files are loaded from the classpath.</li>
 * <li>Files are assumed to be UTF-8 encoded.</li>
 * </ul>
 * 
 * @author <NAME>
 */
public class PropertyFileProvider implements FieldProvider {

  /**
   * Should resources be loaded from the classpath.
   * <p>
   * By default set to {@code true}.
   */
  private final boolean loadFromClasspath;

  /**
   * The property file's charset.
   * <p>
   * By default set to the platform's default character set.
   */
  private final Charset charset;

  /**
   * A list of property file names.
   */
  private final List<String> propertyFileNames;

  private PropertyFileProvider(Map<String, Object> context) {

    @SuppressWarnings("unchecked")
    List<String> propertyFileNames = (List<String>) context.get("propertyFileNames");
    this.propertyFileNames = propertyFileNames;

    Boolean loadFromClasspath = (Boolean) context.get("loadFromClasspath");
    this.loadFromClasspath = loadFromClasspath != null ? loadFromClasspath : true;

    Charset charset = (Charset) context.get("charset");
    this.charset = charset != null ? charset : Charset.forName("UTF-8");
  }

  public static AwaitingLoadBehaviour forFile(String propertyFileName) {
    return forFiles(propertyFileName);
  }

  /**
   * Specifies the list of property files from which to generate the class. The files will be loaded
   * in the order given, i.e. properties defined in files may overwrite previously defined
   * properties.
   * 
   * @param propertyFileNames
   * @return
   */
  public static AwaitingLoadBehaviour forFiles(String... propertyFileNames) {
    String[] copy = new String[propertyFileNames.length];
    System.arraycopy(propertyFileNames, 0, copy, 0, propertyFileNames.length);
{'fraction_non_alphanumeric': 0.09938837920489296, 'fraction_numerical': 0.0035168195718654435, 'mean_word_length': 4.062693498452012, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 33, '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': '7059419', 'n_tokens_mistral': 1858, 'n_tokens_neox': 1798, 'n_words': 634}