View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.configuration2;
18  
19  import java.util.List;
20  
21  import org.apache.commons.configuration2.tree.ExpressionEngine;
22  
23  /**
24   * <p>
25   * An interface for immutable hierarchical configurations.
26   * </p>
27   * <p>
28   * There are some sources of configuration data that cannot be stored very well in a flat configuration object (like
29   * {@link BaseConfiguration}) because then their structure is lost. A prominent example are XML documents.
30   * </p>
31   * <p>
32   * This interface extends the basic {@link ImmutableConfiguration} interface by structured access to configuration
33   * properties. An {@link ExpressionEngine} is used to evaluate complex property keys and to map them to nodes of a
34   * tree-like structure.
35   * </p>
36   *
37   * @since 2.0
38   */
39  public interface ImmutableHierarchicalConfiguration extends ImmutableConfiguration {
40  
41      /**
42       * Gets the expression engine used by this configuration. This method will never return <strong>null</strong>; if no specific
43       * expression engine was set, the default expression engine will be returned.
44       *
45       * @return the current expression engine
46       */
47      ExpressionEngine getExpressionEngine();
48  
49      /**
50       * Gets the maximum defined index for the given key. This is useful if there are multiple values for this key. They
51       * can then be addressed separately by specifying indices from 0 to the return value of this method.
52       *
53       * @param key the key to be checked
54       * @return the maximum defined index for this key
55       */
56      int getMaxIndex(String key);
57  
58      /**
59       * Gets the name of the root element of this configuration. This information may be of use in some cases, for example for
60       * sub configurations created using the {@code immutableConfigurationsAt()} method. The exact meaning of the string
61       * returned by this method is specific to a concrete implementation. For instance, an XML configuration might return the
62       * name of the document element.
63       *
64       * @return the name of the root element of this configuration
65       */
66      String getRootElementName();
67  
68      /**
69       * Returns a list of immutable configurations for all direct child elements of the node selected by the given key. With
70       * this method it is possible to inspect the content of a hierarchical structure; all children of a given node can be
71       * queried without having to know their exact names. If the passed in key does not point to a single node, an empty list
72       * is returned. This is also the result if the node referred to by the key does not have child elements.
73       *
74       * @param key the key for selecting the desired parent node
75       * @return a collection with immutable configurations for all child nodes of the selected parent node
76       */
77      List<ImmutableHierarchicalConfiguration> immutableChildConfigurationsAt(String key);
78  
79      /**
80       * Returns an immutable hierarchical configuration for the node specified by the given key. This is a short form for
81       * {@code immutableConfigurationAt(key,
82       * <strong>false</strong>)}.
83       *
84       * @param key the key that selects the sub tree
85       * @return a hierarchical configuration that contains this sub tree
86       */
87      ImmutableHierarchicalConfiguration immutableConfigurationAt(String key);
88  
89      /**
90       * <p>
91       * Returns an immutable hierarchical configuration object that wraps the configuration node specified by the given key.
92       * This method provides an easy means of accessing sub trees of a hierarchical configuration. In the returned
93       * configuration the sub tree can directly be accessed, it becomes the root node of this configuration. Because of this
94       * the passed in key must select exactly one configuration node; otherwise an {@code IllegalArgumentException} will be
95       * thrown.
96       * </p>
97       * <p>
98       * The difference between this method and the {@link #immutableSubset(String)} method is that {@code immutableSubset()}
99       * supports arbitrary subsets of configuration nodes while {@code immutableConfigurationAt()} only returns a single sub
100      * tree. Please refer to the documentation of the {@code SubnodeConfiguration} class to obtain further information about
101      * subnode configurations and when they should be used.
102      * </p>
103      *
104      * @param key the key that selects the sub tree
105      * @param supportUpdates a flag whether the returned subnode configuration should be able to handle updates of its
106      *        parent
107      * @return a hierarchical configuration that contains this sub tree
108      */
109     ImmutableHierarchicalConfiguration immutableConfigurationAt(String key, boolean supportUpdates);
110 
111     /**
112      * Returns a list of immutable configurations for all configuration nodes selected by the given key. This method will
113      * evaluate the passed in key (using the current {@code ExpressionEngine}) and then create an immutable subnode
114      * configuration for each returned node (like {@link #immutableConfigurationAt(String)}}). This is especially useful
115      * when dealing with list-like structures. As an example consider the configuration that contains data about database
116      * tables and their fields. If you need access to all fields of a certain table, you can simply do
117      *
118      * <pre>
119      * List&lt;ImmutableHierarchicalConfiguration&gt; fields =
120      *   config.immutableConfigurationsAt("tables.table(0).fields.field");
121      * for(Iterator&lt;ImmutableHierarchicalConfiguration&gt; it = fields.iterator();
122      *   it.hasNext();)
123      * {
124      *     ImmutableHierarchicalConfiguration sub = it.next();
125      *     // now the children and attributes of the field node can be
126      *     // directly accessed
127      *     String fieldName = sub.getString("name");
128      *     String fieldType = sub.getString("type");
129      *     ...
130      * </pre>
131      *
132      * @param key the key for selecting the desired nodes
133      * @return a list with immutable hierarchical configuration objects; each configuration represents one of the nodes
134      *         selected by the passed in key
135      */
136     List<ImmutableHierarchicalConfiguration> immutableConfigurationsAt(String key);
137 }