    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.Collection;
    020
    021import org.apache.commons.configuration2.builder.BuilderParameters;
    022import org.apache.commons.configuration2.builder.FileBasedBuilderParametersImpl;
    023import org.apache.commons.configuration2.ex.ConfigurationException;
    024
    025/**
    026 * <p>
    027 * A specialized implementation of {@link ConfigurationBuilderProvider} which determines the name of the result
    028 * configuration class based on the extension of the file to load.
    029 * </p>
    030 * <p>
    031 * This class works analogously to its base class {@link BaseConfigurationBuilderProvider}; especially, the resulting
    032 * builder is created based on reflection. It extends the super class's functionality by a specific mechanism for
    033 * determining the resulting configuration class: At construction time two configuration class names and a file
    034 * extension are passed in. If a file name is provided in the builder's initialization parameters and this file name has
    035 * the specified extension, then the first configuration class name is used; otherwise the default configuration class
    036 * name is selected.
    037 * </p>
    038 * <p>
    039 * There are some tags for {@code CombinedConfigurationProvider} which can produce different results depending on the
    040 * configuration files they have to load. This class can be used to implement this feature in a generic way.
    041 * </p>
    042 *
    043 * @since 2.0
    044 */
    045public class FileExtensionConfigurationBuilderProvider extends BaseConfigurationBuilderProvider {
    046    /** Constant for the file extension separator. */
    047    private static final char EXT_SEPARATOR = '.';
    048
    049    /** The matching configuration class. */
    050    private final String matchingConfigurationClass;
    051
    052    /** The file extension. */
    053    private final String extension;
    054
    055    /**
    056     * Creates a new instance of {@code FileExtensionConfigurationBuilderProvider}.
    057     *
    058     * @param bldrCls the name of the builder class
    059     * @param reloadBldrCls the name of a builder class to be used if reloading support is required (<b>null</b> if
    060     *        reloading is not supported)
    061     * @param matchingConfigCls the name of the configuration class to be used if the provided file extension matches (must
    062     *        not be <b>null</b>)
    063     * @param defConfigClass the name of the configuration class to be used if the provided file extension does not match
    064     *        (must not be <b>null</b>)
    065     * @param ext the file extension to select the configuration class (must not be <b>null</b>)
    066     * @param paramCls a collection with the names of parameters classes; an instance of a parameters object with basic
    067     *        properties is created automatically and does not need to be contained in this list; the collection can be
    068     *        <b>null</b> if no additional parameter objects are needed
    069     * @throws IllegalArgumentException if a required parameter is missing
    070     */
    071    public FileExtensionConfigurationBuilderProvider(final String bldrCls, final String reloadBldrCls, final String matchingConfigCls,
    072        final String defConfigClass, final String ext, final Collection<String> paramCls) {
    073        super(bldrCls, reloadBldrCls, defConfigClass, paramCls);
    074        if (matchingConfigCls == null) {
    075            throw new IllegalArgumentException("Matching configuration class must not be null!");
    076        }
    077        if (ext == null) {
    078            throw new IllegalArgumentException("File extension must not be null!");
    079        }
    080
    081        matchingConfigurationClass = matchingConfigCls;
    082        extension = ext;
    083    }
    084
    085    /**
    086     * Gets the name of the matching configuration class. This class is used if the file extension matches the extension
    087     * of this provider.
    088     *
    089     * @return the matching configuration class
    090     */
    091    public String getMatchingConfigurationClass() {
    092        return matchingConfigurationClass;
    093    }
    094
    095    /**
    096     * Gets the file extension of this provider.
    097     *
    098     * @return the file extension to match
    099     */
    100    public String getExtension() {
    101        return extension;
    102    }
    103
    104    /**
    105     * {@inheritDoc} This implementation tries to find a {@link FileBasedBuilderParametersImpl} object in the parameter
    106     * objects. If one is found, the extension of the file name is obtained and compared against the stored file extension.
    107     * In case of a match, the matching configuration class is selected, otherwise the default one.
    108     */
    109    @Override
    110    protected String determineConfigurationClass(final ConfigurationDeclaration decl, final Collection<BuilderParameters> params)
    111        throws ConfigurationException {
    112        final String currentExt = extractExtension(fetchCurrentFileName(params));
    113        return getExtension().equalsIgnoreCase(currentExt) ? getMatchingConfigurationClass() : getConfigurationClass();
    114    }
    115
    116    /**
    117     * Tries to obtain the current file name from the given list of parameter objects.
    118     *
    119     * @param params the parameter objects
    120     * @return the file name or <b>null</b> if unspecified
    121     */
    122    private static String fetchCurrentFileName(final Collection<BuilderParameters> params) {
    123        for (final BuilderParameters p : params) {
    124            if (p instanceof FileBasedBuilderParametersImpl) {
    125                final FileBasedBuilderParametersImpl fp = (FileBasedBuilderParametersImpl) p;
    126                return fp.getFileHandler().getFileName();
    127            }
    128        }
    129        return null;
    130    }
    131
    132    /**
    133     * Extracts the extension from the given file name. The name can be <b>null</b>.
    134     *
    135     * @param fileName the file name
    136     * @return the extension (<b>null</b> if there is none)
    137     */
    138    private static String extractExtension(final String fileName) {
    139        if (fileName == null) {
    140            return null;
    141        }
    142
    143        final int pos = fileName.lastIndexOf(EXT_SEPARATOR);
    144        return pos < 0 ? null : fileName.substring(pos + 1);
    145    }
    146}