001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.core.io; 018 019/** 020 * {@link ResourceLoader} implementation that resolves plain paths as 021 * file system resources rather than as class path resources 022 * (the latter is {@link DefaultResourceLoader}'s default strategy). 023 * 024 * <p><b>NOTE:</b> Plain paths will always be interpreted as relative 025 * to the current VM working directory, even if they start with a slash. 026 * (This is consistent with the semantics in a Servlet container.) 027 * <b>Use an explicit "file:" prefix to enforce an absolute file path.</b> 028 * 029 * <p>{@link org.springframework.context.support.FileSystemXmlApplicationContext} 030 * is a full-fledged ApplicationContext implementation that provides 031 * the same resource path resolution strategy. 032 * 033 * @author Juergen Hoeller 034 * @since 1.1.3 035 * @see DefaultResourceLoader 036 * @see org.springframework.context.support.FileSystemXmlApplicationContext 037 */ 038public class FileSystemResourceLoader extends DefaultResourceLoader { 039 040 /** 041 * Resolve resource paths as file system paths. 042 * <p>Note: Even if a given path starts with a slash, it will get 043 * interpreted as relative to the current VM working directory. 044 * @param path the path to the resource 045 * @return the corresponding Resource handle 046 * @see FileSystemResource 047 * @see org.springframework.web.context.support.ServletContextResourceLoader#getResourceByPath 048 */ 049 @Override 050 protected Resource getResourceByPath(String path) { 051 if (path != null && path.startsWith("/")) { 052 path = path.substring(1); 053 } 054 return new FileSystemContextResource(path); 055 } 056 057 058 /** 059 * FileSystemResource that explicitly expresses a context-relative path 060 * through implementing the ContextResource interface. 061 */ 062 private static class FileSystemContextResource extends FileSystemResource implements ContextResource { 063 064 public FileSystemContextResource(String path) { 065 super(path); 066 } 067 068 @Override 069 public String getPathWithinContext() { 070 return getPath(); 071 } 072 } 073 074}