com.objectwave.utility
Class FileFinder

java.lang.Object
  |
  +--com.objectwave.utility.FileFinder

public class FileFinder
extends java.lang.Object

Useful for doing directory listings or locating a specific file. When doing directory listings via the getAllFilenames command, the default filter is for a file that ends with ".java".
One of the most beneficial commands is the getUrl command. Often times we need to find files for our applications, and Java supports 3 ways to look for these files. Rather than write this same code over and over this provides all 3 in single method.

Version:
$Id: FileFinder.java,v 2.3 2002/02/28 04:19:56 dave_hoag Exp $
Author:
dhoag
See Also:
getUrl(java.lang.Class, java.lang.String), getAllFilenames()

Nested Class Summary
static class FileFinder.Test
           
 
Field Summary
static java.io.FilenameFilter dirFilter
           
static java.io.FilenameFilter JAVA_FILTER
           
 java.lang.String patternString
          Set a pattern string to limit the results to files matching that pattern.
 
Constructor Summary
FileFinder()
          Constructor for the FileFinder object
FileFinder(java.io.File f)
          Constructor for the FileFinder object
 
Method Summary
 java.lang.String[] getAllFilenames()
          Starting at the initial file location, search for all filenames matching the pattern string.
 java.lang.String[] getAllFilenames(java.io.File initialDirectory)
          Starting at the initialDirectory location, search for all filenames matching the pattern string.
 java.lang.String[] getAllFilenames(java.io.File initialDirectory, boolean recurse)
          Starting at the initialDirectory location, search for all filenames matching the pattern string.
 java.lang.String[] getAllFilenames(java.lang.String file)
          Starting at the initialDirectory location, search for all filenames matching the pattern string.
 java.lang.String getPatternString()
          Gets the PatternString attribute of the FileFinder object
 java.lang.String getRelativePath(java.io.File targetFile)
          Find the relative path to the provided file, using the initialFile as the source location.
 java.lang.String getRelativePath(java.io.File sourceDir, java.io.File targetFile)
          Find the relative path between two files.
 java.net.URL getUrl(java.lang.Class sourceClass, java.lang.String fileName)
          Look for the fileName to be an abosolute reference to a file.
 java.io.FilenameFilter getUserFilter()
          Gets the UserFilter attribute of the FileFinder object
protected  void initialize()
           
 java.lang.String[] list()
          Provide a list of files in the initialFile directory.
static void main(java.lang.String[] argv)
          The main program for the FileFinder class
 void setInitialFile(java.io.File f)
          Sets the InitialFile attribute of the FileFinder object
 void setPatternString(java.lang.String str)
          Sets the pattern that is to limit the result set.
 void setUserFilter(java.io.FilenameFilter filt)
          Use the provided FilenameFilter when getting file names.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

JAVA_FILTER

public static final java.io.FilenameFilter JAVA_FILTER

dirFilter

public static final java.io.FilenameFilter dirFilter

patternString

public java.lang.String patternString
Set a pattern string to limit the results to files matching that pattern.

Constructor Detail

FileFinder

public FileFinder()
Constructor for the FileFinder object


FileFinder

public FileFinder(java.io.File f)
Constructor for the FileFinder object

Parameters:
f -
Method Detail

main

public static void main(java.lang.String[] argv)
The main program for the FileFinder class

Parameters:
argv - The command line arguments

setInitialFile

public void setInitialFile(java.io.File f)
Sets the InitialFile attribute of the FileFinder object

Parameters:
f - The new InitialFile value

setPatternString

public void setPatternString(java.lang.String str)
Sets the pattern that is to limit the result set.
ex. "*", or "*.class", or "T*.java".

Parameters:
str - The pattern or null - If null, change filter to be "*.java"

setUserFilter

public void setUserFilter(java.io.FilenameFilter filt)
Use the provided FilenameFilter when getting file names. Use this to customize your results, or use setPatternString and don't worry about FilenameFilter instances at all!

Parameters:
filt - A custom FilenameFilter

getUrl

public java.net.URL getUrl(java.lang.Class sourceClass,
                           java.lang.String fileName)
                    throws java.net.MalformedURLException
Look for the fileName to be an abosolute reference to a file. If not, look for the file to be located in the same directory as the sourceClass. If not, look for the resource to be anywhere on the classpath.

Parameters:
sourceClass -
fileName -
Returns:
The Url value
Throws:
java.net.MalformedURLException

getRelativePath

public java.lang.String getRelativePath(java.io.File targetFile)
Find the relative path to the provided file, using the initialFile as the source location. The initialFile must be set prior to calling this method.

Parameters:
targetFile -
Returns:
The RelativePath value

getRelativePath

public java.lang.String getRelativePath(java.io.File sourceDir,
                                        java.io.File targetFile)
Find the relative path between two files.

Parameters:
sourceDir - Starting at this file
targetFile - navigate to this file
Returns:
The RelativePath value

getAllFilenames

public java.lang.String[] getAllFilenames()
Starting at the initial file location, search for all filenames matching the pattern string.

Returns:
The AllFilenames value

getAllFilenames

public java.lang.String[] getAllFilenames(java.io.File initialDirectory)
Starting at the initialDirectory location, search for all filenames matching the pattern string.

Parameters:
initialDirectory - A file representing a directory
Returns:
All filenames that match the pattern string

getAllFilenames

public java.lang.String[] getAllFilenames(java.io.File initialDirectory,
                                          boolean recurse)
Starting at the initialDirectory location, search for all filenames matching the pattern string. If recurse is true, examine all subdirectories for additional files matching the pattern.

Parameters:
initialDirectory - The place to begin the file search
recurse - boolean indicating to to traverse subdirectories
Returns:
All filenames that match the pattern string

getAllFilenames

public java.lang.String[] getAllFilenames(java.lang.String file)
Starting at the initialDirectory location, search for all filenames matching the pattern string.

Parameters:
file -
Returns:
All filenames that match the pattern string

getPatternString

public java.lang.String getPatternString()
Gets the PatternString attribute of the FileFinder object

Returns:
The PatternString value

getUserFilter

public java.io.FilenameFilter getUserFilter()
Gets the UserFilter attribute of the FileFinder object

Returns:
The UserFilter value

list

public java.lang.String[] list()
Provide a list of files in the initialFile directory.

Returns:

initialize

protected void initialize()