1 package com.explosion.expfmodules.search;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 }