01: /****************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one *
03: * or more contributor license agreements. See the NOTICE file *
04: * distributed with this work for additional information *
05: * regarding copyright ownership. The ASF licenses this file *
06: * to you under the Apache License, Version 2.0 (the *
07: * "License"); you may not use this file except in compliance *
08: * with the License. You may obtain a copy of the License at *
09: * *
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: ****************************************************************/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<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: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $
43: * @since 4.0
44: */
45: public class ExtensionFileFilter implements FilenameFilter {
46: private String[] m_extensions;
47:
48: public ExtensionFileFilter(final String[] extensions) {
49: m_extensions = extensions;
50: }
51:
52: public ExtensionFileFilter(final String extension) {
53: m_extensions = new String[] { extension };
54: }
55:
56: public boolean accept(final File file, final String name) {
57: for (int i = 0; i < m_extensions.length; i++) {
58: if (name.endsWith(m_extensions[i])) {
59: return true;
60: }
61: }
62: return false;
63: }
64: }
|