View Javadoc

1   /* 
2    * Copyright 1999-2006 The Apache Software Foundation
3    * Licensed  under the  Apache License,  Version 2.0  (the "License");
4    * you may not use  this file  except in  compliance with the License.
5    * You may obtain a copy of the License at 
6    * 
7    *   http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed  under the  License is distributed on an "AS IS" BASIS,
11   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
12   * implied.
13   * 
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.james.util.io;
19  
20  import java.io.File;
21  import java.io.FilenameFilter;
22  
23  /***
24   * This filters files based on the extension (what the filename
25   * ends with). This is used in retrieving all the files of a
26   * particular type.
27   *
28   * <p>Eg., to retrieve and print all <code>*.java</code> files in the current directory:</p>
29   *
30   * <pre>
31   * File dir = new File(".");
32   * String[] files = dir.list( new ExtensionFileFilter( new String[]{"java"} ) );
33   * for (int i=0; i&lt;files.length; i++)
34   * {
35   *     System.out.println(files[i]);
36   * }
37   * </pre>
38   *
39   * @author  Federico Barbieri <fede@apache.org>
40   * @author Serge Knystautas <sergek@lokitech.com>
41   * @author Peter Donald
42   * @version CVS $Revision: 365582 $ $Date: 2006-01-03 08:51:21 +0000 (mar, 03 gen 2006) $
43   * @since 4.0
44   */
45  public class ExtensionFileFilter
46      implements FilenameFilter
47  {
48      private String[] m_extensions;
49  
50      public ExtensionFileFilter( final String[] extensions )
51      {
52          m_extensions = extensions;
53      }
54  
55      public ExtensionFileFilter( final String extension )
56      {
57          m_extensions = new String[]{extension};
58      }
59  
60      public boolean accept( final File file, final String name )
61      {
62          for( int i = 0; i < m_extensions.length; i++ )
63          {
64              if( name.endsWith( m_extensions[ i ] ) )
65              {
66                  return true;
67              }
68          }
69          return false;
70      }
71  }
72  
73