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.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 tries to 027 * construct a file path from the locator's base path and file name. 028 * </p> 029 * <p> 030 * This strategies ignores the URL stored in the passed in {@link FileLocator}. 031 * It generates a path by concatenating the base path (if present) and the file 032 * name. If the resulting path points to a valid file, the corresponding URL is 033 * returned. 034 * </p> 035 * 036 * @version $Id: BasePathLocationStrategy.java 1842194 2018-09-27 22:24:23Z ggregory $ 037 * @since 2.0 038 */ 039public class BasePathLocationStrategy implements FileLocationStrategy 040{ 041 /** 042 * {@inheritDoc} This implementation uses utility methods from 043 * {@code FileLocatorUtils} to generate a {@code File} from the locator's 044 * base path and file name. If this {@code File} exists, its URL is 045 * returned. 046 */ 047 @Override 048 public URL locate(final FileSystem fileSystem, final FileLocator locator) 049 { 050 if (StringUtils.isNotEmpty(locator.getFileName())) 051 { 052 final File file = 053 FileLocatorUtils.constructFile(locator.getBasePath(), 054 locator.getFileName()); 055 if (file.isFile()) 056 { 057 return FileLocatorUtils.convertFileToURL(file); 058 } 059 } 060 061 return null; 062 } 063}