org.apache.sshd.common.util.io.PathScanningMatcher Maven / Gradle / Ivy
Go to download
This artifact provides a single jar that contains all classes required to use remote EJB and JMS, including
all dependencies. It is intended for use by those not using maven, maven users should just import the EJB and
JMS BOM's instead (shaded JAR's cause lots of problems with maven, as it is very easy to inadvertently end up
with different versions on classes on the class path).
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.sshd.common.util.io;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.OsUtils;
import org.apache.sshd.common.util.SelectorUtils;
import org.apache.sshd.common.util.ValidateUtils;
/**
* @author Apache MINA SSHD Project
*/
public abstract class PathScanningMatcher {
/**
* Whether or not the file system should be treated as a case sensitive one.
*/
protected boolean caseSensitive = OsUtils.isUNIX();
/**
* The file separator to use to parse paths - default=local O/S separator
*/
protected String separator = File.separator;
/**
* The patterns for the files to be included.
*/
protected List includePatterns;
protected PathScanningMatcher() {
super();
}
/**
*
* Sets the list of include patterns to use. All '/' and '\' characters are replaced by
* File.separatorChar
, so the separator used need not match File.separatorChar
.
*
*
*
* When a pattern ends with a '/' or '\', "**" is appended.
*
*
* @param includes A list of include patterns. May be {@code null}, indicating that all files should be included. If
* a non-{@code null} list is given, all elements must be non-{@code null}.
*/
public void setIncludes(String... includes) {
setIncludes(GenericUtils.isEmpty(includes) ? Collections.emptyList() : Arrays.asList(includes));
}
/**
* @return Un-modifiable list of the inclusion patterns
*/
public List getIncludes() {
return includePatterns;
}
public void setIncludes(Collection includes) {
this.includePatterns = GenericUtils.isEmpty(includes)
? Collections.emptyList()
: Collections.unmodifiableList(
includes.stream()
.map(PathScanningMatcher::normalizePattern)
.collect(Collectors.toCollection(() -> new ArrayList<>(includes.size()))));
}
/**
* @return Whether or not the file system should be treated as a case sensitive one.
*/
public boolean isCaseSensitive() {
return caseSensitive;
}
public void setCaseSensitive(boolean caseSensitive) {
this.caseSensitive = caseSensitive;
}
/**
* @return The file separator to use to parse paths - default=local O/S separator
*/
public String getSeparator() {
return separator;
}
public void setSeparator(String separator) {
this.separator = ValidateUtils.checkNotNullAndNotEmpty(separator, "No separator provided");
}
/**
* Tests whether or not a name matches against at least one include pattern.
*
* @param name The name to match. Must not be {@code null}.
* @return true
when the name matches against at least one include pattern, or false
* otherwise.
*/
protected boolean isIncluded(String name) {
Collection includes = getIncludes();
if (GenericUtils.isEmpty(includes)) {
return false;
}
boolean cs = isCaseSensitive();
String sep = getSeparator();
for (String include : includes) {
if (SelectorUtils.matchPath(include, name, sep, cs)) {
return true;
}
}
return false;
}
/**
* Tests whether or not a name matches the start of at least one include pattern.
*
* @param name The name to match. Must not be {@code null}.
* @return true
when the name matches against the start of at least one include pattern, or
* false
otherwise.
*/
protected boolean couldHoldIncluded(String name) {
Collection includes = getIncludes();
if (GenericUtils.isEmpty(includes)) {
return false;
}
boolean cs = isCaseSensitive();
String sep = getSeparator();
for (String include : includes) {
if (SelectorUtils.matchPatternStart(include, name, sep, cs)) {
return true;
}
}
return false;
}
/**
* Normalizes the pattern, e.g. converts forward and backward slashes to the platform-specific file separator.
*
* @param pattern The pattern to normalize, must not be {@code null}.
* @return The normalized pattern, never {@code null}.
*/
public static String normalizePattern(String pattern) {
pattern = pattern.trim();
if (pattern.startsWith(SelectorUtils.REGEX_HANDLER_PREFIX)) {
if (File.separatorChar == '\\') {
pattern = GenericUtils.replace(pattern, "/", "\\\\", -1);
} else {
pattern = GenericUtils.replace(pattern, "\\\\", "/", -1);
}
} else {
pattern = pattern.replace(File.separatorChar == '/' ? '\\' : '/', File.separatorChar);
if (pattern.endsWith(File.separator)) {
pattern += "**";
}
}
return pattern;
}
}