Java Doc for DirectoryWalker.java in  » Library » apache-common-IO » org » apache » commons » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » apache common IO » org.apache.commons.io 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.apache.commons.io.DirectoryWalker

DirectoryWalker
abstract public class DirectoryWalker (Code)
Abstract class that walks through a directory hierarchy and provides subclasses with convenient hooks to add specific behaviour.

This class operates with a FileFilter and maximum depth to limit the files and direcories visited. Commons IO supplies many common filter implementations in the filefilter package.

The following sections describe:

1. Example Implementation

There are many possible extensions, for example, to delete all files and '.svn' directories, and return a list of deleted files:
 public class FileCleaner extends DirectoryWalker {
 public FileCleaner() {
 super();
 }
 public List clean(File startDirectory) {
 List results = new ArrayList();
 walk(startDirectory, results);
 return results;
 }
 protected boolean handleDirectory(File directory, int depth, Collection results) {
 // delete svn directories and then skip
 if (".svn".equals(directory.getName())) {
 directory.delete();
 return false;
 } else {
 return true;
 }
 }
 protected void handleFile(File file, int depth, Collection results) {
 // delete file and add to list of deleted
 file.delete();
 results.add(file);
 }
 }
 

2. Filter Example

Choosing which directories and files to process can be a key aspect of using this class. This information can be setup in three ways, via three different constructors.

The first option is to visit all directories and files. This is achieved via the no-args constructor.

The second constructor option is to supply a single FileFilter that describes the files and directories to visit. Care must be taken with this option as the same filter is used for both directories and files.

For example, if you wanted all directories which are not hidden and files which end in ".txt":

 public class FooDirectoryWalker extends DirectoryWalker {
 public FooDirectoryWalker(FileFilter filter) {
 super(filter, -1);
 }
 }
 // Build up the filters and create the walker
 // Create a filter for Non-hidden directories
 IOFileFilter fooDirFilter = 
 FileFilterUtils.andFileFilter(FileFilterUtils.directoryFileFilter,
 HiddenFileFilter.VISIBLE);
 // Create a filter for Files ending in ".txt"
 IOFileFilter fooFileFilter = 
 FileFilterUtils.andFileFilter(FileFilterUtils.fileFileFilter,
 FileFilterUtils.suffixFileFilter(".txt"));
 // Combine the directory and file filters using an OR condition
 java.io.FileFilter fooFilter = 
 FileFilterUtils.orFileFilter(fooDirFilter, fooFileFilter);
 // Use the filter to construct a DirectoryWalker implementation
 FooDirectoryWalker walker = new FooDirectoryWalker(fooFilter);
 

The third constructor option is to specify separate filters, one for directories and one for files. These are combined internally to form the correct FileFilter, something which is very easy to get wrong when attempted manually, particularly when trying to express constructs like 'any file in directories named docs'.

For example, if you wanted all directories which are not hidden and files which end in ".txt":

 public class FooDirectoryWalker extends DirectoryWalker {
 public FooDirectoryWalker(IOFileFilter dirFilter, IOFileFilter fileFilter) {
 super(dirFilter, fileFilter, -1);
 }
 }
 // Use the filters to construct the walker
 FooDirectoryWalker walker = new FooDirectoryWalker(
 HiddenFileFilter.VISIBLE,
 FileFilterUtils.suffixFileFilter(".txt"),
 );
 
This is much simpler than the previous example, and is why it is the preferred option for filtering.

3. Cancellation

The DirectoryWalker contains some of the logic required for cancel processing. Subclasses must complete the implementation.

What DirectoryWalker does provide for cancellation is:

  • CancelException which can be thrown in any of the lifecycle methods to stop processing.
  • The walk() method traps thrown CancelException and calls the handleCancelled() method, providing a place for custom cancel processing.

Implementations need to provide:

  • The decision logic on whether to cancel processing or not.
  • Constructing and throwing a CancelException .
  • Custom cancel processing in the handleCancelled() method.

Two possible scenarios are envisaged for cancellation:

The following sections provide example implementations for these two different scenarios.

3.1 External / Multi-threaded

This example provides a public cancel() method that can be called by another thread to stop the processing. A typical example use-case would be a cancel button on a GUI. Calling this method sets a volatile flag to ensure it will work properly in a multi-threaded environment. The flag is returned by the handleIsCancelled() method, which will cause the walk to stop immediately. The handleCancelled() method will be the next, and last, callback method received once cancellation has occurred.
 public class FooDirectoryWalker extends DirectoryWalker {
 private volatile boolean cancelled = false;
 public void cancel() {
 cancelled = true;
 }
 private void handleIsCancelled(File file, int depth, Collection results) {
 return cancelled;
 }
 protected void handleCancelled(File startDirectory, Collection results, CancelException cancel) {
 // implement processing required when a cancellation occurs
 }
 }
 

3.2 Internal

