1   package com.mccrory.scott.base;
2   
3   ////////////////////////////////////////////////////////////////////////////////
4   // Copyright (C) 2002  Scott McCrory
5   //
6   // This program is free software; you can redistribute it and/or
7   // modify it under the terms of the GNU General Public License
8   // as published by the Free Software Foundation; either version 2
9   // of the License, or (at your option) any later version.
10  //
11  // This program is distributed in the hope that it will be useful,
12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  // GNU General Public License for more details.
15  //
16  // You should have received a copy of the GNU General Public License
17  // along with this program; if not, write to the Free Software
18  // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  ////////////////////////////////////////////////////////////////////////////////
20  
21  import java.io.File;
22  import java.io.FilenameFilter;
23  
24  /**
25   * <P>A case-insensitive, concrete implementation of java.io.FilenameFilter.
26   * Patterned after an example in Bruce Eckel's "Thinking in Java".
27   * Example:
28   * <pre>
29   *  // Get a list of XML files in a directory
30   *  ConcreteFilenameFilter xmlFilter = new ConcreteFilenameFilter(".xml");
31   *  File fileList[] = prefsDir.listFiles(xmlFilter);
32   *  if (fileList == null || fileList.length < 1) {
33   *      throw new IOException("'" + prefsDirString + "' has no .xml files in it!");
34   *  }
35   * </pre></P>
36   * @author <a href="mailto:smccrory@users.sourceforge.net">Scott McCrory</a>.
37   * @version CVS $Id: ConcreteFilenameFilter.java,v 1.19 2002/08/04 22:04:53 smccrory Exp $
38   */
39  public class ConcreteFilenameFilter implements FilenameFilter {
40  
41      /** The filename to check **/
42      private String filenameFilter;
43  
44      /** The name of this class to log context without introspection **/
45      private static final String CLASS_NAME = "ConcreteFilenameFilter";
46  
47      /** The version of this class (filled in by CVS) **/
48      private static final String VERSION = "CVS $Revision: 1.19 $";
49  
50      /**
51       * ConcreteFilenameFilter constructor.
52       *
53       * @param fileFilter    The filename filter
54       */
55      public ConcreteFilenameFilter(String fileFilter) {
56          this.filenameFilter = fileFilter;
57      }
58  
59      /**
60       * Required for the FilenameFilter interface.
61       *
62       * @param   dir         The directory
63       * @param   name        The filename
64       * @return  boolean     Validity indicator
65       */
66      public boolean accept(File dir, String name) {
67  
68          // Strip path information:
69          String f = new File(name).getName();
70  
71          // Case insensitive
72          f = f.toLowerCase();
73          filenameFilter = filenameFilter.toLowerCase();
74  
75          // See if the pattern matches 
76          return f.indexOf(filenameFilter) != -1;
77  
78      }
79  
80  }
81