public class AntPathMatcher extends Object
| 限定符和类型 | 类和说明 |
|---|---|
protected static class |
AntPathMatcher.AntPathStringMatcher
Tests whether or not a string matches against a pattern via a
Pattern. |
protected static class |
AntPathMatcher.AntPatternComparator
The default
Comparator implementation returned by
getPatternComparator(String). |
private static class |
AntPathMatcher.PathSeparatorPatternCache
A simple cache for patterns that depend on the configured path separator.
|
| 限定符和类型 | 字段和说明 |
|---|---|
private static int |
CACHE_TURNOFF_THRESHOLD |
private Boolean |
cachePatterns |
private boolean |
caseSensitive |
static String |
DEFAULT_PATH_SEPARATOR
Default path separator: "/".
|
private String |
pathSeparator |
private AntPathMatcher.PathSeparatorPatternCache |
pathSeparatorPatternCache |
(专用程序包) Map<String,AntPathMatcher.AntPathStringMatcher> |
stringMatcherCache |
private Map<String,String[]> |
tokenizedPatternCache |
private boolean |
trimTokens |
private static Pattern |
VARIABLE_PATTERN |
private static char[] |
WILDCARD_CHARS |
| 构造器和说明 |
|---|
AntPathMatcher()
Create a new instance with the
DEFAULT_PATH_SEPARATOR. |
AntPathMatcher(String pathSeparator)
A convenient, alternative constructor to use with a custom path separator.
|
| 限定符和类型 | 方法和说明 |
|---|---|
private void |
deactivatePatternCache() |
protected boolean |
doMatch(String pattern,
String path,
boolean fullMatch,
Map<String,String> uriTemplateVariables)
Actually match the given
path against the given pattern. |
String |
extractPathWithinPattern(String pattern,
String path)
Given a pattern and a full path, determine the pattern-mapped part.
|
Map<String,String> |
extractUriTemplateVariables(String pattern,
String path) |
Comparator<String> |
getPatternComparator(String path)
Given a full path, returns a
Comparator suitable for sorting patterns in order of
explicitness. |
protected AntPathMatcher.AntPathStringMatcher |
getStringMatcher(String pattern)
Build or retrieve an
AntPathMatcher.AntPathStringMatcher for the given pattern. |
boolean |
isPattern(String path) |
private boolean |
isPotentialMatch(String path,
String[] pattDirs) |
private boolean |
isWildcardChar(char c) |
boolean |
match(String pattern,
String path) |
boolean |
matchStart(String pattern,
String path) |
private boolean |
matchStrings(String pattern,
String str,
Map<String,String> uriTemplateVariables)
Test whether or not a string matches against a pattern.
|
void |
setCachePatterns(boolean cachePatterns)
Specify whether to cache parsed pattern metadata for patterns passed
into this matcher's
match(java.lang.String, java.lang.String) method. |
void |
setCaseSensitive(boolean caseSensitive)
Specify whether to perform pattern matching in a case-sensitive fashion.
|
void |
setPathSeparator(String pathSeparator)
Set the path separator to use for pattern parsing.
|
void |
setTrimTokens(boolean trimTokens)
Specify whether to trim tokenized paths and patterns.
|
private int |
skipSegment(String path,
int pos,
String prefix) |
private int |
skipSeparator(String path,
int pos,
String separator) |
protected String[] |
tokenizePath(String path)
Tokenize the given path into parts, based on this matcher's settings.
|
protected String[] |
tokenizePattern(String pattern)
Tokenize the given path pattern into parts, based on this matcher's settings.
|
public static final String DEFAULT_PATH_SEPARATOR
private static final int CACHE_TURNOFF_THRESHOLD
private static final Pattern VARIABLE_PATTERN
private static final char[] WILDCARD_CHARS
private String pathSeparator
private AntPathMatcher.PathSeparatorPatternCache pathSeparatorPatternCache
private boolean caseSensitive
private boolean trimTokens
private volatile Boolean cachePatterns
private final Map<String,String[]> tokenizedPatternCache
final Map<String,AntPathMatcher.AntPathStringMatcher> stringMatcherCache
public AntPathMatcher()
DEFAULT_PATH_SEPARATOR.public AntPathMatcher(String pathSeparator)
pathSeparator - the path separator to use, must not be null.public void setPathSeparator(String pathSeparator)
Default is "/", as in Ant.
public void setCaseSensitive(boolean caseSensitive)
Default is true. Switch this to false for case-insensitive matching. *
public void setTrimTokens(boolean trimTokens)
Default is false.
public void setCachePatterns(boolean cachePatterns)
match(java.lang.String, java.lang.String) method. A value of true
activates an unlimited pattern cache; a value of false turns
the pattern cache off completely.
Default is for the cache to be on, but with the variant to automatically turn it off when encountering too many patterns to cache at runtime (the threshold is 65536), assuming that arbitrary permutations of patterns are coming in, with little chance for encountering a recurring pattern.
getStringMatcher(String)private void deactivatePatternCache()
public boolean matchStart(String pattern, String path)
protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String,String> uriTemplateVariables)
path against the given pattern.pattern - the pattern to match againstpath - the path to testfullMatch - whether a full pattern match is required (else a pattern match
as far as the given base path goes is sufficient)true if the supplied path matched, false if it didn'tprivate boolean isPotentialMatch(String path, String[] pattDirs)
private int skipSegment(String path, int pos, String prefix)
private int skipSeparator(String path, int pos, String separator)
private boolean isWildcardChar(char c)
protected String[] tokenizePattern(String pattern)
Performs caching based on setCachePatterns(boolean), delegating to
tokenizePath(String) for the actual tokenization algorithm.
pattern - the pattern to tokenizeprotected String[] tokenizePath(String path)
path - the path to tokenizeprivate boolean matchStrings(String pattern, String str, Map<String,String> uriTemplateVariables)
pattern - the pattern to match against (never null)str - the String which must be matched against the pattern (never null)true if the string matches against the pattern, or false otherwiseprotected AntPathMatcher.AntPathStringMatcher getStringMatcher(String pattern)
AntPathMatcher.AntPathStringMatcher for the given pattern.
The default implementation checks this AntPathMatcher's internal cache
(see setCachePatterns(boolean)), creating a new AntPathStringMatcher instance
if no cached copy is found.
When encountering too many patterns to cache at runtime (the threshold is 65536), it turns the default cache off, assuming that arbitrary permutations of patterns are coming in, with little chance for encountering a recurring pattern.
This method may be overridden to implement a custom cache strategy.
pattern - the pattern to match against (never null)null)setCachePatterns(boolean)public String extractPathWithinPattern(String pattern, String path)
For example:
/docs/cvs/commit.html' and '/docs/cvs/commit.html -> ''/docs/*' and '/docs/cvs/commit -> 'cvs/commit'/docs/cvs/*.html' and '/docs/cvs/commit.html -> 'commit.html'/docs/**' and '/docs/cvs/commit -> 'cvs/commit'/docs/**\/*.html' and '/docs/cvs/commit.html -> 'cvs/commit.html'/*.html' and '/docs/cvs/commit.html -> 'docs/cvs/commit.html'*.html' and '/docs/cvs/commit.html -> '/docs/cvs/commit.html'*' and '/docs/cvs/commit.html -> '/docs/cvs/commit.html'Assumes that match(java.lang.String, java.lang.String) returns true for 'pattern' and 'path', but
does not enforce this.
public Map<String,String> extractUriTemplateVariables(String pattern, String path)
public Comparator<String> getPatternComparator(String path)
Comparator suitable for sorting patterns in order of
explicitness.
This Comparator will sort
a list so that more specific patterns (without URI templates or wild cards) come before
generic patterns. So given a list with the following patterns, the returned comparator
will sort this list so that the order will be as indicated.
/hotels/new/hotels/{hotel}/hotels/*The full path given as parameter is used to test for exact matches. So when the given path
is /hotels/2, the pattern /hotels/2 will be sorted before /hotels/1.
path - the full path to use for comparisonCopyright © 2024. All rights reserved.