This shows an example of how internal cancellation processing could be implemented. Note the decision logic and throwing a CancelException could be implemented in any of the lifecycle methods.
 public class BarDirectoryWalker extends DirectoryWalker {
 protected boolean handleDirectory(File directory, int depth, Collection results) throws IOException {
 // cancel if hidden directory
 if (directory.isHidden()) {
 throw new CancelException(file, depth);
 }
 return true;
 }
 protected void handleFile(File file, int depth, Collection results) throws IOException {
 // cancel if read-only file
 if (!file.canWrite()) {
 throw new CancelException(file, depth);
 }
 results.add(file);
 }
 protected void handleCancelled(File startDirectory, Collection results, CancelException cancel) {
 // implement processing required when a cancellation occurs
 }
 }
 

since:
   Commons IO 1.3
version:
   $Revision: 424748 $

Inner Class :public static class CancelException extends IOException


Constructor Summary
protected  DirectoryWalker()
     Construct an instance with no filtering and unlimited depth.
protected  DirectoryWalker(FileFilter filter, int depthLimit)
     Construct an instance with a filter and limit the depth navigated to.

The filter controls which files and directories will be navigated to as part of the walk.

protected  DirectoryWalker(IOFileFilter directoryFilter, IOFileFilter fileFilter, int depthLimit)
     Construct an instance with a directory and a file filter and an optional limit on the depth navigated to.

The filters control which files and directories will be navigated to as part of the walk.


Method Summary
final protected  voidcheckIfCancelled(File file, int depth, Collection results)
     Checks whether the walk has been cancelled by calling DirectoryWalker.handleIsCancelled , throwing a CancelException if it has.

Writers of subclasses should not normally call this method as it is called automatically by the walk of the tree.

protected  voidhandleCancelled(File startDirectory, Collection results, CancelException cancel)
     Overridable callback method invoked when the operation is cancelled. The file being processed when the cancellation occurred can be obtained from the exception.

This implementation just re-throws the CancelException .
Parameters:
  startDirectory - the directory that the walk started from
Parameters:
  results - the collection of result objects, may be updated
Parameters:
  cancel - the exception throw to cancel further processingcontaining details at the point of cancellation.

protected  booleanhandleDirectory(File directory, int depth, Collection results)
     Overridable callback method invoked to determine if a directory should be processed.
protected  voidhandleDirectoryEnd(File directory, int depth, Collection results)
     Overridable callback method invoked at the end of processing each directory.
protected  voidhandleDirectoryStart(File directory, int depth, Collection results)
     Overridable callback method invoked at the start of processing each directory.
protected  voidhandleEnd(Collection results)
     Overridable callback method invoked at the end of processing.
protected  voidhandleFile(File file, int depth, Collection results)
     Overridable callback method invoked for each (non-directory) file.
protected  booleanhandleIsCancelled(File file, int depth, Collection results)
     Overridable callback method invoked to determine if the entire walk operation should be immediately cancelled.

This method should be implemented by those subclasses that want to provide a public cancel() method available from another thread.

protected  voidhandleRestricted(File directory, int depth, Collection results)
     Overridable callback method invoked for each restricted directory.
protected  voidhandleStart(File startDirectory, Collection results)
     Overridable callback method invoked at the start of processing.
final protected  voidwalk(File startDirectory, Collection results)
     Internal method that walks the directory hierarchy in a depth-first manner.

Users of this class do not need to call this method.



Constructor Detail
DirectoryWalker
protected DirectoryWalker()(Code)
Construct an instance with no filtering and unlimited depth.



DirectoryWalker
protected DirectoryWalker(FileFilter filter, int depthLimit)(Code)
Construct an instance with a filter and limit the depth navigated to.

The filter controls which files and directories will be navigated to as part of the walk. The FileFilterUtils class is useful for combining various filters together. A null filter means that no filtering should occur and all files and directories will be visited.
Parameters:
  filter - the filter to apply, null means visit all files
Parameters:
  depthLimit - controls how deep the hierarchy isnavigated to (less than 0 means unlimited)




DirectoryWalker
protected DirectoryWalker(IOFileFilter directoryFilter, IOFileFilter fileFilter, int depthLimit)(Code)
Construct an instance with a directory and a file filter and an optional limit on the depth navigated to.

The filters control which files and directories will be navigated to as part of the walk. This constructor uses FileFilterUtils.makeDirectoryOnly(IOFileFilter) and FileFilterUtils.makeFileOnly(IOFileFilter) internally to combine the filters. A null filter means that no filtering should occur.
Parameters:
  directoryFilter - the filter to apply to directories, null means visit all directories
Parameters:
  fileFilter - the filter to apply to files, null means visit all files
Parameters:
  depthLimit - controls how deep the hierarchy isnavigated to (less than 0 means unlimited)





Method Detail
checkIfCancelled
final protected void checkIfCancelled(File file, int depth, Collection results) throws IOException(Code)
Checks whether the walk has been cancelled by calling DirectoryWalker.handleIsCancelled , throwing a CancelException if it has.

Writers of subclasses should not normally call this method as it is called automatically by the walk of the tree. However, sometimes a single method, typically DirectoryWalker.handleFile , may take a long time to run. In that case, you may wish to check for cancellation by calling this method.
Parameters:
  file - the current file being processed
Parameters:
  depth - the current file level (starting directory = 0)
