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.match;
26  
27  import java.util.regex.*;
28  
29  import mk.fsgrep.Fsgrep;
30  import mk.fsgrep.util.TargetFile;
31  
32  /***
33   * Search for a regular expression in a file.
34   * 
35   * @author  Murali Krishnan
36   *
37   */
38  public abstract class FileSearch implements Runnable {
39  
40      //------------------------------------------------------------
41      //- Class Variables
42  
43  
44  
45      //------------------------------------------------------------
46      //- Class Functions
47  
48  
49      //------------------------------------------------------------
50      //- Instance Variables
51  
52      private Pattern _re = null;
53      private TargetFile _file = null;
54      private int _count = 0;
55      private MatchResult _results = null;
56      private Fsgrep _model = null;
57      private int _linesSearched = 0;
58  
59  
60  
61      //------------------------------------------------------------
62      //- Constructors
63  
64  
65      //------------------------------------------------------------
66      //- Accessors
67  
68      protected Pattern getRE() {return _re;}
69      protected TargetFile getFile() {return _file;}
70      protected Fsgrep getModel() {return _model;}
71      public int getCount() {return _count;}
72      public MatchResult getResults() {return _results;}
73      public int getLinesSearched() {return _linesSearched;}
74  
75  
76  
77      //------------------------------------------------------------
78      //- Settors
79  
80      public void setResults(MatchResult val) {_results=val;}
81  
82  
83  
84      //------------------------------------------------------------
85      //- Private/Protected Utility Functions
86  
87      protected void incrementCount() {_count++;}
88  
89  
90      protected void incrementLinesSearched() {_linesSearched++;}
91  
92  
93      public MatchResult getOrCreateResults() {
94          if (getResults() == null) {
95              setResults(new MatchResult(getFile()));
96          }
97  
98          MatchResult result = getResults();
99  
100         return result;
101     }
102 
103 
104     //------------------------------------------------------------
105     //- Public Interface Functions
106 
107     public void initialize(Pattern pRE, TargetFile pFile, Fsgrep pModel) {
108         _re = pRE;
109         _file = pFile;
110         _model = pModel;
111     }
112 
113 
114     public void run() {
115         search();
116 
117         getModel().finishFile(this);
118     }
119 
120 
121     //------------------------------------------------------------
122     //- Class Interface Functions
123 
124     public abstract void search();
125 
126     public void stopAfterFirstMatch() {
127         // Do nothing.
128     }
129 
130 
131     //------------------------------------------------------------
132     //- Inner Classes
133 
134 
135 
136     //------------------------------------------------------------
137     //- Main
138 
139 
140 
141 }