View Javadoc

1   /*
2    * Copyright (C) 2004-2006 Christian Siefkes <christian@siefkes.net>.
3    * Development of this software is supported by the German Research Society,
4    * Berlin-Brandenburg Graduate School in Distributed Information Systems
5    * (DFG grant no. GRK 316).
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, visit
19   * http://www.gnu.org/licenses/gpl.html or write to the Free Software
20   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
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          // filter files + delegate to abstract method
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 }