package org.springframework.core.io;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Resource implementation for classpath resources.
 * Uses either the Thread context class loader
 * or a given Class for loading resources.
 * @author Juergen Hoeller
 * @since 28.12.2003
 * @see java.lang.Thread#getContextClassLoader
 * @see java.lang.ClassLoader#getResourceAsStream
 * @see java.lang.Class#getResourceAsStream
 */
public class ClassPathResource extends AbstractResource {

	private final String path;

	private Class clazz;

	/**
	 * Create a new ClassPathResource for ClassLoader usage.
	 * A leading slash will be removed, as the ClassLoader
	 * resource access methods will not accept it.
	 * @param path the absolute path within the classpath
	 * @see java.lang.ClassLoader#getResourceAsStream
	 */
	public ClassPathResource(String path) {
		if (path.startsWith("/")) {
			path = path.substring(1);
		}
		this.path = path;
	}

	/**
	 * Create a new ClassPathResource for Class usage.
	 * The path can be relative to the given class,
	 * or absolute within the classpath via a leading slash.
	 * @param path relative or absolute path within the classpath
	 * @param clazz the class to load resources with
	 * @see java.lang.Class#getResourceAsStream
	 */
	public ClassPathResource(String path, Class clazz) {
		this.path = path;
		this.clazz = clazz;
	}

	public InputStream getInputStream() throws IOException {
		InputStream is = null;
		if (this.clazz != null) {
			is = this.clazz.getResourceAsStream(this.path);
		}
		else {
			ClassLoader ccl = Thread.currentThread().getContextClassLoader();
			is = ccl.getResourceAsStream(this.path);
		}
		if (is == null) {
			throw new FileNotFoundException("Could not open " + getDescription());
		}
		return is;
	}

	public String getDescription() {
		return "classpath resource [" + this.path + "]";
	}

}

@@ -31,16 +31,37 @@
 re.io;%0A%0A
+import java.io.File;%0A
 import j
@@ -145,16 +145,37 @@
 tStream;
+%0Aimport java.net.URL;
 %0A%0A/**%0A *
@@ -204,24 +204,25 @@
 on for class
+ 
 path resourc
@@ -271,19 +271,16 @@
 s loader
-%0A *
  or a gi
@@ -282,16 +282,19 @@
  a given
+%0A *
  Class f
@@ -315,16 +315,146 @@
 ources.%0A
+ *%0A * %3Cp%3ESupports resolution as File if the class path resource%0A * resides in the file system, but not for resources in a JAR.%0A *%0A
  * @auth
@@ -697,16 +697,74 @@
 urce %7B%0A%0A
+%09private static final String URL_PROTOCOL_FILE = %22file%22;%0A%0A
 %09private
@@ -2043,24 +2043,741 @@
 urn is;%0A%09%7D%0A%0A
+%09public File getFile() throws IOException %7B%0A%09%09URL url = null;%0A%09%09if (this.clazz != null) %7B%0A%09%09%09url = this.clazz.getResource(this.path);%0A%09%09%7D%0A%09%09else %7B%0A%09%09%09ClassLoader ccl = Thread.currentThread().getContextClassLoader();%0A%09%09%09url = ccl.getResource(this.path);%0A%09%09%7D%0A%09%09if (url == null) %7B%0A%09%09%09throw new FileNotFoundException(getDescription() + %22 cannot be resolved to absolute file path %22 +%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%22because it does not exist%22);%0A%09%09%7D%0A%09%09if (!URL_PROTOCOL_FILE.equals(url.getProtocol())) %7B%0A%09%09%09throw new FileNotFoundException(getDescription() + %22 cannot be resolved to absolute file path %22 +%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09%22because it does not reside in the file system: URL=%5B%22 + url + %22%5D%22);%0A%09%09%7D%0A%09%09return new File(url.getFile());%0A%09%7D%0A%0A
 %09public Stri

return a File on "getFile" if the classpath resource resides in the file system