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.builder.combined;
018
019import java.util.Set;
020
021import org.apache.commons.configuration2.HierarchicalConfiguration;
022import org.apache.commons.configuration2.beanutils.XMLBeanDeclaration;
023
024/**
025 * <p>
026 * A specialized {@code BeanDeclaration} implementation that represents the declaration of a configuration source.
027 * </p>
028 * <p>
029 * Instances of this class are able to extract all information about a configuration source from the configuration
030 * definition file. The declaration of a configuration source is very similar to a bean declaration processed by
031 * {@code XMLBeanDeclaration}. There are very few differences, e.g. some reserved attributes like {@code optional} and
032 * {@code at}, and the fact that a bean factory is never needed.
033 * </p>
034 *
035 * @since 2.0
036 */
037public class ConfigurationDeclaration extends XMLBeanDeclaration {
038    /** Stores a reference to the associated configuration builder. */
039    private final CombinedConfigurationBuilder configurationBuilder;
040
041    /**
042     * Creates a new instance of {@code ConfigurationDeclaration} and initializes it.
043     *
044     * @param builder the associated configuration builder
045     * @param config the configuration this declaration is based onto
046     */
047    public ConfigurationDeclaration(final CombinedConfigurationBuilder builder, final HierarchicalConfiguration<?> config) {
048        super(config);
049        configurationBuilder = builder;
050    }
051
052    /**
053     * Gets the associated configuration builder.
054     *
055     * @return the configuration builder
056     */
057    public CombinedConfigurationBuilder getConfigurationBuilder() {
058        return configurationBuilder;
059    }
060
061    /**
062     * Gets the value of the {@code at} attribute.
063     *
064     * @return the value of the {@code at} attribute (can be <b>null</b>)
065     */
066    public String getAt() {
067        final String result = this.getConfiguration().getString(CombinedConfigurationBuilder.ATTR_AT_RES);
068        return result == null ? this.getConfiguration().getString(CombinedConfigurationBuilder.ATTR_AT) : result;
069    }
070
071    /**
072     * Gets a flag whether this is an optional configuration.
073     *
074     * @return a flag if this declaration points to an optional configuration
075     */
076    public boolean isOptional() {
077        Boolean value = this.getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_OPTIONAL_RES, null);
078        if (value == null) {
079            value = this.getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_OPTIONAL, Boolean.FALSE);
080        }
081        return value.booleanValue();
082    }
083
084    /**
085     * Gets a flag whether this configuration should always be created and added to the resulting combined configuration.
086     * This flag is evaluated only for optional configurations whose normal creation has caused an error. If for such a
087     * configuration the {@code forceCreate} attribute is set and the corresponding configuration provider supports this
088     * mode, an empty configuration will be created and added to the resulting combined configuration.
089     *
090     * @return the value of the {@code forceCreate} attribute
091     */
092    public boolean isForceCreate() {
093        return this.getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_FORCECREATE, false);
094    }
095
096    /**
097     * Returns a flag whether a builder with reloading support should be created. This may not be supported by all
098     * configuration builder providers.
099     *
100     * @return a flag whether a reloading builder should be created
101     */
102    public boolean isReload() {
103        return getConfiguration().getBoolean(CombinedConfigurationBuilder.ATTR_RELOAD, false);
104    }
105
106    /**
107     * Gets the name for the represented configuration source. The name is optional, so this method can return
108     * <b>null</b>.
109     *
110     * @return the name of the associated configuration source or <b>null</b>
111     */
112    public String getName() {
113        return getConfiguration().getString(CombinedConfigurationBuilder.ATTR_NAME);
114    }
115
116    /**
117     * Gets the name of the bean factory. For configuration source declarations always a reserved factory is used. This
118     * factory's name is returned by this implementation.
119     *
120     * @return the name of the bean factory
121     */
122    @Override
123    public String getBeanFactoryName() {
124        return CombinedConfigurationBuilder.CONFIG_BEAN_FACTORY_NAME;
125    }
126
127    /**
128     * Gets the bean's class name. This implementation will always return <b>null</b>.
129     *
130     * @return the name of the bean's class
131     */
132    @Override
133    public String getBeanClassName() {
134        return null;
135    }
136
137    /**
138     * {@inheritDoc} This implementation checks for additional reserved attribute names. Note that in some cases the
139     * presence of other attribute names determine whether a name is reserved or not. For instance, per default the
140     * attribute {@code config-at} is reserved. However, if this attribute is not present, the attribute {@code at} is also
141     * considered as a reserved attribute. (This is mainly done for dealing with legacy configuration files supported by
142     * earlier versions of this library.)
143     */
144    @Override
145    protected boolean isReservedAttributeName(final String name) {
146        if (super.isReservedAttributeName(name)) {
147            return true;
148        }
149
150        final Set<String> attributes = getAttributeNames();
151        return CombinedConfigurationBuilder.ATTR_ATNAME.equals(name) && !attributes.contains(RESERVED_PREFIX + CombinedConfigurationBuilder.ATTR_ATNAME)
152            || CombinedConfigurationBuilder.ATTR_OPTIONALNAME.equals(name)
153                && !attributes.contains(RESERVED_PREFIX + CombinedConfigurationBuilder.ATTR_OPTIONALNAME);
154    }
155}