Class DefaultConfigurationKey
- java.lang.Object
-
- org.apache.commons.configuration2.tree.DefaultConfigurationKey
-
public class DefaultConfigurationKey extends java.lang.Object
A simple class that supports creation of and iteration on configuration keys supported by a
DefaultExpressionEngine
object.For key creation the class works similar to a StringBuffer: There are several
appendXXXX()
methods with which single parts of a key can be constructed. All these methods return a reference to the actual object so they can be written in a chain. When using this methods the exact syntax for keys need not be known.This class also defines a specialized iterator for configuration keys. With such an iterator a key can be tokenized into its single parts. For each part it can be checked whether it has an associated index.
Instances of this class are always associated with an instance of
DefaultExpressionEngine
, from which the current delimiters are obtained. So key creation and parsing is specific to this associated expression engine.- Since:
- 1.3
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
DefaultConfigurationKey.KeyIterator
A specialized iterator class for tokenizing a configuration key.
-
Field Summary
Fields Modifier and Type Field Description private DefaultExpressionEngine
expressionEngine
Stores a reference to the associated expression engine.private static int
INITIAL_SIZE
Constant for the initial StringBuffer size.private java.lang.StringBuilder
keyBuffer
Holds a buffer with the so far created key.
-
Constructor Summary
Constructors Constructor Description DefaultConfigurationKey(DefaultExpressionEngine engine)
Creates a new instance ofDefaultConfigurationKey
and sets the associated expression engine.DefaultConfigurationKey(DefaultExpressionEngine engine, java.lang.String key)
Creates a new instance ofDefaultConfigurationKey
and sets the associated expression engine and an initial key.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description DefaultConfigurationKey
append(java.lang.String property)
Appends the name of a property to this key.DefaultConfigurationKey
append(java.lang.String property, boolean escape)
Appends the name of a property to this key.DefaultConfigurationKey
appendAttribute(java.lang.String attr)
Appends an attribute to this configuration key.DefaultConfigurationKey
appendIndex(int index)
Appends an index to this configuration key.java.lang.String
attributeName(java.lang.String key)
Extracts the name of the attribute from the given attribute key.DefaultConfigurationKey
commonKey(DefaultConfigurationKey other)
Returns a configuration key object that is initialized with the part of the key that is common to this key and the passed in key.java.lang.String
constructAttributeKey(java.lang.String key)
Decorates the given key so that it represents an attribute.DefaultConfigurationKey
differenceKey(DefaultConfigurationKey other)
Returns the "difference key" to a given key.boolean
equals(java.lang.Object obj)
Checks if twoConfigurationKey
objects are equal.private java.lang.String
escapeDelimiters(java.lang.String key)
Escapes the delimiters in the specified string.DefaultExpressionEngine
getExpressionEngine()
Gets the associated default expression engine.private DefaultExpressionEngineSymbols
getSymbols()
Gets the symbols object from the associated expression engine.int
hashCode()
Returns the hash code for this object.private boolean
hasLeadingDelimiter(java.lang.String key)
Helper method that checks if the specified key starts with a property delimiter.private boolean
hasTrailingDelimiter(java.lang.String key)
Helper method that checks if the specified key ends with a property delimiter.boolean
isAttributeKey(java.lang.String key)
Tests if the specified key represents an attribute according to the current expression engine.DefaultConfigurationKey.KeyIterator
iterator()
Returns an iterator for iterating over the single components of this configuration key.int
length()
Returns the actual length of this configuration key.private static boolean
partsEqual(DefaultConfigurationKey.KeyIterator it1, DefaultConfigurationKey.KeyIterator it2)
Helper method for comparing two key parts.private java.lang.String
removeAttributeMarkers(java.lang.String key)
Helper method for removing attribute markers from a key.void
setLength(int len)
Sets the new length of this configuration key.java.lang.String
toString()
Returns a string representation of this object.java.lang.String
trim(java.lang.String key)
Removes delimiters at the beginning and the end of the specified key.java.lang.String
trimLeft(java.lang.String key)
Removes leading property delimiters from the specified key.java.lang.String
trimRight(java.lang.String key)
Removes trailing property delimiters from the specified key.private java.lang.String
unescapeDelimiters(java.lang.String key)
Unescapes the delimiters in the specified string.
-
-
-
Field Detail
-
INITIAL_SIZE
private static final int INITIAL_SIZE
Constant for the initial StringBuffer size.- See Also:
- Constant Field Values
-
expressionEngine
private final DefaultExpressionEngine expressionEngine
Stores a reference to the associated expression engine.
-
keyBuffer
private final java.lang.StringBuilder keyBuffer
Holds a buffer with the so far created key.
-
-
Constructor Detail
-
DefaultConfigurationKey
public DefaultConfigurationKey(DefaultExpressionEngine engine)
Creates a new instance ofDefaultConfigurationKey
and sets the associated expression engine.- Parameters:
engine
- the expression engine (must not be null)- Throws:
java.lang.IllegalArgumentException
- if the expression engine is null
-
DefaultConfigurationKey
public DefaultConfigurationKey(DefaultExpressionEngine engine, java.lang.String key)
Creates a new instance ofDefaultConfigurationKey
and sets the associated expression engine and an initial key.- Parameters:
engine
- the expression engine (must not be null)key
- the key to be wrapped- Throws:
java.lang.IllegalArgumentException
- if the expression engine is null
-
-
Method Detail
-
partsEqual
private static boolean partsEqual(DefaultConfigurationKey.KeyIterator it1, DefaultConfigurationKey.KeyIterator it2)
Helper method for comparing two key parts.- Parameters:
it1
- the iterator with the first partit2
- the iterator with the second part- Returns:
- a flag if both parts are equal
-
append
public DefaultConfigurationKey append(java.lang.String property)
Appends the name of a property to this key. If necessary, a property delimiter will be added. Property delimiters in the given string will not be escaped.- Parameters:
property
- the name of the property to be added- Returns:
- a reference to this object
-
append
public DefaultConfigurationKey append(java.lang.String property, boolean escape)
Appends the name of a property to this key. If necessary, a property delimiter will be added. If the boolean argument is set to true, property delimiters contained in the property name will be escaped.- Parameters:
property
- the name of the property to be addedescape
- a flag if property delimiters in the passed in property name should be escaped- Returns:
- a reference to this object
-
appendAttribute
public DefaultConfigurationKey appendAttribute(java.lang.String attr)
Appends an attribute to this configuration key.- Parameters:
attr
- the name of the attribute to be appended- Returns:
- a reference to this object
-
appendIndex
public DefaultConfigurationKey appendIndex(int index)
Appends an index to this configuration key.- Parameters:
index
- the index to be appended- Returns:
- a reference to this object
-
attributeName
public java.lang.String attributeName(java.lang.String key)
Extracts the name of the attribute from the given attribute key. This method removes the attribute markers - if any - from the specified key.- Parameters:
key
- the attribute key- Returns:
- the name of the corresponding attribute
-
commonKey
public DefaultConfigurationKey commonKey(DefaultConfigurationKey other)
Returns a configuration key object that is initialized with the part of the key that is common to this key and the passed in key.- Parameters:
other
- the other key- Returns:
- a key object with the common key part
-
constructAttributeKey
public java.lang.String constructAttributeKey(java.lang.String key)
Decorates the given key so that it represents an attribute. Adds special start and end markers. The passed in string will be modified only if does not already represent an attribute.- Parameters:
key
- the key to be decorated- Returns:
- the decorated attribute key
-
differenceKey
public DefaultConfigurationKey differenceKey(DefaultConfigurationKey other)
Returns the "difference key" to a given key. This value is the part of the passed in key that differs from this key. There is the following relation:other = key.commonKey(other) + key.differenceKey(other)
for an arbitrary configuration keykey
.- Parameters:
other
- the key for which the difference is to be calculated- Returns:
- the difference key
-
equals
public boolean equals(java.lang.Object obj)
Checks if twoConfigurationKey
objects are equal. Two instances of this class are considered equal if they have the same content (i.e. their internal string representation is equal). The expression engine property is not taken into account.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- the object to compare- Returns:
- a flag if both objects are equal
-
escapeDelimiters
private java.lang.String escapeDelimiters(java.lang.String key)
Escapes the delimiters in the specified string.- Parameters:
key
- the key to be escaped- Returns:
- the escaped key
-
getExpressionEngine
public DefaultExpressionEngine getExpressionEngine()
Gets the associated default expression engine.- Returns:
- the associated expression engine
-
getSymbols
private DefaultExpressionEngineSymbols getSymbols()
Gets the symbols object from the associated expression engine.- Returns:
- the
DefaultExpressionEngineSymbols
-
hashCode
public int hashCode()
Returns the hash code for this object.- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- the hash code
-
hasLeadingDelimiter
private boolean hasLeadingDelimiter(java.lang.String key)
Helper method that checks if the specified key starts with a property delimiter.- Parameters:
key
- the key to check- Returns:
- a flag if there is a leading delimiter
-
hasTrailingDelimiter
private boolean hasTrailingDelimiter(java.lang.String key)
Helper method that checks if the specified key ends with a property delimiter.- Parameters:
key
- the key to check- Returns:
- a flag if there is a trailing delimiter
-
isAttributeKey
public boolean isAttributeKey(java.lang.String key)
Tests if the specified key represents an attribute according to the current expression engine.- Parameters:
key
- the key to be checked- Returns:
- true if this is an attribute key, false otherwise
-
iterator
public DefaultConfigurationKey.KeyIterator iterator()
Returns an iterator for iterating over the single components of this configuration key.- Returns:
- an iterator for this key
-
length
public int length()
Returns the actual length of this configuration key.- Returns:
- the length of this key
-
removeAttributeMarkers
private java.lang.String removeAttributeMarkers(java.lang.String key)
Helper method for removing attribute markers from a key.- Parameters:
key
- the key- Returns:
- the key with removed attribute markers
-
setLength
public void setLength(int len)
Sets the new length of this configuration key. With this method it is possible to truncate the key, e.g. to return to a state prior calling someappend()
methods. The semantic is the same as thesetLength()
method ofStringBuilder
.- Parameters:
len
- the new length of the key
-
toString
public java.lang.String toString()
Returns a string representation of this object. This is the configuration key as a plain string.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a string for this object
-
trim
public java.lang.String trim(java.lang.String key)
Removes delimiters at the beginning and the end of the specified key.- Parameters:
key
- the key- Returns:
- the key with removed property delimiters
-
trimLeft
public java.lang.String trimLeft(java.lang.String key)
Removes leading property delimiters from the specified key.- Parameters:
key
- the key- Returns:
- the key with removed leading property delimiters
-
trimRight
public java.lang.String trimRight(java.lang.String key)
Removes trailing property delimiters from the specified key.- Parameters:
key
- the key- Returns:
- the key with removed trailing property delimiters
-
unescapeDelimiters
private java.lang.String unescapeDelimiters(java.lang.String key)
Unescapes the delimiters in the specified string.- Parameters:
key
- the key to be unescaped- Returns:
- the unescaped key
-
-