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.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
41
42
43
44
45
46
47
48
49
50
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
62
63 public TargetFile(String pFilename) {
64 this (new File(pFilename));
65 }
66
67
68 public TargetFile(File pFile) {
69 vFile = pFile;
70
71 {
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
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
128
129 protected void setLineNumberReader(LineNumberReader val) {
130 vLineNumberReader = val;
131 }
132
133
134
135
136
137
138
139
140
141
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
196
197
198 public int compareTo(Object object) {
199 int result = -1;
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
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;
259
260 if (hasMoreLines()) {
261 result = getActiveLines().removeFirst();
262 }
263
264 return result;
265 }
266 }
267
268
269
270
271
272
273
274
275 }