View Javadoc

1   /*
2    * Fsgrep is a simple Java application which allows a user to
3    * search all files in a directory structure for lines matching
4    * a given pattern.  Its functionality is a combination of the
5    * Unix 'find' and 'grep' utilities.
6    * Visit [http://fsgrep.sourceforge.net/] for more information.
7    * 
8    * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9    * 
10   * Fsgrep is free software; you can redistribute it and/or modify
11   * it under the terms of version 2 of the GNU General Public
12   * License as published by the Free Software Foundation.
13   * 
14   * Fsgrep 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 Fsgrep (see the file named LICENSE.txt); if not, write
21   * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22   * Boston, MA  02111-1307  USA
23   */
24  
25  package mk.fsgrep.find;
26  
27  
28  import java.io.*;
29  import java.util.*;
30  
31  import mk.fsgrep.Fsgrep;
32  import mk.fsgrep.util.output.OutputDestination;
33  import mk.fsgrep.util.ResultCounter;
34  import mk.fsgrep.util.TargetFile;
35  import mk.fsgrep.util.Timer;
36  
37  
38  /***
39   * The agent which collects the list of files to search.
40   * 
41   * @author  Murali Krishnan
42   *
43   */
44  public class Finder {
45  
46      //------------------------------------------------------------
47      //- Class Variables
48  
49  
50  
51      //------------------------------------------------------------
52      //- Class Functions
53  
54  
55  
56      //------------------------------------------------------------
57      //- Instance Variables
58  
59      private Fsgrep _model = null;
60      private ScanProfile _profile = null;
61      private Collection<TargetFile> _fileList = new TreeSet<TargetFile>();
62      private LinkedList<File> _remaining = new LinkedList<File>();
63      private ResultCounter _fileCount = new ResultCounter();
64  
65  
66  
67      //------------------------------------------------------------
68      //- Constructors
69  
70      public Finder(Fsgrep pModel, ScanProfile pProfile) {
71          _model = pModel;
72          _profile = pProfile;
73      }
74  
75  
76      protected Finder() {
77      }
78  
79  
80      //------------------------------------------------------------
81      //- Accessors
82  
83      protected Fsgrep getModel() {return _model;}
84      protected LinkedList<File> getRemaining() {return _remaining;}
85      public Collection<TargetFile> getFileList() {return _fileList;}
86      public ScanProfile getProfile() {return _profile;}
87      public ResultCounter getFileCount() {return _fileCount;}
88  
89  
90  
91      //------------------------------------------------------------
92      //- Settors
93  
94      public void setFileCount(ResultCounter val) {_fileCount=val;}
95      public void setProfile(ScanProfile val) {_profile = val;}
96  
97  
98  
99      //------------------------------------------------------------
100     //- Delegators
101 
102     public File getRoot() {return getProfile().getRoot();}
103 
104     protected FilenameFilter getFilter() {return getProfile().getFilter();}
105 
106     protected OutputDestination getReportOutput() {return getModel().getReportOutput();}
107 
108     protected OutputDestination getStatusOutput() {return getModel().getStatusOutput();}
109 
110 
111     //------------------------------------------------------------
112     //- Private/Protected Utility Functions
113 
114     protected synchronized void addFiles(File[] files) {
115         if (files != null) {
116             for (File file : files) {
117                 getFileList().add(new TargetFile(file));
118             }
119 
120             getFileCount().increment(files.length);
121         }
122     }
123 
124 
125     protected synchronized void addSubDirs(File[] subdirs) {
126         if (subdirs != null) {
127             for (File subdir : subdirs) {
128                 getRemaining().addFirst(subdir);
129             }
130         }
131     }
132 
133 
134     //------------------------------------------------------------
135     //- Public Interface Functions
136 
137     public String toString() {
138         // String result = "Finder[" + getProfile() + "]";
139         String spec = "";
140         if (getProfile() != null) {
141             spec = getProfile().createSpecification();
142         }
143         String result = "Finder[" + spec + "]";
144 
145         return result;
146     }
147 
148 
149     public String toProfileString() {
150         String result = "[no profile]";
151 
152         if (getProfile() != null) {
153             result = getProfile().createSpecification();
154         }
155 
156         return result;
157     }
158 
159 
160     public synchronized void reset() {
161         getFileList().clear();
162         getRemaining().clear();
163         getFileCount().reset();
164     }
165 
166 
167     public void scan() {
168         reset();
169         getStatusOutput().print("Looking for files ...");
170 
171         getRemaining().addFirst(getRoot());
172 
173         Timer timer = new Timer();
174         int numberDirsExamined = 0;
175 
176         getReportOutput().print("Scanning directories for files ...");
177 
178         while (!getModel().isAborted() && !getRemaining().isEmpty()) {
179             File dir = getRemaining().removeFirst();
180             ++numberDirsExamined;
181             DirScan scan = new DirScan(this, dir);
182             scan.run();
183         }
184 
185         getReportOutput().println(" examined " + numberDirsExamined + " directories.");
186 
187         timer.stop();
188         long total = timer.getTotal();
189         int count = getFileList().size();
190         double avg = timer.findAverage(count);
191 
192         getReportOutput().println("Scan time: " + total + " ms, " +
193                                   count + " files, " + avg + " ms per file.");
194 
195         // If it was aborted before finishing, then reset everything to
196         // ensure no partial data.
197         if (!getRemaining().isEmpty()) {
198             reset();
199         }
200     }
201 
202 
203     //------------------------------------------------------------
204     //- Class Interface Functions
205 
206 
207 
208     //------------------------------------------------------------
209     //- Inner Classes
210 
211 
212 
213     //------------------------------------------------------------
214     //- Main
215 
216 
217 
218 }