📄 filefilterabs.java
字号:
package jodd.file.filters;
import java.io.File;
import java.io.FileFilter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jodd.util.StringFlags;
import jodd.util.StringUtil;
/**
* Abstract FileFilter contains default logic and parameters for all
* implemented file filters. <p>
*
* By default, both files and directories are matched, but only by name (not
* including the path). This behaviour can be changed with following flags:
*
* <ul>
* <li>p - match also the path part of the file name</li>
* <li>d - do not match directories (skip)</li>
* </ul>
*
* Note: when path is also used for matching (flag p), non-unix file path
* separator is <b>always</b> changed to unix separator ('/'). Therefore,
* matching must be always done against unix file separators.
*/
abstract class FileFilterAbs implements FileFilter {
// ---------------------------------------------------------------- initialization
/**
* Fast constructor that uses a string of options for setting file filter
* properties.
*
* @param pattern pattern to match against
* @param opt options, subset of following flags:
* <ul>
* <li>p - match also the path part of the file name</li>
* <li>d - do not match directories, i.e. all directories matches ok.</li>
* </ul>
*/
public FileFilterAbs(String pattern, String opt) {
flags.parse(opt, this);
setPattern(pattern);
}
/**
* File filter with default behaviour.
*
* @param pattern
*/
public FileFilterAbs(String pattern) {
setPattern(pattern);
}
/**
* Regular Expression file filter with no behaviour and that may be
* configured later.
*/
public FileFilterAbs() {
}
// ---------------------------------------------------------------- flags
private static StringFlags flags = new StringFlags();
static {
flags.addFlag('p', "matchPath", true);
flags.addFlag('d', "skipDirs", true);
}
boolean matchPath;
/**
* Set match path flag.
*
* @param v
*/
public void setMatchPath(boolean v) {
matchPath = v;
}
/**
* Returns match path flag.
*
* @return match flag
*/
public boolean getMatchPath() {
return matchPath;
}
boolean skipDirs;
/**
* Set skip directory flag.
*
* @param v
*/
public void setSkipDirs(boolean v) {
skipDirs = v;
}
/**
* Returns skip directory flag.
*
* @return skip dirs flag
*/
public boolean getSkipDirs() {
return skipDirs;
}
String pattern;
/**
* Set pattern.
*
* @param p pattern
*/
public void setPattern(String p) {
pattern = p;
}
/**
* Returns pattern.
*
* @return pattern
*/
public String getPattern() {
return pattern;
}
// ---------------------------------------------------------------- FileFilter
static boolean isUnix = false;
static {
if (File.separatorChar == '/') {
isUnix = true;
} else {
isUnix = false;
}
}
/**
* Prepares the tests whether or not the specified File matches conditions.
*
* @param f file to be tested
*
* @return <code>true</code> if file matches ok, otherwise <code>false</code>
*/
public boolean accept(File f) {
if (f.isDirectory()) { // file is a directory
if (skipDirs == true) { // return if no matching required
return true;
}
}
if (pattern == null) { // no pattern specified -> no matching possible
return true; // therefore file is ok
}
return match(f);
}
/**
* Returns the file name of a File, either just file name either full name
* with path included, regarding of file filter parameters. Path separators
* are changed to unix-alike. This helper method should be used by the
* implementations.
*
* @param f file
*
* @return file name
*/
String getFileName(File f) {
String fileName = null;
if (matchPath == false) {
fileName = f.getName(); // do not match the path
} else {
fileName = f.getPath(); // match complete file name, including path
if (isUnix == false) {
fileName = StringUtil.replace(fileName, File.separatorChar, '/');
}
}
return fileName;
}
/**
* User-defined matching.
*
* @param file file to match
*
* @return <code>true</code> if file matches the pattern, <code>false</code>
* otherwise.
*/
abstract boolean match(File file);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -