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 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 }