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 LineReplacer 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 String replaceString;
43     private boolean resultsFound = false;
44    
45    /***
46     * Constructor for LineSearcher.
47     */
48    public LineReplacer(String searchForString , String replaceString)
49    {
50      pattern = Pattern.compile(searchForString);
51      this.replaceString = replaceString;
52    }
53  
54    /***
55     * @see com.explosion.utilities.process.LineProcessor#open()
56     */
57    public void open() throws Exception
58    {
59      fileResultsCollection = new FileSearchResultCollection();
60      filenameSet = false;
61      resultsFound = false;
62    }
63  
64  
65    /***
66     * @see com.explosion.utilities.process.LineProcessor#close()
67     */
68    public void close() throws Exception
69    {    
70    	if (resultsFound)
71    	   fileSearchResults.addElement(fileResultsCollection);
72    }
73  
74    
75    /***
76     * @see com.explosion.utilities.process.LineProcessor#processLine(String)
77     */
78    public String processLine(String line,int lineNumber, String filename) throws Exception
79    {
80       String newLine = line;
81        if (!filenameSet)
82       {
83         fileResultsCollection.setFileName(filename);
84         filenameSet = true;
85       }  
86       
87       if (line != null && line.trim().length() >0)
88       {
89          matcher = pattern.matcher(line);
90          if (matcher.find())
91          {
92             resultsFound = true;
93             fileResultsCollection.addResult(new SearchResult(lineNumber, filename, line));
94             newLine = matcher.replaceAll(replaceString);
95          }   
96       }
97       
98       return newLine;
99    }
100 
101   /***
102    * Returns the fileSearchResults.
103    * @return Vector
104    */
105   public Vector getFileSearchResults()
106   {
107     return fileSearchResults;
108   }
109 }