/**
* Part of the Hippikon API, a powerful authoriation
* security framework for Java applications.
*
* Copyright (C) 2005 Dale Churchett
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Contact: Dale Churchett <dale@hippikon.com>
* Website: http://www.hippikon.com, http://www.hippikon.org
*
*/
package com.hippikon.io;
import java.io.*;
import java.util.StringTokenizer;
/**
* Provides a utility method for file based I/O
* operations that need to find a file in the system classpath. With the
* <code>findFileInClasspath()</code> method, a named file need only be located
* on the classpath, negating the need for providing absolute paths, reletive paths
* or messing around with various system properties.<p>
*
* If an application needs to add search paths to the system classpath, the
* <code>fileutil.search.path</code> attribute may be specified as an
* environment variable, where the values are additional absolute pathnames to
* directories in which to search. This class is platform independent.<p>
*
* <b>Example:</b><p>
*
* File myFile = FileUtil.findFileInClasspath("my-properties-file.txt");<p>
*
* @author Dale Churchett
* @version $Id: FileUtil.java,v 1.3 2005/07/14 22:42:55 dalehippikon Exp $
* @since JDK 1.2.2
*/
public abstract class FileUtil {
private static final String PS = System.getProperty("path.separator");
private static final String FS = System.getProperty("file.separator");
private static final String CLASSPATH = System.getProperty("java.class.path");
private static final String ADDITIONAL_SEARCH_PATH = System.getProperty("fileutil.search.path", "");
/**
* Returns a File object located on the system classpath by a specified
* filename. If the file is not found in a classpath directory, the "fileutil.search.path"
* environment variable is checked if set.<p>
*
* The filename passed as a method parameter should and not
* contain any path information (e.g., "myfile.txt").<p>
*
* @param filename a relative filename (e.g., myfile.txt)
*
* @exception FileNotFoundException thrown if the file could not be
* found or a file reference obtained
*/
public static final File findFileInClasspath(String filename) throws IOException {
String searchPaths = CLASSPATH + PS + ADDITIONAL_SEARCH_PATH;
StringTokenizer st = new StringTokenizer(searchPaths, PS);
while (st.hasMoreElements()) {
String path = (String)st.nextElement();
File f = new File(path + FS + filename);
if (f.exists()) return f;
}
throw new FileNotFoundException("Could not find " + filename + " in the classpath");
}
}