src/core/net/sf/basedb/util/RegexpFileFilter.java

Code
Comments
Other
Rev Date Author Line
3832 15 Oct 07 nicklas 1 /**
3832 15 Oct 07 nicklas 2   $Id$
3832 15 Oct 07 nicklas 3
3832 15 Oct 07 nicklas 4   Copyright (C) Authors contributing to this file.
3832 15 Oct 07 nicklas 5
3832 15 Oct 07 nicklas 6   This file is part of BASE - BioArray Software Environment.
3832 15 Oct 07 nicklas 7   Available at http://base.thep.lu.se/
3832 15 Oct 07 nicklas 8
3832 15 Oct 07 nicklas 9   BASE is free software; you can redistribute it and/or
3832 15 Oct 07 nicklas 10   modify it under the terms of the GNU General Public License
4479 05 Sep 08 jari 11   as published by the Free Software Foundation; either version 3
3832 15 Oct 07 nicklas 12   of the License, or (at your option) any later version.
3832 15 Oct 07 nicklas 13
3832 15 Oct 07 nicklas 14   BASE is distributed in the hope that it will be useful,
3832 15 Oct 07 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
3832 15 Oct 07 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3832 15 Oct 07 nicklas 17   GNU General Public License for more details.
3832 15 Oct 07 nicklas 18
3832 15 Oct 07 nicklas 19   You should have received a copy of the GNU General Public License
4515 11 Sep 08 jari 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
3832 15 Oct 07 nicklas 21 */
3832 15 Oct 07 nicklas 22 package net.sf.basedb.util;
3832 15 Oct 07 nicklas 23
3832 15 Oct 07 nicklas 24 import java.io.File;
3832 15 Oct 07 nicklas 25 import java.io.FileFilter;
3832 15 Oct 07 nicklas 26 import java.util.regex.Pattern;
3832 15 Oct 07 nicklas 27
3832 15 Oct 07 nicklas 28 /**
3837 15 Oct 07 nicklas 29   An implementation of the {@link FileFilter} interface
3837 15 Oct 07 nicklas 30   that filter files and directories based on regular expressions.
3832 15 Oct 07 nicklas 31
3832 15 Oct 07 nicklas 32   @author nicklas
3832 15 Oct 07 nicklas 33   @version 2.5
3832 15 Oct 07 nicklas 34   @base.modified $Date$
3832 15 Oct 07 nicklas 35 */
3832 15 Oct 07 nicklas 36 public class RegexpFileFilter
3837 15 Oct 07 nicklas 37   implements FileFilter
3832 15 Oct 07 nicklas 38 {
3832 15 Oct 07 nicklas 39
3832 15 Oct 07 nicklas 40   private final Pattern filePattern;
3832 15 Oct 07 nicklas 41   private final Pattern directoryPattern;
3832 15 Oct 07 nicklas 42   
3832 15 Oct 07 nicklas 43   /**
3832 15 Oct 07 nicklas 44     Create a new file filter from a string regular expressions.
3832 15 Oct 07 nicklas 45     @param fileRegexp The regular expression used to match file names or null to 
3832 15 Oct 07 nicklas 46       match all files
3832 15 Oct 07 nicklas 47     @param directoryRegexp The regular expression used to match directory names
3832 15 Oct 07 nicklas 48       or null to match all directories
3832 15 Oct 07 nicklas 49   */
3832 15 Oct 07 nicklas 50   public RegexpFileFilter(String fileRegexp, String directoryRegexp)
3832 15 Oct 07 nicklas 51   {
3832 15 Oct 07 nicklas 52     this.filePattern = fileRegexp == null ? null : Pattern.compile(fileRegexp);
3832 15 Oct 07 nicklas 53     this.directoryPattern = directoryRegexp == null ? null : Pattern.compile(directoryRegexp);
3832 15 Oct 07 nicklas 54   }
3832 15 Oct 07 nicklas 55   
3832 15 Oct 07 nicklas 56   /**
3832 15 Oct 07 nicklas 57     Create a new file filter from patterns.
3832 15 Oct 07 nicklas 58     @param filePattern The pattern used to match file names, or null
3832 15 Oct 07 nicklas 59       to match all files
3832 15 Oct 07 nicklas 60     @param directoryPattern The pattern used to match the directory names, or null
3832 15 Oct 07 nicklas 61       to match all directories
3832 15 Oct 07 nicklas 62   */
3832 15 Oct 07 nicklas 63   public RegexpFileFilter(Pattern filePattern, Pattern directoryPattern)
3832 15 Oct 07 nicklas 64   {
3832 15 Oct 07 nicklas 65     this.filePattern = filePattern;
3832 15 Oct 07 nicklas 66     this.directoryPattern = directoryPattern;
3832 15 Oct 07 nicklas 67   }
3832 15 Oct 07 nicklas 68   
3832 15 Oct 07 nicklas 69   /**
3832 15 Oct 07 nicklas 70     Accept files with a name matching the regular expression
3832 15 Oct 07 nicklas 71   */
6127 14 Sep 12 nicklas 72   @Override
3832 15 Oct 07 nicklas 73   public boolean accept(File file)
3832 15 Oct 07 nicklas 74   {
3832 15 Oct 07 nicklas 75     if (file == null)
3832 15 Oct 07 nicklas 76     {
3832 15 Oct 07 nicklas 77       return directoryPattern == null && filePattern == null;
3832 15 Oct 07 nicklas 78     }
3832 15 Oct 07 nicklas 79     else if (file.isDirectory())
3832 15 Oct 07 nicklas 80     {
3832 15 Oct 07 nicklas 81       return directoryPattern == null || directoryPattern.matcher(file.getName()).matches();
3832 15 Oct 07 nicklas 82     }
3832 15 Oct 07 nicklas 83     else
3832 15 Oct 07 nicklas 84     {
3832 15 Oct 07 nicklas 85       return filePattern == null || filePattern.matcher(file.getName()).matches();
3832 15 Oct 07 nicklas 86     }
3832 15 Oct 07 nicklas 87   }
3832 15 Oct 07 nicklas 88
3832 15 Oct 07 nicklas 89 }