FileDocCategorySizeDatePackage
FileFinder.javaAPI DocAzureus 3.0.3.411101Fri Mar 12 11:00:20 GMT 2004org.pf.file

FileFinder

public class FileFinder extends Object implements FileHandler
Helper class with convenient methods to find files.
author
Manfred Duchrow
version
1.2

Fields Summary
private List
collectedFiles
Constructors Summary
protected FileFinder()
Initialize the new instance with default values.

  	super() ;
  	this.setCollectedFiles( new ArrayList() ) ;
  
Methods Summary
protected java.io.File[]collectFiles(java.lang.String dir, java.lang.String pattern, boolean recursive, java.lang.Character digitWildcard)

		FileWalker fileWalker ;
		List list ;
		
		fileWalker = new FileWalker( this ) ;
		if ( digitWildcard != null )
			fileWalker.setDigitWildcardChar( digitWildcard.charValue() ) ;
			
		fileWalker.walkThrough( dir, pattern, recursive ) ;
		list = this.getCollectedFiles() ;
		return (File[])list.toArray( new File[list.size()]) ;
	
public booleandirectoryEnd(java.io.File dir)
This method is called for each directory, that a FileWalker finished to walk through. It must return true, if the FileWalker should continue. To stop the calling FileWalker it can return false.

param
dir The directory, the FileWalker has finished to walk through
return
true to continue, false to terminate processing of files

		return true ;
	
public booleandirectoryStart(java.io.File dir, int count)
This method is called for each directory, that a FileWalker starts to walk through. It must return true, if the FileWalker should continue. To stop the calling FileWalker it can return false.

param
dir The directory, the FileWalker is starting to walk through
param
count The number of files and directories the FileWalker found in the directory
return
true to continue, false to terminate processing of files

		return true ;
	
private static booleanfileExists(java.io.File file)

		boolean success = false ;
		if ( file != null )
		{
			try
			{
				FileLocator locator = FileLocator.create( file ) ;
				success = locator.exists() ;
			}
			catch ( Exception ex )
			{
				// nothing to do here
			}
		}
		return success ;
	
public static java.io.FilefindFile(java.lang.String filename)
Tries to find the file with the given Name. First it looks if the file exists directly under the given name. If not, it searches the classpath to find it. If the file was found and really exists, then it will be returned. In all other cases null will be returned.

  	File aFile	= null ;
  	
  	aFile = new File( filename ) ;
  	if ( fileExists( aFile ) )
  		return aFile ;
  	
  	aFile = findFileOnClasspath( filename ) ;
  	
  	return aFile ;
  
public static java.io.FilefindFileOnClasspath(java.lang.String filename)
Tries to find the file with the given Name on the classpath. If the file was found and really exists, then it will be returned. In all other cases null will be returned.

    ClassLoader cl 							= null ;
    File file										= null ;
    URL url											= null ;

    try
    {
      cl = FileFinder.class.getClassLoader() ;
      if ( cl == null )
      {
        // System.out.println( "No classloader found !\n<P>" ) ;
        return null ;
      }
      url = cl.getResource( filename ) ;
      if ( url == null )
      {
        // System.out.println( "Settings file '" + filename + "' not found in CLASSPATH !!!" ) ;
      }
      else
      {
        file = new File( url.getFile() ) ;
        // System.out.println( "Settings file '" + file.getAbsolutePath() + "' exists: " + file.exists() ) ;
        if ( ! fileExists( file ) )
          file = null ;
      }
    }
    catch ( Exception ex )
    {
      // ex.printStackTrace() ;
    }
    return file ;
  
public static java.io.File[]findFiles(java.lang.String dir, java.lang.String pattern)
Return all files that match the given pattern(s) start searching in the specified dir. Searches in all sub directories as well. More than one pattern can be specified in parameter pattern. They have to be separated by ';'.

param
dir The directory to start searching (must not be null)
param
pattern The pattern(s) the filenames must match (must not be null )
return
All file found that matched to at least one of the patterns
throws
IllegalArgumentException If dir or pattern is null

		return findFiles( dir, pattern, true ) ;
	
public static java.io.File[]findFiles(java.lang.String dir, java.lang.String pattern, boolean recursive)
Return all files that match the given pattern(s) start searching in the specified dir. Look into sub directories if recursive is true. More than one pattern can be specified in parameter pattern. They have to be separated by ';'.

param
dir The directory to start searching (must not be null)
param
pattern The pattern(s) the filenames must match (must not be null )
param
recursive If false, only dir is searched, otherwise all sub directories as well
return
All file found that matched to at least one of the patterns
throws
IllegalArgumentException If dir or pattern is null

		return findFiles( dir, pattern, recursive, (char)0 ) ;
	
public static java.io.File[]findFiles(java.lang.String dir, java.lang.String pattern, boolean recursive, char digitWildcard)
Return all files that match the given pattern(s) start searching in the specified dir. Look into sub directories if recursive is true. Use the given digit wildcard in patterns to match single digits in filenames.
More than one pattern can be specified in parameter pattern. They have to be separated by ';'.

param
dir The directory to start searching (must not be null)
param
pattern The pattern(s) the filenames must match (must not be null )
param
recursive If false, only dir is searched, otherwise all sub directories as well
param
digitWildcard The wildcard character for digit representation in the pattern(s)
return
All file found that matched to at least one of the patterns
throws
IllegalArgumentException If dir or pattern is null

		FileFinder finder ;
		Character digitChar = null ;
		
		if ( dir == null )
			throw new IllegalArgumentException( "FileFinder.findFiles(): dir is null" ) ;

		if ( pattern == null )
			throw new IllegalArgumentException( "FileFinder.findFiles(): pattern is null" ) ;
		
		if ( digitWildcard > 0 )
			digitChar = new Character(digitWildcard) ;
		
		finder = new FileFinder() ;
		return finder.collectFiles( dir, pattern, recursive, digitChar ) ;
	
protected java.util.ListgetCollectedFiles()

      return collectedFiles ; 
public booleanhandleException(java.lang.Exception ex, java.io.File file)
This method is called for whenever an exception occurs in walking through the directories.
The method must return true, if the FileWalker should continue. To stop the calling FileWalker it can return false.

param
ex The exception to handle
param
The file, currently found by the FileWalker instance
return
true to continue, false to terminate processing of files

		// System.out.println( "Problem with '" + file + "'" ) ;
		// System.out.println( ex ) ;
		return false ;
	
public booleanhandleFile(java.io.File file)
This method is called for each file, that a FileWalker instance finds. It must return true, if the FileWalker should continue. To stop the calling FileWalker it can return false.

param
The file, currently found by the FileWalker instance
return
true to continue, false to terminate processing of files

		this.getCollectedFiles().add( file ) ;			
		return true ;
	
protected voidsetCollectedFiles(java.util.List newValue)

 collectedFiles = newValue ;