com.ibm.are.common
Class DirectoryTreeBuilder

java.lang.Object
  extended by com.ibm.are.common.DirectoryTreeBuilder
All Implemented Interfaces:
java.io.FileFilter

public class DirectoryTreeBuilder
extends java.lang.Object
implements java.io.FileFilter

The DirectoryTreeBuilder class provides a simple interface for iterating through the contents of a directory tree (a directory and all of its sub-directories). Options are provided to specify how symbolic links should be handled (follow or do not follow), as well as the ability to exclude certainly directories from being traversed when iterating through the directory tree.

It should be noted that this class uses a recursive algorithm to traverse the directory tree. Further, the contents of the entire directory tree are stored in an internally managed list. Generally these attributes are not problematic, but for very large directory trees this may become a performance issue. For iterating through very large directory trees, the LargeDirTreeIterator class provides better performance and has a much smaller memory footprint than this class.

Version:
1.0
Author:
IBM

Field Summary
static java.lang.String COPYRIGHT
          Copyright for class bytecode
protected  java.util.ArrayList entireList
          The entire list of items in the directory tree
protected  java.io.FileFilter fileFilter
          File filter
static int INFINITE_DEPTH
          Represents no maximum depth limit when traversing the directory tree
 
Constructor Summary
DirectoryTreeBuilder(java.lang.String root)
          Constructs a DirectoryTreeBuilder object with the specified root directory
DirectoryTreeBuilder(java.lang.String root, java.io.FileFilter filter)
          Constructs a DirectoryTreeBuilder object with the specified root directory and filter.
 
Method Summary
 boolean accept(java.io.File pathname)
          Determines whether or not the current file or directory should be accepted and thus made part of the iterator.
 void addExcludeDirectories(StringList dirList)
          Adds a list of directories that will NOT be traversed by the iterator
 void addExcludeDirectory(java.lang.String dirName)
          Adds a new directory to the list of directories that will NOT be traversed by the iterator
protected  void buildListOfFilesAndDirs(java.io.File rootDir, int level)
          Builds the list of files and directories in the tree.
 java.util.Iterator buildTree(boolean sorted)
          Builds a list of the directory tree contents and returns an iterator over the list
 java.io.File getRootDirectory()
          Retrieves the root directory for the tree
 java.util.Iterator getTree()
          Retrieves an iterator over the entire contents of the directory tree
 int numberOfDirectories()
          Retrieves the number of directories in the directory tree
 int numberOfFiles()
          Retrieves the number of files in the directory tree
 void setFollowSymbolicLinks(boolean yesOrNo)
          Sets whether or not symbolic links are followed.
 void setMaximumDepth(int newDepth)
          Sets the maximum depth that will be traversed in the directory tree.
 void setNumberOfDirectories(int numberOfDirectories)
          Deprecated. Sets the number of directories in the directory tree. This method is deprecated and will be removed in a future release.
 void setNumberOfFiles(int numberOfFiles)
          Deprecated. Sets the number of files in the directory tree. This method is deprecated and will be removed in a future release.
 void setRootDirectory(java.lang.String rootDir)
          Sets the root directory for the tree
 void sort()
          Alphabetically sorts the list of the directory tree contents.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

COPYRIGHT

public static final java.lang.String COPYRIGHT
Copyright for class bytecode

See Also:
Constant Field Values

INFINITE_DEPTH

public static final int INFINITE_DEPTH
Represents no maximum depth limit when traversing the directory tree

See Also:
Constant Field Values

entireList

protected java.util.ArrayList entireList
The entire list of items in the directory tree


fileFilter

protected java.io.FileFilter fileFilter
File filter

Constructor Detail

DirectoryTreeBuilder

public DirectoryTreeBuilder(java.lang.String root)
Constructs a DirectoryTreeBuilder object with the specified root directory

Parameters:
root - The root of the directory tree

DirectoryTreeBuilder

public DirectoryTreeBuilder(java.lang.String root,
                            java.io.FileFilter filter)
Constructs a DirectoryTreeBuilder object with the specified root directory and filter.

Parameters:
root - The root of the directory tree
filter - An optional filter to only select certain items in the tree
Method Detail

addExcludeDirectory

public void addExcludeDirectory(java.lang.String dirName)
Adds a new directory to the list of directories that will NOT be traversed by the iterator

Parameters:
dirName - The name of the directory to add. Must be the full path name of the directory.

addExcludeDirectories

public void addExcludeDirectories(StringList dirList)
Adds a list of directories that will NOT be traversed by the iterator

Parameters:
dirList - The list of directories to add. Each directory must be the full path name of the directory.

buildTree

public java.util.Iterator buildTree(boolean sorted)
Builds a list of the directory tree contents and returns an iterator over the list

Parameters:
sorted - Whether or not the contents within each directory are sorted alphabetically
Returns:
An iterator over the entire contents of the directory tree

sort

public void sort()
Alphabetically sorts the list of the directory tree contents.


getTree

public java.util.Iterator getTree()
Retrieves an iterator over the entire contents of the directory tree

Returns:
An iterator over the entire contents of the directory tree

numberOfDirectories

public int numberOfDirectories()
Retrieves the number of directories in the directory tree

Returns:
The number of directories in the directory tree

setNumberOfDirectories

public void setNumberOfDirectories(int numberOfDirectories)
Deprecated. Sets the number of directories in the directory tree. This method is deprecated and will be removed in a future release.

Parameters:
numberOfDirectories - The number of directories

numberOfFiles

public int numberOfFiles()
Retrieves the number of files in the directory tree

Returns:
The number of files in the directory tree

setNumberOfFiles

public void setNumberOfFiles(int numberOfFiles)
Deprecated. Sets the number of files in the directory tree. This method is deprecated and will be removed in a future release.

Parameters:
numberOfFiles - The number of files

getRootDirectory

public java.io.File getRootDirectory()
Retrieves the root directory for the tree

Returns:
The root directory for the tree

setRootDirectory

public void setRootDirectory(java.lang.String rootDir)
Sets the root directory for the tree

Parameters:
rootDir - The root directory for the tree

setFollowSymbolicLinks

public void setFollowSymbolicLinks(boolean yesOrNo)
Sets whether or not symbolic links are followed. This is only applicable to symbolic links to directories.

Parameters:
yesOrNo - true if symbolic links should be followed, false if they should not

setMaximumDepth

public void setMaximumDepth(int newDepth)
Sets the maximum depth that will be traversed in the directory tree. Default maximum depth is infinite.

Parameters:
newDepth - The maximum depth that will be traversed in the directory tree.

buildListOfFilesAndDirs

protected void buildListOfFilesAndDirs(java.io.File rootDir,
                                       int level)
Builds the list of files and directories in the tree.

Parameters:
rootDir - The directory tree root
level - The current level (i.e. as we are traversing) within the directory tree

accept

public boolean accept(java.io.File pathname)
Determines whether or not the current file or directory should be accepted and thus made part of the iterator. Defaults to always returning true

Specified by:
accept in interface java.io.FileFilter