📄 wildcardfilter.java
字号:
/**
* DuMP3 version morpheus_0.2.9 - a duplicate/similar file finder in Java<BR>
* Copyright 2005 Alexander Grässer<BR>
* All Rights Reserved, http://dump3.sourceforge.net/<BR>
* <BR>
* This file is part of DuMP3.<BR>
* <BR>
* DuMP3 is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later version.<BR>
* <BR>
* DuMP3 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.<BR>
* <BR>
* You should have received a copy of the GNU General Public License along with DuMP3; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
* Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.za.grasser.duplicate.file;
import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Pattern;
/**
* @author <a href="http://sourceforge.net/sendmessage.php?touser=733840">pyropunk at sourceforge dot net</a>
* @version $Revision: 1.9 $
* @modelguid {5966E67B-7CB9-4DF4-86E4-F6284FC9F3F3}
*/
public class WildcardFilter implements FilenameFilter {
/**
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return new String(regex.pattern());
}
/** @modelguid {E253A5F7-ADD8-43C2-86A1-1645ADD2DEF4} */
Pattern regex;
/** @modelguid {AAA4D667-9DB7-4C75-A058-9533A11B35FB} */
boolean mbMatchDirs = false;
/**
* @param str String - wildcard expression
* @modelguid {6A11D3E8-15DC-4175-AEBD-AA86B08F2331}
*/
public WildcardFilter(final String str) {
this(str, false, false);
}
/**
* This method turns a wildcard string into a regex string. Only ? and * are handled.<BR>
* TODO expand to handle globbing
*
* @param str
* @return regular expression string created from a wildcard string.
*/
public static String createRegEx(final String str) {
final StringBuffer regex = new StringBuffer();
regex.append("^");
for (int i = 0; i < str.length(); i++) {
switch (str.charAt(i)) {
case '*':
regex.append(".*");
break;
case '?':
regex.append(".");
break;
case '.':
regex.append("\\.");
break;
default:
regex.append(str.charAt(i));
break;
}
}
regex.append("$");
return regex.toString();
}
/**
* @param pRegex Pattern - use a regex directly.
*/
public WildcardFilter(final Pattern pRegex) {
regex = pRegex;
}
/**
* @param str String - wildcard expression
* @param pbMatchDirs boolean - match directory names to the wildcard expression
* @modelguid {E9967F71-464E-4F3E-BC9F-F00BB84739AF}
*/
public WildcardFilter(final String str, final boolean pbMatchDirs) {
this(str, pbMatchDirs, false);
}
/**
* @param str String - wildcard expression
* @param pbMatchDirs boolean - match directory names to the wildcard expression
* @param pbCaseSensitive boolean - case sensitive matching
* @modelguid {9DFFAFC9-CE31-4D02-989E-DF5641A38E18}
*/
public WildcardFilter(final String str, final boolean pbMatchDirs, final boolean pbCaseSensitive) {
mbMatchDirs = pbMatchDirs;
if (pbCaseSensitive) {
regex = Pattern.compile(createRegEx(str), Pattern.CASE_INSENSITIVE);
} else {
regex = Pattern.compile(createRegEx(str));
}
}
/**
* @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
* @modelguid {6417C474-71E9-411E-89A4-4C6BBB8F9296}
*/
public boolean accept(final File dir, final String name) {
if (!mbMatchDirs && (new File(dir.getPath() + File.separatorChar + name)).isDirectory()) {
return true;
}
return regex.matcher(name).matches();
}
/**
* @param pMatchDirs boolean
*/
public void setMatchDirs(final boolean pMatchDirs) {
mbMatchDirs = pMatchDirs;
}
/**
* @param pCaseSens boolean
*/
public void setCaseSensitive(final boolean pCaseSens) {
if (pCaseSens) {
regex = Pattern.compile(regex.pattern());
} else {
regex = Pattern.compile(regex.pattern(), Pattern.CASE_INSENSITIVE);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -