1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package mk.fsgrep.match;
26
27
28 import java.util.*;
29
30 import mk.fsgrep.Fsgrep;
31 import mk.fsgrep.util.Formatable;
32 import mk.fsgrep.util.TargetFile;
33
34
35 /***
36 * The match results of a file search.
37 *
38 * @author Murali Krishnan
39 *
40 */
41 public class MatchResult implements Formatable {
42
43
44
45
46 public static final String SEPARATOR = "-------------------------------------------------------------------------------\n";
47
48
49
50
51 protected static String encode(String string) {
52 StringBuffer buffer = new StringBuffer();
53
54 char[] chars = string.toCharArray();
55 for (char ch : chars) {
56 if (ch == '<') {
57 buffer.append("<");
58 } else if (ch == '>') {
59 buffer.append(">");
60 } else {
61 buffer.append(ch);
62 }
63 }
64
65 return buffer.toString();
66 }
67
68
69
70
71
72 private String _filename = "";
73 private List<Entry> _lineMatches = new ArrayList<Entry>();
74
75
76
77
78
79 public MatchResult(TargetFile file) {
80 _filename = file.getName();
81 }
82
83
84
85
86
87 public String getFilename() {return _filename;}
88 protected List<Entry> getLineMatches() {return _lineMatches;}
89
90
91
92
93
94
95 public void setFilename(String val) {_filename=val;}
96
97
98
99
100
101
102
103
104
105
106
107 public void add(String line) {
108 getLineMatches().add(new Entry(line));
109 }
110
111
112 public void add(int number, String line) {
113 getLineMatches().add(new Entry(number, line));
114 }
115
116
117 public boolean equals(Object object) {
118 boolean result = false;
119
120 if (object instanceof MatchResult) {
121 MatchResult other = (MatchResult) object;
122
123 result = toString().equals(other.toString());
124 }
125
126 return result;
127 }
128
129
130 public String toString() {
131 StringBuffer buffer = new StringBuffer();
132
133 if (getLineMatches().isEmpty()) {
134 buffer.append(getFilename()).append("\n");
135 } else {
136 buffer.append(SEPARATOR).append(getFilename()).append("\n");
137
138 for (Entry entry : getLineMatches()) {
139 buffer.append(entry);
140 }
141 }
142
143 return buffer.toString();
144 }
145
146
147 public String toHtml() {
148 StringBuffer buffer = new StringBuffer();
149
150 String filename = "<a href=\"" + getFilename() + "\">" + getFilename() + "</a>";
151 buffer.append("<tr bgcolor=\"#e0e0e0\"><td colspan=\"2\"><b>").append(filename).append("</b></td></tr>\n");
152
153 for (Entry entry : getLineMatches()) {
154 buffer.append(entry.toHtml());
155 }
156
157 return buffer.toString();
158 }
159
160
161 public TargetFile toTargetFile() {
162 TargetFile result = new TargetFile(getFilename());
163
164 for (Entry entry : getLineMatches()) {
165 result.addFilterNumber(entry.getNum());
166 }
167
168 return result;
169 }
170
171
172
173
174
175
176
177
178
179 class Entry {
180 private int _num = 0;
181 private String _line = "";
182
183 protected int getNum() {return _num;}
184 protected String getLine() {return _line;}
185
186 Entry(int pNum, String pLine) {
187 _num = pNum;
188 _line = Fsgrep.stripEol(pLine);
189 }
190
191 Entry(String pLine) {
192 this(0, pLine);
193 }
194
195 public String toString() {
196 StringBuffer buffer = new StringBuffer();
197
198 if (getNum() > 0) {
199 buffer.append(String.valueOf(getNum()));
200
201 while (buffer.length() < 4) {
202 buffer.insert(0, " ");
203 }
204
205 buffer.append(": ");
206 }
207
208 buffer.append(getLine()).append("\n");
209
210 return buffer.toString();
211 }
212
213
214 public String toHtml() {
215 StringBuffer buffer = new StringBuffer("<tr>");
216
217 String linenum = "<a href=\"" + getFilename()+"?"+getNum() + "\">" + getNum() + "</a>";
218
219 if (getNum() > 0) {
220 buffer.append("<td align=\"right\" valign=\"top\">").append(linenum).append("</td>");
221 buffer.append("<td width=\"100%\">");
222 } else {
223 buffer.append("<td colspan=\"2\" width=\"100%\">");
224 }
225
226 buffer.append(encode(getLine().trim())).append("</td></tr>\n");
227
228 return buffer.toString();
229 }
230 }
231
232
233
234
235
236
237
238 }