org.apache.commons.configuration.tree.DefaultConfigurationKey Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.configuration.tree;
import java.util.Iterator;
import java.util.NoSuchElementException;
import org.apache.commons.lang.StringUtils;
/**
*
* A simple class that supports creation of and iteration on configuration keys
* supported by a {@link DefaultExpressionEngine} object.
*
*
* For key creation the class works similar to a StringBuffer: There are several
* {@code 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
* {@link DefaultExpressionEngine}, from which the current
* delimiters are obtained. So key creation and parsing is specific to this
* associated expression engine.
*
*
* @since 1.3
* @author Commons
* Configuration team
* @version $Id: DefaultConfigurationKey.java 1231724 2012-01-15 18:40:31Z oheger $
*/
public class DefaultConfigurationKey
{
/** Constant for the initial StringBuffer size. */
private static final int INITIAL_SIZE = 32;
/** Stores a reference to the associated expression engine. */
private DefaultExpressionEngine expressionEngine;
/** Holds a buffer with the so far created key. */
private StringBuilder keyBuffer;
/**
* Creates a new instance of {@code DefaultConfigurationKey} and sets
* the associated expression engine.
*
* @param engine the expression engine
*/
public DefaultConfigurationKey(DefaultExpressionEngine engine)
{
keyBuffer = new StringBuilder(INITIAL_SIZE);
setExpressionEngine(engine);
}
/**
* Creates a new instance of {@code DefaultConfigurationKey} and sets
* the associated expression engine and an initial key.
*
* @param engine the expression engine
* @param key the key to be wrapped
*/
public DefaultConfigurationKey(DefaultExpressionEngine engine, String key)
{
setExpressionEngine(engine);
keyBuffer = new StringBuilder(trim(key));
}
/**
* Returns the associated default expression engine.
*
* @return the associated expression engine
*/
public DefaultExpressionEngine getExpressionEngine()
{
return expressionEngine;
}
/**
* Sets the associated expression engine.
*
* @param expressionEngine the expression engine (must not be null)
*/
public void setExpressionEngine(DefaultExpressionEngine expressionEngine)
{
if (expressionEngine == null)
{
throw new IllegalArgumentException(
"Expression engine must not be null!");
}
this.expressionEngine = expressionEngine;
}
/**
* 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.
*
* @param property the name of the property to be added
* @param escape a flag if property delimiters in the passed in property name
* should be escaped
* @return a reference to this object
*/
public DefaultConfigurationKey append(String property, boolean escape)
{
String key;
if (escape && property != null)
{
key = escapeDelimiters(property);
}
else
{
key = property;
}
key = trim(key);
if (keyBuffer.length() > 0 && !isAttributeKey(property)
&& key.length() > 0)
{
keyBuffer.append(getExpressionEngine().getPropertyDelimiter());
}
keyBuffer.append(key);
return this;
}
/**
* 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.
*
* @param property the name of the property to be added
* @return a reference to this object
*/
public DefaultConfigurationKey append(String property)
{
return append(property, false);
}
/**
* Appends an index to this configuration key.
*
* @param index the index to be appended
* @return a reference to this object
*/
public DefaultConfigurationKey appendIndex(int index)
{
keyBuffer.append(getExpressionEngine().getIndexStart());
keyBuffer.append(index);
keyBuffer.append(getExpressionEngine().getIndexEnd());
return this;
}
/**
* Appends an attribute to this configuration key.
*
* @param attr the name of the attribute to be appended
* @return a reference to this object
*/
public DefaultConfigurationKey appendAttribute(String attr)
{
keyBuffer.append(constructAttributeKey(attr));
return this;
}
/**
* Returns the actual length of this configuration key.
*
* @return the length of this key
*/
public int length()
{
return keyBuffer.length();
}
/**
* 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
* some {@code append()} methods. The semantic is the same as the
* {@code setLength()} method of {@code StringBuilder}.
*
* @param len the new length of the key
*/
public void setLength(int len)
{
keyBuffer.setLength(len);
}
/**
* Checks if two {@code ConfigurationKey} objects are equal. The
* method can be called with strings or other objects, too.
*
* @param c the object to compare
* @return a flag if both objects are equal
*/
@Override
public boolean equals(Object c)
{
if (c == null)
{
return false;
}
return keyBuffer.toString().equals(c.toString());
}
/**
* Returns the hash code for this object.
*
* @return the hash code
*/
@Override
public int hashCode()
{
return String.valueOf(keyBuffer).hashCode();
}
/**
* Returns a string representation of this object. This is the configuration
* key as a plain string.
*
* @return a string for this object
*/
@Override
public String toString()
{
return keyBuffer.toString();
}
/**
* Tests if the specified key represents an attribute according to the
* current expression engine.
*
* @param key the key to be checked
* @return true if this is an attribute key, false otherwise
*/
public boolean isAttributeKey(String key)
{
if (key == null)
{
return false;
}
return key.startsWith(getExpressionEngine().getAttributeStart())
&& (getExpressionEngine().getAttributeEnd() == null || key
.endsWith(getExpressionEngine().getAttributeEnd()));
}
/**
* 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.
*
* @param key the key to be decorated
* @return the decorated attribute key
*/
public String constructAttributeKey(String key)
{
if (key == null)
{
return StringUtils.EMPTY;
}
if (isAttributeKey(key))
{
return key;
}
else
{
StringBuilder buf = new StringBuilder();
buf.append(getExpressionEngine().getAttributeStart()).append(key);
if (getExpressionEngine().getAttributeEnd() != null)
{
buf.append(getExpressionEngine().getAttributeEnd());
}
return buf.toString();
}
}
/**
* Extracts the name of the attribute from the given attribute key. This
* method removes the attribute markers - if any - from the specified key.
*
* @param key the attribute key
* @return the name of the corresponding attribute
*/
public String attributeName(String key)
{
return isAttributeKey(key) ? removeAttributeMarkers(key) : key;
}
/**
* Removes leading property delimiters from the specified key.
*
* @param key the key
* @return the key with removed leading property delimiters
*/
public String trimLeft(String key)
{
if (key == null)
{
return StringUtils.EMPTY;
}
else
{
String result = key;
while (hasLeadingDelimiter(result))
{
result = result.substring(getExpressionEngine()
.getPropertyDelimiter().length());
}
return result;
}
}
/**
* Removes trailing property delimiters from the specified key.
*
* @param key the key
* @return the key with removed trailing property delimiters
*/
public String trimRight(String key)
{
if (key == null)
{
return StringUtils.EMPTY;
}
else
{
String result = key;
while (hasTrailingDelimiter(result))
{
result = result
.substring(0, result.length()
- getExpressionEngine().getPropertyDelimiter()
.length());
}
return result;
}
}
/**
* Removes delimiters at the beginning and the end of the specified key.
*
* @param key the key
* @return the key with removed property delimiters
*/
public String trim(String key)
{
return trimRight(trimLeft(key));
}
/**
* Returns an iterator for iterating over the single components of this
* configuration key.
*
* @return an iterator for this key
*/
public KeyIterator iterator()
{
return new KeyIterator();
}
/**
* Helper method that checks if the specified key ends with a property
* delimiter.
*
* @param key the key to check
* @return a flag if there is a trailing delimiter
*/
private boolean hasTrailingDelimiter(String key)
{
return key.endsWith(getExpressionEngine().getPropertyDelimiter())
&& (getExpressionEngine().getEscapedDelimiter() == null || !key
.endsWith(getExpressionEngine().getEscapedDelimiter()));
}
/**
* Helper method that checks if the specified key starts with a property
* delimiter.
*
* @param key the key to check
* @return a flag if there is a leading delimiter
*/
private boolean hasLeadingDelimiter(String key)
{
return key.startsWith(getExpressionEngine().getPropertyDelimiter())
&& (getExpressionEngine().getEscapedDelimiter() == null || !key
.startsWith(getExpressionEngine().getEscapedDelimiter()));
}
/**
* Helper method for removing attribute markers from a key.
*
* @param key the key
* @return the key with removed attribute markers
*/
private String removeAttributeMarkers(String key)
{
return key
.substring(
getExpressionEngine().getAttributeStart().length(),
key.length()
- ((getExpressionEngine().getAttributeEnd() != null) ? getExpressionEngine()
.getAttributeEnd().length()
: 0));
}
/**
* Unescapes the delimiters in the specified string.
*
* @param key the key to be unescaped
* @return the unescaped key
*/
private String unescapeDelimiters(String key)
{
return (getExpressionEngine().getEscapedDelimiter() == null) ? key
: StringUtils.replace(key, getExpressionEngine()
.getEscapedDelimiter(), getExpressionEngine()
.getPropertyDelimiter());
}
/**
* Escapes the delimiters in the specified string.
*
* @param key the key to be escaped
* @return the escaped key
*/
private String escapeDelimiters(String key)
{
return (getExpressionEngine().getEscapedDelimiter() == null || key
.indexOf(getExpressionEngine().getPropertyDelimiter()) < 0) ? key
: StringUtils.replace(key, getExpressionEngine()
.getPropertyDelimiter(), getExpressionEngine()
.getEscapedDelimiter());
}
/**
* A specialized iterator class for tokenizing a configuration key. This
* class implements the normal iterator interface. In addition it provides
* some specific methods for configuration keys.
*/
public class KeyIterator implements Iterator