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.util.FileLine;
30  import mk.fsgrep.util.TargetFile;
31  
32  /***
33   * 
34   * 
35   * @author  Murali Krishnan
36   *
37   */
38  public class TextSearch extends FileSearch {
39  
40      //------------------------------------------------------------
41      //- Class Variables
42  
43      public static final Pattern RE_BLANK_LINE = Pattern.compile("^//s*$");
44  
45  
46      //------------------------------------------------------------
47      //- Class Functions
48  
49  
50  
51      //------------------------------------------------------------
52      //- Instance Variables
53  
54      private boolean _stopAfterFirstMatch = false;
55      private boolean _continueReading = true;
56  
57  
58      //------------------------------------------------------------
59      //- Constructors
60  
61  
62      //------------------------------------------------------------
63      //- Accessors
64  
65      public boolean shouldStopAfterFirstMatch() {return _stopAfterFirstMatch;}
66      protected boolean shouldContinueReading() {return _continueReading;}
67  
68  
69  
70      //------------------------------------------------------------
71      //- Settors
72  
73      public void setStopAfterFirstMatch(boolean val) {_stopAfterFirstMatch=val;}
74      protected void setContinueReading(boolean val) {_continueReading=val;}
75  
76  
77  
78      //------------------------------------------------------------
79      //- Private/Protected Utility Functions
80  
81  
82      //------------------------------------------------------------
83      //- Public Interface Functions
84  
85  
86  
87      //------------------------------------------------------------
88      //- Class Interface Functions
89  
90      public void search() {
91          TargetFile.LineReader reader = getFile().createLineReader();
92          while (shouldContinueReading() && reader.hasMoreLines()) {
93              FileLine line = reader.nextLine();
94              if (!RE_BLANK_LINE.matcher(line.getText()).find()) {
95                  process(line.getNumber(), line.getText());
96              }
97          }
98      }
99  
100 
101     protected void process(int linenum, String line) {
102         if (getRE().matcher(line).find()) {
103             handleMatch(linenum, line);
104         }
105 
106         incrementLinesSearched();
107     }
108 
109 
110     protected void handleMatch(int linenum, String line) {
111         incrementCount();
112         MatchResult result = getOrCreateResults();
113 
114         if (shouldStopAfterFirstMatch()) {
115             setContinueReading(false);
116         } else {
117             result.add(linenum, line);
118         }
119     }
120 
121     public void stopAfterFirstMatch() {
122         setStopAfterFirstMatch(true);
123     }
124 
125 
126 
127     //------------------------------------------------------------
128     //- Inner Classes
129 
130 
131 
132     //------------------------------------------------------------
133     //- Main
134 
135 
136 
137 }