下面列出了org.apache.commons.io.filefilter.AndFileFilter#org.apache.commons.io.IOCase 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* The comparison between path is always case-sensitive in this general file system context.
*/
public PathAlterationObserver(final FileStatusEntry rootEntry, final PathFilter pathFilter)
throws IOException {
if (rootEntry == null) {
throw new IllegalArgumentException("Root entry is missing");
}
if (rootEntry.getPath() == null) {
throw new IllegalArgumentException("Root directory is missing");
}
this.rootEntry = rootEntry;
this.pathFilter = pathFilter;
this.fs = rootEntry.getPath().getFileSystem(new Configuration());
// By default, the comparsion is case sensitive.
this.comparator = new Comparator<Path>() {
@Override
public int compare(Path o1, Path o2) {
return IOCase.SENSITIVE.checkCompareTo(o1.toUri().toString(), o2.toUri().toString());
}
};
}
/**
* Construct an observer for the specified directory, file filter and
* file comparator.
*
* @param rootEntry the root directory to observe
* @param fileFilter The file filter or null if none
* @param caseSensitivity what case sensitivity to use comparing file names, null means system sensitive
*/
protected FileAlterationObserver(FileEntry rootEntry, FileFilter fileFilter, IOCase caseSensitivity) {
if (rootEntry == null) {
throw new IllegalArgumentException("Root entry is missing");
}
if (rootEntry.getFile() == null) {
throw new IllegalArgumentException("Root directory is missing");
}
this.rootEntry = rootEntry;
this.fileFilter = fileFilter;
if (caseSensitivity == null || caseSensitivity.equals(IOCase.SYSTEM)) {
this.comparator = NameFileComparator.NAME_SYSTEM_COMPARATOR;
} else if (caseSensitivity.equals(IOCase.INSENSITIVE)) {
this.comparator = NameFileComparator.NAME_INSENSITIVE_COMPARATOR;
} else {
this.comparator = NameFileComparator.NAME_COMPARATOR;
}
}
/**
* This function figures out the right lineage file path+name to process sorted by the last
* time they are modified. (old -> new)
* @return get the lineage files from given directory with given prefix.
*/
public File[] getCurrentFiles() {
try {
LOG.info("Scanning: " + directoryName);
File folder = new File(directoryName);
File[] listOfFiles = folder.listFiles((FileFilter) new PrefixFileFilter(prefix, IOCase.SENSITIVE));
if ((listOfFiles == null) || (listOfFiles.length == 0)) {
LOG.info("Found no lineage files.");
return new File[0];
}
if(listOfFiles.length > 1) {
Arrays.sort(listOfFiles, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
}
LOG.info("Found {} lineage files" + listOfFiles.length);
return listOfFiles;
} catch(Exception e) {
LOG.error("Import lineage file failed.", e);
}
return new File[0];
}
/**
* Construct an observer for the specified directory, file filter and
* file comparator.
*
* @param rootEntry the root directory to observe
* @param fileFilter The file filter or null if none
* @param caseSensitivity what case sensitivity to use comparing file names, null means system sensitive
*/
protected FileAlterationObserver(final FileEntry rootEntry, final FileFilter fileFilter,
final IOCase caseSensitivity) {
if (rootEntry == null) {
throw new IllegalArgumentException("Root entry is missing");
}
if (rootEntry.getFile() == null) {
throw new IllegalArgumentException("Root directory is missing");
}
this.rootEntry = rootEntry;
this.fileFilter = fileFilter;
if (caseSensitivity == null || caseSensitivity.equals(IOCase.SYSTEM)) {
this.comparator = NameFileComparator.NAME_SYSTEM_COMPARATOR;
} else if (caseSensitivity.equals(IOCase.INSENSITIVE)) {
this.comparator = NameFileComparator.NAME_INSENSITIVE_COMPARATOR;
} else {
this.comparator = NameFileComparator.NAME_COMPARATOR;
}
}
private List<String> collectFiles(Collection<File> files) {
List<String> fileNames = new ArrayList<>();
ImmutableSet<String> extensions = ImmutableSet.copyOf(FILE_EXTENSIONS);
for (File file : files) {
if (file.isDirectory()) {
SuffixFileFilter suffixFileFilter = new SuffixFileFilter(toSuffixes(".", FILE_EXTENSIONS), IOCase.INSENSITIVE);
Collection<File> nestedFiles = FileUtils.listFiles(file, suffixFileFilter, TrueFileFilter.INSTANCE);
nestedFiles.stream().map(File::getPath).forEach(fileNames::add);
} else {
boolean allowedFileExtension = extensions.contains(FilenameUtils.getExtension(file.getName()).toLowerCase());
if (allowedFileExtension) {
fileNames.add(file.getPath());
}
}
}
return fileNames;
}
/**
* 扫描由注解生成器生成到包 {@link Const#GEN_PKG_SERVICE} 里的初始化类
*/
private void scanDir(File dir, Set<String> initClasses) throws IOException {
File packageDir = new File(dir, INIT_SERVICE_DIR);
if (packageDir.exists() && packageDir.isDirectory()) {
Collection<File> files = FileUtils.listFiles(packageDir,
new SuffixFileFilter(SdkConstants.DOT_CLASS, IOCase.INSENSITIVE), TrueFileFilter.INSTANCE);
for (File f : files) {
String className = trimName(f.getAbsolutePath(), dir.getAbsolutePath().length() + 1)
.replace(File.separatorChar, '.');
initClasses.add(className);
WMRouterLogger.info(" find ServiceInitClass: %s", className);
}
}
}
boolean emptyOrContainsMatch(List<String> list, String value) {
if (list == null || list.isEmpty()) {
return true;
}
for (String item : list) {
if (StringUtils.equalsIgnoreCase(item, value) || FilenameUtils.wildcardMatch(value, item, IOCase.INSENSITIVE)) {
return true;
}
}
return false;
}
/**
* Construct a new name file filter specifying case-sensitivity.
*
* @param name the name to allow, must not be null
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the name is null
*/
public NameFileFilter(String name, IOCase caseSensitivity) {
if (name == null) {
throw new IllegalArgumentException("The wildcard must not be null");
}
this.names = new String[] {name};
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
}
private static boolean filePathEquals(String path1, String path2) {
// Mac is case-insensitive, but IOCase.SYSTEM.isCaseSenstive returns true,
// so don't use this value for Mac.
// (TODO but Mac can become case-sensitive if an user changes system setting..)
if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX) {
return StringUtils.equalsIgnoreCase(path1, path2);
}
if (IOCase.SYSTEM.isCaseSensitive()) {
return StringUtils.equals(path1, path2);
} else {
return StringUtils.equalsIgnoreCase(path1, path2);
}
}
@Test
public void whenSortDirWithPathFileComparator_thenFirstFileaaatxt() throws IOException {
PathFileComparator pathFileComparator = new PathFileComparator(IOCase.INSENSITIVE);
String path = FilenameUtils.getFullPath(getClass().getClassLoader().getResource("fileTest.txt").getPath());
File dir = new File(path);
File[] files = dir.listFiles();
pathFileComparator.sort(files);
Assert.assertEquals("aaa.txt", files[0].getName());
}
protected boolean matchesResourceToOperateWithin(String resource) {
if (equalsIgnoreCase("*", this.resourceToOperateWithin)) {
return true;
}
return FilenameUtils.wildcardMatch(resource, this.resourceToOperateWithin, IOCase.INSENSITIVE);
}
/**
* Construct a new regular expression filter with the specified flags case sensitivity.
*
* @param pattern regular string expression to match
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the pattern is null
*/
public RegexFileFilter(final String pattern, final IOCase caseSensitivity) {
if (pattern == null) {
throw new IllegalArgumentException("Pattern is missing");
}
int flags = 0;
if (caseSensitivity != null && !caseSensitivity.isCaseSensitive()) {
flags = Pattern.CASE_INSENSITIVE;
}
this.pattern = Pattern.compile(pattern, flags);
}
private List<File> getAddonJarFiles() {
File addonsPath = new File(systemEnvironment.get(SystemEnvironment.ADDONS_PATH));
if (!addonsPath.exists() || !addonsPath.canRead()) {
return new ArrayList<>();
}
return new ArrayList<>(FileUtils.listFiles(addonsPath, new SuffixFileFilter("jar", IOCase.INSENSITIVE), FalseFileFilter.INSTANCE));
}
/**
* Construct a new name file filter specifying case-sensitivity.
*
* @param name the name to allow, must not be null
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the name is null
*/
public NameFileFilter(final String name, final IOCase caseSensitivity) {
if (name == null) {
throw new IllegalArgumentException("The wildcard must not be null");
}
this.names = new String[] {name};
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
}
/**
* Constructs a new name file filter for an array of names specifying case-sensitivity.
*
* @param names the names to allow, must not be null
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the names array is null
*/
public NameFileFilter(final String[] names, final IOCase caseSensitivity) {
if (names == null) {
throw new IllegalArgumentException("The array of names must not be null");
}
this.names = new String[names.length];
System.arraycopy(names, 0, this.names, 0, names.length);
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
}
public static File getWindupDistributionSubdir(File tempFolder) throws WindupException
{
File[] matchingDirs = tempFolder.listFiles((FilenameFilter) FileFilterUtils.prefixFileFilter("windup-distribution-", IOCase.INSENSITIVE));
if (matchingDirs.length == 0)
return null;
return matchingDirs[0];
}
/**
* Construct a new wildcard filter for a single wildcard specifying case-sensitivity.
*
* @param wildcard the wildcard to match, not null
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the pattern is null
*/
public WildcardFileFilter(final String wildcard, final IOCase caseSensitivity) {
if (wildcard == null) {
throw new IllegalArgumentException("The wildcard must not be null");
}
this.wildcards = new String[] { wildcard };
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
}
/**
* Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.
* <p>
*
* @param wildcards the array of wildcards to match, not null
* @param caseSensitivity how to handle case sensitivity, null means case-sensitive
* @throws IllegalArgumentException if the pattern array is null
*/
public WildcardFileFilter(final String[] wildcards, final IOCase caseSensitivity) {
if (wildcards == null) {
throw new IllegalArgumentException("The wildcard array must not be null");
}
this.wildcards = new String[wildcards.length];
System.arraycopy(wildcards, 0, this.wildcards, 0, wildcards.length);
this.caseSensitivity = caseSensitivity == null ? IOCase.SENSITIVE : caseSensitivity;
}
/**
* Creates a filter that returns everything in the "mods" folder except
* the specified cubes which are distributed with each new release and
* any existing themes which are now found in the "themes" folder.
*/
private FileFilter getModsFileFilter() {
final String[] excludedCubes = new String[]{
"legacy_cube.txt", "modern_cube.txt", "standard_cube.txt", "extended_cube.txt", "ubeefx_cube.txt"
};
final IOFileFilter cubesFilter = new NameFileFilter(excludedCubes, IOCase.INSENSITIVE);
final IOFileFilter excludeCubes = FileFilterUtils.notFileFilter(cubesFilter);
final IOFileFilter excludeThemes = FileFilterUtils.notFileFilter(new WildcardFileFilter("*_theme*"));
return FileFilterUtils.and(excludeCubes, excludeThemes);
}
protected boolean matchesResource(String resource) {
if (equalsIgnoreCase("*", this.resource)) {
return true;
}
return FilenameUtils.wildcardMatch(resource, this.resource, IOCase.INSENSITIVE);
}
/**
* Create a ModuleOverrides object which will process the modules according to the given options.
*
* @param includeFields - List of field names to include
* @param includeModulesList - list of module filename rules to include
* @param excludeFields - list of field names to exclude
* @param excludeModulesList - list of module filename rules to exclude
*/
public ModuleOverrides(List<String> includeFields, List<String> includeModulesList,
List<String> excludeFields, List<String> excludeModulesList) {
this.includeFields = includeFields;
this.excludeFields = excludeFields;
if (includeModulesList != null) {
this.includeModules = new WildcardFileFilter(includeModulesList, IOCase.INSENSITIVE);
}
if (excludeModulesList != null) {
this.excludeModules = new WildcardFileFilter(excludeModulesList, IOCase.INSENSITIVE);
}
}
private Predicate<String> getModulePredicate() {
if (options.enabledModules == null) {
return path -> true;
}
FilenameFilter filenameFilter = new WildcardFileFilter(options.enabledModules,
IOCase.INSENSITIVE);
return path -> filenameFilter.accept(null, path);
}
public static boolean existFile(File srcFile) {
String absPath = srcFile.getAbsolutePath();
if (srcFile.exists()) {
String canonicalPath;
try {
canonicalPath = srcFile.getCanonicalPath();
} catch (IOException ex) {
return false;
}
return FilenameUtils.equals(canonicalPath, absPath, true, IOCase.SENSITIVE);
}
return false;
}
@Test
public void whenGetFilewith_ANDFileFilter_thenFindsampletxt() throws IOException {
String path = getClass().getClassLoader().getResource("fileTest.txt").getPath();
File dir = FileUtils.getFile(FilenameUtils.getFullPath(path));
Assert.assertEquals("sample.txt", dir.list(new AndFileFilter(new WildcardFileFilter("*ple*", IOCase.INSENSITIVE), new SuffixFileFilter("txt")))[0]);
}
@Override
boolean isMatch(String resourceValue, Map<String, Object> evalContext) {
return FilenameUtils.wildcardMatch(resourceValue, getExpandedValue(evalContext), IOCase.SENSITIVE);
}
@Override
boolean isMatch(String resourceValue, Map<String, Object> evalContext) {
return RangerPathResourceMatcher.isRecursiveWildCardMatch(resourceValue, getExpandedValue(evalContext), levelSeparatorChar, IOCase.INSENSITIVE);
}
/**
* Construct a case sensitive file extension comparator instance.
*/
public ExtensionFileComparator() {
this.caseSensitivity = IOCase.SENSITIVE;
}
/**
* Checks a filename to see if it matches the specified wildcard matcher
* allowing control over case-sensitivity.
* <p>
* The wildcard matcher uses the characters '?' and '*' to represent a
* single or multiple (zero or more) wildcard characters.
* N.B. the sequence "*?" does not work properly at present in match strings.
*
* @param filename the filename to match on
* @param wildcardMatcher the wildcard string to match against
* @param caseSensitivity what case sensitivity rule to use, null means case-sensitive
* @return true if the filename matches the wilcard string
* @since Commons IO 1.3
*/
public static boolean wildcardMatch(String filename, String wildcardMatcher, IOCase caseSensitivity) {
if (filename == null && wildcardMatcher == null) {
return true;
}
if (filename == null || wildcardMatcher == null) {
return false;
}
if (caseSensitivity == null) {
caseSensitivity = IOCase.SENSITIVE;
}
String[] wcs = splitOnTokens(wildcardMatcher);
boolean anyChars = false;
int textIdx = 0;
int wcsIdx = 0;
Stack<int[]> backtrack = new Stack<int[]>();
// loop around a backtrack stack, to handle complex * matching
do {
if (backtrack.size() > 0) {
int[] array = backtrack.pop();
wcsIdx = array[0];
textIdx = array[1];
anyChars = true;
}
// loop whilst tokens and text left to process
while (wcsIdx < wcs.length) {
if (wcs[wcsIdx].equals("?")) {
// ? so move to next text char
textIdx++;
if (textIdx > filename.length()) {
break;
}
anyChars = false;
} else if (wcs[wcsIdx].equals("*")) {
// set any chars status
anyChars = true;
if (wcsIdx == wcs.length - 1) {
textIdx = filename.length();
}
} else {
// matching text token
if (anyChars) {
// any chars then try to locate text token
textIdx = caseSensitivity.checkIndexOf(filename, textIdx, wcs[wcsIdx]);
if (textIdx == -1) {
// token not found
break;
}
int repeat = caseSensitivity.checkIndexOf(filename, textIdx + 1, wcs[wcsIdx]);
if (repeat >= 0) {
backtrack.push(new int[] {wcsIdx, repeat});
}
} else {
// matching from current position
if (!caseSensitivity.checkRegionMatches(filename, textIdx, wcs[wcsIdx])) {
// couldnt match token
break;
}
}
// matched text token, move text index to end of matched token
textIdx += wcs[wcsIdx].length();
anyChars = false;
}
wcsIdx++;
}
// full match
if (wcsIdx == wcs.length && textIdx == filename.length()) {
return true;
}
} while (backtrack.size() > 0);
return false;
}
/**
* Construct a case sensitive file name comparator instance.
*/
public NameFileComparator() {
this.caseSensitivity = IOCase.SENSITIVE;
}
@Override
boolean isMatch(String resourceValue, Map<String, Object> evalContext) {
return RangerPathResourceMatcher.isRecursiveWildCardMatch(resourceValue, getExpandedValue(evalContext), levelSeparatorChar, IOCase.SENSITIVE);
}