Class LegacyListDelimiterHandler
- java.lang.Object
-
- org.apache.commons.configuration2.convert.AbstractListDelimiterHandler
-
- org.apache.commons.configuration2.convert.LegacyListDelimiterHandler
-
- All Implemented Interfaces:
ListDelimiterHandler
public class LegacyListDelimiterHandler extends AbstractListDelimiterHandler
A specialized implementation of
ListDelimiterHandler
which simulates the list delimiter handling as it was used byPropertiesConfiguration
in Commons Configuration 1.x.This class mainly exists for compatibility reasons. It is intended to be used by applications which have to deal with properties files created by an older version of this library.
In the 1.x series of Commons Configuration list handling was not fully consistent. The escaping of property values was done in a different way if they contained a list delimiter or not. From version 2.0 on, escaping is more stringent which might cause slightly different results when parsing properties files created by or for Configuration 1.x. If you encounter such problems, you can switch to this
ListDelimiterHandler
implementation rather than the default one. In other cases, this class should not be used!Implementation note: An instance of this class can safely be shared between multiple
Configuration
instances.- Since:
- 2.0
-
-
Field Summary
Fields Modifier and Type Field Description private char
delimiter
The list delimiter character.private static java.lang.String
DOUBLE_ESC
Constant for the escaped escaping character.private static java.lang.String
ESCAPE
Constant for the escaping character.private static java.lang.String
QUAD_ESC
Constant for a duplicated sequence of escaping characters.-
Fields inherited from interface org.apache.commons.configuration2.convert.ListDelimiterHandler
NOOP_TRANSFORMER
-
-
Constructor Summary
Constructors Constructor Description LegacyListDelimiterHandler(char listDelimiter)
Creates a new instance ofLegacyListDelimiterHandler
and sets the list delimiter character.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static int
countTrailingBS(java.lang.String line)
Returns the number of trailing backslashes.java.lang.Object
escape(java.lang.Object value, ValueTransformer transformer)
Escapes the specified single value object.protected java.lang.String
escapeBackslashs(java.lang.Object value, boolean inList)
Performs the escaping of backslashes in the specified properties value.java.lang.Object
escapeList(java.util.List<?> values, ValueTransformer transformer)
Escapes all values in the given list and concatenates them to a single string.protected java.lang.String
escapeString(java.lang.String s)
Escapes the specified string.protected java.lang.String
escapeValue(java.lang.Object value, boolean inList, ValueTransformer transformer)
Escapes the given property value.char
getDelimiter()
Gets the list delimiter character.protected java.util.Collection<java.lang.String>
splitString(java.lang.String s, boolean trim)
Actually splits the passed in string which is guaranteed to be not null.-
Methods inherited from class org.apache.commons.configuration2.convert.AbstractListDelimiterHandler
flatten, flattenIterator, parse, split
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.apache.commons.configuration2.convert.ListDelimiterHandler
flatten
-
-
-
-
Field Detail
-
ESCAPE
private static final java.lang.String ESCAPE
Constant for the escaping character.- See Also:
- Constant Field Values
-
DOUBLE_ESC
private static final java.lang.String DOUBLE_ESC
Constant for the escaped escaping character.- See Also:
- Constant Field Values
-
QUAD_ESC
private static final java.lang.String QUAD_ESC
Constant for a duplicated sequence of escaping characters.- See Also:
- Constant Field Values
-
delimiter
private final char delimiter
The list delimiter character.
-
-
Method Detail
-
countTrailingBS
private static int countTrailingBS(java.lang.String line)
Returns the number of trailing backslashes. This is sometimes needed for the correct handling of escape characters.- Parameters:
line
- the string to investigate- Returns:
- the number of trailing backslashes
-
escape
public java.lang.Object escape(java.lang.Object value, ValueTransformer transformer)
Escapes the specified single value object. This method is called for properties containing only a single value. So this method can rely on the fact that the passed in object is not a list. An implementation has to check whether the value contains list delimiter characters and - if so - escape them accordingly. This implementation checks whether the object to be escaped is a string. If yes, it delegates toAbstractListDelimiterHandler.escapeString(String)
, otherwise no escaping is performed. Eventually, the passed in transformer is invoked so that additional encoding can be performed. This implementation performs delimiter escaping for a single value (which is not part of a list).- Specified by:
escape
in interfaceListDelimiterHandler
- Overrides:
escape
in classAbstractListDelimiterHandler
- Parameters:
value
- the value to be escapedtransformer
- aValueTransformer
for an additional encoding (must not be null)- Returns:
- the escaped value
-
escapeBackslashs
protected java.lang.String escapeBackslashs(java.lang.Object value, boolean inList)
Performs the escaping of backslashes in the specified properties value. Because a double backslash is used to escape the escape character of a list delimiter, double backslashes also have to be escaped if the property is part of a (single line) list. In addition, because the output is written into a properties file, each occurrence of a backslash again has to be doubled. This method is called byescapeValue()
.- Parameters:
value
- the value to be escapedinList
- a flag whether the value is part of a list- Returns:
- the value with escaped backslashes as string
-
escapeList
public java.lang.Object escapeList(java.util.List<?> values, ValueTransformer transformer)
Escapes all values in the given list and concatenates them to a single string. This operation is required by configurations that have to represent properties with multiple values in a single line in their external configuration representation. This may require an advanced escaping in some cases. This implementation performs a special encoding of backslashes at the end of a string so that they are not interpreted as escape character for a following list delimiter.- Parameters:
values
- the list with all the values to be converted to a single valuetransformer
- aValueTransformer
for an additional encoding (must not be null)- Returns:
- the resulting escaped value
-
escapeString
protected java.lang.String escapeString(java.lang.String s)
Escapes the specified string. This method is called byescape()
if the passed in object is a string. Concrete subclasses have to implement their specific escaping logic here, so that the list delimiters they support are properly escaped. This is just a dummy implementation. It is never called.- Specified by:
escapeString
in classAbstractListDelimiterHandler
- Parameters:
s
- the string to be escaped (not null)- Returns:
- the escaped string
-
escapeValue
protected java.lang.String escapeValue(java.lang.Object value, boolean inList, ValueTransformer transformer)
Escapes the given property value. This method is called on saving the configuration for each property value. It ensures a correct handling of backslash characters and also takes care that list delimiter characters in the value are escaped.- Parameters:
value
- the property valueinList
- a flag whether the value is part of a listtransformer
- theValueTransformer
- Returns:
- the escaped property value
-
getDelimiter
public char getDelimiter()
Gets the list delimiter character.- Returns:
- the list delimiter character
-
splitString
protected java.util.Collection<java.lang.String> splitString(java.lang.String s, boolean trim)
Actually splits the passed in string which is guaranteed to be not null. This method is called by the base implementation of thesplit()
method. Here the actual splitting logic has to be implemented. This implementation simulates the old splitting algorithm. The string is split at the delimiter character if it is not escaped. If the delimiter character is not found, the input is returned unchanged.- Specified by:
splitString
in classAbstractListDelimiterHandler
- Parameters:
s
- the string to be split (not null)trim
- a flag whether the single components have to be trimmed- Returns:
- a collection with the extracted components of the passed in string
-
-