    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 *     https://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.io;
    018
    019import java.io.File;
    020import java.net.URL;
    021
    022import org.apache.commons.lang3.StringUtils;
    023
    024/**
    025 * A specialized implementation of {@code FileLocationStrategy} which tries to construct a file path from the locator's
    026 * base path and file name.
    027 * <p>
    028 * This strategies ignores the URL stored in the passed in {@link FileLocator}. It generates a path by concatenating the
    029 * base path (if present) and the file name. If the resulting path points to a valid file, the corresponding URL is
    030 * returned.
    031 * </p>
    032 * <p>
    033 * See {@link AbstractFileLocationStrategy} learn how to grant an deny URL schemes and hosts.
    034 * </p>
    035 *
    036 * @see AbstractFileLocationStrategy
    037 * @since 2.0
    038 */
    039public class BasePathLocationStrategy extends AbstractFileLocationStrategy {
    040
    041    /**
    042     * Builds new instances of {@link ProvidedURLLocationStrategy}.
    043     *
    044     * @return a new builder.
    045     * @since 2.15.0
    046     */
    047    public static StrategyBuilder<BasePathLocationStrategy> builder() {
    048        return new StrategyBuilder<>(BasePathLocationStrategy::new);
    049    }
    050
    051    /**
    052     * Constructs a new instance where URL resources are bound by {@link AbstractFileLocationStrategy.AbstractBuilder}.
    053     */
    054    public BasePathLocationStrategy() {
    055        // empty
    056    }
    057
    058    /**
    059     * Constructs a new instance where URL resources are bound by {@link AbstractFileLocationStrategy.AbstractBuilder}.
    060     *
    061     * @param builder How to build the instance.
    062     * @since 2.15.0
    063     */
    064    public BasePathLocationStrategy(final AbstractBuilder<?, ?> builder) {
    065        super(builder);
    066    }
    067
    068    /**
    069     * {@inheritDoc} This implementation uses utility methods from {@code FileLocatorUtils} to generate a {@code File} from
    070     * the locator's base path and file name. If this {@code File} exists, its URL is returned.
    071     */
    072    @Override
    073    public URL locate(final FileSystem fileSystem, final FileLocator locator) {
    074        if (StringUtils.isNotEmpty(locator.getFileName())) {
    075            final File file = FileLocatorUtils.constructFile(locator.getBasePath(), locator.getFileName());
    076            if (file.isFile()) {
    077                return check(FileLocatorUtils.convertFileToURL(file));
    078            }
    079        }
    080        return null;
    081    }
    082}