org.apache.commons.configuration.tree.xpath
public class XPathExpressionEngine extends Object implements ExpressionEngine
A specialized implementation of the ExpressionEngine
interface
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 powerfull 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:
addProperty()
method follow:
"/tables/table[1] type"
This will add a new type
node as a child of the first
table
element.
"/tables/table[1] @type"
Similar to the example above, but this time a new attribute named
type
will be added to the first table
element.
"/tables table/fields/field/name"
This example shows how a complex path can be added. Parent node is the
tables
element. Here a new branch consisting of the nodes
table
, fields
, field
, and
name
will be added.
Since: 1.3
Version: $Id: XPathExpressionEngine.java 466413 2006-10-21 15:23:45Z oheger $
Field Summary | |
---|---|
static String | ATTR_DELIMITER Constant for the attribute delimiter. |
static String | NODE_PATH_DELIMITERS Constant for the delimiters for splitting node paths. |
static String | PATH_DELIMITER Constant for the path delimiter. |
Method Summary | |
---|---|
protected JXPathContext | createContext(ConfigurationNode root, String key)
Creates the JXPathContext used for executing a query. |
protected void | initNodeAddData(NodeAddData data, String path)
Initializes most properties of a NodeAddData object. |
void | invalidPath(String path, String msg)
Helper method for throwing an exception about an invalid path.
|
String | nodeKey(ConfigurationNode node, String parentKey)
Returns a (canonic) key for the given node based on the parent's key.
|
NodeAddData | prepareAdd(ConfigurationNode root, String key)
Prepares an add operation for a configuration property. |
List | query(ConfigurationNode root, String key)
Executes a query. |
JXPathContext
used for executing a query. This
method will create a new context and ensure that it is correctly
initialized.
Parameters: root the configuration root node key the key to be queried
Returns: the new context
NodeAddData
object. This
method is called by prepareAdd()
after the parent node has
been found. Its task is to interprete the passed in path of the new node.
Parameters: data the data object to initialize path the path of the new node
Parameters: path the invalid path msg the exception message
nodeKey()
implementation of
DefaultExpressionEngine
this method will not return indices for nodes. So all child nodes of a
given parent whith the same name will have the same key.
Parameters: node the node for which a key is to be constructed parentKey the key of the parent node
Returns: the key for the given node
Parameters: root the configuration's root node key the key describing the target of the add operation and the path of the new node
Returns: a data object to be evaluated by the calling configuration object
Parameters: root the configuration root node key the query to be executed
Returns: a list with the nodes that are selected by the query