1 package com.explosion.utilities.process;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.BufferedReader;
24 import java.io.File;
25 import java.io.FileReader;
26 import java.io.IOException;
27
28 import javax.swing.SwingUtilities;
29
30 import org.apache.log4j.LogManager;
31 import org.apache.log4j.Logger;
32
33 import com.explosion.utilities.FileIterator;
34 import com.explosion.utilities.exception.ExceptionManagerFactory;
35 import com.explosion.utilities.process.threads.Finishable;
36 import com.explosion.utilities.process.threads.ProcessThread;
37 import com.explosion.utilities.process.threads.UnitisedProcessThread;
38
39 /***
40 * @author Stephen Cowx Date created:@12-Feb-2003
41 */
42 public class MultiFileLineByLineParser implements com.explosion.utilities.process.threads.UnitisedProcess
43 {
44
45 private static Logger log = LogManager.getLogger(MultiFileLineByLineParser.class);
46
47 private FileIterator iterator;
48
49 private LineProcessor lineProcessor;
50
51 private int currentFileIndex = 0;
52
53 private String statusText = "Processing";
54
55 private boolean processUnitComplete = false;
56
57 private ProcessThread parent;
58
59 private Finishable finishable;
60
61 private boolean initialised = false;
62
63 private int numberofLinesProcessed = 0;
64
65 private boolean isUserProcess = true;
66
67 public MultiFileLineByLineParser(Finishable finishable, String directoryPath, boolean recursive, String filenamePattern, LineProcessor lineProcessor) throws IOException
68 {
69 this.lineProcessor = lineProcessor;
70 this.finishable = finishable;
71
72 iterator = new FileIterator(directoryPath, filenamePattern, true, recursive, true);
73
74 parent = new UnitisedProcessThread();
75 parent.setProcess(this);
76 }
77
78 /***
79 * Returns a boolean value indicating whther this is a user process or not
80 * @return
81 */
82 public boolean isUserProcess()
83 {
84 return this.isUserProcess;
85 }
86
87 /***
88 * Sets whtether or not this is a user process. True means that it is
89 * @param truth
90 */
91 public void setIsUserProcess(boolean truth)
92 {
93 this.isUserProcess = true;
94 }
95
96 public void start()
97 {
98 parent.start();
99 }
100
101 public ProcessThread getProcessControl()
102 {
103 return parent;
104 }
105
106 public void setProcessControl(ProcessThread processThread)
107 {
108 parent = processThread;
109 }
110
111 public final boolean isStopped()
112 {
113 return parent.getStatus() == ProcessThread.THREAD_STOPPED;
114 }
115
116 /***
117 * @see com.explosion.utilities.process.UnitisedProcess#initialiseProcessing()
118 */
119 public void initialiseProcessing() throws Exception
120 {
121
122 }
123
124 /***
125 * @see com.explosion.utilities.process.UnitisedProcess#finaliseProcessing()
126 */
127 public void finaliseProcessing() throws Exception
128 {
129 Runnable doFinished = new Runnable()
130 {
131
132 public void run()
133 {
134 finishable.finish();
135 }
136 };
137 SwingUtilities.invokeLater(doFinished);
138 statusText = "Done";
139 }
140
141 /***
142 * @see com.explosion.utilities.process.UnitisedProcess#finaliseProcessing(Exception)
143 */
144 public void finaliseProcessing(Exception e)
145 {
146 ExceptionManagerFactory.getExceptionManager().manageException(e, "Experienced exception while processing.");
147 try
148 {
149 finaliseProcessing();
150 } catch (Exception e1)
151 {
152
153 log.debug("Exception while finalising processing.");
154 }
155 }
156
157 /***
158 * @see com.explosion.utilities.process.UnitisedProcess#getPercentCompete()
159 */
160 public int getPercentComplete()
161 {
162 return iterator.getPercentComplete();
163 }
164
165 /***
166 * @see com.explosion.utilities.process.UnitisedProcess#getPercentCompete()
167 */
168 public void setPercentComplete(int percentComplete)
169 {}
170
171 /***
172 * @see com.explosion.utilities.process.UnitisedProcess#getStatusText()
173 */
174 public String getStatusText()
175 {
176 return statusText;
177 }
178
179 /***
180 * @see com.explosion.utilities.process.UnitisedProcess#beginProcessUnit()
181 */
182 public void beginProcessUnit() throws Exception
183 {
184 processUnitComplete = false;
185 }
186
187 /***
188 * @see com.explosion.utilities.process.UnitisedProcess#processUnit()
189 */
190 public void processUnit() throws Exception
191 {
192 if (iterator.hasNext())
193 {
194 File file = iterator.next();
195 statusText = "Searching " + file.getName();
196 try
197 {
198 processFile(file.getAbsolutePath());
199 } catch (Exception e)
200 {
201 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception while processing " + file.getName());
202
203
204
205
206
207 }
208 }
209 processUnitComplete = true;
210 }
211
212 /***
213 * @see com.explosion.utilities.process.UnitisedProcess#endProcessUnit()
214 */
215 public void endProcessUnit() throws Exception
216 {
217 currentFileIndex++;
218 }
219
220 /***
221 * @see com.explosion.utilities.process.UnitisedProcess#processUnitComplete()
222 */
223 public boolean processUnitComplete()
224 {
225 return processUnitComplete;
226 }
227
228 public void processFile(String filepath) throws Exception
229 {
230 int num = 0;
231 BufferedReader reader = null;
232 try
233 {
234 if (!parentStopped())
235 {
236 lineProcessor.open();
237 reader = new BufferedReader(new FileReader(filepath));
238 while (!parentStopped() && reader.ready())
239 {
240 String s = reader.readLine();
241 num++;
242 numberofLinesProcessed++;
243 if (s != null)
244 lineProcessor.processLine(s, num, filepath);
245 else
246 break;
247 }
248 }
249 } finally
250 {
251 try
252 {
253 if (lineProcessor != null) lineProcessor.close();
254 } catch (Exception e)
255 {
256 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while closing line action.");
257 }
258 try
259 {
260 if (reader != null) reader.close();
261 } catch (Exception e)
262 {
263 ExceptionManagerFactory.getExceptionManager().manageException(e, "Exception caught while closing reader.");
264 }
265 }
266 }
267
268 public boolean parentStopped()
269 {
270 return parent.getStatus() == ProcessThread.THREAD_STOPPED;
271 }
272
273 /***
274 * Sets the value ofthestatusText for this process.
275 */
276 public void setStatusText(String statusText)
277 {
278 this.statusText = statusText;
279 }
280
281 public void log(String string)
282 {}
283
284 public void log(Exception exception, String message)
285 {}
286
287 }