    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;
    018
    019import java.util.List;
    020
    021import org.apache.commons.configuration2.tree.ExpressionEngine;
    022
    023/**
    024 * <p>
    025 * An interface for immutable hierarchical configurations.
    026 * </p>
    027 * <p>
    028 * There are some sources of configuration data that cannot be stored very well in a flat configuration object (like
    029 * {@link BaseConfiguration}) because then their structure is lost. A prominent example are XML documents.
    030 * </p>
    031 * <p>
    032 * This interface extends the basic {@link ImmutableConfiguration} interface by structured access to configuration
    033 * properties. An {@link ExpressionEngine} is used to evaluate complex property keys and to map them to nodes of a
    034 * tree-like structure.
    035 * </p>
    036 *
    037 * @since 2.0
    038 */
    039public interface ImmutableHierarchicalConfiguration extends ImmutableConfiguration {
    040    /**
    041     * Gets the expression engine used by this configuration. This method will never return <b>null</b>; if no specific
    042     * expression engine was set, the default expression engine will be returned.
    043     *
    044     * @return the current expression engine
    045     */
    046    ExpressionEngine getExpressionEngine();
    047
    048    /**
    049     * Gets the maximum defined index for the given key. This is useful if there are multiple values for this key. They
    050     * can then be addressed separately by specifying indices from 0 to the return value of this method.
    051     *
    052     * @param key the key to be checked
    053     * @return the maximum defined index for this key
    054     */
    055    int getMaxIndex(String key);
    056
    057    /**
    058     * Gets the name of the root element of this configuration. This information may be of use in some cases, e.g. for
    059     * sub configurations created using the {@code immutableConfigurationsAt()} method. The exact meaning of the string
    060     * returned by this method is specific to a concrete implementation. For instance, an XML configuration might return the
    061     * name of the document element.
    062     *
    063     * @return the name of the root element of this configuration
    064     */
    065    String getRootElementName();
    066
    067    /**
    068     * <p>
    069     * Returns an immutable hierarchical configuration object that wraps the configuration node specified by the given key.
    070     * This method provides an easy means of accessing sub trees of a hierarchical configuration. In the returned
    071     * configuration the sub tree can directly be accessed, it becomes the root node of this configuration. Because of this
    072     * the passed in key must select exactly one configuration node; otherwise an {@code IllegalArgumentException} will be
    073     * thrown.
    074     * </p>
    075     * <p>
    076     * The difference between this method and the {@link #immutableSubset(String)} method is that {@code immutableSubset()}
    077     * supports arbitrary subsets of configuration nodes while {@code immutableConfigurationAt()} only returns a single sub
    078     * tree. Please refer to the documentation of the {@code SubnodeConfiguration} class to obtain further information about
    079     * subnode configurations and when they should be used.
    080     * </p>
    081     *
    082     * @param key the key that selects the sub tree
    083     * @param supportUpdates a flag whether the returned subnode configuration should be able to handle updates of its
    084     *        parent
    085     * @return a hierarchical configuration that contains this sub tree
    086     */
    087    ImmutableHierarchicalConfiguration immutableConfigurationAt(String key, boolean supportUpdates);
    088
    089    /**
    090     * Returns an immutable hierarchical configuration for the node specified by the given key. This is a short form for
    091     * {@code immutableConfigurationAt(key,
    092     * <b>false</b>)}.
    093     *
    094     * @param key the key that selects the sub tree
    095     * @return a hierarchical configuration that contains this sub tree
    096     */
    097    ImmutableHierarchicalConfiguration immutableConfigurationAt(String key);
    098
    099    /**
    100     * Returns a list of immutable configurations for all configuration nodes selected by the given key. This method will
    101     * evaluate the passed in key (using the current {@code ExpressionEngine}) and then create an immutable subnode
    102     * configuration for each returned node (like {@link #immutableConfigurationAt(String)}}). This is especially useful
    103     * when dealing with list-like structures. As an example consider the configuration that contains data about database
    104     * tables and their fields. If you need access to all fields of a certain table, you can simply do
    105     *
    106     * <pre>
    107     * List&lt;ImmutableHierarchicalConfiguration&gt; fields =
    108     *   config.immutableConfigurationsAt("tables.table(0).fields.field");
    109     * for(Iterator&lt;ImmutableHierarchicalConfiguration&gt; it = fields.iterator();
    110     *   it.hasNext();)
    111     * {
    112     *     ImmutableHierarchicalConfiguration sub = it.next();
    113     *     // now the children and attributes of the field node can be
    114     *     // directly accessed
    115     *     String fieldName = sub.getString("name");
    116     *     String fieldType = sub.getString("type");
    117     *     ...
    118     * </pre>
    119     *
    120     * @param key the key for selecting the desired nodes
    121     * @return a list with immutable hierarchical configuration objects; each configuration represents one of the nodes
    122     *         selected by the passed in key
    123     */
    124    List<ImmutableHierarchicalConfiguration> immutableConfigurationsAt(String key);
    125
    126    /**
    127     * Returns a list of immutable configurations for all direct child elements of the node selected by the given key. With
    128     * this method it is possible to inspect the content of a hierarchical structure; all children of a given node can be
    129     * queried without having to know their exact names. If the passed in key does not point to a single node, an empty list
    130     * is returned. This is also the result if the node referred to by the key does not have child elements.
    131     *
    132     * @param key the key for selecting the desired parent node
    133     * @return a collection with immutable configurations for all child nodes of the selected parent node
    134     */
    135    List<ImmutableHierarchicalConfiguration> immutableChildConfigurationsAt(String key);
    136}