001/* 002 * Copyright (c) 2005 Einar Pehrson <einar@pehrson.nu>. 003 * 004 * This file is part of 005 * CleanSheets - a spreadsheet application for the Java platform. 006 * 007 * CleanSheets is free software; you can redistribute it and/or modify 008 * it under the terms of the GNU General Public License as published by 009 * the Free Software Foundation; either version 2 of the License, or 010 * (at your option) any later version. 011 * 012 * CleanSheets is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 015 * GNU General Public License for more details. 016 * 017 * You should have received a copy of the GNU General Public License 018 * along with CleanSheets; if not, write to the Free Software 019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 */ 021package csheets.io; 022 023import java.io.File; 024import java.io.FilenameFilter; 025import java.util.Arrays; 026import java.util.List; 027 028/** 029 * A filename filter for filtering on filename extensions. 030 * @author Einar Pehrson 031 */ 032public class FilenameExtensionFilter implements FilenameFilter { 033 034 /** The extensions of allowed files. */ 035 private List<String> extensions; 036 037 /** 038 * Creates a new filter for files with the given extensions. 039 * @param extensions the extension of allowed files 040 */ 041 public FilenameExtensionFilter(String... extensions) { 042 this(Arrays.asList(extensions)); 043 } 044 045 /** 046 * Creates a new filter for files with the given extensions. 047 * @param extensions the extension of allowed files 048 */ 049 public FilenameExtensionFilter(List<String> extensions) { 050 if (extensions.size() == 0) 051 throw new IllegalArgumentException(); 052 this.extensions = extensions; 053 } 054 055 public boolean accept(File file, String filename) { 056 String extension = filename.substring(filename.lastIndexOf('.') + 1).toLowerCase().trim(); 057 return extensions.contains(extension); 058 } 059 060 /** 061 * Returns the file extensions that the filter allows. 062 * @return the file extensions that the filter allows 063 */ 064 public String[] getExtensions() { 065 return extensions.toArray(new String[extensions.size()]); 066 } 067}