View Javadoc

1   package com.explosion.expfmodules.search;
2   
3   /* =============================================================================
4    *       
5    *     Copyright 2004 Stephen Cowx
6    *
7    *     Licensed under the Apache License, Version 2.0 (the "License");
8    *     you may not use this file except in compliance with the License.
9    *     You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *     Unless required by applicable law or agreed to in writing, software
14   *     distributed under the License is distributed on an "AS IS" BASIS,
15   *     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   *     See the License for the specific language governing permissions and
17   *     limitations under the License.
18   * 
19   * =============================================================================
20   */
21  
22  
23  import java.util.Vector;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import com.explosion.utilities.process.LineProcessor;
28  
29  /***
30   * @author Stephen Cowx
31   * Date created:@04-Feb-2003
32   */
33  public class LineSearcher implements LineProcessor
34  {
35  	
36     private Pattern pattern = null;
37     private Matcher matcher = null;
38     private FileSearchResultCollection fileResultsCollection;
39     private Vector fileSearchResults = new Vector();
40     
41     private boolean filenameSet = false;
42     private boolean resultsFound = false;
43    
44    /***
45     * Constructor for LineSearcher.
46     */
47    public LineSearcher(String searchForString )
48    {
49      pattern = Pattern.compile(searchForString);
50    }
51  
52    /***
53     * @see com.explosion.utilities.process.LineProcessor#open()
54     */
55    public void open() throws Exception
56    {
57      fileResultsCollection = new FileSearchResultCollection();
58      filenameSet = false;
59      resultsFound = false;
60    }
61  
62  
63    /***
64     * @see com.explosion.utilities.process.LineProcessor#close()
65     */
66    public void close() throws Exception
67    {    
68    	if (resultsFound)
69    	   fileSearchResults.addElement(fileResultsCollection);
70    }
71  
72    
73    /***
74     * @see com.explosion.utilities.process.LineProcessor#processLine(String)
75     */
76    public String processLine(String line,int lineNumber, String filename) throws Exception
77    {
78       if (!filenameSet)
79       {
80         fileResultsCollection.setFileName(filename);
81         filenameSet = true;
82       }  
83       
84       if (line != null && line.trim().length() >0)
85       {
86          matcher = pattern.matcher(line);
87          if (matcher.find())
88          {
89             resultsFound = true;
90             fileResultsCollection.addResult(new SearchResult(lineNumber, filename, line));
91          }   
92       }
93       
94       return line;
95    }
96  
97    /***
98     * Returns the fileSearchResults.
99     * @return Vector
100    */
101   public Vector getFileSearchResults()
102   {
103     return fileSearchResults;
104   }
105 }