PathMatcher
public class AntPathMatcher extends Object implements PathMatcher
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{beeline:[a-z]+}
matches the regexp [a-z]+
as a path variable named "beeline"com/t?st.jsp
— matches com/test.jsp
but also
com/tast.jsp
or com/txst.jsp
com/*.jsp
— matches all .jsp
files in the
com
directorycom/**/test.jsp
— matches all test.jsp
files underneath the com
pathio/honeycomb/**/*.jsp
— matches all
.jsp
files underneath the io/honeycomb
pathio/**/servlet/bla.jsp
— matches
io/honeycomb/servlet/bla.jsp
but also
io/honeycomb/testing/servlet/bla.jsp
and io/beeline/bla.jsp
com/{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.
NB:: This class is forked from the Spring Framework with modifications.
This was to avoid having a dependency on Spring in the core Beeline module, but also to avoid rewriting an implementation of Ant-style path pattern matching. The approach was to keep only the #match method from the Spring implementation and remove anything else that #match does not use (e.g. fields). Also removed or forked were any methods calls to other classes within Spring. The original copy was taken from 94e3210 in the Spring Framework Repo.
As per the Apache 2.0 license, the original copyright notice and all author and copyright information have remained in tact.
Modifier and Type | Class | Description |
---|---|---|
protected static class |
AntPathMatcher.AntPathStringMatcher |
Tests whether or not a string matches against a pattern via a
Pattern . |
Modifier and Type | Field | Description |
---|---|---|
static String |
DEFAULT_PATH_SEPARATOR |
Default path separator: "/".
|
Constructor | Description |
---|---|
AntPathMatcher() |
Create a new instance with the
DEFAULT_PATH_SEPARATOR . |
AntPathMatcher(String pathSeparator) |
A convenient, alternative constructor to use with a custom path separator.
|
Modifier and Type | Method | Description |
---|---|---|
protected boolean |
doMatch(String pattern,
String path,
boolean fullMatch,
Map<String,String> uriTemplateVariables) |
Actually match the given
path against the given pattern . |
protected AntPathMatcher.AntPathStringMatcher |
getStringMatcher(String pattern) |
Build or retrieve an
AntPathMatcher.AntPathStringMatcher for the given pattern. |
boolean |
match(String pattern,
String path) |
Return
true if the given path matches the given pattern , otherwise
false . |
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. |
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
public AntPathMatcher()
DEFAULT_PATH_SEPARATOR
.public AntPathMatcher(String pathSeparator)
pathSeparator
- the path separator to use, must not be null
.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)
public boolean match(String pattern, String path)
PathMatcher
true
if the given path
matches the given pattern
, otherwise
false
.match
in interface PathMatcher
pattern
- the pattern to match againstpath
- the path to matchprotected 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'tprotected 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 tokenizeprotected 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)
Copyright © 2019–2020 Honeycomb. All rights reserved.