public class DefaultListDelimiterHandler extends AbstractListDelimiterHandler
The default implementation of the ListDelimiterHandler
interface.
This class supports list splitting and delimiter escaping using a delimiter character that can be specified when
constructing an instance. Splitting of strings works by scanning the input for the list delimiter character. The list
delimiter character can be escaped by a backslash. So, provided that a comma is configured as list delimiter, in the
example val1,val2,val3
three values are recognized. In 3\,1415
the list delimiter is escaped so that
only a single element is detected. (Note that when writing these examples in Java code, each backslash has to be
doubled. This is also true for all other examples in this documentation.)
Because the backslash has a special meaning as escaping character it is always treated in a special way. If it occurs
as a normal character in a property value, it has to be escaped using another backslash (similar to the rules of the
Java programming language). The following example shows the correct way to define windows network shares:
\\\\Server\\path
. Note that each backslash is doubled. When combining the list delimiter with backslashes the
same escaping rules apply. For instance, in C:\\Temp\\,D:\\data\\
the list delimiter is recognized; it is not
escaped by the preceding backslash because this backslash is itself escaped. In contrast,
C:\\Temp\\\,D:\\data\\
defines a single element with a comma being part of the value; two backslashes after
Temp
result in a single one, the third backslash escapes the list delimiter.
As can be seen, there are some constellations which are a bit tricky and cause a larger number of backslashes in sequence. Nevertheless, the escaping rules are consistent and do not cause ambiguous results.
Implementation node: An instance of this class can safely be shared between multiple Configuration
instances.
NOOP_TRANSFORMER
Constructor and Description |
---|
DefaultListDelimiterHandler(char listDelimiter)
Creates a new instance of
DefaultListDelimiterHandler and sets the list delimiter character. |
Modifier and Type | Method and Description |
---|---|
Object |
escapeList(List<?> values,
ValueTransformer transformer)
Escapes all values in the given list and concatenates them to a single string.
|
protected String |
escapeString(String s)
Escapes the specified string.
|
char |
getDelimiter()
Returns the list delimiter character used by this instance.
|
protected Collection<String> |
splitString(String s,
boolean trim)
Actually splits the passed in string which is guaranteed to be not null.
|
escape, parse, split
public DefaultListDelimiterHandler(char listDelimiter)
DefaultListDelimiterHandler
and sets the list delimiter character.listDelimiter
- the list delimiter characterpublic char getDelimiter()
public Object escapeList(List<?> values, ValueTransformer transformer)
ListDelimiterHandler
values
- the list with all the values to be converted to a single valuetransformer
- a ValueTransformer
for an additional encoding (must not be null)protected String escapeString(String s)
AbstractListDelimiterHandler
escape()
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.escapeString
in class AbstractListDelimiterHandler
s
- the string to be escaped (not null)protected Collection<String> splitString(String s, boolean trim)
split()
method. Here the actual splitting logic has to be implemented. This implementation reverses the escaping done by the escape()
methods of this class. However,
it tries to be tolerant with unexpected escaping sequences: If after the escape character "\" no allowed character
follows, both the backslash and the following character are output.splitString
in class AbstractListDelimiterHandler
s
- the string to be split (not null)trim
- a flag whether the single components have to be trimmedCopyright © 2001–2022 The Apache Software Foundation. All rights reserved.