source: trunk/gli/src/org/greenstone/gatherer/shell/GImportProgressMonitor.java@ 8236

Last change on this file since 8236 was 8236, checked in by mdewsnip, 20 years ago

Replaced all Gatherer.print* with DebugStream.print*.

  • Property svn:keywords set to Author Date Id Revision
File size: 17.3 KB
Line 
1/**
2 *#########################################################################
3 *
4 * A component of the Gatherer application, part of the Greenstone digital
5 * library suite from the New Zealand Digital Library Project at the
6 * University of Waikato, New Zealand.
7 *
8 * <BR><BR>
9 *
10 * Author: John Thompson, Greenstone Digital Library, University of Waikato
11 *
12 * <BR><BR>
13 *
14 * Copyright (C) 1999 New Zealand Digital Library Project
15 *
16 * <BR><BR>
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * <BR><BR>
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * <BR><BR>
31 *
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35 *########################################################################
36 */
37package org.greenstone.gatherer.shell;
38
39import java.awt.Component;
40import java.util.ArrayList;
41import java.util.StringTokenizer;
42import javax.swing.JProgressBar;
43import org.greenstone.gatherer.Configuration;
44import org.greenstone.gatherer.DebugStream;
45import org.greenstone.gatherer.Dictionary;
46import org.greenstone.gatherer.Gatherer;
47import org.greenstone.gatherer.shell.GShellElement;
48import org.greenstone.gatherer.shell.GShellProgressMonitor;
49import org.greenstone.gatherer.util.StaticStrings;
50
51/** This implementation of <i>GShellProgressMonitor</i> is designed to parse and translate the progress of a import.pl call.
52 * @author John Thompson, Greenstone Digital Library, University of Waikato
53 * @version 2.1
54 */
55public class GImportProgressMonitor
56 implements GShellProgressMonitor {
57 /** Indicates if the progress bar is currently showing a string. */
58 private boolean showing_string = false;
59 /** Indicates if the GUI has asked the process this object monitors to stop. */
60 private boolean stop = false;
61 /** A count of the extracted files processed so far. */
62 private int extracted_file_count;
63 /** The number of documents processed (or rejected) so far. */
64 private int file_count;
65 /** The next value to be set for the progress bar - I use this rather than a compounding progress measure to try to limit rounding inaccuracies (nothing looks worse than the progress bar having to jump the last 10-15%) */
66 private int next_progress_value;
67 /** The number of files expected to be scanned by this import process. */
68 private int num_expected_docs;
69 /** This holds the number of documents actually processed by the import command, as garnered from the final block of text output. */
70 private int num_docs;
71
72 private int threshold = Configuration.SYSTEMS_MODE;
73 /** The progress bar this monitor updates. */
74 private JProgressBar progress_bar;
75
76 /** The progress bar that is shared with build monitor. */
77 private JProgressBar shared_progress_bar;
78
79 /** */
80 static final private String BLOCKED_ATTRIBUTE = "blocked";
81 static final private String CONSIDERED_ATTRIBUTE = "considered";
82 static final private String FAILED_ATTRIBUTE = "failed";
83 static final private String IGNORED_ATTRIBUTE = "ignored";
84 static final private String IMPORT_ELEMENT = "Import";
85 static final private String PROCESSED_ATTRIBUTE = "processed";
86 /** */
87 static final private String NAME_ATTRIBUTE = "n";
88 /** */
89 static final private String PLUGIN_ATTRIBUTE = "p";
90 /** The fixed portion of the progress bar used for the calculating of file size and other pre-import functions. */
91 static final private int CALCULATION = 50000;
92 /** The fixed portion of the progress bar used for extracted metadata. */
93 static final private int EXTRACTED = 200000;
94 /** The minimum value for this progress bar. */
95 static final private int MIN = 0;
96 /** The fixed portion of the progress bar used for processed documents. */
97 static final private int PROCESSED = 750000;
98 /** The element name of a file detected message. */
99 static final private String FILE_ELEMENT = "File";
100 /** The element name of a file processing message. */
101 static final private String PROCESSING_ELEMENT = "Processing";
102 /** The element name of a processing error message. */
103 static final private String PROCESSINGERROR_ELEMENT = "ProcessingError";
104 /** The element name of an import complete message. */
105 static final private String IMPORTCOMPLETE_ELEMENT = "ImportComplete";
106 /** The element name of a file not processed. */
107 static final private String NONPROCESSEDFILE_ELEMENT = "NonProcessedFile";
108 /** The element name of a file not recognised. */
109 static final private String NONRECOGNISEDFILE_ELEMENT = "NonRecognisedFile";
110
111
112 public GImportProgressMonitor() {
113 progress_bar = new JProgressBar();
114 progress_bar.setIndeterminate(false);
115 progress_bar.setMaximum(CALCULATION + PROCESSED + EXTRACTED);
116 progress_bar.setMinimum(MIN);
117 progress_bar.setString(null);
118 progress_bar.setStringPainted(true);
119 next_progress_value = CALCULATION;
120 shared_progress_bar = new JProgressBar();
121 shared_progress_bar.setIndeterminate(false);
122 shared_progress_bar.setMaximum((CALCULATION + PROCESSED + EXTRACTED) * 2);
123 shared_progress_bar.setMinimum(MIN);
124 shared_progress_bar.setString(null); // Default string
125 shared_progress_bar.setStringPainted(true);
126 setValue(MIN);
127 }
128
129 /** Method to register a new progress bar with this monitor.
130 * @param progress_bar The new <strong>JProgressBar</strong>.
131 */
132 public void addProgressBar(JProgressBar progress_bar) {
133 this.progress_bar = progress_bar;
134 progress_bar.setMaximum(CALCULATION + PROCESSED + EXTRACTED);
135 progress_bar.setMinimum(MIN);
136 setValue(MIN);
137 next_progress_value = CALCULATION;
138 }
139
140 /** Determine the script exit value according to the progress monitor. This gets around a problem where several script failures actually result with a successful exit value.
141 * @return A <i>int</i> with a value of zero if and only if the script was successful.
142 */
143 public int exitValue() {
144 if(num_docs > 0) {
145 return 0;
146 }
147 return 1;
148 }
149
150 /** Retrieve the number of documents recorded by the import monitor during the execution of the import scripts.
151 * @return An <i>int</i> giving the document number.
152 */
153 public int getNumberOfDocuments() {
154 return num_docs;
155 }
156
157 /** Method to retrieve whatever control is being used as the progress indicator. Usually a <strong>JProgressBar</strong> but there may be others implemented later.
158 * @return A <strong>Component</strong> on which the progress of the process is being displayed.
159 */
160 public Component getProgress() {
161 return progress_bar;
162 }
163
164 public JProgressBar getSharedProgress() {
165 return shared_progress_bar;
166 }
167
168 public void messageOnProgressBar(String message)
169 {
170 if ((message!=null) & (message!="")) {
171 progress_bar.setString(message);
172 shared_progress_bar.setString(message);
173 showing_string = true;
174 }
175 else {
176 progress_bar.setString(null);
177 shared_progress_bar.setString(null);
178 showing_string = false;
179 }
180 }
181
182 /** Method to determine the state of the stop flag, which may be set by the visual component that created this monitor.
183 * @return A <strong>boolean</strong> indicating if the process has been asked to stop.
184 */
185 public synchronized boolean hasSignalledStop() {
186 return stop;
187 }
188
189 /** Inform the progress bar that it should programatically increment progress by one step. This is only called during the metadata archive extraction so each step should be (1000000 / 5) / num_docs. */
190 public void increment() {
191 extracted_file_count++;
192 // The current progress is calculated to be:
193 // The fixed calculation value plus the fixed processed value plus some portion of the fixed extracted value. This portion is the extracted_file_count over the total number of documents available. Note that this breaks badly for bibliographical files (for now).
194 setValue(CALCULATION + PROCESSED + ((EXTRACTED * extracted_file_count) / num_docs));
195 }
196
197 /** This method is used to 'feed in' a line of text captured from the process.
198 * @param queue a queue which at the moment should contain a single GShellEvent
199 */
200 public void process(ArrayList queue) {
201 // Retrieve the first event.
202 GShellEvent event = (GShellEvent) queue.get(0);
203 // Remove 'import.pl> ' bit
204 String line = event.getMessage();
205 line = line.substring(line.indexOf(StaticStrings.GREATER_THAN_CHARACTER) + 1);
206 line = line.trim();
207 // System.err.println("**** line = " + line);
208
209 ///ystem.err.println("Process: " + line);
210 if(line.startsWith(StaticStrings.LESS_THAN_CHARACTER) && line.endsWith(StaticStrings.GREATER_THAN_CHARACTER)) {
211 // No original event is passed on, even in the lower modes
212 event.veto();
213 // Create a new element from it
214 GShellElement element = new GShellElement(line);
215 String name = element.getElementName();
216 ///ystem.err.println("Gatherer tag: " + name);
217 // We may be reading a file. Remember we have to offset process as we recieve this message 'before' a document is processed. Hence the use of 'next_progress_value'
218 if(name.equals(IMPORT_ELEMENT)) {
219 ///ystem.err.println("#Import");
220 // We're into parsing output, so we don't need the 'calculating file size' etc string.
221 if(showing_string) {
222 progress_bar.setString(null);
223 showing_string = false;
224 }
225 if(Configuration.getMode() <= threshold) {
226 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportBegun1"), event.getStatus()));
227 }
228 }
229 else if(name.equals(FILE_ELEMENT)) {
230 ///ystem.err.println("#File");
231 file_count++;
232 // Set the next progress
233 if(next_progress_value > 0) {
234 setValue(next_progress_value);
235 }
236 // Now we calculate the next progress value
237 next_progress_value = CALCULATION + ((PROCESSED * file_count) / num_expected_docs);
238 event.veto(); // Unconditional veto
239 }
240 // Or we're being told what plugin is actually processing the file
241 else if(name.equals(PROCESSING_ELEMENT)) {
242 ///ystem.err.println("#FileProcessing");
243 // If we are at lower mode settings fire a new 'dumbed down' event
244 if(Configuration.getMode() <= threshold) {
245 String args[] = new String[2];
246 args[0] = element.getAttribute(NAME_ATTRIBUTE);
247 args[1] = element.getAttribute(PLUGIN_ATTRIBUTE);
248 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.FileProcessing", args), event.getStatus()));
249 args = null;
250 }
251 }
252 // processing error
253 else if (name.equals(PROCESSINGERROR_ELEMENT)) {
254 if(Configuration.getMode() <= threshold) {
255 String args[] = new String[1];
256 args[0] = element.getAttribute(NAME_ATTRIBUTE);
257 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.FileProcessingError", args), event.getStatus()));
258 args = null;
259 }
260 }
261 // unrecognised file
262 else if (name.equals(NONRECOGNISEDFILE_ELEMENT)) {
263 if(Configuration.getMode() <= threshold) {
264 String args[] = new String[1];
265 args[0] =element.getAttribute(NAME_ATTRIBUTE);
266 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.FileNotRecognised", args), event.getStatus()));
267 args = null;
268 }
269 }
270 // unprocessed file
271 else if (name.equals(NONPROCESSEDFILE_ELEMENT)) {
272 if(Configuration.getMode() <= threshold) {
273 String args[] = new String[1];
274 args[0] =element.getAttribute(NAME_ATTRIBUTE);
275 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.FileNotProcessed", args), event.getStatus()));
276 args = null;
277 }
278 }
279 // Or the import complete element
280 else if(name.equals(IMPORTCOMPLETE_ELEMENT)) {
281 // Set the next progress
282 setValue(next_progress_value);
283 // If we are at lower mode settings fire a new 'dumbed down' event
284 if(Configuration.getMode() <= threshold) {
285 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete1"), event.getStatus()));
286 String considered_str = element.getAttribute(CONSIDERED_ATTRIBUTE);
287
288 if (considered_str.equals(StaticStrings.ONE_CHARACTER)) {
289 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete2.1"), event.getStatus()));
290 } else {
291 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete2", considered_str), event.getStatus()));
292 }
293 considered_str = null;
294 // The number of documents processed
295 String processed_str = element.getAttribute(PROCESSED_ATTRIBUTE);
296 if(!processed_str.equals(StaticStrings.ZERO_CHARACTER)) {
297 if(processed_str.equals(StaticStrings.ONE_CHARACTER)) {
298 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Processed.1"), event.getStatus()));
299 }
300 else {
301 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Processed", processed_str), event.getStatus()));
302 }
303 }
304 // Try to set num docs
305 try {
306 num_docs = Integer.parseInt(processed_str);
307 }
308 catch(Exception exception) {
309 num_docs = 0;
310 }
311 processed_str = null;
312
313 // The number of documents blocked
314 String blocked_str = element.getAttribute(BLOCKED_ATTRIBUTE);
315 if(!blocked_str.equals(StaticStrings.ZERO_CHARACTER)) {
316 if (blocked_str.equals(StaticStrings.ONE_CHARACTER)) {
317 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Blocked.1"), event.getStatus()));
318 } else {
319 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Blocked", blocked_str), event.getStatus()));
320 }
321 }
322 blocked_str = null;
323
324 // The number of documents ignored
325 String ignored_str = element.getAttribute(IGNORED_ATTRIBUTE);
326 if(!ignored_str.equals(StaticStrings.ZERO_CHARACTER)) {
327 if(ignored_str.equals(StaticStrings.ONE_CHARACTER)) {
328 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Ignored.1"), event.getStatus()));
329 } else {
330 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Ignored", ignored_str), event.getStatus()));
331 }
332 }
333 ignored_str = null;
334
335 // The number of documents failed
336 String failed_str = element.getAttribute(FAILED_ATTRIBUTE);
337 if(!failed_str.equals(StaticStrings.ZERO_CHARACTER)) {
338 if(failed_str.equals(StaticStrings.ONE_CHARACTER)) {
339 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Failed.1"), event.getStatus()));
340 } else {
341 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), " "+Dictionary.get("GShell.Import.ImportComplete.Failed", failed_str), event.getStatus()));
342 }
343 }
344 failed_str = null;
345 // Message is finally ready
346 queue.add(new GShellEvent(event.getSource(), 0, event.getType(), Dictionary.get("GShell.Import.ImportComplete3"), event.getStatus()));
347 } // if mode below threshhold
348 else {
349 // Try to set num docs
350 String processed_str = element.getAttribute(PROCESSED_ATTRIBUTE);
351 try {
352 num_docs = Integer.parseInt(processed_str);
353 }
354 catch(Exception exception) {
355 num_docs = 0;
356 }
357 }
358 }
359 else {
360 // Veto it
361 event.veto();
362 }
363 } // GLI output
364 else if(Configuration.getMode() <= threshold) {
365 event.veto();
366 }
367 }
368
369 public void reset() {
370 setValue(MIN);
371 progress_bar.setString(null);
372 progress_bar.updateUI();
373 }
374
375 public void saving() {
376 progress_bar.setString(Dictionary.get("SaveProgressDialog.Title"));
377 showing_string = true;
378 }
379
380 /** Since the creator of this process monitor is actually in the GUI, this class provides the simpliest way to send a cancel process message between the two.
381 * @param state The desired state of the stop flag as a <strong>boolean</strong>.
382 */
383 public synchronized void setStop(boolean state) {
384 this.stop = state;
385 }
386
387 /** This method resets this monitor to the start, reseting the process parsing and progress bar.
388 */
389 public void start() {
390 stop = false;
391 setValue(MIN);
392 progress_bar.setString(Dictionary.get("FileActions.Calculating_Size"));
393 showing_string = true;
394 extracted_file_count = 0;
395 file_count = 0;
396 next_progress_value = -1;
397 num_docs = -1;
398 num_expected_docs = Gatherer.c_man.getCollection().getCount();
399 }
400
401 /** This method indicates the process is complete.
402 */
403 public void stop() {
404 setValue(CALCULATION + PROCESSED + EXTRACTED);
405 }
406
407 int previous_value = -1;
408 private void setValue(int value) {
409 progress_bar.setValue(value);
410 shared_progress_bar.setValue(value);
411 if(value != 0 && value < previous_value) {
412 DebugStream.println("Progress is decreasing! " + previous_value + " -> " + value);
413 }
414 previous_value = value;
415 }
416}
417
Note: See TracBrowser for help on using the repository browser.