View Javadoc

1   /*
2    * Fsgrep is a simple Java application which allows a user to
3    * search all files in a directory structure for lines matching
4    * a given pattern.  Its functionality is a combination of the
5    * Unix 'find' and 'grep' utilities.
6    * Visit [http://fsgrep.sourceforge.net/] for more information.
7    * 
8    * Copyright (C) 2003-2006 Murali Krishnan [murali_ca_us@users.sourceforge.net]
9    * 
10   * Fsgrep is free software; you can redistribute it and/or modify
11   * it under the terms of version 2 of the GNU General Public
12   * License as published by the Free Software Foundation.
13   * 
14   * Fsgrep is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * 
19   * You should have received a copy of the GNU General Public License
20   * along with Fsgrep (see the file named LICENSE.txt); if not, write
21   * to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22   * Boston, MA  02111-1307  USA
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      //- Class Variables
45  
46      public static final String SEPARATOR = "-------------------------------------------------------------------------------\n";
47  
48      //------------------------------------------------------------
49      //- Class Functions
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("&lt;");
58              } else if (ch == '>') {
59                  buffer.append("&gt;");
60              } else {
61                  buffer.append(ch);
62              }
63          }
64  
65          return buffer.toString();
66      }
67  
68  
69      //------------------------------------------------------------
70      //- Instance Variables
71  
72      private String _filename = "";
73      private List<Entry> _lineMatches = new ArrayList<Entry>();
74  
75  
76      //------------------------------------------------------------
77      //- Constructors
78  
79      public MatchResult(TargetFile file) {
80          _filename = file.getName();
81      }
82  
83  
84      //------------------------------------------------------------
85      //- Accessors
86  
87      public String getFilename() {return _filename;}
88      protected List<Entry> getLineMatches() {return _lineMatches;}
89  
90  
91  
92      //------------------------------------------------------------
93      //- Settors
94  
95      public void setFilename(String val) {_filename=val;}
96  
97  
98  
99      //------------------------------------------------------------
100     //- Private/Protected Utility Functions
101 
102 
103 
104     //------------------------------------------------------------
105     //- Public Interface Functions
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;  // Pessimistic.
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     //- Class Interface Functions
174 
175 
176     //------------------------------------------------------------
177     //- Inner Classes
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     //- Main
235 
236 
237 
238 }