001/****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one   *
003 * or more contributor license agreements.  See the NOTICE file *
004 * distributed with this work for additional information        *
005 * regarding copyright ownership.  The ASF licenses this file   *
006 * to you under the Apache License, Version 2.0 (the            *
007 * "License"); you may not use this file except in compliance   *
008 * with the License.  You may obtain a copy of the License at   *
009 *                                                              *
010 *   http://www.apache.org/licenses/LICENSE-2.0                 *
011 *                                                              *
012 * Unless required by applicable law or agreed to in writing,   *
013 * software distributed under the License is distributed on an  *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
015 * KIND, either express or implied.  See the License for the    *
016 * specific language governing permissions and limitations      *
017 * under the License.                                           *
018 ****************************************************************/
019
020package org.apache.james.repository.file;
021
022import java.io.File;
023import java.io.FilenameFilter;
024
025/**
026 * This filters files based on the extension (what the filename ends with). This
027 * is used in retrieving all the files of a particular type.
028 * 
029 * <p>
030 * Eg., to retrieve and print all <code>*.java</code> files in the current
031 * directory:
032 * </p>
033 * 
034 * <pre>
035 * File dir = new File(&quot;.&quot;);
036 * String[] files = dir.list(new ExtensionFileFilter(new String[] { &quot;java&quot; }));
037 * for (int i = 0; i &lt; files.length; i++) {
038 *     System.out.println(files[i]);
039 * }
040 * </pre>
041 */
042public class ExtensionFileFilter implements FilenameFilter {
043    private final String[] m_extensions;
044
045    public ExtensionFileFilter(String[] extensions) {
046        m_extensions = extensions;
047    }
048
049    public ExtensionFileFilter(String extension) {
050        m_extensions = new String[] { extension };
051    }
052
053    public boolean accept(File file, String name) {
054        for (String m_extension : m_extensions) {
055            if (name.endsWith(m_extension)) {
056                return true;
057            }
058        }
059        return false;
060    }
061}