public class XPathExpressionEngine extends Object implements ExpressionEngine
A specialized implementation of the
that is able to evaluate XPATH expressions.
This class makes use of Commons JXPath for handling XPath expressions and mapping them to the nodes of a hierarchical configuration. This makes the rich and powerful XPATH syntax available for accessing properties from a configuration object.
For selecting properties arbitrary XPATH expressions can be used, which
select single or multiple configuration nodes. The associated
Configuration instance will directly pass the specified property
keys into this engine. If a key is not syntactically correct, an exception
will be thrown.
For adding new properties, this expression engine uses a specific syntax: the "key" of a new property must consist of two parts that are separated by whitespace:
This will add a new
type node as a child of the first
Similar to the example above, but this time a new attribute named
type will be added to the first
This example shows how a complex path can be added. Parent node is the
tables element. Here a new branch consisting of the nodes
name will be added.
This is similar to the last example, but in this case a complex path ending with an attribute is defined.
Note: This extended syntax for adding properties only works
not support creating new nodes this way.
From version 1.7 on, it is possible to use regular keys in calls to
addProperty() (i.e. keys that do not have to contain a
whitespace as delimiter). In this case the key is evaluated, and the biggest
part pointing to an existing node is determined. The remaining part is then
added as new path. As an example consider the key
"tables/table[last()]/fields/field/name"If the key does not point to an existing node, the engine will check the paths
"tables/table[last()]", and so on, until a key is found which points to a node. Let's assume that the last key listed above can be resolved in this way. Then from this key the following key is derived:
"tables/table[last()] fields/field/name"by appending the remaining part after a whitespace. This key can now be processed using the original algorithm. Keys of this form can also be used with the
setProperty()method. However, it is still recommended to use the old format because it makes explicit at which position new nodes should be added. For keys without a whitespace delimiter there may be ambiguities.
|Constructor and Description|
|Modifier and Type||Method and Description|
Initializes most properties of a
Returns a (canonical) key for the given node based on the parent's key.
Prepares an add operation for a configuration property.
Executes a query.
public List<ConfigurationNode> query(ConfigurationNode root, String key)
public String nodeKey(ConfigurationNode node, String parentKey)
DefaultExpressionEnginethis method will not return indices for nodes. So all child nodes of a given parent with the same name will have the same key.
public NodeAddData prepareAdd(ConfigurationNode root, String key)
protected org.apache.commons.jxpath.JXPathContext createContext(ConfigurationNode root, String key)
JXPathContextused for executing a query. This method will create a new context and ensure that it is correctly initialized.
root- the configuration root node
key- the key to be queried
protected void initNodeAddData(NodeAddData data, String path)
NodeAddDataobject. This method is called by
prepareAdd()after the parent node has been found. Its task is to interpret the passed in path of the new node.
data- the data object to initialize
path- the path of the new node
Copyright © 2001–2014 The Apache Software Foundation. All rights reserved.