3832 |
15 Oct 07 |
nicklas |
1 |
/** |
3832 |
15 Oct 07 |
nicklas |
$Id$ |
3832 |
15 Oct 07 |
nicklas |
3 |
|
3832 |
15 Oct 07 |
nicklas |
Copyright (C) Authors contributing to this file. |
3832 |
15 Oct 07 |
nicklas |
5 |
|
3832 |
15 Oct 07 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
3832 |
15 Oct 07 |
nicklas |
Available at http://base.thep.lu.se/ |
3832 |
15 Oct 07 |
nicklas |
8 |
|
3832 |
15 Oct 07 |
nicklas |
BASE is free software; you can redistribute it and/or |
3832 |
15 Oct 07 |
nicklas |
modify it under the terms of the GNU General Public License |
4479 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
3832 |
15 Oct 07 |
nicklas |
of the License, or (at your option) any later version. |
3832 |
15 Oct 07 |
nicklas |
13 |
|
3832 |
15 Oct 07 |
nicklas |
BASE is distributed in the hope that it will be useful, |
3832 |
15 Oct 07 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
3832 |
15 Oct 07 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3832 |
15 Oct 07 |
nicklas |
GNU General Public License for more details. |
3832 |
15 Oct 07 |
nicklas |
18 |
|
3832 |
15 Oct 07 |
nicklas |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
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 |
An implementation of the {@link FileFilter} interface |
3837 |
15 Oct 07 |
nicklas |
that filter files and directories based on regular expressions. |
3832 |
15 Oct 07 |
nicklas |
31 |
|
3832 |
15 Oct 07 |
nicklas |
@author nicklas |
3832 |
15 Oct 07 |
nicklas |
@version 2.5 |
3832 |
15 Oct 07 |
nicklas |
@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 |
Create a new file filter from a string regular expressions. |
3832 |
15 Oct 07 |
nicklas |
@param fileRegexp The regular expression used to match file names or null to |
3832 |
15 Oct 07 |
nicklas |
match all files |
3832 |
15 Oct 07 |
nicklas |
@param directoryRegexp The regular expression used to match directory names |
3832 |
15 Oct 07 |
nicklas |
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 |
Create a new file filter from patterns. |
3832 |
15 Oct 07 |
nicklas |
@param filePattern The pattern used to match file names, or null |
3832 |
15 Oct 07 |
nicklas |
to match all files |
3832 |
15 Oct 07 |
nicklas |
@param directoryPattern The pattern used to match the directory names, or null |
3832 |
15 Oct 07 |
nicklas |
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 |
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 |
} |