KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > utility > DirectoryWalker


1 package net.firstpartners.nounit.utility;
2
3 /**
4  * Title: NoUnit - Identify Classes that are not being unit Tested
5  *
6  * Copyright (C) 2001 Paul Browne , FirstPartners.net
7  *
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * @author Paul Browne
24  * @version 0.7
25  */

26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.HashSet JavaDoc;
30
31 /**
32  * 'Walks' the directory structure and returns files found
33  * (including those in sub folders)
34  */

35 public class DirectoryWalker {
36
37     /**
38      * Get all the files in the starting directory (and sub dirs)
39      * No filter
40      * @param startingDirectory
41      * @return filesFound (as HashSet)
42      * @exception java.io.IOException
43      */

44     public static HashSet JavaDoc getFiles( String JavaDoc startDir ) throws IOException JavaDoc {
45        return getFiles( startDir, null );
46     }
47   
48    /**
49      * Get all the files in the starting directory (but NOT sub dirs).
50      * returns only files whose names end with the mask passed in
51      * @param startingDirectory
52      * @return filesFound (as HashSet)
53      * @exception java.io.IOException
54      */

55     public static HashSet JavaDoc getFiles( String JavaDoc startingDirectory,
56                                     String JavaDoc mask )
57         throws java.io.IOException JavaDoc{
58         
59         HashSet JavaDoc filesFound = new HashSet JavaDoc();
60         
61         //Check that this is a valid directory
62
File JavaDoc startDir = new File JavaDoc( startingDirectory );
63         if (!startDir.isDirectory()) {
64             String JavaDoc msg = startingDirectory + " was not a valid directory";
65             throw new java.io.IOException JavaDoc( msg );
66         }
67         
68         //Get the contents of the current directory
69
String JavaDoc[] thisDirContents = startDir.list();
70         
71         if (thisDirContents!=null) {
72
73             //Now loop through , apply filter , or adding them to list of
74
//sub dirs
75
for (int a=0; a<thisDirContents.length;a++) {
76
77                 File JavaDoc tmp = new File JavaDoc( startingDirectory, thisDirContents[a] );
78
79                 //Skip directories
80
if ( tmp.isFile() ) {
81                     if ( ( null==mask )|| ( tmp.toString().endsWith(mask) ) ) {
82                         filesFound.add( tmp.toString() );
83                     }
84                 }
85             }
86         }
87         return filesFound;
88     }
89     
90     /**
91      * Get all the Directories in the starting directory (and sub dirs)
92      * returns only files ending this the filename
93      * @param startingDirectory
94      * @return filesFound (as HashSet)
95      * @exception java.io.IOException
96      */

97     public static HashSet JavaDoc getDirs(String JavaDoc startingDirectory)
98         throws java.io.IOException JavaDoc{
99         
100         //Check that this is a valid directory
101
File JavaDoc startDir = new File JavaDoc(startingDirectory);
102         if (!startDir.isDirectory()) {
103             throw new java.io.IOException JavaDoc(startingDirectory+" was not a valid directory");
104         }
105         
106         //Add the current directory to the output list
107
HashSet JavaDoc dirsFound = new HashSet JavaDoc();
108         dirsFound.add(startingDirectory);
109         
110         //Get the contents of the current directory
111
String JavaDoc[] thisDirContents = startDir.list();
112         
113         if (thisDirContents!=null) {
114         //Now loop through , apply filter , or adding them to list of sub dirs
115
for (int a=0; a<thisDirContents.length;a++) {
116
117                 //Get Handle to (full) file (inc path)
118
File JavaDoc tmpFile =
119                     new File JavaDoc( startingDirectory, thisDirContents[a] );
120                 
121                 //We're only interested in directories
122
if (tmpFile.isDirectory()) {
123                     
124                     //Add this to the directory list
125
dirsFound.add( tmpFile.toString() );
126                     
127                     //Now Do Recursive Call (to this method)if Directory
128
File JavaDoc tmpSubDir = new File JavaDoc( startingDirectory,
129                                                thisDirContents[a] );
130                     HashSet JavaDoc subDirFilesFound =
131                         DirectoryWalker.getDirs( tmpSubDir.toString() );
132                     dirsFound.addAll(subDirFilesFound);
133                 }
134             }
135         }
136         return dirsFound;
137     }
138 }
139
Popular Tags