类 PathPattern
java.lang.Object
cn.taketoday.web.util.pattern.PathPattern
- 所有已实现的接口:
Comparable<PathPattern>
Representation of a parsed path pattern. Includes a chain of path elements
for fast matching and accumulates computed state for quick comparison of
patterns.
PathPattern matches URL paths using the following rules:
?matches one character*matches zero or more characters within a path segment**matches zero or more path segments until the end of the path{spring}matches a path segment and captures it as a variable named "spring"{spring:[a-z]+}matches the regexp[a-z]+as a path variable named "spring"{*spring}matches zero or more path segments until the end of the path and captures it as a variable named "spring"
Note: In contrast to
AntPathMatcher, ** is supported only
at the end of a pattern. For example /pages/{**} is valid but
/pages/{**}/details is not. The same applies also to the capturing
variant {*spring}. The aim is to eliminate ambiguity when
comparing patterns for specificity.
Examples
/pages/t?st.html— matches/pages/test.htmlas well as/pages/tXst.htmlbut not/pages/toast.html/resources/*.png— matches all.pngfiles in theresourcesdirectory/resources/**— matches all files underneath the/resources/path, including/resources/image.pngand/resources/css/spring.css/resources/{*path}— matches all files underneath the/resources/, as well as/resources, and captures their relative path in a variable named "path";/resources/image.pngwill match with "path" → "/image.png", and/resources/css/spring.csswill match with "path" → "/css/spring.css"/resources/{filename:\\w+}.datwill match/resources/spring.datand assign the value"spring"to thefilenamevariable
- 从以下版本开始:
- 4.0
- 作者:
- Andy Clement, Rossen Stoyanchev
- 另请参阅:
-
嵌套类概要
嵌套类修饰符和类型类说明(专用程序包) classEncapsulates context when attempting a match.static classHolder for the result of a match on the start of a pattern. -
字段概要
字段修饰符和类型字段说明private intHow many variables are captured in this pattern.private final booleanWill this match candidates in a case sensitive way?private booleanDoes the pattern end with {*...}.private static final PathContainerprivate booleanDoes the pattern end with '<separator>'.private final PathElementFirst path element in the parsed chain of path elements for this pattern.private final booleanIf this pattern has no trailing slash, allow candidates to include one and still match successfully.private intThe normalized length is trying to measure the 'active' part of the pattern.private final PathPatternParserThe parser used to construct this pattern.private final PathContainer.OptionsThe options to use to parse a pattern.private final StringThe text of the parsed pattern.private intScore is used to quickly compare patterns.static final Comparator<PathPattern>Comparator that sorts patterns by specificity as follows: Null instances are last. -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明combine(PathPattern pattern2string) Combine this pattern with another.intcompareTo(PathPattern otherPattern) Compare this pattern with a supplied pattern: return -1,0,+1 if this pattern is more specific, the same or less specific than the supplied pattern.(专用程序包) StringReturn the string form of the pattern built from walking the path element chain.private StringJoin two paths together including a separator if necessary.booleanDetermine the pattern-mapped part for the given path.(专用程序包) int(专用程序包) PathElement(专用程序包) intThe normalized length is trying to measure the 'active' part of the pattern.Return the original String that was parsed to create this PathPattern.(专用程序包) intgetScore()(专用程序包) charreturns variable namesinthashCode()private booleanhasLength(PathContainer container) Return if the container is not null and has more than zero elements.booleanWhether the pattern string contains pattern syntax that would require use ofmatches(PathContainer), or if it is a regular String that could be compared directly to others.(专用程序包) booleanmatchAndExtract(PathContainer pathContainer) Match this pattern to the given URI path and return extracted URI template variables as well as path parameters (matrix variables).booleanmatches(PathContainer pathContainer) Whether this pattern matches the given path.matchStartOfPath(PathContainer pathContainer) Match the beginning of the given path and return the remaining portion not covered by this pattern.private booleanpathContainerIsJustSeparator(PathContainer pathContainer) private static intscoreByNormalizedLength(PathPattern pattern) (专用程序包) StringtoString()
-
字段详细资料
-
EMPTY_PATH
-
SPECIFICITY_COMPARATOR
Comparator that sorts patterns by specificity as follows:- Null instances are last.
- Catch-all patterns are last.
- If both patterns are catch-all, consider the length (longer wins).
- Compare wildcard and captured variable count (lower wins).
- Consider length (longer wins)
-
patternString
The text of the parsed pattern. -
parser
The parser used to construct this pattern. -
pathOptions
The options to use to parse a pattern. -
matchOptionalTrailingSeparator
private final boolean matchOptionalTrailingSeparatorIf this pattern has no trailing slash, allow candidates to include one and still match successfully. -
caseSensitive
private final boolean caseSensitiveWill this match candidates in a case sensitive way? (case sensitivity at parse time). -
head
First path element in the parsed chain of path elements for this pattern. -
capturedVariableCount
private int capturedVariableCountHow many variables are captured in this pattern. -
normalizedLength
private int normalizedLengthThe normalized length is trying to measure the 'active' part of the pattern. It is computed by assuming all captured variables have a normalized length of 1. Effectively this means changing your variable name lengths isn't going to change the length of the active part of the pattern. Useful when comparing two patterns. -
endsWithSeparatorWildcard
private boolean endsWithSeparatorWildcardDoes the pattern end with '<separator>'. -
score
private int scoreScore is used to quickly compare patterns. Different pattern components are given different weights. A 'lower score' is more specific. Current weights:- Captured variables are worth 1
- Wildcard is worth 100
-
catchAll
private boolean catchAllDoes the pattern end with {*...}. -
variableNames
-
-
构造器详细资料
-
PathPattern
PathPattern(String patternText, PathPatternParser parser, @Nullable PathElement head)
-
-
方法详细资料
-
getPatternString
Return the original String that was parsed to create this PathPattern. -
hasPatternSyntax
public boolean hasPatternSyntax()Whether the pattern string contains pattern syntax that would require use ofmatches(PathContainer), or if it is a regular String that could be compared directly to others. -
matches
Whether this pattern matches the given path.- 参数:
pathContainer- the candidate path to attempt to match against- 返回:
trueif the path matches this pattern
-
matchAndExtract
Match this pattern to the given URI path and return extracted URI template variables as well as path parameters (matrix variables).- 参数:
pathContainer- the candidate path to attempt to match against- 返回:
- info object with the extracted variables, or
nullfor no match
-
matchStartOfPath
Match the beginning of the given path and return the remaining portion not covered by this pattern. This is useful for matching nested routes where the path is matched incrementally at each level.- 参数:
pathContainer- the candidate path to attempt to match against- 返回:
- info object with the match result or
nullfor no match
-
extractPathWithinPattern
Determine the pattern-mapped part for the given 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'
Notes:
- Assumes that
matches(cn.taketoday.http.server.PathContainer)returnstruefor the same path but does not enforce this. - Duplicate occurrences of separators within the returned result are removed
- Leading and trailing separators are removed from the returned result
- 参数:
path- a path that matches this pattern- 返回:
- the subset of the path that is matched by pattern or "" if none of it is matched by pattern elements
- '
-
getVariableNames
returns variable names -
compareTo
Compare this pattern with a supplied pattern: return -1,0,+1 if this pattern is more specific, the same or less specific than the supplied pattern. The aim is to sort more specific patterns first.- 指定者:
compareTo在接口中Comparable<PathPattern>
-
combine
Combine this pattern with another. -
equals
-
hashCode
public int hashCode() -
toString
-
getScore
int getScore() -
isCatchAll
boolean isCatchAll() -
getNormalizedLength
int getNormalizedLength()The normalized length is trying to measure the 'active' part of the pattern. It is computed by assuming all capture variables have a normalized length of 1. Effectively this means changing your variable name lengths isn't going to change the length of the active part of the pattern. Useful when comparing two patterns. -
getSeparator
char getSeparator() -
getCapturedVariableCount
int getCapturedVariableCount() -
toChainString
String toChainString() -
computePatternString
String computePatternString()Return the string form of the pattern built from walking the path element chain.- 返回:
- the string form of the pattern
-
getHeadSection
-
concat
Join two paths together including a separator if necessary. Extraneous separators are removed (if the first path ends with one and the second path starts with one).- 参数:
path1- first pathpath2- second path- 返回:
- joined path that may include separator if necessary
-
hasLength
Return if the container is not null and has more than zero elements.- 参数:
container- a path container- 返回:
truehas more than zero elements
-
scoreByNormalizedLength
-
pathContainerIsJustSeparator
-