public class XPathExpressionEngine extends java.lang.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.
Modifier and Type | Field and Description |
---|---|
(package private) static java.lang.String |
ATTR_DELIMITER
Constant for the attribute delimiter.
|
private static java.lang.String |
NODE_PATH_DELIMITERS
Constant for the delimiters for splitting node paths.
|
(package private) static java.lang.String |
PATH_DELIMITER
Constant for the path delimiter.
|
Constructor and Description |
---|
XPathExpressionEngine() |
Modifier and Type | Method and Description |
---|---|
protected org.apache.commons.jxpath.JXPathContext |
createContext(ConfigurationNode root,
java.lang.String key)
Creates the
JXPathContext used for executing a query. |
protected void |
initNodeAddData(NodeAddData data,
java.lang.String path)
Initializes most properties of a
NodeAddData object. |
private void |
invalidPath(java.lang.String path,
java.lang.String msg)
Helper method for throwing an exception about an invalid path.
|
java.lang.String |
nodeKey(ConfigurationNode node,
java.lang.String parentKey)
Returns a (canonic) key for the given node based on the parent's key.
|
NodeAddData |
prepareAdd(ConfigurationNode root,
java.lang.String key)
Prepares an add operation for a configuration property.
|
java.util.List |
query(ConfigurationNode root,
java.lang.String key)
Executes a query.
|
static final java.lang.String PATH_DELIMITER
static final java.lang.String ATTR_DELIMITER
private static final java.lang.String NODE_PATH_DELIMITERS
public java.util.List query(ConfigurationNode root, java.lang.String key)
query
in interface ExpressionEngine
root
- the configuration root nodekey
- the query to be executedpublic java.lang.String nodeKey(ConfigurationNode node, java.lang.String parentKey)
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.nodeKey
in interface ExpressionEngine
node
- the node for which a key is to be constructedparentKey
- the key of the parent nodepublic NodeAddData prepareAdd(ConfigurationNode root, java.lang.String key)
prepareAdd
in interface ExpressionEngine
root
- the configuration's root nodekey
- the key describing the target of the add operation and the
path of the new nodeprotected org.apache.commons.jxpath.JXPathContext createContext(ConfigurationNode root, java.lang.String key)
JXPathContext
used for executing a query. This
method will create a new context and ensure that it is correctly
initialized.root
- the configuration root nodekey
- the key to be queriedprotected void initNodeAddData(NodeAddData data, java.lang.String path)
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.data
- the data object to initializepath
- the path of the new nodeprivate void invalidPath(java.lang.String path, java.lang.String msg)
path
- the invalid pathmsg
- the exception message