KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > RecursiveFileSearch


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003,2004 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 import java.io.File JavaDoc;
23 import java.io.FileFilter JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.LinkedList JavaDoc;
29
30 /**
31  * Recursively search a directory, its subdirectories, etc.
32  * Note that the search algorithm uses a worklist, so its implementation does
33  * not use recursive method calls.
34  *
35  * @author David Hovemeyer
36  */

37 public class RecursiveFileSearch {
38     private String JavaDoc baseDir;
39     private FileFilter JavaDoc fileFilter;
40     private LinkedList JavaDoc<File JavaDoc> directoryWorkList;
41     private HashSet JavaDoc<String JavaDoc> directoriesScanned = new HashSet JavaDoc<String JavaDoc>();
42     private ArrayList JavaDoc<String JavaDoc> resultList;
43
44     /**
45      * Constructor.
46      *
47      * @param baseDir the base directory for the search
48      * @param fileFilter chooses files to add to the results, and subdirectories
49      * to continue the search in
50      */

51     public RecursiveFileSearch(String JavaDoc baseDir, FileFilter JavaDoc fileFilter) {
52         this.baseDir = baseDir;
53         this.fileFilter = fileFilter;
54         this.directoryWorkList = new LinkedList JavaDoc<File JavaDoc>();
55         this.resultList = new ArrayList JavaDoc<String JavaDoc>();
56     }
57
58     static String JavaDoc bestEffortCanonicalPath(File JavaDoc f) {
59         try {
60             return f.getCanonicalPath();
61         } catch (IOException JavaDoc e) {
62             return f.getAbsolutePath();
63         }
64     }
65     /**
66      * Perform the search.
67      *
68      * @return this object
69      * @throws InterruptedException if the thread is interrupted before the
70      * search completes
71      */

72     public RecursiveFileSearch search() throws InterruptedException JavaDoc {
73         File JavaDoc baseFile = new File JavaDoc(baseDir);
74         String JavaDoc basePath = bestEffortCanonicalPath(baseFile);
75         directoryWorkList.add(baseFile);
76         directoriesScanned.add(basePath);
77
78         while (!directoryWorkList.isEmpty()) {
79             File JavaDoc dir = directoryWorkList.removeFirst();
80             if (!dir.isDirectory())
81                 continue;
82
83             File JavaDoc[] contentList = dir.listFiles();
84             if (contentList == null) continue;
85             for (File JavaDoc aContentList : contentList) {
86                 if (Thread.interrupted())
87                     throw new InterruptedException JavaDoc();
88
89                 File JavaDoc file = aContentList;
90
91                 if (!fileFilter.accept(file))
92                     continue;
93                 if (file.isDirectory()) {
94                     String JavaDoc myPath = bestEffortCanonicalPath(file);
95                     if (myPath.startsWith(basePath) && directoriesScanned.add(myPath))
96                         directoryWorkList.add(file);
97                 }
98                 else
99                     resultList.add(file.getPath());
100             }
101         }
102
103         return this;
104     }
105
106     /**
107      * Get an iterator over the files found by the search.
108      * The full path names of the files are returned.
109      */

110     public Iterator JavaDoc<String JavaDoc> fileNameIterator() {
111         return resultList.iterator();
112     }
113
114 }
115
116 // vim:ts=4
117
Popular Tags