    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *     http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.configuration2.tree;
    018
    019import java.util.List;
    020
    021/**
    022 * <p>
    023 * Definition of an interface for evaluating keys for hierarchical configurations.
    024 * </p>
    025 * <p>
    026 * An <em>expression engine</em> knows how to map a key for a configuration's property to a single or a set of
    027 * configuration nodes. Thus it defines the way how properties are addressed in this configuration. Methods of a
    028 * configuration that have to handle property keys (e.g. {@code getProperty()} or {@code addProperty()} do not interpret
    029 * the passed in keys on their own, but delegate this task to an associated expression engine. This expression engine
    030 * will then find out, which configuration nodes are addressed by the key.
    031 * </p>
    032 * <p>
    033 * Separating the task of evaluating property keys from the configuration object has the advantage that multiple
    034 * different expression languages (i.e. ways for querying or setting properties) can be supported. Just set a suitable
    035 * implementation of this interface as the configuration's expression engine, and you can use the syntax provided by
    036 * this implementation.
    037 * </p>
    038 * <p>
    039 * An {@code ExpressionEngine} can deal with nodes of different types. To achieve this, a {@link NodeHandler} for the
    040 * desired type must be passed to the methods.
    041 * </p>
    042 *
    043 * @since 1.3
    044 */
    045public interface ExpressionEngine {
    046    /**
    047     * Finds the nodes and/or attributes that are matched by the specified key. This is the main method for interpreting
    048     * property keys. An implementation must traverse the given root node and its children to find all results that are
    049     * matched by the given key. If the key is not correct in the syntax provided by that implementation, it is free to
    050     * throw a (runtime) exception indicating this error condition. The passed in {@code NodeHandler} can be used to gather
    051     * the required information from the node object.
    052     *
    053     * @param <T> the type of the node to be processed
    054     * @param root the root node of a hierarchy of nodes
    055     * @param key the key to be evaluated
    056     * @param handler the {@code NodeHandler} for accessing the node
    057     * @return a list with the results that are matched by the key (should never be <b>null</b>)
    058     */
    059    <T> List<QueryResult<T>> query(T root, String key, NodeHandler<T> handler);
    060
    061    /**
    062     * Returns the key for the specified node in the expression language supported by an implementation. This method is
    063     * called whenever a property key for a node has to be constructed, e.g. by the
    064     * {@link org.apache.commons.configuration2.Configuration#getKeys() getKeys()} method.
    065     *
    066     * @param <T> the type of the node to be processed
    067     * @param node the node, for which the key must be constructed
    068     * @param parentKey the key of this node's parent (can be <b>null</b> for the root node)
    069     * @param handler the {@code NodeHandler} for accessing the node
    070     * @return this node's key
    071     */
    072    <T> String nodeKey(T node, String parentKey, NodeHandler<T> handler);
    073
    074    /**
    075     * Returns the key of an attribute. The passed in {@code parentKey} must reference the parent node of the attribute. A
    076     * concrete implementation must concatenate this parent key with the attribute name to a valid key for this attribute.
    077     *
    078     * @param parentKey the key to the node owning this attribute
    079     * @param attributeName the name of the attribute in question
    080     * @return the resulting key referencing this attribute
    081     */
    082    String attributeKey(String parentKey, String attributeName);
    083
    084    /**
    085     * Determines a &quot;canonical&quot; key for the specified node in the expression language supported by this
    086     * implementation. This means that always a unique key if generated pointing to this specific node. For most concrete
    087     * implementations, this means that an index is added to the node name to ensure that there are no ambiguities with
    088     * child nodes having the same names.
    089     *
    090     * @param <T> the type of the node to be processed
    091     * @param node the node, for which the key must be constructed
    092     * @param parentKey the key of this node's parent (can be <b>null</b> for the root node)
    093     * @param handler the {@code NodeHandler} for accessing the node
    094     * @return the canonical key of this node
    095     */
    096    <T> String canonicalKey(T node, String parentKey, NodeHandler<T> handler);
    097
    098    /**
    099     * Returns information needed for an add operation. This method gets called when new properties are to be added to a
    100     * configuration. An implementation has to interpret the specified key, find the parent node for the new elements, and
    101     * provide all information about new nodes to be added.
    102     *
    103     * @param <T> the type of the node to be processed
    104     * @param root the root node
    105     * @param key the key for the new property
    106     * @param handler the {@code NodeHandler} for accessing the node
    107     * @return an object with all information needed for the add operation
    108     */
    109    <T> NodeAddData<T> prepareAdd(T root, String key, NodeHandler<T> handler);
    110}