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.util;
26  
27  import java.io.*;
28  import java.util.*;
29  
30  /***
31   * A wrapper for the File object which caches the canonical name and the
32   * suffix, so these do not have to be determined multiple times.
33   * 
34   * @author  Murali Krishnan
35   *
36   */
37  public class TargetFile implements Comparable {
38  
39      //------------------------------------------------------------
40      //- Class Variables
41  
42  
43  
44      //------------------------------------------------------------
45      //- Class Functions
46  
47  
48  
49      //------------------------------------------------------------
50      //- Instance Variables
51  
52      private File vFile = null;
53      private String vSuffix = null;
54      private String vName = null;
55      private LineNumberReader vLineNumberReader = null;
56      private Collection<Integer> vFilterNumbers = new HashSet<Integer>();
57  
58  
59  
60      //------------------------------------------------------------
61      //- Constructors
62  
63      public TargetFile(String pFilename) {
64          this (new File(pFilename));
65      }
66  
67  
68      public TargetFile(File pFile) {
69          vFile = pFile;
70  
71          {   // Parse the suffix from the file name.
72              if (vFile.isFile()) {
73                  String string = vFile.getName();
74                  int index = string.lastIndexOf('.');
75                  if (index != -1) {
76                      vSuffix = string.substring(index+1);
77                  }
78              }
79          }
80      }
81  
82  
83      public TargetFile(File pFile, String pSuffix) {
84          vFile = pFile;
85          vSuffix = pSuffix;
86      }
87  
88  
89      protected TargetFile() {
90      }
91  
92  
93      //------------------------------------------------------------
94      //- Accessors
95  
96      public File getFile() {
97          return vFile;
98      }
99  
100     public String getSuffix() {
101         return vSuffix;
102     }
103 
104     public String getName() {
105         if (vName == null) {
106             try {
107                 vName = getFile().getCanonicalPath();
108             } catch (IOException ioe) {
109                 vName = getFile().getAbsolutePath();
110             }
111         }
112 
113         return vName;
114     }
115 
116     protected LineNumberReader getLineNumberReader() {
117         return vLineNumberReader;
118     }
119 
120     public Collection<Integer> getFilterNumbers() {
121         return vFilterNumbers;
122     }
123 
124 
125 
126     //------------------------------------------------------------
127     //- Settors
128 
129     protected void setLineNumberReader(LineNumberReader val) {
130         vLineNumberReader = val;
131     }
132 
133 
134 
135     //------------------------------------------------------------
136     //- Private/Protected Utility Functions
137 
138 
139 
140     //------------------------------------------------------------
141     //- Public Interface Functions
142 
143     public String toString() {
144         String result = getName();
145 
146         if (!getFilterNumbers().isEmpty()) {
147             result += " lines" + getFilterNumbers();
148         }
149 
150         return result;
151     }
152 
153 
154     public void backup() {
155         String newName = getName() + ".orig";
156         File newFile = new File(newName);
157 
158         if (newFile.exists() && !newFile.delete()) {
159             System.err.println("Could not remove existing [" + newName + "].");
160         }
161 
162         if (!getFile().renameTo(newFile)) {
163             System.err.println("Could not rename to [" + newName + "].");
164         }
165     }
166 
167 
168     public LineReader createLineReader() {
169         RawLineReader rlr = new RawLineReader(getFile());
170         LineReader reader = new LineReader(rlr, getFilterNumbers());
171         return reader;
172     }
173 
174 
175     public void saveContent(Collection<FileLine> lines) {
176         try {
177             FileWriter writer = new FileWriter(getFile());
178             for (FileLine fileLine : lines) {
179                 String line = String.valueOf(fileLine);
180                 writer.write(line);
181             }
182             writer.close();
183         } catch (IOException ioe) {
184             System.err.println("Could not write [" + getFile() + "].");
185             ioe.printStackTrace();
186         }
187     }
188 
189     public void addFilterNumber(int number) {
190         getFilterNumbers().add(number);
191     }
192 
193 
194     //------------------------------------------------------------
195     //- Class Interface Functions
196 
197 
198     public int compareTo(Object object) {
199         int result = -1;  // Pessimistic.
200 
201         if (object instanceof TargetFile) {
202             TargetFile other = (TargetFile) object;
203 
204             result = getName().compareTo(other.getName());
205         }
206 
207         return result;
208     }
209 
210 
211     public boolean equals(Object object) {
212         boolean result = (compareTo(object) == 0);
213 
214         return result;
215     }
216 
217 
218 
219 
220     //------------------------------------------------------------
221     //- Inner Classes
222 
223 
224     public class LineReader {
225         private List<FileLine> vAllLines = new ArrayList<FileLine>();
226         private LinkedList<FileLine> vActiveLines = new LinkedList<FileLine>();
227 
228 
229         public LineReader(RawLineReader rlr, Collection<Integer> linesToKeep) {
230             while (rlr.hasMore()) {
231                 FileLine line = rlr.nextLine();
232 
233                 getAllLines().add(line);
234 
235                 Integer num = line.getNumber();
236                 if (linesToKeep.isEmpty() || linesToKeep.contains(num)) {
237                     getActiveLines().add(line);
238                 }
239             }
240         }
241 
242         public List<FileLine> getAllLines() {
243             return vAllLines;
244         }
245 
246         protected LinkedList<FileLine> getActiveLines() {
247             return vActiveLines;
248         }
249 
250 
251         public boolean hasMoreLines() {
252             boolean result = !getActiveLines().isEmpty();
253             return result;
254         }
255 
256 
257         public FileLine nextLine() {
258             FileLine result = null;  // Pessimistic.
259 
260             if (hasMoreLines()) {
261                 result = getActiveLines().removeFirst();
262             }
263 
264             return result;
265         }
266     }
267 
268 
269 
270     //------------------------------------------------------------
271     //- Main
272 
273 
274 
275 }