    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 checks whether the provided file name is already
    026 * an absolute file name.
    027 * <p>
    028 * This strategy ignores the URL and the base path stored in the passed in {@link FileLocator}. It is only triggered by
    029 * absolute names in the locator's {@code fileName} component.
    030 * </p>
    031 * <p>
    032 * See {@link AbstractFileLocationStrategy} learn how to grant an deny URL schemes and hosts.
    033 * </p>
    034 *
    035 * @see AbstractFileLocationStrategy
    036 * @since 2.0
    037 */
    038public class AbsoluteNameLocationStrategy extends AbstractFileLocationStrategy {
    039
    040    /**
    041     * Builds new instances of {@link ProvidedURLLocationStrategy}.
    042     *
    043     * @return a new builder.
    044     * @since 2.15.0
    045     */
    046    public static StrategyBuilder<AbsoluteNameLocationStrategy> builder() {
    047        return new StrategyBuilder<>(AbsoluteNameLocationStrategy::new);
    048    }
    049
    050    /**
    051     * Constructs a new instance where URL resources are bound by {@link AbstractFileLocationStrategy.AbstractBuilder}.
    052     */
    053    public AbsoluteNameLocationStrategy() {
    054        // empty
    055    }
    056
    057    /**
    058     * Constructs a new instance where URL resources are bound by {@link AbstractFileLocationStrategy.AbstractBuilder}.
    059     *
    060     * @param builder How to build the instance.
    061     * @since 2.15.0
    062     */
    063    public AbsoluteNameLocationStrategy(final AbstractBuilder<?, ?> builder) {
    064        super(builder);
    065    }
    066
    067    /**
    068     * {@inheritDoc}
    069     * <p>
    070     * This implementation constructs a {@code File} object from the locator's file name (if defined). If this
    071     * results in an absolute file name pointing to an existing file, the corresponding URL is returned.
    072     * </p>
    073     */
    074    @Override
    075    public URL locate(final FileSystem fileSystem, final FileLocator locator) {
    076        if (StringUtils.isNotEmpty(locator.getFileName())) {
    077            final File file = new File(locator.getFileName());
    078            if (file.isAbsolute() && file.exists()) {
    079                return check(FileLocatorUtils.convertFileToURL(file));
    080            }
    081        }
    082        return null;
    083    }
    084}