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

FileWalker

public class FileWalker extends Object
This class provides services to navigate through a file directory and handle files that match a name filter.
author
Manfred Duchrow
version
1.2

Fields Summary
public static final char
PATTERN_SEPARATOR_CHAR
The character to be used to separate filename patterns (';').
public static final String
PATTERN_SEPARATOR
The character to be used to separate filename patterns (';') as String.
private FileHandler
fileHandler
private boolean
goOn
private Character
digitWildcard
Constructors Summary
public FileWalker(FileHandler handler)
Initialize the new instance with default values.

  	this.setFileHandler( handler ) ;
  
public FileWalker(FileHandler handler, char digitWildcard)
Initialize the new instance with a file handler and a wildcard character for digits.

param
handler The file handler that gets all found files
param
digitWildcard A character that is used as wildcard for digits in filname patterns

  	this( handler ) ;
		this.setDigitWildcardChar( digitWildcard ) ;  	
  
Methods Summary
protected java.lang.String[]extractPatterns(java.lang.String pattern)

		return StringUtil.current().parts( pattern, PATTERN_SEPARATOR ) ;
	
protected java.lang.CharactergetDigitWildcard()

      return digitWildcard ; 
protected chargetDigitWildcardChar()

		if ( this.hasDigitWildcard() )
			return this.getDigitWildcard().charValue() ;
		else
			return '\0" ;
	
protected FileHandlergetFileHandler()

      return fileHandler ; 
protected booleangetGoOn()

      return goOn ; 
protected booleanhasDigitWildcard()

		return this.getDigitWildcard() != null ;
	
protected voidsetDigitWildcard(java.lang.Character newValue)

 digitWildcard = newValue ; 
public voidsetDigitWildcardChar(char digitWildcard)
Sets the given character as a wildcard character to match digits ('0'-'9') only.

param
digitWildcard The placeholder character for digits

		if ( digitWildcard <= 0 )
		{
			this.setDigitWildcard( null ) ;
		}
		else
		{
			this.setDigitWildcard( new Character( digitWildcard ) ) ;
		}
	
protected voidsetFileHandler(FileHandler newValue)

 fileHandler = newValue ; 
protected voidsetGoOn(boolean newValue)

 goOn = newValue ; 
protected longwalkThrough(java.lang.String dir, java.io.FilenameFilter filter, boolean recursive)

		long counter			= 0 ;
		File directory		= null ;
		File file					= null ;
		File[] files			= null ;
		int index					= 0 ;
		
		directory = new File( dir ) ;
		files = directory.listFiles( filter ) ;
		if ( files == null )  // BUGFIX suggested by Kyle Gossman
			return counter ;
			
		this.setGoOn( this.getFileHandler().directoryStart( directory, files.length ) ) ;
		if ( ! this.getGoOn() )
			return counter ;
		
		for ( index = 0 ; index < files.length ; index++ )
		{
			file = files[index] ;
			
			if ( file.isDirectory() )
			{
				if ( recursive )
				{
					counter += this.walkThrough( file.getPath(), filter, recursive ) ;
				}
			}
			else
			{
				this.setGoOn( this.getFileHandler().handleFile( file ) ) ;
				counter++ ;
			}
			if ( ! this.getGoOn() )
				break ;
		} // for
		this.setGoOn( this.getFileHandler().directoryEnd( directory ) ) ;
				
		return counter ;
	
public longwalkThrough(java.lang.String dir, java.lang.String pattern, boolean recursive)
This method starts in the given directory to search for all files matching the given pattern(s).
There can be more than one pattern in the pattern parameter. They have to be separated by the PATTERN_SEPARATOR (';').

If recursive is true it goes down to each subdirectory and doing the same there.
For each matching file (non-directory) the defined FileHandler.handle() is called.

param
dir The directory where to start
param
pattern The file name pattern(s) for filtering out the correct files ( wildcards '*' and '?' )
param
recursive If set to true, the file selection is going down to all subdirectories
return
the number of found files, that have matched the given pattern.

		ExtendedFileFilter filter		= null ;
		String[] patterns						= null ;
		String strPattern ;
		
		this.setGoOn( true ) ;
		filter = new ExtendedFileFilter() ;
		
		patterns = this.extractPatterns( pattern ) ;
		for (int i = 0; i < patterns.length; i++)
		{
			strPattern = patterns[i] ;
			if ( this.hasDigitWildcard() )
			{
				filter.addPattern( strPattern, true, this.getDigitWildcardChar() ) ;
			}
			else
			{
				filter.addPattern( strPattern, true ) ;
			}
		}
		
		if ( recursive )
			filter.alwaysIncludeDirectories() ;
		else
			filter.alwaysExcludeDirectories() ;

		return this.walkThrough( dir, filter, recursive ) ;