    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 * <p>
    026 * A specialized implementation of {@code FileLocationStrategy} which checks whether the provided file name is already
    027 * an absolute file name.
    028 * </p>
    029 * <p>
    030 * This strategy ignores the URL and the base path stored in the passed in {@link FileLocator}. It is only triggered by
    031 * absolute names in the locator's {@code fileName} component.
    032 * </p>
    033 *
    034 * @since 2.0
    035 */
    036public class AbsoluteNameLocationStrategy implements FileLocationStrategy {
    037
    038    /**
    039     * A singleton instance of this strategy.
    040     */
    041    static final AbsoluteNameLocationStrategy INSTANCE = new AbsoluteNameLocationStrategy();
    042
    043    /**
    044     * Constructs a new instance.
    045     */
    046    public AbsoluteNameLocationStrategy() {
    047        // empty
    048    }
    049
    050    /**
    051     * {@inheritDoc} This implementation constructs a {@code File} object from the locator's file name (if defined). If this
    052     * results in an absolute file name pointing to an existing file, the corresponding URL is returned.
    053     */
    054    @Override
    055    public URL locate(final FileSystem fileSystem, final FileLocator locator) {
    056        if (StringUtils.isNotEmpty(locator.getFileName())) {
    057            final File file = new File(locator.getFileName());
    058            if (file.isAbsolute() && file.exists()) {
    059                return FileLocatorUtils.convertFileToURL(file);
    060            }
    061        }
    062
    063        return null;
    064    }
    065}