/**
*
* Example: setKeyValue("key.1_level.2_level","the value for k_1_2")
null if the key is not a subtree referece
*/
private PropertyTree getLastSubTree(String splitKey) {
String[] keys = splitKey.split(SPLIT_STRING);
PropertyTree curr_tree = this;
for (int key_idx = 0; key_idx < keys.length; key_idx++) {
String key = keys[key_idx];
if (!curr_tree.containsNode(key)) {
return null;
}
curr_tree = (PropertyTree) curr_tree.getSubNode(key);
}
return curr_tree;
}
/**
* This method return the subtree that corresponds to a particular key. The key does not split.
* Therefore the key must be a children of the current node. Search only in the key map of the
* current node.
*
* @param key the key that has to be a sub node
* @return a sub tree (PropertyTree) or null
if the key is not a children of the
* current node
*/
private PropertyTree getSubNode(String key) {
return (PropertyTree) keys_.get(key);
}
/**
* Returns the last value (keys can be overloaded) of a key. The key are splitted into subnodes
* and the last node of the key is the current value holder. If a key or subnode is not in the sub
* tree the return value is null.
*
* @param key the key that holds the value (can be a nested key like "key.1.2.3"
)
* @return the value of the key (last node of the key) or null
otherwise
*/
public String getLastValue(String key) {
PropertyTree curr_tree = getLastSubTree(key);
String result = null;
if (curr_tree != null && !curr_tree.values_.isEmpty()) {
result = (String) curr_tree.values_.lastElement();
}
// if (logger_.isDebugEnabled()) {
// logger_.debug("getLastValue:" + key + "=" + result);
// }
return result;
}
/**
* Returns the first value (keys can be overloaded) of a key. The key are splitted into subnodes
* and the last node of the key is the current value holder. If a key or subnode is not in the sub
* tree the return value is null
.
*
* @param key the key that holds the value (can be a nested key like "key.1.2.3"
)
* @return the value of the key (last node of the key) or null
otherwise
*/
public String getFirstValue(String key) {
PropertyTree curr_tree = getLastSubTree(key);
String result = null;
if (curr_tree != null && !curr_tree.values_.isEmpty()) {
result = (String) curr_tree.values_.firstElement();
}
// if (logger_.isDebugEnabled()) {
// logger_.debug("getFirstValue:" + key + "=" + result);
// }
return result;
}
/**
* This method return all values of the current node. The values are stored as String values.
*
* @return the values (type String) of the current node
* @see Vector
*/
public Vector getValues() {
return values_;
}
/**
* This method return all keys (sub tree references) of the current node as a Map. The keys are
* stored as String values.
*
* @return the keys (type String) of the current node
* @see Map
*/
public Map getKeyEntries() {
return keys_;
}
/**
* This method return all keys (sub tree references) of the current node as an ArrayList. The keys
* are stored as String values.
*
* @return the keys (type String) of the current node
* @see ArrayList
*/
public ArrayList getKeys() {
if (!keys_.isEmpty()) {
Object[] objs = keys_.keySet().toArray();
ArrayList keys = new ArrayList(objs.length);
for (int idx = 0; idx < objs.length; idx++) {
keys.add((String) objs[idx]);
}
return keys;
}
return null;
}
/**
*
* This method return all sub tree references of a key as an ArrayList. The keys are stored as
* String values.
*
* @param key (can be a nested key like "key.1.2.3"
)
* @return the keys (type String) of the current node
* @see ArrayList
*/
public ArrayList getKeys(String key) {
PropertyTree curr_tree = getLastSubTree(key);
if (curr_tree != null) {
return curr_tree.getKeys();
}
return null;
}
/**
* This method return all values of a key. The values are stored as String values.
*
* @param key (can be a nested key like "key.1.2.3"
)
* @return the values (type Vector) of the key or null
if the key is not in the sub
* tree of the current node
* @see Vector
*/
public Vector getValues(String key) {
PropertyTree curr_tree = getLastSubTree(key);
if (curr_tree != null) {
return curr_tree.values_;
}
return null;
}
/**
* Store a sub tree (type PropertyTree) in the current node. The key and it's sub tree are stored
* in a HashMap.
*
* @param key the reference of the sub tree
* @param tree the sub tree of the key
* @see HashMap
*/
private void setSubTree(String key, PropertyTree tree) {
if (tree == null) {
tree = new PropertyTree();
}
keys_.put(key, tree);
}
/**
* Extracts a sub tree of a nested key. The Method returns the last sub tree of the nested key.
* Example: if the key is like: key.1.2.3
the sub tree of the last
* node 3
is returned.
*
* @param key the reference of the sub tree
* @return a sub tree of the key or null
if the key can not be found
*/
public PropertyTree getSubTree(String key) {
return getLastSubTree(key);
}
/**
* This method checks if a key is a reference to a sub tree in the current node.
*
* @param key a simple key that is a parent reference of a sub tree
* @return true if the key is found, false otherwise
*/
public boolean containsNode(String key) {
return keys_.containsKey(key);
}
/**
* The default toString method. It starts with the current node recursively downwards and return
* the String representation of the node.
*
* @return the string representation of the node
*/
public String toString() {
return toString("", this);
}
/**
* This is a helper function to define the prefix for different levels in the toString method, not
* realy nice ;-).