public class AntPathMatcher extends Object
Part of this mapping code has been kindly borrowed from Apache Ant.
The mapping matches URLs using the following rules:
? matches one character* matches zero or more characters** matches zero or more directories in a path{spring:[a-z]+} matches the regexp [a-z]+ as a path variable named "spring"com/t?st.jsp — matches com/test.jsp but also
com/tast.jsp or com/txst.jspcom/*.jsp — matches all .jsp files in the
com directorycom/**/test.jsp — matches all test.jsp
files underneath the com pathorg/springframework/**/*.jsp — matches all
.jsp files underneath the org/springframework pathorg/**/servlet/bla.jsp — matches
org/springframework/servlet/bla.jsp but also
org/springframework/testing/servlet/bla.jsp and org/servlet/bla.jspcom/{filename:\\w+}.jsp will match com/test.jsp and assign the value test
to the filename variableNote: a pattern and a path must both be absolute or must both be relative in order for the two to match. Therefore it is recommended that users of this implementation to sanitize patterns in order to prefix them with "/" as it makes sense in the context in which they're used.
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
DEFAULT_PATH_SEPARATOR
Default path separator: "/"
|
| 构造器和说明 |
|---|
AntPathMatcher()
Create a new instance with the
DEFAULT_PATH_SEPARATOR. |
AntPathMatcher(String pathSeparator)
A convenient, alternative constructor to use with a custom path separator.
|
| 限定符和类型 | 方法和说明 |
|---|---|
String |
combine(String pattern1,
String pattern2)
Combine two patterns into a new 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. |
static boolean |
hasText(CharSequence str) |
boolean |
isPattern(String path) |
boolean |
match(String pattern,
String path) |
boolean |
match(String pattern,
String path,
String allToken) |
boolean |
matchStart(String pattern,
String path) |
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, 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.
|
static String[] |
tokenizeToStringArray(String str,
String delimiters,
boolean trimTokens,
boolean ignoreEmptyTokens) |
public AntPathMatcher()
DEFAULT_PATH_SEPARATOR.public AntPathMatcher(String pathSeparator)
pathSeparator - the path separator to use, must not be null.public void setPathSeparator(String pathSeparator)
pathSeparator - pathSeparatorpublic void setCaseSensitive(boolean caseSensitive)
true. Switch this to false for case-insensitive matching.caseSensitive - caseSensitivepublic void setTrimTokens(boolean trimTokens)
false.trimTokens - trimTokenspublic void setCachePatterns(boolean cachePatterns)
match(java.lang.String, 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.
cachePatterns - cachePatternsgetStringMatcher(String)public boolean isPattern(String path)
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, java.lang.String) returns true for 'pattern' and 'path', but
does not enforce this.
pattern - patternpath - pathpublic Map<String,String> extractUriTemplateVariables(String pattern, String path)
public String combine(String pattern1, String pattern2)
This implementation simply concatenates the two patterns, unless
the first pattern contains a file extension match (e.g., *.html).
In that case, the second pattern will be merged into the first. Otherwise,
an IllegalArgumentException will be thrown.
/*.html /hotels.html /hotels.html
pattern1 - the first patternpattern2 - the second patternIllegalArgumentException - if the two patterns cannot be combinedpublic Comparator<String> getPatternComparator(String path)
Comparator suitable for sorting patterns in order of
explicitness.
ThisComparator 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:
/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 comparisonpublic static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
public static boolean hasText(CharSequence str)
Copyright © 2021. All rights reserved.