View Javadoc

1   /*
2    * $Id: RegExpFileFilter.java,v 1.2 2004/04/27 11:01:42 johndavidtaylor Exp $
3    * Created on Mar 9, 2004 by jdt The alipes project (c) 2004
4    */
5   package org.abraracourcix.alipes.common;
6   import java.io.File;
7   import java.io.FileFilter;
8   import java.util.regex.Matcher;
9   import java.util.regex.Pattern;
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  /***
13   *
14   * A file filter which takes a regular expression and filters files in a single
15   * directory
16  * @author jdt
17   */
18  public final class RegExpFileFilter implements FileFilter {
19      /***
20       * Standard commons logging
21       */
22      private Log log = LogFactory.getLog("RegExpFileFilter.class");
23      /***
24       * Inherited
25       * @param file file to be tested
26       * @return true if file matches
27       * @see java.io.FileFilter#accept(java.io.File)
28       */
29      public boolean accept(final File file) {
30          if (file == null) {
31              return false;
32          }
33          final String filename = file.getName();
34          final File parent = file.getParentFile();
35          log.debug("Test file " + file);
36          log.debug("Filename " + filename);
37          log.debug("Parent " + parent);
38          if (rootDir!=null && !(rootDir.equals(parent))) {
39              return false;
40          }
41          final Matcher matcher = pattern.matcher(filename);
42          return matcher.matches();
43      }
44      /***
45       * The pattern against which we compare
46       */
47      private Pattern pattern;
48      /***
49       * Files must be in this folder
50       */
51      private File rootDir;
52      /***
53       * Constructor
54       * 
55       * @param regexp
56       *            the regular expression to match
57       * @param rootDirectory
58       *            the directory in which to search - can be null if we don't care
59       */
60      public RegExpFileFilter(final String regexp, final File rootDirectory) {
61          pattern = Pattern.compile(regexp);
62          this.rootDir = rootDirectory;
63          log.debug("Root directory: " + rootDir);
64      }
65  }
66  /*
67   * $Log: RegExpFileFilter.java,v $
68   * Revision 1.2  2004/04/27 11:01:42  johndavidtaylor
69   * Directory can now be null, in which case the match is done entirely
70   * on the file name
71   *
72   * Revision 1.1  2004/03/18 15:21:54  johndavidtaylor
73   * Copied across from incubation in beanpeeler
74   *
75   * Revision 1.4  2004/03/14 18:49:15  johndavidtaylor
76   * corrected comments
77   *
78   * Revision 1.3  2004/03/11 15:46:35  johndavidtaylor
79   * Applied coding standards
80   *
81   * Revision 1.2  2004/03/11 09:49:24  johndavidtaylor
82   * Loads of updates
83   *
84   */