PathMatcher implementation for Ant-style path patterns.
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.
| Modifier and Type | Class and Description |
|---|---|
| protected static class | AntPathMatcher.AntPathStringMatcher
Tests whether a string matches against a pattern via a |
| protected static class | AntPathMatcher.AntPatternComparator
The default |
| private static class | AntPathMatcher.PathSeparatorPatternCache
A simple cache for patterns that depend on the configured path separator. |
| Modifier and Type | Field and Description |
|---|---|
| private static final int | |
| private volatile Boolean | |
| private boolean | |
| public static final String | DEFAULT_PATH_SEPARATOR
Default path separator: "/". |
| private String | |
| private AntPathMatcher. | |
| pack-priv final Map | |
| private final Map | |
| private boolean | |
| private static final Pattern | |
| private static final char[] |
| Access | Constructor and Description |
|---|---|
| public | |
| public | AntPathMatcher(String
the path separator to use, must not be pathSeparator)null.A convenient, alternative constructor to use with a custom path separator. |
| Modifier and Type | Method and Description |
|---|---|
| public String | Returns: the combination of the two patternsthe first pattern pattern1, String the second pattern pattern2)Implements org. |
| private String | |
| private void | |
| protected boolean | Returns: true if the supplied path matched, false if it didn'tthe pattern to match against pattern, String the path to test path, boolean whether a full pattern match is required (else a pattern match
as far as the given base path goes is sufficient) fullMatch, Map<String, String> uriTemplateVariables)Actually match the given |
| public String | extractPathWithinPattern(String
the path pattern pattern, String the full path to introspect path)Implements org. |
| public Map | extractUriTemplateVariables(String
the path pattern, possibly containing URI templates pattern, String the full path to extract template variables from path)Implements org. |
| public Comparator | Returns: a comparator capable of sorting patterns in order of explicitnessthe full path to use for comparison path)Implements org. Comparator suitable for sorting patterns in order of
explicitness.
|
| protected AntPathMatcher. | Returns: a corresponding AntPathStringMatcher (nevernull)the pattern to match against (never pattern)null)Build or retrieve an |
| public boolean | isPattern(String
the path to check path)Implements org. path represent a pattern that can be matched
by an implementation of this interface?
|
| private boolean | |
| private boolean | |
| public boolean | match(String
the pattern to match against pattern, String the path to test path)Implements org. path against the given pattern,
according to this PathMatcher's matching strategy.
|
| public boolean | matchStart(String
the pattern to match against pattern, String the path to test path)Implements org. path against the corresponding part of the given
pattern, according to this PathMatcher's matching strategy.
|
| private boolean | Returns: true if the string matches against the pattern, or false otherwisethe pattern to match against (never pattern, String null)the String which must be matched against the pattern (never str, Map<String, String> uriTemplateVariables)null)Test whether a string matches against a pattern. |
| public void | setCachePatterns(boolean cachePatterns)
Specify whether to cache parsed pattern metadata for patterns passed
into this matcher's |
| public void | setCaseSensitive(boolean caseSensitive)
Specify whether to perform pattern matching in a case-sensitive fashion. |
| public void | |
| public void | |
| private int | |
| private int | |
| protected String[] | Returns: the tokenized path partsthe path to tokenize path)Tokenize the given path into parts, based on this matcher's settings. |
| protected String[] | Returns: the tokenized pattern partsthe pattern to tokenize pattern)Tokenize the given path pattern into parts, based on this matcher's settings. |
| CACHE_TURNOFF_THRESHOLD | back to summary |
|---|---|
| private static final int CACHE_TURNOFF_THRESHOLD | |
| cachePatterns | back to summary |
|---|---|
| private volatile Boolean cachePatterns
| |
| caseSensitive | back to summary |
|---|---|
| private boolean caseSensitive | |
| DEFAULT_PATH_SEPARATOR | back to summary |
|---|---|
| public static final String DEFAULT_PATH_SEPARATOR Default path separator: "/". | |
| pathSeparator | back to summary |
|---|---|
| private String pathSeparator | |
| pathSeparatorPatternCache | back to summary |
|---|---|
| private AntPathMatcher. | |
| stringMatcherCache | back to summary |
|---|---|
| pack-priv final Map<String, AntPathMatcher. | |
| tokenizedPatternCache | back to summary |
|---|---|
| private final Map<String, String[]> tokenizedPatternCache | |
| trimTokens | back to summary |
|---|---|
| private boolean trimTokens | |
| VARIABLE_PATTERN | back to summary |
|---|---|
| private static final Pattern VARIABLE_PATTERN | |
| WILDCARD_CHARS | back to summary |
|---|---|
| private static final char[] WILDCARD_CHARS | |
| AntPathMatcher | back to summary |
|---|---|
| public AntPathMatcher() Create a new instance with the | |
| AntPathMatcher | back to summary |
|---|---|
| public AntPathMatcher(String pathSeparator) A convenient, alternative constructor to use with a custom path separator.
| |
| combine | back to summary | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| public String combine(String pattern1, String pattern2) Implements org. Combine two patterns into a new pattern. This implementation simply concatenates the two patterns, unless
the first pattern contains a file extension match (e.g., Examples
| |||||||||||||||||||||||||||||||||||||||||||
| concat | back to summary |
|---|---|
| private String concat(String path1, String path2) | |
| deactivatePatternCache | back to summary |
|---|---|
| private void deactivatePatternCache() | |
| doMatch | back to summary |
|---|---|
| protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String, String> uriTemplateVariables) Actually match the given | |
| extractPathWithinPattern | back to summary |
|---|---|
| public String extractPathWithinPattern(String pattern, String path) Implements org. Given a pattern and a full path, determine the pattern-mapped part. For example:
Assumes that
| |
| extractUriTemplateVariables | back to summary |
|---|---|
| public Map Implements org. Doc from org. Given a pattern and a full path, extract the URI template variables. URI template variables are expressed through curly brackets ('{' and '}'). For example: For pattern "/hotels/{hotel}" and path "/hotels/1", this method will return a map containing "hotel" → "1". | |
| getPatternComparator | back to summary |
|---|---|
| public Comparator Implements org. Given a full path, returns a This
The full path given as parameter is used to test for exact matches. So when the given path
is
| |
| getStringMatcher | back to summary |
|---|---|
| protected AntPathMatcher. Build or retrieve an The default implementation checks this AntPathMatcher's internal cache
(see 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.
| |
| isPattern | back to summary |
|---|---|
| public boolean isPattern(String path) Implements org. Doc from org. Does the given If the return value is | |
| isPotentialMatch | back to summary |
|---|---|
| private boolean isPotentialMatch(String path, String[] pattDirs) | |
| isWildcardChar | back to summary |
|---|---|
| private boolean isWildcardChar(char c) | |
| match | back to summary |
|---|---|
| public boolean match(String pattern, String path) Implements org. Doc from org. Match the given | |
| matchStart | back to summary |
|---|---|
| public boolean matchStart(String pattern, String path) Implements org. Doc from org. Match the given Determines whether the pattern at least matches as far as the given base path goes, assuming that a full path may then match as well. | |
| matchStrings | back to summary |
|---|---|
| private boolean matchStrings(String pattern, String str, Map<String, String> uriTemplateVariables) Test whether a string matches against a pattern. | |
| setCachePatterns | back to summary |
|---|---|
| public void setCachePatterns(boolean cachePatterns) Specify whether to cache parsed pattern metadata for patterns passed
into this matcher's 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.
| |
| setCaseSensitive | back to summary |
|---|---|
| public void setCaseSensitive(boolean caseSensitive) Specify whether to perform pattern matching in a case-sensitive fashion. Default is
| |
| setPathSeparator | back to summary |
|---|---|
| public void setPathSeparator(String pathSeparator) Set the path separator to use for pattern parsing. Default is "/", as in Ant. | |
| setTrimTokens | back to summary |
|---|---|
| public void setTrimTokens(boolean trimTokens) Specify whether to trim tokenized paths and patterns. Default is | |
| skipSegment | back to summary |
|---|---|
| private int skipSegment(String path, int pos, String prefix) | |
| skipSeparator | back to summary |
|---|---|
| private int skipSeparator(String path, int pos, String separator) | |
| tokenizePath | back to summary |
|---|---|
| protected String[] tokenizePath(String path) Tokenize the given path into parts, based on this matcher's settings. | |
| tokenizePattern | back to summary |
|---|---|
| protected String[] tokenizePattern(String pattern) Tokenize the given path pattern into parts, based on this matcher's settings. Performs caching based on | |
Pattern.
The pattern may contain special characters: '*' means zero or more characters; '?' means one and
only one character; '{' and '}' indicate a URI template pattern. For example /users/{user}.
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | |
| private static final String | |
| private final boolean | |
| private static final Pattern | |
| private final Pattern | |
| private final String | |
| private final List |
| Access | Constructor and Description |
|---|---|
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: true if the string matches against the pattern, or false otherwise.Main entry point. |
| private String |
| caseSensitive | back to summary |
|---|---|
| private final boolean caseSensitive | |
| DEFAULT_VARIABLE_PATTERN | back to summary |
|---|---|
| private static final String DEFAULT_VARIABLE_PATTERN | |
| exactMatch | back to summary |
|---|---|
| private final boolean exactMatch | |
| GLOB_PATTERN | back to summary |
|---|---|
| private static final Pattern GLOB_PATTERN | |
| pattern | back to summary |
|---|---|
| private final Pattern pattern
| |
| rawPattern | back to summary |
|---|---|
| private final String rawPattern | |
| variableNames | back to summary |
|---|---|
| private final List<String> variableNames | |
| AntPathStringMatcher | back to summary |
|---|---|
| public AntPathStringMatcher(String pattern) | |
| AntPathStringMatcher | back to summary |
|---|---|
| public AntPathStringMatcher(String pattern, boolean caseSensitive) | |
| matchStrings | back to summary |
|---|---|
| public boolean matchStrings(String str, Map<String, String> uriTemplateVariables) Main entry point.
| |
| quote | back to summary |
|---|---|
| private String quote(String s, int start, int end) | |
Comparator implementation returned by
getPatternComparator(String).
In order, the most "generic" pattern is determined by the following:
| Modifier and Type | Class and Description |
|---|---|
| private static class | AntPathMatcher.AntPatternComparator.PatternInfo
Value class that holds information about the pattern, e.g. number of occurrences of "*", "**", and "{" pattern elements. |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public int | Returns: a negative integer, zero, or a positive integer as pattern1 is more specific, equally specific, or less specific than pattern2.the first object to be compared. pattern1, String the second object to be compared. pattern2)Implements java. |
| path | back to summary |
|---|---|
| private final String path | |
| AntPatternComparator | back to summary |
|---|---|
| public AntPatternComparator(String path) | |
| compare | back to summary |
|---|---|
| public int compare(String pattern1, String pattern2) Implements java. Compare two patterns to determine which should match first, i.e. which is the most specific regarding the current path.
| |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private int | |
| private Integer | |
| private final String | |
| private boolean | |
| private int | |
| private int |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| public int | |
| public int | getLength()
Returns the length of the given pattern, where template variables are considered to be 1 long. |
| public int | |
| public int | |
| public int | |
| protected void | |
| public boolean | |
| public boolean |
| catchAllPattern | back to summary |
|---|---|
| private boolean catchAllPattern | |
| doubleWildcards | back to summary |
|---|---|
| private int doubleWildcards | |
| length | back to summary |
|---|---|
| private Integer length
| |
| pattern | back to summary |
|---|---|
| private final String pattern
| |
| prefixPattern | back to summary |
|---|---|
| private boolean prefixPattern | |
| singleWildcards | back to summary |
|---|---|
| private int singleWildcards | |
| uriVars | back to summary |
|---|---|
| private int uriVars | |
| PatternInfo | back to summary |
|---|---|
| public PatternInfo(String pattern) | |
| getDoubleWildcards | back to summary |
|---|---|
| public int getDoubleWildcards() | |
| getLength | back to summary |
|---|---|
| public int getLength() Returns the length of the given pattern, where template variables are considered to be 1 long. | |
| getSingleWildcards | back to summary |
|---|---|
| public int getSingleWildcards() | |
| getTotalCount | back to summary |
|---|---|
| public int getTotalCount() | |
| getUriVars | back to summary |
|---|---|
| public int getUriVars() | |
| initCounters | back to summary |
|---|---|
| protected void initCounters() | |
| isLeastSpecific | back to summary |
|---|---|
| public boolean isLeastSpecific() | |
| isPrefixPattern | back to summary |
|---|---|
| public boolean isPrefixPattern() | |
| Access | Constructor and Description |
|---|---|
| public |
| endsOnDoubleWildCard | back to summary |
|---|---|
| private final String endsOnDoubleWildCard | |
| endsOnWildCard | back to summary |
|---|---|
| private final String endsOnWildCard | |
| PathSeparatorPatternCache | back to summary |
|---|---|
| public PathSeparatorPatternCache(String pathSeparator) | |
| getEndsOnDoubleWildCard | back to summary |
|---|---|
| public String getEndsOnDoubleWildCard() | |
| getEndsOnWildCard | back to summary |
|---|---|
| public String getEndsOnWildCard() | |