Class DefaultExpressionEngine
- All Implemented Interfaces:
ExpressionEngine
A default implementation of the ExpressionEngine
interface providing the "native" expression
language for hierarchical configurations.
This class implements a rather simple expression language for navigating through a hierarchy of configuration nodes. It supports the following operations:
- Navigating from a node to one of its children using the child node delimiter, which is by the default a dot (".").
- Navigating from a node to one of its attributes using the attribute node delimiter, which by default follows the
XPATH like syntax
[@<attributeName>]
. - If there are multiple child or attribute nodes with the same name, a specific node can be selected using a numerical index. By default indices are written in parenthesis.
As an example consider the following XML document:
<database> <tables> <table type="system"> <name>users</name> <fields> <field> <name>lid</name> <type>long</name> </field> <field> <name>usrName</name> <type>java.lang.String</type> </field> ... </fields> </table> <table> <name>documents</name> <fields> <field> <name>docid</name> <type>long</type> </field> ... </fields> </table> ... </tables> </database>
If this document is parsed and stored in a hierarchical configuration object, for instance the key
tables.table(0).name
can be used to find out the name of the first table. In opposite
tables.table.name
would return a collection with the names of all available tables. Similarly the key
tables.table(1).fields.field.name
returns a collection with the names of all fields of the second table. If
another index is added after the field
element, a single field can be accessed:
tables.table(1).fields.field(0).name
. The key tables.table(0)[@type]
would select the type attribute
of the first table.
This example works with the default values for delimiters and index markers. It is also possible to set custom values
for these properties so that you can adapt a DefaultExpressionEngine
to your personal needs.
The concrete symbols used by an instance are determined by a DefaultExpressionEngineSymbols
object passed to
the constructor. By providing a custom symbols object the syntax for querying properties in a hierarchical
configuration can be altered.
Instances of this class are thread-safe and can be shared between multiple hierarchical configuration objects.
- Since:
- 1.3
-
Field Summary
Modifier and TypeFieldDescriptionstatic final DefaultExpressionEngine
A default instance of this class that is used as expression engine for hierarchical configurations per default. -
Constructor Summary
ConstructorDescriptionCreates a new instance ofDefaultExpressionEngine
and initializes its symbols.DefaultExpressionEngine
(DefaultExpressionEngineSymbols syms, NodeMatcher<String> nodeNameMatcher) Creates a new instance ofDefaultExpressionEngine
and initializes its symbols and the matcher for comparing node names. -
Method Summary
Modifier and TypeMethodDescriptionattributeKey
(String parentKey, String attributeName) Returns the key of an attribute.<T> String
canonicalKey
(T node, String parentKey, NodeHandler<T> handler) Determines a "canonical" key for the specified node in the expression language supported by this implementation.protected <T> T
findLastPathNode
(DefaultConfigurationKey.KeyIterator keyIt, T node, NodeHandler<T> handler) Finds the last existing node for an add operation.protected <T> void
findNodesForKey
(DefaultConfigurationKey.KeyIterator keyPart, T node, Collection<QueryResult<T>> results, NodeHandler<T> handler) Recursive helper method for evaluating a key.Gets theDefaultExpressionEngineSymbols
object associated with this instance.<T> String
nodeKey
(T node, String parentKey, NodeHandler<T> handler) Returns the key for the specified node in the expression language supported by an implementation.<T> NodeAddData<T>
prepareAdd
(T root, String key, NodeHandler<T> handler) Prepares Adding the property with the specified key.<T> List<QueryResult<T>>
query
(T root, String key, NodeHandler<T> handler) Finds the nodes and/or attributes that are matched by the specified key.
-
Field Details
-
INSTANCE
A default instance of this class that is used as expression engine for hierarchical configurations per default.
-
-
Constructor Details
-
DefaultExpressionEngine
Creates a new instance ofDefaultExpressionEngine
and initializes its symbols.- Parameters:
syms
- the object with the symbols (must not be null)- Throws:
IllegalArgumentException
- if the symbols are null
-
DefaultExpressionEngine
public DefaultExpressionEngine(DefaultExpressionEngineSymbols syms, NodeMatcher<String> nodeNameMatcher) Creates a new instance ofDefaultExpressionEngine
and initializes its symbols and the matcher for comparing node names. The passed in matcher is always used when the names of nodes have to be matched against parts of configuration keys.- Parameters:
syms
- the object with the symbols (must not be null)nodeNameMatcher
- the matcher for node names; can be null, then a default matcher is used- Throws:
IllegalArgumentException
- if the symbols are null
-
-
Method Details
-
attributeKey
Description copied from interface:ExpressionEngine
Returns the key of an attribute. The passed inparentKey
must reference the parent node of the attribute. A concrete implementation must concatenate this parent key with the attribute name to a valid key for this attribute.- Specified by:
attributeKey
in interfaceExpressionEngine
- Parameters:
parentKey
- the key to the node owning this attributeattributeName
- the name of the attribute in question- Returns:
- the resulting key referencing this attribute
-
canonicalKey
Determines a "canonical" key for the specified node in the expression language supported by this implementation. This means that always a unique key if generated pointing to this specific node. For most concrete implementations, this means that an index is added to the node name to ensure that there are no ambiguities with child nodes having the same names. This implementation works similar tonodeKey()
; however, each key returned by this method has an index (except for the root node). The parent key is prepended to the name of the current node in any case and without further checks. If it is null, only the name of the current node with its index is returned.- Specified by:
canonicalKey
in interfaceExpressionEngine
- Type Parameters:
T
- the type of the node to be processed- Parameters:
node
- the node, for which the key must be constructedparentKey
- the key of this node's parent (can be null for the root node)handler
- theNodeHandler
for accessing the node- Returns:
- the canonical key of this node
-
findLastPathNode
protected <T> T findLastPathNode(DefaultConfigurationKey.KeyIterator keyIt, T node, NodeHandler<T> handler) Finds the last existing node for an add operation. This method traverses the node tree along the specified key. The last existing node on this path is returned.- Type Parameters:
T
- the type of the nodes to be dealt with- Parameters:
keyIt
- the key iteratornode
- the current nodehandler
- the node handler- Returns:
- the last existing node on the given path
-
findNodesForKey
protected <T> void findNodesForKey(DefaultConfigurationKey.KeyIterator keyPart, T node, Collection<QueryResult<T>> results, NodeHandler<T> handler) Recursive helper method for evaluating a key. This method processes all facets of a configuration key, traverses the tree of properties and fetches the results of all matching properties.- Type Parameters:
T
- the type of nodes to be dealt with- Parameters:
keyPart
- the configuration key iteratornode
- the current noderesults
- here the found results are storedhandler
- the node handler
-
getSymbols
Gets theDefaultExpressionEngineSymbols
object associated with this instance.- Returns:
- the
DefaultExpressionEngineSymbols
used by this engine - Since:
- 2.0
-
nodeKey
Returns the key for the specified node in the expression language supported by an implementation. This method is called whenever a property key for a node has to be constructed, e.g. by thegetKeys()
method. This implementation takes the given parent key, adds a property delimiter, and then adds the node's name. The name of the root node is a blank string. Note that no indices are returned.- Specified by:
nodeKey
in interfaceExpressionEngine
- Type Parameters:
T
- the type of the node to be processed- Parameters:
node
- the node, for which the key must be constructedparentKey
- the key of this node's parent (can be null for the root node)handler
- theNodeHandler
for accessing the node- Returns:
- this node's key
-
prepareAdd
Prepares Adding the property with the specified key.
To be able to deal with the structure supported by hierarchical configuration implementations the passed in key is of importance, especially the indices it might contain. The following example should clarify this: Suppose the current node structure looks like the following:
tables +-- table +-- name = user +-- fields +-- field +-- name = uid +-- field +-- name = firstName ... +-- table +-- name = documents +-- fields ...
In this example a database structure is defined, e.g. all fields of the first table could be accessed using the key
tables.table(0).fields.field.name
. If now properties are to be added, it must be exactly specified at which position in the hierarchy the new property is to be inserted. So to add a new field name to a table it is not enough to say justconfig.addProperty("tables.table.fields.field.name", "newField");
The statement given above contains some ambiguity. For instance it is not clear, to which table the new field should be added. If this method finds such an ambiguity, it is resolved by following the last valid path. Here this would be the last table. The same is true for the
field
; because there are multiple fields and no explicit index is provided, a newname
property would be added to the last field - which is probably not what was desired.To make things clear explicit indices should be provided whenever possible. In the example above the exact table could be specified by providing an index for the
table
element as intables.table(1).fields
. By specifying an index it can also be expressed that at a given position in the configuration tree a new branch should be added. In the example above we did not want to add an additionalname
element to the last field of the table, but we want a complete newfield
element. This can be achieved by specifying an invalid index (like -1) after the element where a new branch should be created. Given this our example would run:config.addProperty("tables.table(1).fields.field(-1).name", "newField");
With this notation it is possible to add new branches everywhere. We could for instance create a new
table
element by specifyingconfig.addProperty("tables.table(-1).fields.field.name", "newField2");
(Note that because after the
table
element a new branch is created indices in following elements are not relevant; the branch is new so there cannot be any ambiguities.)- Specified by:
prepareAdd
in interfaceExpressionEngine
- Type Parameters:
T
- the type of the nodes to be dealt with- Parameters:
root
- the root node of the nodes hierarchykey
- the key of the new propertyhandler
- the node handler- Returns:
- a data object with information needed for the add operation
-
query
Finds the nodes and/or attributes that are matched by the specified key. This is the main method for interpreting property keys. An implementation must traverse the given root node and its children to find all results that are matched by the given key. If the key is not correct in the syntax provided by that implementation, it is free to throw a (runtime) exception indicating this error condition. The passed inNodeHandler
can be used to gather the required information from the node object. This method supports the syntax as described in the class comment.- Specified by:
query
in interfaceExpressionEngine
- Type Parameters:
T
- the type of the node to be processed- Parameters:
root
- the root node of a hierarchy of nodeskey
- the key to be evaluatedhandler
- theNodeHandler
for accessing the node- Returns:
- a list with the results that are matched by the key (should never be null)
-