View Javadoc

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