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 * Returns a list of immutable configurations for all direct child elements of the node selected by the given key. With 069 * this method it is possible to inspect the content of a hierarchical structure; all children of a given node can be 070 * queried without having to know their exact names. If the passed in key does not point to a single node, an empty list 071 * is returned. This is also the result if the node referred to by the key does not have child elements. 072 * 073 * @param key the key for selecting the desired parent node 074 * @return a collection with immutable configurations for all child nodes of the selected parent node 075 */ 076 List<ImmutableHierarchicalConfiguration> immutableChildConfigurationsAt(String key); 077 078 /** 079 * Returns an immutable hierarchical configuration for the node specified by the given key. This is a short form for 080 * {@code immutableConfigurationAt(key, 081 * <b>false</b>)}. 082 * 083 * @param key the key that selects the sub tree 084 * @return a hierarchical configuration that contains this sub tree 085 */ 086 ImmutableHierarchicalConfiguration immutableConfigurationAt(String key); 087 088 /** 089 * <p> 090 * Returns an immutable hierarchical configuration object that wraps the configuration node specified by the given key. 091 * This method provides an easy means of accessing sub trees of a hierarchical configuration. In the returned 092 * configuration the sub tree can directly be accessed, it becomes the root node of this configuration. Because of this 093 * the passed in key must select exactly one configuration node; otherwise an {@code IllegalArgumentException} will be 094 * thrown. 095 * </p> 096 * <p> 097 * The difference between this method and the {@link #immutableSubset(String)} method is that {@code immutableSubset()} 098 * supports arbitrary subsets of configuration nodes while {@code immutableConfigurationAt()} only returns a single sub 099 * tree. Please refer to the documentation of the {@code SubnodeConfiguration} class to obtain further information about 100 * subnode configurations and when they should be used. 101 * </p> 102 * 103 * @param key the key that selects the sub tree 104 * @param supportUpdates a flag whether the returned subnode configuration should be able to handle updates of its 105 * parent 106 * @return a hierarchical configuration that contains this sub tree 107 */ 108 ImmutableHierarchicalConfiguration immutableConfigurationAt(String key, boolean supportUpdates); 109 110 /** 111 * Returns a list of immutable configurations for all configuration nodes selected by the given key. This method will 112 * evaluate the passed in key (using the current {@code ExpressionEngine}) and then create an immutable subnode 113 * configuration for each returned node (like {@link #immutableConfigurationAt(String)}}). This is especially useful 114 * when dealing with list-like structures. As an example consider the configuration that contains data about database 115 * tables and their fields. If you need access to all fields of a certain table, you can simply do 116 * 117 * <pre> 118 * List<ImmutableHierarchicalConfiguration> fields = 119 * config.immutableConfigurationsAt("tables.table(0).fields.field"); 120 * for(Iterator<ImmutableHierarchicalConfiguration> it = fields.iterator(); 121 * it.hasNext();) 122 * { 123 * ImmutableHierarchicalConfiguration sub = it.next(); 124 * // now the children and attributes of the field node can be 125 * // directly accessed 126 * String fieldName = sub.getString("name"); 127 * String fieldType = sub.getString("type"); 128 * ... 129 * </pre> 130 * 131 * @param key the key for selecting the desired nodes 132 * @return a list with immutable hierarchical configuration objects; each configuration represents one of the nodes 133 * selected by the passed in key 134 */ 135 List<ImmutableHierarchicalConfiguration> immutableConfigurationsAt(String key); 136}