public class PathPattern extends java.lang.Object implements java.lang.Comparable<PathPattern>
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"/pages/t?st.html
— matches /pages/test.html
but also
/pages/tast.html
but not /pages/toast.html
/resources/*.png
— matches all .png
files in the
resources
directory/resources/**
— matches all files
underneath the /resources/
path, including /resources/image.png
and /resources/css/spring.css
/resources/{*path}
— matches all files
underneath the /resources/
path and captures their relative path in
a variable named "path"; /resources/image.png
will match with
"spring" -> "/image.png", and /resources/css/spring.css
will match
with "spring" -> "/css/spring.css"/resources/{filename:\\w+}.dat
will match /resources/spring.dat
and assign the value "spring"
to the filename
variablePathContainer
Modifier and Type | Class and Description |
---|---|
(package private) class |
PathPattern.MatchingContext
Encapsulates context when attempting a match.
|
static class |
PathPattern.PathMatchInfo
Holder for URI variables and path parameters (matrix variables) extracted
based on the pattern for a given matched path.
|
static class |
PathPattern.PathRemainingMatchInfo
Holder for the result of a match on the start of a pattern.
|
Modifier and Type | Field and Description |
---|---|
private int |
capturedVariableCount
How many variables are captured in this pattern
|
private boolean |
caseSensitive
Will this match candidates in a case sensitive way? (case sensitivity at parse time)
|
private boolean |
catchAll
Does the pattern end with {*...}
|
private static PathContainer |
EMPTY_PATH |
private boolean |
endsWithSeparatorWildcard
Does the pattern end with '<separator>*'
|
private PathElement |
head
First path element in the parsed chain of path elements for this pattern
|
private boolean |
matchOptionalTrailingSeparator
If this pattern has no trailing slash, allow candidates to include one and still match successfully
|
private int |
normalizedLength
The normalized length is trying to measure the 'active' part of the pattern.
|
private PathPatternParser |
parser
The parser used to construct this pattern
|
private java.lang.String |
patternString
The text of the parsed pattern
|
private int |
score
Score is used to quickly compare patterns.
|
private char |
separator
The separator used when parsing the pattern
|
static java.util.Comparator<PathPattern> |
SPECIFICITY_COMPARATOR
Comparator that sorts patterns by specificity as follows:
Null instances are last.
|
Constructor and Description |
---|
PathPattern(java.lang.String patternText,
PathPatternParser parser,
PathElement head) |
Modifier and Type | Method and Description |
---|---|
PathPattern |
combine(PathPattern pattern2string)
Combine this pattern with another.
|
int |
compareTo(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.
|
(package private) java.lang.String |
computePatternString() |
private java.lang.String |
concat(java.lang.String path1,
java.lang.String path2)
Join two paths together including a separator if necessary.
|
boolean |
equals(java.lang.Object other) |
PathContainer |
extractPathWithinPattern(PathContainer path)
Determine the pattern-mapped part for the given path.
|
private java.lang.String |
extractPathWithinPattern(java.lang.String path) |
(package private) int |
getCapturedVariableCount() |
(package private) PathElement |
getHeadSection() |
(package private) int |
getNormalizedLength()
The normalized length is trying to measure the 'active' part of the pattern.
|
java.lang.String |
getPatternString()
Return the original String that was parsed to create this PathPattern.
|
(package private) int |
getScore() |
(package private) char |
getSeparator() |
int |
hashCode() |
private boolean |
hasLength(PathContainer container) |
(package private) boolean |
isCatchAll() |
PathPattern.PathMatchInfo |
matchAndExtract(PathContainer pathContainer)
Match this pattern to the given URI path and return extracted URI template
variables as well as path parameters (matrix variables).
|
boolean |
matches(PathContainer pathContainer)
Whether this pattern matches the given path.
|
PathPattern.PathRemainingMatchInfo |
matchStartOfPath(PathContainer pathContainer)
Match the beginning of the given path and return the remaining portion
not covered by this pattern.
|
private static int |
scoreByNormalizedLength(PathPattern pattern) |
(package private) java.lang.String |
toChainString() |
java.lang.String |
toString() |
private static final PathContainer EMPTY_PATH
private final java.lang.String patternString
private final PathPatternParser parser
private final char separator
private final boolean matchOptionalTrailingSeparator
private final boolean caseSensitive
@Nullable private final PathElement head
private int capturedVariableCount
private int normalizedLength
private boolean endsWithSeparatorWildcard
private int score
private boolean catchAll
public static final java.util.Comparator<PathPattern> SPECIFICITY_COMPARATOR
PathPattern(java.lang.String patternText, PathPatternParser parser, @Nullable PathElement head)
public java.lang.String getPatternString()
public boolean matches(PathContainer pathContainer)
pathContainer
- the candidate path to attempt to match againsttrue
if the path matches this pattern@Nullable public PathPattern.PathMatchInfo matchAndExtract(PathContainer pathContainer)
pathContainer
- the candidate path to attempt to match againstnull
for no match@Nullable public PathPattern.PathRemainingMatchInfo matchStartOfPath(PathContainer pathContainer)
pathContainer
- the candidate path to attempt to match againstnull
for no matchpublic PathContainer extractPathWithinPattern(PathContainer 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
'Note: Assumes that matches(org.springframework.http.server.PathContainer)
returns true
for
the same path but does not enforce this.
path
- a path that matches this patternprivate java.lang.String extractPathWithinPattern(java.lang.String path)
public int compareTo(@Nullable PathPattern otherPattern)
compareTo
in interface java.lang.Comparable<PathPattern>
public PathPattern combine(PathPattern pattern2string)
public boolean equals(java.lang.Object other)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
int getScore()
boolean isCatchAll()
int getNormalizedLength()
char getSeparator()
int getCapturedVariableCount()
java.lang.String toChainString()
java.lang.String computePatternString()
@Nullable PathElement getHeadSection()
private java.lang.String concat(java.lang.String path1, java.lang.String path2)
path1
- first pathpath2
- second pathprivate boolean hasLength(@Nullable PathContainer container)
container
- a path containerprivate static int scoreByNormalizedLength(PathPattern pattern)