KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > cornerstone > blocks > masterstore > ExtensionFileFilter


1 /*
2  * Copyright 1999-2004 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.avalon.cornerstone.blocks.masterstore;
19
20 import java.io.File JavaDoc;
21 import java.io.FilenameFilter JavaDoc;
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: 1.1 $ $Date: 2004/03/15 15:42:05 $
43  * @since 4.0
44  */

45 public class ExtensionFileFilter
46     implements FilenameFilter JavaDoc
47 {
48     private String JavaDoc[] m_extensions;
49
50     public ExtensionFileFilter( final String JavaDoc[] extensions )
51     {
52         m_extensions = extensions;
53     }
54
55     public ExtensionFileFilter( final String JavaDoc extension )
56     {
57         m_extensions = new String JavaDoc[]{extension};
58     }
59
60     public boolean accept( final File JavaDoc file, final String JavaDoc 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
74
Popular Tags