1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package de.fu_berlin.ties;
23
24 import java.io.File;
25 import java.io.FileFilter;
26 import java.io.IOException;
27
28 import org.apache.commons.lang.builder.ToStringBuilder;
29
30 /***
31 * Abstract base class for a {@link de.fu_berlin.ties.Processor} that operates
32 * on the contents of directories.
33 *
34 * @author Christian Siefkes
35 * @version $Revision: 1.6 $, $Date: 2006/10/21 16:03:52 $, $Author: siefkes $
36 */
37 public abstract class DirectoryProcessor extends ConfigurableProcessor {
38
39 /***
40 * The filter used to decide which files to accept.
41 */
42 private final FileFilter fileFilter;
43
44 /***
45 * Creates a new instance.
46 *
47 * @param filter the filter used to decide which files to accept
48 * @param conf used to configure this instance; if <code>null</code>,
49 * the {@linkplain TiesConfiguration#CONF standard configuration} is used
50 */
51 public DirectoryProcessor(final FileFilter filter,
52 final TiesConfiguration conf) {
53 super(conf);
54 fileFilter = filter;
55 }
56
57 /***
58 * Processes an array of files.
59 *
60 * @param files the array of files to process
61 * @param context a map of objects that are made available for processing;
62 * will be empty when called from the implemented <code>process</code>
63 * methods in this class
64 * @throws IOException if an I/O error occurs
65 * @throws ProcessingException if an error occurs during processing
66 */
67 public abstract void process(final File[] files, final ContextMap context)
68 throws IOException, ProcessingException;
69
70
71 /***
72 * Processes the contents of a directory.
73 *
74 * @param directory the the directory to process
75 * @throws IOException if an I/O error occurs
76 * @throws ProcessingException if an error occurs during processing
77 */
78 public final void process(final File directory)
79 throws IOException, ProcessingException {
80 if (!directory.isDirectory() || !directory.canRead()) {
81 throw new IllegalArgumentException(
82 directory + " is not a readable directory");
83 }
84
85
86 final File[] files = directory.listFiles(fileFilter);
87 final ContextMap context = new ContextMap();
88 process(files, context);
89 }
90
91 /***
92 * Processes an input argument that must specify a directory.
93 *
94 * @param inputName the name of the directory to process
95 * @throws IOException if an I/O error occurs
96 * @throws ProcessingException if an error occurs during processing
97 */
98 public final void process(final String inputName)
99 throws IOException, ProcessingException {
100 final File directory = new File(inputName);
101 process(directory);
102 }
103
104 /***
105 * Returns a string representation of this object.
106 *
107 * @return a textual representation
108 */
109 public String toString() {
110 return new ToStringBuilder(this)
111 .append("file filter", fileFilter)
112 .toString();
113 }
114
115 }