PropertiesConfiguration.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.configuration2;

  18. import java.io.FileNotFoundException;
  19. import java.io.FilterWriter;
  20. import java.io.IOException;
  21. import java.io.LineNumberReader;
  22. import java.io.Reader;
  23. import java.io.Writer;
  24. import java.net.URL;
  25. import java.nio.charset.StandardCharsets;
  26. import java.util.ArrayList;
  27. import java.util.Collection;
  28. import java.util.Collections;
  29. import java.util.Deque;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. import java.util.regex.Matcher;
  34. import java.util.regex.Pattern;

  35. import org.apache.commons.configuration2.convert.ListDelimiterHandler;
  36. import org.apache.commons.configuration2.convert.ValueTransformer;
  37. import org.apache.commons.configuration2.event.ConfigurationEvent;
  38. import org.apache.commons.configuration2.ex.ConfigurationException;
  39. import org.apache.commons.configuration2.ex.ConfigurationRuntimeException;
  40. import org.apache.commons.configuration2.io.FileHandler;
  41. import org.apache.commons.configuration2.io.FileLocator;
  42. import org.apache.commons.configuration2.io.FileLocatorAware;
  43. import org.apache.commons.configuration2.io.FileLocatorUtils;
  44. import org.apache.commons.lang3.ArrayUtils;
  45. import org.apache.commons.lang3.StringUtils;
  46. import org.apache.commons.text.StringEscapeUtils;
  47. import org.apache.commons.text.translate.AggregateTranslator;
  48. import org.apache.commons.text.translate.CharSequenceTranslator;
  49. import org.apache.commons.text.translate.EntityArrays;
  50. import org.apache.commons.text.translate.LookupTranslator;
  51. import org.apache.commons.text.translate.UnicodeEscaper;

  52. /**
  53.  * This is the "classic" Properties loader which loads the values from a single or multiple files (which can be chained
  54.  * with "include =". All given path references are either absolute or relative to the file name supplied in the
  55.  * constructor.
  56.  * <p>
  57.  * In this class, empty PropertyConfigurations can be built, properties added and later saved. include statements are
  58.  * (obviously) not supported if you don't construct a PropertyConfiguration from a file.
  59.  *
  60.  * <p>
  61.  * The properties file syntax is explained here, basically it follows the syntax of the stream parsed by
  62.  * {@link java.util.Properties#load} and adds several useful extensions:
  63.  *
  64.  * <ul>
  65.  * <li>Each property has the syntax {@code key &lt;separator&gt; value}. The separators accepted are {@code '='},
  66.  * {@code ':'} and any white space character. Examples:
  67.  *
  68.  * <pre>
  69.  *  key1 = value1
  70.  *  key2 : value2
  71.  *  key3   value3
  72.  * </pre>
  73.  *
  74.  * </li>
  75.  * <li>The <em>key</em> may use any character, separators must be escaped:
  76.  *
  77.  * <pre>
  78.  *  key\:foo = bar
  79.  * </pre>
  80.  *
  81.  * </li>
  82.  * <li><em>value</em> may be separated on different lines if a backslash is placed at the end of the line that continues
  83.  * below.</li>
  84.  * <li>The list delimiter facilities provided by {@link AbstractConfiguration} are supported, too. If an appropriate
  85.  * {@link ListDelimiterHandler} is set (for instance a
  86.  * {@link org.apache.commons.configuration2.convert.DefaultListDelimiterHandler D efaultListDelimiterHandler} object
  87.  * configured with a comma as delimiter character), <em>value</em> can contain <em>value delimiters</em> and will then be
  88.  * interpreted as a list of tokens. So the following property definition
  89.  *
  90.  * <pre>
  91.  *  key = This property, has multiple, values
  92.  * </pre>
  93.  *
  94.  * will result in a property with three values. You can change the handling of delimiters using the
  95.  * {@link AbstractConfiguration#setListDelimiterHandler(ListDelimiterHandler)} method. Per default, list splitting is
  96.  * disabled.</li>
  97.  * <li>Commas in each token are escaped placing a backslash right before the comma.</li>
  98.  * <li>If a <em>key</em> is used more than once, the values are appended like if they were on the same line separated with
  99.  * commas. <em>Note</em>: When the configuration file is written back to disk the associated
  100.  * {@link PropertiesConfigurationLayout} object (see below) will try to preserve as much of the original format as
  101.  * possible, i.e. properties with multiple values defined on a single line will also be written back on a single line,
  102.  * and multiple occurrences of a single key will be written on multiple lines. If the {@code addProperty()} method was
  103.  * called multiple times for adding multiple values to a property, these properties will per default be written on
  104.  * multiple lines in the output file, too. Some options of the {@code PropertiesConfigurationLayout} class have
  105.  * influence on that behavior.</li>
  106.  * <li>Blank lines and lines starting with character '#' or '!' are skipped.</li>
  107.  * <li>If a property is named "include" (or whatever is defined by setInclude() and getInclude() and the value of that
  108.  * property is the full path to a file on disk, that file will be included into the configuration. You can also pull in
  109.  * files relative to the parent configuration file. So if you have something like the following:
  110.  *
  111.  * include = additional.properties
  112.  *
  113.  * Then "additional.properties" is expected to be in the same directory as the parent configuration file.
  114.  *
  115.  * The properties in the included file are added to the parent configuration, they do not replace existing properties
  116.  * with the same key.
  117.  *
  118.  * </li>
  119.  * <li>You can define custom error handling for the special key {@code "include"} by using
  120.  * {@link #setIncludeListener(ConfigurationConsumer)}.</li>
  121.  * </ul>
  122.  *
  123.  * <p>
  124.  * Here is an example of a valid extended properties file:
  125.  * </p>
  126.  *
  127.  * <pre>
  128.  *      # lines starting with # are comments
  129.  *
  130.  *      # This is the simplest property
  131.  *      key = value
  132.  *
  133.  *      # A long property may be separated on multiple lines
  134.  *      longvalue = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \
  135.  *                  aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
  136.  *
  137.  *      # This is a property with many tokens
  138.  *      tokens_on_a_line = first token, second token
  139.  *
  140.  *      # This sequence generates exactly the same result
  141.  *      tokens_on_multiple_lines = first token
  142.  *      tokens_on_multiple_lines = second token
  143.  *
  144.  *      # commas may be escaped in tokens
  145.  *      commas.escaped = Hi\, what'up?
  146.  *
  147.  *      # properties can reference other properties
  148.  *      base.prop = /base
  149.  *      first.prop = ${base.prop}/first
  150.  *      second.prop = ${first.prop}/second
  151.  * </pre>
  152.  *
  153.  * <p>
  154.  * A {@code PropertiesConfiguration} object is associated with an instance of the {@link PropertiesConfigurationLayout}
  155.  * class, which is responsible for storing the layout of the parsed properties file (i.e. empty lines, comments, and
  156.  * such things). The {@code getLayout()} method can be used to obtain this layout object. With {@code setLayout()} a new
  157.  * layout object can be set. This should be done before a properties file was loaded.
  158.  * <p>
  159.  * Like other {@code Configuration} implementations, this class uses a {@code Synchronizer} object to control concurrent
  160.  * access. By choosing a suitable implementation of the {@code Synchronizer} interface, an instance can be made
  161.  * thread-safe or not. Note that access to most of the properties typically set through a builder is not protected by
  162.  * the {@code Synchronizer}. The intended usage is that these properties are set once at construction time through the
  163.  * builder and after that remain constant. If you wish to change such properties during life time of an instance, you
  164.  * have to use the {@code lock()} and {@code unlock()} methods manually to ensure that other threads see your changes.
  165.  * <p>
  166.  * As this class extends {@link AbstractConfiguration}, all basic features like variable interpolation, list handling,
  167.  * or data type conversions are available as well. This is described in the chapter
  168.  * <a href="https://commons.apache.org/proper/commons-configuration/userguide/howto_basicfeatures.html"> Basic features
  169.  * and AbstractConfiguration</a> of the user's guide. There is also a separate chapter dealing with
  170.  * <a href="commons.apache.org/proper/commons-configuration/userguide/howto_properties.html"> Properties files</a> in
  171.  * special.
  172.  *
  173.  * @see java.util.Properties#load
  174.  */
  175. public class PropertiesConfiguration extends BaseConfiguration implements FileBasedConfiguration, FileLocatorAware {

  176.     /**
  177.      * <p>
  178.      * A default implementation of the {@code IOFactory} interface.
  179.      * </p>
  180.      * <p>
  181.      * This class implements the {@code createXXXX()} methods defined by the {@code IOFactory} interface in a way that the
  182.      * default objects (i.e. {@code PropertiesReader} and {@code PropertiesWriter} are returned. Customizing either the
  183.      * reader or the writer (or both) can be done by extending this class and overriding the corresponding
  184.      * {@code createXXXX()} method.
  185.      * </p>
  186.      *
  187.      * @since 1.7
  188.      */
  189.     public static class DefaultIOFactory implements IOFactory {
  190.         /**
  191.          * The singleton instance.
  192.          */
  193.         static final DefaultIOFactory INSTANCE = new DefaultIOFactory();

  194.         @Override
  195.         public PropertiesReader createPropertiesReader(final Reader in) {
  196.             return new PropertiesReader(in);
  197.         }

  198.         @Override
  199.         public PropertiesWriter createPropertiesWriter(final Writer out, final ListDelimiterHandler handler) {
  200.             return new PropertiesWriter(out, handler);
  201.         }
  202.     }

  203.     /**
  204.      * <p>
  205.      * Definition of an interface that allows customization of read and write operations.
  206.      * </p>
  207.      * <p>
  208.      * For reading and writing properties files the inner classes {@code PropertiesReader} and {@code PropertiesWriter} are
  209.      * used. This interface defines factory methods for creating both a {@code PropertiesReader} and a
  210.      * {@code PropertiesWriter}. An object implementing this interface can be passed to the {@code setIOFactory()} method of
  211.      * {@code PropertiesConfiguration}. Every time the configuration is read or written the {@code IOFactory} is asked to
  212.      * create the appropriate reader or writer object. This provides an opportunity to inject custom reader or writer
  213.      * implementations.
  214.      * </p>
  215.      *
  216.      * @since 1.7
  217.      */
  218.     public interface IOFactory {
  219.         /**
  220.          * Creates a {@code PropertiesReader} for reading a properties file. This method is called whenever the
  221.          * {@code PropertiesConfiguration} is loaded. The reader returned by this method is then used for parsing the properties
  222.          * file.
  223.          *
  224.          * @param in the underlying reader (of the properties file)
  225.          * @return the {@code PropertiesReader} for loading the configuration
  226.          */
  227.         PropertiesReader createPropertiesReader(Reader in);

  228.         /**
  229.          * Creates a {@code PropertiesWriter} for writing a properties file. This method is called before the
  230.          * {@code PropertiesConfiguration} is saved. The writer returned by this method is then used for writing the properties
  231.          * file.
  232.          *
  233.          * @param out the underlying writer (to the properties file)
  234.          * @param handler the list delimiter delimiter for list parsing
  235.          * @return the {@code PropertiesWriter} for saving the configuration
  236.          */
  237.         PropertiesWriter createPropertiesWriter(Writer out, ListDelimiterHandler handler);
  238.     }

  239.     /**
  240.      * An alternative {@link IOFactory} that tries to mimic the behavior of {@link java.util.Properties} (Jup) more closely.
  241.      * The goal is to allow both of them be used interchangeably when reading and writing properties files without losing or
  242.      * changing information.
  243.      * <p>
  244.      * It also has the option to <em>not</em> use Unicode escapes. When using UTF-8 encoding (which is for example the new default
  245.      * for resource bundle properties files since Java 9), Unicode escapes are no longer required and avoiding them makes
  246.      * properties files more readable with regular text editors.
  247.      * <p>
  248.      * Some of the ways this implementation differs from {@link DefaultIOFactory}:
  249.      * <ul>
  250.      * <li>Trailing whitespace will not be trimmed from each line.</li>
  251.      * <li>Unknown escape sequences will have their backslash removed.</li>
  252.      * <li>{@code \b} is not a recognized escape sequence.</li>
  253.      * <li>Leading spaces in property values are preserved by escaping them.</li>
  254.      * <li>All natural lines (i.e. in the file) of a logical property line will have their leading whitespace trimmed.</li>
  255.      * <li>Natural lines that look like comment lines within a logical line are not treated as such; they're part of the
  256.      * property value.</li>
  257.      * </ul>
  258.      *
  259.      * @since 2.4
  260.      */
  261.     public static class JupIOFactory implements IOFactory {

  262.         /**
  263.          * Whether characters less than {@code \u0020} and characters greater than {@code \u007E} in property keys or values
  264.          * should be escaped using Unicode escape sequences. Not necessary when for example writing as UTF-8.
  265.          */
  266.         private final boolean escapeUnicode;

  267.         /**
  268.          * Constructs a new {@link JupIOFactory} with Unicode escaping.
  269.          */
  270.         public JupIOFactory() {
  271.             this(true);
  272.         }

  273.         /**
  274.          * Constructs a new {@link JupIOFactory} with optional Unicode escaping. Whether Unicode escaping is required depends on
  275.          * the encoding used to save the properties file. E.g. for ISO-8859-1 this must be turned on, for UTF-8 it's not
  276.          * necessary. Unfortunately this factory can't determine the encoding on its own.
  277.          *
  278.          * @param escapeUnicode whether Unicode characters should be escaped
  279.          */
  280.         public JupIOFactory(final boolean escapeUnicode) {
  281.             this.escapeUnicode = escapeUnicode;
  282.         }

  283.         @Override
  284.         public PropertiesReader createPropertiesReader(final Reader in) {
  285.             return new JupPropertiesReader(in);
  286.         }

  287.         @Override
  288.         public PropertiesWriter createPropertiesWriter(final Writer out, final ListDelimiterHandler handler) {
  289.             return new JupPropertiesWriter(out, handler, escapeUnicode);
  290.         }

  291.     }

  292.     /**
  293.      * A {@link PropertiesReader} that tries to mimic the behavior of {@link java.util.Properties}.
  294.      *
  295.      * @since 2.4
  296.      */
  297.     public static class JupPropertiesReader extends PropertiesReader {

  298.         /**
  299.          * Constructs a new instance.
  300.          *
  301.          * @param reader A Reader.
  302.          */
  303.         public JupPropertiesReader(final Reader reader) {
  304.             super(reader);
  305.         }

  306.         @Override
  307.         protected void parseProperty(final String line) {
  308.             final String[] property = doParseProperty(line, false);
  309.             initPropertyName(property[0]);
  310.             initPropertyValue(property[1]);
  311.             initPropertySeparator(property[2]);
  312.         }

  313.         @Override
  314.         public String readProperty() throws IOException {
  315.             getCommentLines().clear();
  316.             final StringBuilder buffer = new StringBuilder();

  317.             while (true) {
  318.                 String line = readLine();
  319.                 if (line == null) {
  320.                     // EOF
  321.                     if (buffer.length() > 0) {
  322.                         break;
  323.                     }
  324.                     return null;
  325.                 }

  326.                 // while a property line continues there are no comments (even if the line from
  327.                 // the file looks like one)
  328.                 if (isCommentLine(line) && buffer.length() == 0) {
  329.                     getCommentLines().add(line);
  330.                     continue;
  331.                 }

  332.                 // while property line continues left trim all following lines read from the
  333.                 // file
  334.                 if (buffer.length() > 0) {
  335.                     // index of the first non-whitespace character
  336.                     int i;
  337.                     for (i = 0; i < line.length(); i++) {
  338.                         if (!Character.isWhitespace(line.charAt(i))) {
  339.                             break;
  340.                         }
  341.                     }

  342.                     line = line.substring(i);
  343.                 }

  344.                 if (!checkCombineLines(line)) {
  345.                     buffer.append(line);
  346.                     break;
  347.                 }
  348.                 line = line.substring(0, line.length() - 1);
  349.                 buffer.append(line);
  350.             }
  351.             return buffer.toString();
  352.         }

  353.         @Override
  354.         protected String unescapePropertyValue(final String value) {
  355.             return unescapeJava(value, true);
  356.         }

  357.     }

  358.     /**
  359.      * A {@link PropertiesWriter} that tries to mimic the behavior of {@link java.util.Properties}.
  360.      *
  361.      * @since 2.4
  362.      */
  363.     public static class JupPropertiesWriter extends PropertiesWriter {

  364.         /**
  365.          * The starting ASCII printable character.
  366.          */
  367.         private static final int PRINTABLE_INDEX_END = 0x7e;

  368.         /**
  369.          * The ending ASCII printable character.
  370.          */
  371.         private static final int PRINTABLE_INDEX_START = 0x20;

  372.         /**
  373.          * A UnicodeEscaper for characters outside the ASCII printable range.
  374.          */
  375.         private static final UnicodeEscaper ESCAPER = UnicodeEscaper.outsideOf(PRINTABLE_INDEX_START, PRINTABLE_INDEX_END);

  376.         /**
  377.          * Characters that need to be escaped when wring a properties file.
  378.          */
  379.         private static final Map<CharSequence, CharSequence> JUP_CHARS_ESCAPE;
  380.         static {
  381.             final Map<CharSequence, CharSequence> initialMap = new HashMap<>();
  382.             initialMap.put("\\", "\\\\");
  383.             initialMap.put("\n", "\\n");
  384.             initialMap.put("\t", "\\t");
  385.             initialMap.put("\f", "\\f");
  386.             initialMap.put("\r", "\\r");
  387.             JUP_CHARS_ESCAPE = Collections.unmodifiableMap(initialMap);
  388.         }

  389.         /**
  390.          * Creates a new instance of {@code JupPropertiesWriter}.
  391.          *
  392.          * @param writer a Writer object providing the underlying stream
  393.          * @param delHandler the delimiter handler for dealing with properties with multiple values
  394.          * @param escapeUnicode whether Unicode characters should be escaped using Unicode escapes
  395.          */
  396.         public JupPropertiesWriter(final Writer writer, final ListDelimiterHandler delHandler, final boolean escapeUnicode) {
  397.             super(writer, delHandler, value -> {
  398.                 String valueString = String.valueOf(value);

  399.                 final CharSequenceTranslator translator;
  400.                 if (escapeUnicode) {
  401.                     translator = new AggregateTranslator(new LookupTranslator(JUP_CHARS_ESCAPE), ESCAPER);
  402.                 } else {
  403.                     translator = new AggregateTranslator(new LookupTranslator(JUP_CHARS_ESCAPE));
  404.                 }

  405.                 valueString = translator.translate(valueString);

  406.                 // escape the first leading space to preserve it (and all after it)
  407.                 if (valueString.startsWith(" ")) {
  408.                     valueString = "\\" + valueString;
  409.                 }

  410.                 return valueString;
  411.             });
  412.         }

  413.     }

  414.     /**
  415.      * This class is used to read properties lines. These lines do not terminate with new-line chars but rather when there
  416.      * is no backslash sign a the end of the line. This is used to concatenate multiple lines for readability.
  417.      */
  418.     public static class PropertiesReader extends LineNumberReader {

  419.         /** The regular expression to parse the key and the value of a property. */
  420.         private static final Pattern PROPERTY_PATTERN = Pattern
  421.             .compile("(([\\S&&[^\\\\" + new String(SEPARATORS) + "]]|\\\\.)*+)(\\s*(\\s+|[" + new String(SEPARATORS) + "])\\s*)?(.*)");

  422.         /** Constant for the index of the group for the key. */
  423.         private static final int IDX_KEY = 1;

  424.         /** Constant for the index of the group for the value. */
  425.         private static final int IDX_VALUE = 5;

  426.         /** Constant for the index of the group for the separator. */
  427.         private static final int IDX_SEPARATOR = 3;

  428.         /**
  429.          * Checks if the passed in line should be combined with the following. This is true, if the line ends with an odd number
  430.          * of backslashes.
  431.          *
  432.          * @param line the line
  433.          * @return a flag if the lines should be combined
  434.          */
  435.         static boolean checkCombineLines(final String line) {
  436.             return countTrailingBS(line) % 2 != 0;
  437.         }

  438.         /**
  439.          * Parse a property line and return the key, the value, and the separator in an array.
  440.          *
  441.          * @param line the line to parse
  442.          * @param trimValue flag whether the value is to be trimmed
  443.          * @return an array with the property's key, value, and separator
  444.          */
  445.         static String[] doParseProperty(final String line, final boolean trimValue) {
  446.             final Matcher matcher = PROPERTY_PATTERN.matcher(line);

  447.             final String[] result = {"", "", ""};

  448.             if (matcher.matches()) {
  449.                 result[0] = matcher.group(IDX_KEY).trim();

  450.                 String value = matcher.group(IDX_VALUE);
  451.                 if (trimValue) {
  452.                     value = value.trim();
  453.                 }
  454.                 result[1] = value;

  455.                 result[2] = matcher.group(IDX_SEPARATOR);
  456.             }

  457.             return result;
  458.         }

  459.         /** Stores the comment lines for the currently processed property. */
  460.         private final List<String> commentLines;

  461.         /** Stores the name of the last read property. */
  462.         private String propertyName;

  463.         /** Stores the value of the last read property. */
  464.         private String propertyValue;

  465.         /** Stores the property separator of the last read property. */
  466.         private String propertySeparator = DEFAULT_SEPARATOR;

  467.         /**
  468.          * Constructs a new instance.
  469.          *
  470.          * @param reader A Reader.
  471.          */
  472.         public PropertiesReader(final Reader reader) {
  473.             super(reader);
  474.             commentLines = new ArrayList<>();
  475.         }

  476.         /**
  477.          * Gets the comment lines that have been read for the last property.
  478.          *
  479.          * @return the comment lines for the last property returned by {@code readProperty()}
  480.          * @since 1.3
  481.          */
  482.         public List<String> getCommentLines() {
  483.             return commentLines;
  484.         }

  485.         /**
  486.          * Gets the name of the last read property. This method can be called after {@link #nextProperty()} was invoked and
  487.          * its return value was <strong>true</strong>.
  488.          *
  489.          * @return the name of the last read property
  490.          * @since 1.3
  491.          */
  492.         public String getPropertyName() {
  493.             return propertyName;
  494.         }

  495.         /**
  496.          * Gets the separator that was used for the last read property. The separator can be stored so that it can later be
  497.          * restored when saving the configuration.
  498.          *
  499.          * @return the separator for the last read property
  500.          * @since 1.7
  501.          */
  502.         public String getPropertySeparator() {
  503.             return propertySeparator;
  504.         }

  505.         /**
  506.          * Gets the value of the last read property. This method can be called after {@link #nextProperty()} was invoked and
  507.          * its return value was <strong>true</strong>.
  508.          *
  509.          * @return the value of the last read property
  510.          * @since 1.3
  511.          */
  512.         public String getPropertyValue() {
  513.             return propertyValue;
  514.         }

  515.         /**
  516.          * Sets the name of the current property. This method can be called by {@code parseProperty()} for storing the results
  517.          * of the parse operation. It also ensures that the property key is correctly escaped.
  518.          *
  519.          * @param name the name of the current property
  520.          * @since 1.7
  521.          */
  522.         protected void initPropertyName(final String name) {
  523.             propertyName = unescapePropertyName(name);
  524.         }

  525.         /**
  526.          * Sets the separator of the current property. This method can be called by {@code parseProperty()}. It allows the
  527.          * associated layout object to keep track of the property separators. When saving the configuration the separators can
  528.          * be restored.
  529.          *
  530.          * @param value the separator used for the current property
  531.          * @since 1.7
  532.          */
  533.         protected void initPropertySeparator(final String value) {
  534.             propertySeparator = value;
  535.         }

  536.         /**
  537.          * Sets the value of the current property. This method can be called by {@code parseProperty()} for storing the results
  538.          * of the parse operation. It also ensures that the property value is correctly escaped.
  539.          *
  540.          * @param value the value of the current property
  541.          * @since 1.7
  542.          */
  543.         protected void initPropertyValue(final String value) {
  544.             propertyValue = unescapePropertyValue(value);
  545.         }

  546.         /**
  547.          * Parses the next property from the input stream and stores the found name and value in internal fields. These fields
  548.          * can be obtained using the provided getter methods. The return value indicates whether EOF was reached (<strong>false</strong>)
  549.          * or whether further properties are available (<strong>true</strong>).
  550.          *
  551.          * @return a flag if further properties are available
  552.          * @throws IOException if an error occurs
  553.          * @since 1.3
  554.          */
  555.         public boolean nextProperty() throws IOException {
  556.             final String line = readProperty();

  557.             if (line == null) {
  558.                 return false; // EOF
  559.             }

  560.             // parse the line
  561.             parseProperty(line);
  562.             return true;
  563.         }

  564.         /**
  565.          * Parses a line read from the properties file. This method is called for each non-comment line read from the source
  566.          * file. Its task is to split the passed in line into the property key and its value. The results of the parse operation
  567.          * can be stored by calling the {@code initPropertyXXX()} methods.
  568.          *
  569.          * @param line the line read from the properties file
  570.          * @since 1.7
  571.          */
  572.         protected void parseProperty(final String line) {
  573.             final String[] property = doParseProperty(line, true);
  574.             initPropertyName(property[0]);
  575.             initPropertyValue(property[1]);
  576.             initPropertySeparator(property[2]);
  577.         }

  578.         /**
  579.          * Reads a property line. Returns null if Stream is at EOF. Concatenates lines ending with "\". Skips lines beginning
  580.          * with "#" or "!" and empty lines. The return value is a property definition ({@code &lt;name&gt;} =
  581.          * {@code &lt;value&gt;})
  582.          *
  583.          * @return A string containing a property value or null
  584.          * @throws IOException in case of an I/O error
  585.          */
  586.         public String readProperty() throws IOException {
  587.             commentLines.clear();
  588.             final StringBuilder buffer = new StringBuilder();

  589.             while (true) {
  590.                 String line = readLine();
  591.                 if (line == null) {
  592.                     // EOF
  593.                     return null;
  594.                 }

  595.                 if (isCommentLine(line)) {
  596.                     commentLines.add(line);
  597.                     continue;
  598.                 }

  599.                 line = line.trim();

  600.                 if (!checkCombineLines(line)) {
  601.                     buffer.append(line);
  602.                     break;
  603.                 }
  604.                 line = line.substring(0, line.length() - 1);
  605.                 buffer.append(line);
  606.             }
  607.             return buffer.toString();
  608.         }

  609.         /**
  610.          * Performs unescaping on the given property name.
  611.          *
  612.          * @param name the property name
  613.          * @return the unescaped property name
  614.          * @since 2.4
  615.          */
  616.         protected String unescapePropertyName(final String name) {
  617.             return StringEscapeUtils.unescapeJava(name);
  618.         }

  619.         /**
  620.          * Performs unescaping on the given property value.
  621.          *
  622.          * @param value the property value
  623.          * @return the unescaped property value
  624.          * @since 2.4
  625.          */
  626.         protected String unescapePropertyValue(final String value) {
  627.             return unescapeJava(value);
  628.         }
  629.     } // class PropertiesReader

  630.     /**
  631.      * This class is used to write properties lines. The most important method is
  632.      * {@code writeProperty(String, Object, boolean)}, which is called during a save operation for each property found in
  633.      * the configuration.
  634.      */
  635.     public static class PropertiesWriter extends FilterWriter {

  636.         /**
  637.          * Properties escape map.
  638.          */
  639.         private static final Map<CharSequence, CharSequence> PROPERTIES_CHARS_ESCAPE;
  640.         static {
  641.             final Map<CharSequence, CharSequence> initialMap = new HashMap<>();
  642.             initialMap.put("\\", "\\\\");
  643.             PROPERTIES_CHARS_ESCAPE = Collections.unmodifiableMap(initialMap);
  644.         }

  645.         /**
  646.          * A translator for escaping property values. This translator performs a subset of transformations done by the
  647.          * ESCAPE_JAVA translator from Commons Lang 3.
  648.          */
  649.         private static final CharSequenceTranslator ESCAPE_PROPERTIES = new AggregateTranslator(new LookupTranslator(PROPERTIES_CHARS_ESCAPE),
  650.             new LookupTranslator(EntityArrays.JAVA_CTRL_CHARS_ESCAPE), UnicodeEscaper.outsideOf(32, 0x7f));

  651.         /**
  652.          * A {@code ValueTransformer} implementation used to escape property values. This implementation applies the
  653.          * transformation defined by the {@link #ESCAPE_PROPERTIES} translator.
  654.          */
  655.         private static final ValueTransformer DEFAULT_TRANSFORMER = value -> {
  656.             final String strVal = String.valueOf(value);
  657.             return ESCAPE_PROPERTIES.translate(strVal);
  658.         };

  659.         /** The value transformer used for escaping property values. */
  660.         private final ValueTransformer valueTransformer;

  661.         /** The list delimiter handler. */
  662.         private final ListDelimiterHandler delimiterHandler;

  663.         /** The separator to be used for the current property. */
  664.         private String currentSeparator;

  665.         /** The global separator. If set, it overrides the current separator. */
  666.         private String globalSeparator;

  667.         /** The line separator. */
  668.         private String lineSeparator;

  669.         /**
  670.          * Creates a new instance of {@code PropertiesWriter}.
  671.          *
  672.          * @param writer a Writer object providing the underlying stream
  673.          * @param delHandler the delimiter handler for dealing with properties with multiple values
  674.          */
  675.         public PropertiesWriter(final Writer writer, final ListDelimiterHandler delHandler) {
  676.             this(writer, delHandler, DEFAULT_TRANSFORMER);
  677.         }

  678.         /**
  679.          * Creates a new instance of {@code PropertiesWriter}.
  680.          *
  681.          * @param writer a Writer object providing the underlying stream
  682.          * @param delHandler the delimiter handler for dealing with properties with multiple values
  683.          * @param valueTransformer the value transformer used to escape property values
  684.          */
  685.         public PropertiesWriter(final Writer writer, final ListDelimiterHandler delHandler, final ValueTransformer valueTransformer) {
  686.             super(writer);
  687.             delimiterHandler = delHandler;
  688.             this.valueTransformer = valueTransformer;
  689.         }

  690.         /**
  691.          * Escapes the key of a property before it gets written to file. This method is called on saving a configuration for
  692.          * each property key. It ensures that separator characters contained in the key are escaped.
  693.          *
  694.          * @param key the key
  695.          * @return the escaped key
  696.          * @since 2.0
  697.          */
  698.         protected String escapeKey(final String key) {
  699.             final StringBuilder newkey = new StringBuilder();

  700.             for (int i = 0; i < key.length(); i++) {
  701.                 final char c = key.charAt(i);

  702.                 if (ArrayUtils.contains(SEPARATORS, c) || ArrayUtils.contains(WHITE_SPACE, c) || c == '\\') {
  703.                     // escape the separator
  704.                     newkey.append('\\');
  705.                 }
  706.                 newkey.append(c);
  707.             }

  708.             return newkey.toString();
  709.         }

  710.         /**
  711.          * Returns the separator to be used for the given property. This method is called by {@code writeProperty()}. The string
  712.          * returned here is used as separator between the property key and its value. Per default the method checks whether a
  713.          * global separator is set. If this is the case, it is returned. Otherwise the separator returned by
  714.          * {@code getCurrentSeparator()} is used, which was set by the associated layout object. Derived classes may implement a
  715.          * different strategy for defining the separator.
  716.          *
  717.          * @param key the property key
  718.          * @param value the value
  719.          * @return the separator to be used
  720.          * @since 1.7
  721.          */
  722.         protected String fetchSeparator(final String key, final Object value) {
  723.             return getGlobalSeparator() != null ? getGlobalSeparator() : StringUtils.defaultString(getCurrentSeparator());
  724.         }

  725.         /**
  726.          * Gets the current property separator.
  727.          *
  728.          * @return the current property separator
  729.          * @since 1.7
  730.          */
  731.         public String getCurrentSeparator() {
  732.             return currentSeparator;
  733.         }

  734.         /**
  735.          * Gets the delimiter handler for properties with multiple values. This object is used to escape property values so
  736.          * that they can be read in correctly the next time they are loaded.
  737.          *
  738.          * @return the delimiter handler for properties with multiple values
  739.          * @since 2.0
  740.          */
  741.         public ListDelimiterHandler getDelimiterHandler() {
  742.             return delimiterHandler;
  743.         }

  744.         /**
  745.          * Gets the global property separator.
  746.          *
  747.          * @return the global property separator
  748.          * @since 1.7
  749.          */
  750.         public String getGlobalSeparator() {
  751.             return globalSeparator;
  752.         }

  753.         /**
  754.          * Gets the line separator.
  755.          *
  756.          * @return the line separator
  757.          * @since 1.7
  758.          */
  759.         public String getLineSeparator() {
  760.             return lineSeparator != null ? lineSeparator : LINE_SEPARATOR;
  761.         }

  762.         /**
  763.          * Sets the current property separator. This separator is used when writing the next property.
  764.          *
  765.          * @param currentSeparator the current property separator
  766.          * @since 1.7
  767.          */
  768.         public void setCurrentSeparator(final String currentSeparator) {
  769.             this.currentSeparator = currentSeparator;
  770.         }

  771.         /**
  772.          * Sets the global property separator. This separator corresponds to the {@code globalSeparator} property of
  773.          * {@link PropertiesConfigurationLayout}. It defines the separator to be used for all properties. If it is undefined,
  774.          * the current separator is used.
  775.          *
  776.          * @param globalSeparator the global property separator
  777.          * @since 1.7
  778.          */
  779.         public void setGlobalSeparator(final String globalSeparator) {
  780.             this.globalSeparator = globalSeparator;
  781.         }

  782.         /**
  783.          * Sets the line separator. Each line written by this writer is terminated with this separator. If not set, the
  784.          * platform-specific line separator is used.
  785.          *
  786.          * @param lineSeparator the line separator to be used
  787.          * @since 1.7
  788.          */
  789.         public void setLineSeparator(final String lineSeparator) {
  790.             this.lineSeparator = lineSeparator;
  791.         }

  792.         /**
  793.          * Writes a comment.
  794.          *
  795.          * @param comment the comment to write
  796.          * @throws IOException if an I/O error occurs.
  797.          */
  798.         public void writeComment(final String comment) throws IOException {
  799.             writeln("# " + comment);
  800.         }

  801.         /**
  802.          * Helper method for writing a line with the platform specific line ending.
  803.          *
  804.          * @param s the content of the line (may be <strong>null</strong>)
  805.          * @throws IOException if an error occurs
  806.          * @since 1.3
  807.          */
  808.         public void writeln(final String s) throws IOException {
  809.             if (s != null) {
  810.                 write(s);
  811.             }
  812.             write(getLineSeparator());
  813.         }

  814.         /**
  815.          * Writes a property.
  816.          *
  817.          * @param key The key of the property
  818.          * @param values The array of values of the property
  819.          * @throws IOException if an I/O error occurs.
  820.          */
  821.         public void writeProperty(final String key, final List<?> values) throws IOException {
  822.             for (final Object value : values) {
  823.                 writeProperty(key, value);
  824.             }
  825.         }

  826.         /**
  827.          * Writes a property.
  828.          *
  829.          * @param key the key of the property
  830.          * @param value the value of the property
  831.          * @throws IOException if an I/O error occurs.
  832.          */
  833.         public void writeProperty(final String key, final Object value) throws IOException {
  834.             writeProperty(key, value, false);
  835.         }

  836.         /**
  837.          * Writes the given property and its value. If the value happens to be a list, the {@code forceSingleLine} flag is
  838.          * evaluated. If it is set, all values are written on a single line using the list delimiter as separator.
  839.          *
  840.          * @param key the property key
  841.          * @param value the property value
  842.          * @param forceSingleLine the &quot;force single line&quot; flag
  843.          * @throws IOException if an error occurs
  844.          * @since 1.3
  845.          */
  846.         public void writeProperty(final String key, final Object value, final boolean forceSingleLine) throws IOException {
  847.             String v;

  848.             if (value instanceof List) {
  849.                 v = null;
  850.                 final List<?> values = (List<?>) value;
  851.                 if (forceSingleLine) {
  852.                     try {
  853.                         v = String.valueOf(getDelimiterHandler().escapeList(values, valueTransformer));
  854.                     } catch (final UnsupportedOperationException ignored) {
  855.                         // the handler may not support escaping lists,
  856.                         // then the list is written in multiple lines
  857.                     }
  858.                 }
  859.                 if (v == null) {
  860.                     writeProperty(key, values);
  861.                     return;
  862.                 }
  863.             } else {
  864.                 v = String.valueOf(getDelimiterHandler().escape(value, valueTransformer));
  865.             }

  866.             write(escapeKey(key));
  867.             write(fetchSeparator(key, value));
  868.             write(v);

  869.             writeln(null);
  870.         }
  871.     } // class PropertiesWriter

  872.     /**
  873.      * Defines default error handling for the special {@code "include"} key by throwing the given exception.
  874.      *
  875.      * @since 2.6
  876.      */
  877.     public static final ConfigurationConsumer<ConfigurationException> DEFAULT_INCLUDE_LISTENER = e -> {
  878.         throw e;
  879.     };

  880.     /**
  881.      * Defines error handling as a noop for the special {@code "include"} key.
  882.      *
  883.      * @since 2.6
  884.      */
  885.     public static final ConfigurationConsumer<ConfigurationException> NOOP_INCLUDE_LISTENER = e -> { /* noop */ };

  886.     /**
  887.      * The default encoding (ISO-8859-1 as specified by https://docs.oracle.com/javase/8/docs/api/java/util/Properties.html)
  888.      */
  889.     public static final String DEFAULT_ENCODING = StandardCharsets.ISO_8859_1.name();

  890.     /** Constant for the supported comment characters. */
  891.     static final String COMMENT_CHARS = "#!";

  892.     /** Constant for the default properties separator. */
  893.     static final String DEFAULT_SEPARATOR = " = ";

  894.     /**
  895.      * A string with special characters that need to be unescaped when reading a properties file.
  896.      * {@link java.util.Properties} escapes these characters when writing out a properties file.
  897.      */
  898.     private static final String UNESCAPE_CHARACTERS = ":#=!\\\'\"";

  899.     /**
  900.      * This is the name of the property that can point to other properties file for including other properties files.
  901.      */
  902.     private static String include = "include";

  903.     /**
  904.      * This is the name of the property that can point to other properties file for including other properties files.
  905.      * <p>
  906.      * If the file is absent, processing continues normally.
  907.      * </p>
  908.      */
  909.     private static String includeOptional = "includeoptional";

  910.     /** The list of possible key/value separators */
  911.     private static final char[] SEPARATORS = {'=', ':'};

  912.     /** The white space characters used as key/value separators. */
  913.     private static final char[] WHITE_SPACE = {' ', '\t', '\f'};

  914.     /** Constant for the platform specific line separator. */
  915.     private static final String LINE_SEPARATOR = System.lineSeparator();

  916.     /** Constant for the radix of hex numbers. */
  917.     private static final int HEX_RADIX = 16;

  918.     /** Constant for the length of a unicode literal. */
  919.     private static final int UNICODE_LEN = 4;

  920.     /**
  921.      * Returns the number of trailing backslashes. This is sometimes needed for the correct handling of escape characters.
  922.      *
  923.      * @param line the string to investigate
  924.      * @return the number of trailing backslashes
  925.      */
  926.     private static int countTrailingBS(final String line) {
  927.         int bsCount = 0;
  928.         for (int idx = line.length() - 1; idx >= 0 && line.charAt(idx) == '\\'; idx--) {
  929.             bsCount++;
  930.         }

  931.         return bsCount;
  932.     }

  933.     /**
  934.      * Gets the property value for including other properties files. By default it is "include".
  935.      *
  936.      * @return A String.
  937.      */
  938.     public static String getInclude() {
  939.         return include;
  940.     }

  941.     /**
  942.      * Gets the property value for including other properties files. By default it is "includeoptional".
  943.      * <p>
  944.      * If the file is absent, processing continues normally.
  945.      * </p>
  946.      *
  947.      * @return A String.
  948.      * @since 2.5
  949.      */
  950.     public static String getIncludeOptional() {
  951.         return includeOptional;
  952.     }

  953.     /**
  954.      * Tests whether a line is a comment, i.e. whether it starts with a comment character.
  955.      *
  956.      * @param line the line
  957.      * @return a flag if this is a comment line
  958.      * @since 1.3
  959.      */
  960.     static boolean isCommentLine(final String line) {
  961.         final String s = line.trim();
  962.         // blank lines are also treated as comment lines
  963.         return s.isEmpty() || COMMENT_CHARS.indexOf(s.charAt(0)) >= 0;
  964.     }

  965.     /**
  966.      * Checks whether the specified character needs to be unescaped. This method is called when during reading a property
  967.      * file an escape character ('\') is detected. If the character following the escape character is recognized as a
  968.      * special character which is escaped per default in a Java properties file, it has to be unescaped.
  969.      *
  970.      * @param ch the character in question
  971.      * @return a flag whether this character has to be unescaped
  972.      */
  973.     private static boolean needsUnescape(final char ch) {
  974.         return UNESCAPE_CHARACTERS.indexOf(ch) >= 0;
  975.     }

  976.     /**
  977.      * Sets the property value for including other properties files. By default it is "include".
  978.      *
  979.      * @param inc A String.
  980.      */
  981.     public static void setInclude(final String inc) {
  982.         include = inc;
  983.     }

  984.     /**
  985.      * Sets the property value for including other properties files. By default it is "include".
  986.      * <p>
  987.      * If the file is absent, processing continues normally.
  988.      * </p>
  989.      *
  990.      * @param inc A String.
  991.      * @since 2.5
  992.      */
  993.     public static void setIncludeOptional(final String inc) {
  994.         includeOptional = inc;
  995.     }

  996.     /**
  997.      * <p>
  998.      * Unescapes any Java literals found in the {@code String} to a {@code Writer}.
  999.      * </p>
  1000.      * This is a slightly modified version of the StringEscapeUtils.unescapeJava() function in commons-lang that doesn't
  1001.      * drop escaped separators (i.e '\,').
  1002.      *
  1003.      * @param str the {@code String} to unescape, may be null
  1004.      * @return the processed string
  1005.      * @throws IllegalArgumentException if the Writer is {@code null}
  1006.      */
  1007.     protected static String unescapeJava(final String str) {
  1008.         return unescapeJava(str, false);
  1009.     }

  1010.     /**
  1011.      * Unescapes Java literals found in the {@code String} to a {@code Writer}.
  1012.      * <p>
  1013.      * When the parameter {@code jupCompatible} is {@code false}, the classic behavior is used (see
  1014.      * {@link #unescapeJava(String)}). When it's {@code true} a slightly different behavior that's compatible with
  1015.      * {@link java.util.Properties} is used (see {@link JupIOFactory}).
  1016.      * </p>
  1017.      *
  1018.      * @param str the {@code String} to unescape, may be null
  1019.      * @param jupCompatible whether unescaping is compatible with {@link java.util.Properties}; otherwise the classic
  1020.      *        behavior is used
  1021.      * @return the processed string
  1022.      * @throws IllegalArgumentException if the Writer is {@code null}
  1023.      */
  1024.     protected static String unescapeJava(final String str, final boolean jupCompatible) {
  1025.         if (str == null) {
  1026.             return null;
  1027.         }
  1028.         final int sz = str.length();
  1029.         final StringBuilder out = new StringBuilder(sz);
  1030.         final StringBuilder unicode = new StringBuilder(UNICODE_LEN);
  1031.         boolean hadSlash = false;
  1032.         boolean inUnicode = false;
  1033.         for (int i = 0; i < sz; i++) {
  1034.             final char ch = str.charAt(i);
  1035.             if (inUnicode) {
  1036.                 // if in unicode, then we're reading unicode
  1037.                 // values in somehow
  1038.                 unicode.append(ch);
  1039.                 if (unicode.length() == UNICODE_LEN) {
  1040.                     // unicode now contains the four hex digits
  1041.                     // which represents our unicode character
  1042.                     try {
  1043.                         final int value = Integer.parseInt(unicode.toString(), HEX_RADIX);
  1044.                         out.append((char) value);
  1045.                         unicode.setLength(0);
  1046.                         inUnicode = false;
  1047.                         hadSlash = false;
  1048.                     } catch (final NumberFormatException nfe) {
  1049.                         throw new ConfigurationRuntimeException("Unable to parse unicode value: " + unicode, nfe);
  1050.                     }
  1051.                 }
  1052.                 continue;
  1053.             }

  1054.             if (hadSlash) {
  1055.                 // handle an escaped value
  1056.                 hadSlash = false;

  1057.                 switch (ch) {
  1058.                 case 'r':
  1059.                     out.append('\r');
  1060.                     break;
  1061.                 case 'f':
  1062.                     out.append('\f');
  1063.                     break;
  1064.                 case 't':
  1065.                     out.append('\t');
  1066.                     break;
  1067.                 case 'n':
  1068.                     out.append('\n');
  1069.                     break;
  1070.                 default:
  1071.                     if (!jupCompatible && ch == 'b') {
  1072.                         out.append('\b');
  1073.                     } else if (ch == 'u') {
  1074.                         // uh-oh, we're in unicode country....
  1075.                         inUnicode = true;
  1076.                     } else {
  1077.                         // JUP simply throws away the \ of unknown escape sequences
  1078.                         if (!needsUnescape(ch) && !jupCompatible) {
  1079.                             out.append('\\');
  1080.                         }
  1081.                         out.append(ch);
  1082.                     }
  1083.                     break;
  1084.                 }

  1085.                 continue;
  1086.             }
  1087.             if (ch == '\\') {
  1088.                 hadSlash = true;
  1089.                 continue;
  1090.             }
  1091.             out.append(ch);
  1092.         }

  1093.         if (hadSlash) {
  1094.             // then we're in the weird case of a \ at the end of the
  1095.             // string, let's output it anyway.
  1096.             out.append('\\');
  1097.         }

  1098.         return out.toString();
  1099.     }

  1100.     /** Stores the layout object. */
  1101.     private PropertiesConfigurationLayout layout;

  1102.     /** The include listener for the special {@code "include"} key. */
  1103.     private ConfigurationConsumer<ConfigurationException> includeListener;

  1104.     /** The IOFactory for creating readers and writers. */
  1105.     private IOFactory ioFactory;

  1106.     /** The current {@code FileLocator}. */
  1107.     private FileLocator locator;

  1108.     /** Allow file inclusion or not */
  1109.     private boolean includesAllowed = true;

  1110.     /**
  1111.      * Creates an empty PropertyConfiguration object which can be used to synthesize a new Properties file by adding values
  1112.      * and then saving().
  1113.      */
  1114.     public PropertiesConfiguration() {
  1115.         installLayout(createLayout());
  1116.     }

  1117.     /**
  1118.      * Creates a copy of this object.
  1119.      *
  1120.      * @return the copy
  1121.      */
  1122.     @Override
  1123.     public Object clone() {
  1124.         final PropertiesConfiguration copy = (PropertiesConfiguration) super.clone();
  1125.         if (layout != null) {
  1126.             copy.setLayout(new PropertiesConfigurationLayout(layout));
  1127.         }
  1128.         return copy;
  1129.     }

  1130.     /**
  1131.      * Creates a standard layout object. This configuration is initialized with such a standard layout.
  1132.      *
  1133.      * @return the newly created layout object
  1134.      */
  1135.     private PropertiesConfigurationLayout createLayout() {
  1136.         return new PropertiesConfigurationLayout();
  1137.     }

  1138.     /**
  1139.      * Gets the footer comment. This is a comment at the very end of the file.
  1140.      *
  1141.      * @return the footer comment
  1142.      * @since 2.0
  1143.      */
  1144.     public String getFooter() {
  1145.         return syncRead(() -> getLayout().getFooterComment(), false);
  1146.     }

  1147.     /**
  1148.      * Gets the comment header.
  1149.      *
  1150.      * @return the comment header
  1151.      * @since 1.1
  1152.      */
  1153.     public String getHeader() {
  1154.         return syncRead(() -> getLayout().getHeaderComment(), false);
  1155.     }

  1156.     /**
  1157.      * Gets the current include listener, never null.
  1158.      *
  1159.      * @return the current include listener, never null.
  1160.      * @since 2.6
  1161.      */
  1162.     public ConfigurationConsumer<ConfigurationException> getIncludeListener() {
  1163.         return includeListener != null ? includeListener : DEFAULT_INCLUDE_LISTENER;
  1164.     }

  1165.     /**
  1166.      * Gets the {@code IOFactory} to be used for creating readers and writers when loading or saving this configuration.
  1167.      *
  1168.      * @return the {@code IOFactory}
  1169.      * @since 1.7
  1170.      */
  1171.     public IOFactory getIOFactory() {
  1172.         return ioFactory != null ? ioFactory : DefaultIOFactory.INSTANCE;
  1173.     }

  1174.     /**
  1175.      * Gets the associated layout object.
  1176.      *
  1177.      * @return the associated layout object
  1178.      * @since 1.3
  1179.      */
  1180.     public PropertiesConfigurationLayout getLayout() {
  1181.         return layout;
  1182.     }

  1183.     /**
  1184.      * Stores the current {@code FileLocator} for a following IO operation. The {@code FileLocator} is needed to resolve
  1185.      * include files with relative file names.
  1186.      *
  1187.      * @param locator the current {@code FileLocator}
  1188.      * @since 2.0
  1189.      */
  1190.     @Override
  1191.     public void initFileLocator(final FileLocator locator) {
  1192.         this.locator = locator;
  1193.     }

  1194.     /**
  1195.      * Installs a layout object. It has to be ensured that the layout is registered as change listener at this
  1196.      * configuration. If there is already a layout object installed, it has to be removed properly.
  1197.      *
  1198.      * @param layout the layout object to be installed
  1199.      */
  1200.     private void installLayout(final PropertiesConfigurationLayout layout) {
  1201.         // only one layout must exist
  1202.         if (this.layout != null) {
  1203.             removeEventListener(ConfigurationEvent.ANY, this.layout);
  1204.         }

  1205.         if (layout == null) {
  1206.             this.layout = createLayout();
  1207.         } else {
  1208.             this.layout = layout;
  1209.         }
  1210.         addEventListener(ConfigurationEvent.ANY, this.layout);
  1211.     }

  1212.     /**
  1213.      * Reports the status of file inclusion.
  1214.      *
  1215.      * @return True if include files are loaded.
  1216.      */
  1217.     public boolean isIncludesAllowed() {
  1218.         return this.includesAllowed;
  1219.     }

  1220.     /**
  1221.      * Helper method for loading an included properties file. This method is called by {@code load()} when an
  1222.      * {@code include} property is encountered. It tries to resolve relative file names based on the current base path. If
  1223.      * this fails, a resolution based on the location of this properties file is tried.
  1224.      *
  1225.      * @param fileName the name of the file to load
  1226.      * @param optional whether or not the {@code fileName} is optional
  1227.      * @param seenStack Stack of seen include URLs
  1228.      * @throws ConfigurationException if loading fails
  1229.      */
  1230.     private void loadIncludeFile(final String fileName, final boolean optional, final Deque<URL> seenStack) throws ConfigurationException {
  1231.         if (locator == null) {
  1232.             throw new ConfigurationException(
  1233.                 "Load operation not properly " + "initialized! Do not call read(InputStream) directly," + " but use a FileHandler to load a configuration.");
  1234.         }

  1235.         URL url = locateIncludeFile(locator.getBasePath(), fileName);
  1236.         if (url == null) {
  1237.             final URL baseURL = locator.getSourceURL();
  1238.             if (baseURL != null) {
  1239.                 url = locateIncludeFile(baseURL.toString(), fileName);
  1240.             }
  1241.         }

  1242.         if (optional && url == null) {
  1243.             return;
  1244.         }

  1245.         if (url == null) {
  1246.             getIncludeListener().accept(new ConfigurationException("Cannot resolve include file " + fileName, new FileNotFoundException(fileName)));
  1247.         } else {
  1248.             final FileHandler fh = new FileHandler(this);
  1249.             fh.setFileLocator(locator);
  1250.             final FileLocator orgLocator = locator;
  1251.             try {
  1252.                 try {
  1253.                     // Check for cycles
  1254.                     if (seenStack.contains(url)) {
  1255.                         throw new ConfigurationException(String.format("Cycle detected loading %s, seen stack: %s", url, seenStack));
  1256.                     }
  1257.                     seenStack.add(url);
  1258.                     try {
  1259.                         fh.load(url);
  1260.                     } finally {
  1261.                         seenStack.pop();
  1262.                     }
  1263.                 } catch (final ConfigurationException e) {
  1264.                     getIncludeListener().accept(e);
  1265.                 }
  1266.             } finally {
  1267.                 locator = orgLocator; // reset locator which is changed by load
  1268.             }
  1269.         }
  1270.     }

  1271.     /**
  1272.      * Tries to obtain the URL of an include file using the specified (optional) base path and file name.
  1273.      *
  1274.      * @param basePath the base path
  1275.      * @param fileName the file name
  1276.      * @return the URL of the include file or <strong>null</strong> if it cannot be resolved
  1277.      */
  1278.     private URL locateIncludeFile(final String basePath, final String fileName) {
  1279.         final FileLocator includeLocator = FileLocatorUtils.fileLocator(locator).sourceURL(null).basePath(basePath).fileName(fileName).create();
  1280.         return FileLocatorUtils.locate(includeLocator);
  1281.     }

  1282.     /**
  1283.      * This method is invoked by the associated {@link PropertiesConfigurationLayout} object for each property definition
  1284.      * detected in the parsed properties file. Its task is to check whether this is a special property definition (for example the
  1285.      * {@code include} property). If not, the property must be added to this configuration. The return value indicates
  1286.      * whether the property should be treated as a normal property. If it is <strong>false</strong>, the layout object will ignore
  1287.      * this property.
  1288.      *
  1289.      * @param key the property key
  1290.      * @param value the property value
  1291.      * @param seenStack the stack of seen include URLs
  1292.      * @return a flag whether this is a normal property
  1293.      * @throws ConfigurationException if an error occurs
  1294.      * @since 1.3
  1295.      */
  1296.     boolean propertyLoaded(final String key, final String value, final Deque<URL> seenStack) throws ConfigurationException {
  1297.         final boolean result;

  1298.         if (StringUtils.isNotEmpty(getInclude()) && key.equalsIgnoreCase(getInclude())) {
  1299.             if (isIncludesAllowed()) {
  1300.                 final Collection<String> files = getListDelimiterHandler().split(value, true);
  1301.                 for (final String f : files) {
  1302.                     loadIncludeFile(interpolate(f), false, seenStack);
  1303.                 }
  1304.             }
  1305.             result = false;
  1306.         } else if (StringUtils.isNotEmpty(getIncludeOptional()) && key.equalsIgnoreCase(getIncludeOptional())) {
  1307.             if (isIncludesAllowed()) {
  1308.                 final Collection<String> files = getListDelimiterHandler().split(value, true);
  1309.                 for (final String f : files) {
  1310.                     loadIncludeFile(interpolate(f), true, seenStack);
  1311.                 }
  1312.             }
  1313.             result = false;
  1314.         } else {
  1315.             addPropertyInternal(key, value);
  1316.             result = true;
  1317.         }

  1318.         return result;
  1319.     }

  1320.     /**
  1321.      * {@inheritDoc} This implementation delegates to the associated layout object which does the actual loading. Note that
  1322.      * this method does not do any synchronization. This lies in the responsibility of the caller. (Typically, the caller is
  1323.      * a {@code FileHandler} object which takes care for proper synchronization.)
  1324.      *
  1325.      * @since 2.0
  1326.      */
  1327.     @Override
  1328.     public void read(final Reader in) throws ConfigurationException, IOException {
  1329.         getLayout().load(this, in);
  1330.     }

  1331.     /**
  1332.      * Sets the footer comment. If set, this comment is written after all properties at the end of the file.
  1333.      *
  1334.      * @param footer the footer comment
  1335.      * @since 2.0
  1336.      */
  1337.     public void setFooter(final String footer) {
  1338.         syncWrite(() -> getLayout().setFooterComment(footer), false);
  1339.     }

  1340.     /**
  1341.      * Sets the comment header.
  1342.      *
  1343.      * @param header the header to use
  1344.      * @since 1.1
  1345.      */
  1346.     public void setHeader(final String header) {
  1347.         syncWrite(() -> getLayout().setHeaderComment(header), false);
  1348.     }

  1349.     /**
  1350.      * Sets the current include listener, may not be null.
  1351.      *
  1352.      * @param includeListener the current include listener, may not be null.
  1353.      * @throws IllegalArgumentException if the {@code includeListener} is null.
  1354.      * @since 2.6
  1355.      */
  1356.     public void setIncludeListener(final ConfigurationConsumer<ConfigurationException> includeListener) {
  1357.         if (includeListener == null) {
  1358.             throw new IllegalArgumentException("includeListener must not be null.");
  1359.         }
  1360.         this.includeListener = includeListener;
  1361.     }

  1362.     /**
  1363.      * Controls whether additional files can be loaded by the {@code include = <xxx>} statement or not. This is <strong>true</strong>
  1364.      * per default.
  1365.      *
  1366.      * @param includesAllowed True if Includes are allowed.
  1367.      */
  1368.     public void setIncludesAllowed(final boolean includesAllowed) {
  1369.         this.includesAllowed = includesAllowed;
  1370.     }

  1371.     /**
  1372.      * Sets the {@code IOFactory} to be used for creating readers and writers when loading or saving this configuration.
  1373.      * Using this method a client can customize the reader and writer classes used by the load and save operations. Note
  1374.      * that this method must be called before invoking one of the {@code load()} and {@code save()} methods. Especially, if
  1375.      * you want to use a custom {@code IOFactory} for changing the {@code PropertiesReader}, you cannot load the
  1376.      * configuration data in the constructor.
  1377.      *
  1378.      * @param ioFactory the new {@code IOFactory} (must not be <strong>null</strong>)
  1379.      * @throws IllegalArgumentException if the {@code IOFactory} is <strong>null</strong>
  1380.      * @since 1.7
  1381.      */
  1382.     public void setIOFactory(final IOFactory ioFactory) {
  1383.         if (ioFactory == null) {
  1384.             throw new IllegalArgumentException("IOFactory must not be null.");
  1385.         }

  1386.         this.ioFactory = ioFactory;
  1387.     }

  1388.     /**
  1389.      * Sets the associated layout object.
  1390.      *
  1391.      * @param layout the new layout object; can be <strong>null</strong>, then a new layout object will be created
  1392.      * @since 1.3
  1393.      */
  1394.     public void setLayout(final PropertiesConfigurationLayout layout) {
  1395.         installLayout(layout);
  1396.     }

  1397.     /**
  1398.      * {@inheritDoc} This implementation delegates to the associated layout object which does the actual saving. Note that,
  1399.      * analogous to {@link #read(Reader)}, this method does not do any synchronization.
  1400.      *
  1401.      * @since 2.0
  1402.      */
  1403.     @Override
  1404.     public void write(final Writer out) throws ConfigurationException, IOException {
  1405.         getLayout().save(this, out);
  1406.     }

  1407. }