Parameters:
  results - the collection of result objects, may be updated
throws:
  IOException - if an I/O Error occurs




handleCancelled
protected void handleCancelled(File startDirectory, Collection results, CancelException cancel) throws IOException(Code)
Overridable callback method invoked when the operation is cancelled. The file being processed when the cancellation occurred can be obtained from the exception.

This implementation just re-throws the CancelException .
Parameters:
  startDirectory - the directory that the walk started from
Parameters:
  results - the collection of result objects, may be updated
Parameters:
  cancel - the exception throw to cancel further processingcontaining details at the point of cancellation.
throws:
  IOException - if an I/O Error occurs




handleDirectory
protected boolean handleDirectory(File directory, int depth, Collection results) throws IOException(Code)
Overridable callback method invoked to determine if a directory should be processed.

This method returns a boolean to indicate if the directory should be examined or not. If you return false, the entire directory and any subdirectories will be skipped. Note that this functionality is in addition to the filtering by file filter.

This implementation does nothing and returns true.
Parameters:
  directory - the current directory being processed
Parameters:
  depth - the current directory level (starting directory = 0)
Parameters:
  results - the collection of result objects, may be updated true to process this directory, false to skip this directory
throws:
  IOException - if an I/O Error occurs




handleDirectoryEnd
protected void handleDirectoryEnd(File directory, int depth, Collection results) throws IOException(Code)
Overridable callback method invoked at the end of processing each directory.

This implementation does nothing.
Parameters:
  directory - the directory being processed
Parameters:
  depth - the current directory level (starting directory = 0)
Parameters:
  results - the collection of result objects, may be updated
throws:
  IOException - if an I/O Error occurs




handleDirectoryStart
protected void handleDirectoryStart(File directory, int depth, Collection results) throws IOException(Code)
Overridable callback method invoked at the start of processing each directory.

This implementation does nothing.
Parameters:
  directory - the current directory being processed
Parameters:
  depth - the current directory level (starting directory = 0)
Parameters:
  results - the collection of result objects, may be updated
throws:
  IOException - if an I/O Error occurs




handleEnd
protected void handleEnd(Collection results) throws IOException(Code)
Overridable callback method invoked at the end of processing.

This implementation does nothing.
Parameters:
  results - the collection of result objects, may be updated
throws:
  IOException - if an I/O Error occurs




handleFile
protected void handleFile(File file, int depth, Collection results) throws IOException(Code)
Overridable callback method invoked for each (non-directory) file.

This implementation does nothing.
Parameters:
  file - the current file being processed
Parameters:
  depth - the current directory level (starting directory = 0)
Parameters:
  results - the collection of result objects, may be updated
throws:
  IOException - if an I/O Error occurs




handleIsCancelled
protected boolean handleIsCancelled(File file, int depth, Collection results) throws IOException(Code)
Overridable callback method invoked to determine if the entire walk operation should be immediately cancelled.

This method should be implemented by those subclasses that want to provide a public cancel() method available from another thread. The design pattern for the subclass should be as follows:

 public class FooDirectoryWalker extends DirectoryWalker {
 private volatile boolean cancelled = false;
 public void cancel() {
 cancelled = true;
 }
 private void handleIsCancelled(File file, int depth, Collection results) {
 return cancelled;
 }
 protected void handleCancelled(File startDirectory,
 Collection results, CancelException cancel) {
 // implement processing required when a cancellation occurs
 }
 }
 

If this method returns true, then the directory walk is immediately cancelled. The next callback method will be DirectoryWalker.handleCancelled .

This implementation returns false.
Parameters:
  file - the file or directory being processed
Parameters:
  depth - the current directory level (starting directory = 0)
Parameters:
  results - the collection of result objects, may be updated true if the walk has been cancelled
throws:
  IOException - if an I/O Error occurs




handleRestricted
protected void handleRestricted(File directory, int depth, Collection results) throws IOException(Code)
Overridable callback method invoked for each restricted directory.

This implementation does nothing.
Parameters:
  directory - the restricted directory
Parameters:
  depth - the current directory level (starting directory = 0)
Parameters:
  results - the collection of result objects, may be updated
throws:
  IOException - if an I/O Error occurs




handleStart
protected void handleStart(File startDirectory, Collection results) throws IOException(Code)
Overridable callback method invoked at the start of processing.

This implementation does nothing.
Parameters:
  startDirectory - the directory to start from
Parameters:
  results - the collection of result objects, may be updated
throws:
  IOException - if an I/O Error occurs




walk
final protected void walk(File startDirectory, Collection results) throws IOException(Code)
Internal method that walks the directory hierarchy in a depth-first manner.

Users of this class do not need to call this method. This method will be called automatically by another (public) method on the specific subclass.

Writers of subclasses should call this method to start the directory walk. Once called, this method will emit events as it walks the hierarchy. The event methods have the prefix handle.
Parameters:
  startDirectory - the directory to start from, not null
Parameters:
  results - the collection of result objects, may be updated
throws:
  NullPointerException - if the start directory is null
throws:
  IOException - if an I/O Error occurs




Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.