类 AntPathMatcher
- java.lang.Object
-
- cc.shacocloud.mirage.utils.AntPathMatcher
-
- 所有已实现的接口:
PathMatcher
public class AntPathMatcher extends Object implements PathMatcher
Ant 风格的路径匹配器匹配URL的规则如下:
?匹配单个字符*匹配0个或多个字符**0个或多个路径中的目录节点{mirage:[a-z]+}匹配以"mirage"命名的正则[a-z]+
例子:
com/t?st.jsp— 匹配com/test.jsp或com/tast.jsp或com/txst.jspcom/*.jsp— 匹配com目录下全部.jsp文件com/**/test.jsp— 匹配com目录下全部test.jsp文件cn/mirage/**/*.jsp— 匹配cn/mirage路径下全部.jsp文件org/**/servlet/bla.jsp— 匹配cn/mirage/servlet/bla.jsp或cn/mirage/testing/servlet/bla.jsp或org/servlet/bla.jspcom/{filename:\\w+}.jsp匹配com/test.jsp并将test关联到filename变量
注意: 表达式和路径必须都为绝对路径或都为相对路径。
-
-
嵌套类概要
嵌套类 修饰符和类型 类 说明 protected static classAntPathMatcher.AntPathStringMatcher通过Pattern测试字符串是否与模式匹配。protected static classAntPathMatcher.AntPatternComparator由getPatternComparator(String)返回的默认Comparator实现。
-
字段概要
字段 修饰符和类型 字段 说明 static StringDEFAULT_PATH_SEPARATOR默认的路径分割符: "/".
-
构造器概要
构造器 构造器 说明 AntPathMatcher()使用DEFAULT_PATH_SEPARATOR作为分隔符构造AntPathMatcher(String pathSeparator)使用自定义的分隔符构造
-
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 Stringcombine(String pattern1, String pattern2)这个实现只是简单地连接两个模式,除非第一个模式包含文件扩展名匹配(例如,.html)。protected booleandoMatch(String pattern, String path, boolean fullMatch, Map<String,String> uriTemplateVariables)执行匹配,判断给定的path是否匹配patternStringextractPathWithinPattern(String pattern, String path)给定一个模式和一个完整路径,确定模式映射部分。Map<String,String>extractUriTemplateVariables(String pattern, String path)给定模式和完整路径,提取URI模板变量。Comparator<String>getPatternComparator(String path)给定一个完整的路径,返回一个Comparator,适合按显式顺序对模式进行排序。protected @NotNull AntPathMatcher.AntPathStringMatchergetStringMatcher(@NotNull String pattern)为给定的模式构建或检索AntPathMatcher.AntPathStringMatcher。booleanisPattern(@Nullable String path)判断给定路径是否是表达式booleanmatch(String pattern, String path)给定路径是否匹配表达式booleanmatchStart(String pattern, String path)前置部分匹配AntPathMatchersetCachePatterns(boolean cachePatterns)指定是否缓存传递到此匹配器的match(java.lang.String, java.lang.String)方法的模式的已解析模式元数据。AntPathMatchersetCaseSensitive(boolean caseSensitive)设置是否大小写敏感,默认为trueAntPathMatchersetPathSeparator(String pathSeparator)设置路径分隔符AntPathMatchersetTrimTokens(boolean trimTokens)设置是否去除路径节点两边的空白符,默认为falseprotected String[]tokenizePath(String path)根据这个匹配器的设置,将给定的路径标记为多个部分protected String[]tokenizePattern(String pattern)根据这个匹配器的设置,将给定的路径模式标记为各个部分
-
-
-
构造器详细资料
-
AntPathMatcher
public AntPathMatcher()
使用DEFAULT_PATH_SEPARATOR作为分隔符构造
-
AntPathMatcher
public AntPathMatcher(String pathSeparator)
使用自定义的分隔符构造- 参数:
pathSeparator- 要使用的路径分隔符,不能是null
-
-
方法详细资料
-
setPathSeparator
public AntPathMatcher setPathSeparator(String pathSeparator)
设置路径分隔符- 参数:
pathSeparator- 分隔符,null表示使用默认分隔符DEFAULT_PATH_SEPARATOR- 返回:
- this
-
setCaseSensitive
public AntPathMatcher setCaseSensitive(boolean caseSensitive)
设置是否大小写敏感,默认为true- 参数:
caseSensitive- 是否大小写敏感- 返回:
- this
-
setTrimTokens
public AntPathMatcher setTrimTokens(boolean trimTokens)
设置是否去除路径节点两边的空白符,默认为false- 参数:
trimTokens- 是否去除路径节点两边的空白符- 返回:
- this
-
setCachePatterns
public AntPathMatcher setCachePatterns(boolean cachePatterns)
- 参数:
cachePatterns- 是否缓存表达式- 返回:
- this
- 另请参阅:
getStringMatcher(String)
-
isPattern
public boolean isPattern(@Nullable @Nullable String path)判断给定路径是否是表达式- 指定者:
isPattern在接口中PathMatcher- 参数:
path- 路径- 返回:
- 是否为表达式
-
match
public boolean match(String pattern, String path)
给定路径是否匹配表达式- 指定者:
match在接口中PathMatcher- 参数:
pattern- 表达式path- 路径- 返回:
- 是否匹配
-
matchStart
public boolean matchStart(String pattern, String path)
前置部分匹配- 指定者:
matchStart在接口中PathMatcher- 参数:
pattern- 表达式path- 路径- 返回:
- 是否匹配
-
doMatch
protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String,String> uriTemplateVariables)
执行匹配,判断给定的path是否匹配pattern- 参数:
pattern- 表达式path- 路径fullMatch- 是否全匹配。true表示全路径匹配,false表示只匹配开始uriTemplateVariables- 变量映射- 返回:
true表示提供的path匹配,false表示不匹配
-
tokenizePattern
protected String[] tokenizePattern(String pattern)
根据这个匹配器的设置,将给定的路径模式标记为各个部分基于
setCachePatterns(boolean)执行缓存,将实际的标记化算法委托给tokenizePath(String)- 参数:
pattern- 要标记的模式- 返回:
- 标记化的模式部分
-
tokenizePath
protected String[] tokenizePath(String path)
根据这个匹配器的设置,将给定的路径标记为多个部分- 参数:
path- 标记化的路径- 返回:
- 标记化的路径部分
-
getStringMatcher
@NotNull protected @NotNull AntPathMatcher.AntPathStringMatcher getStringMatcher(@NotNull @NotNull String pattern)
为给定的模式构建或检索AntPathMatcher.AntPathStringMatcher。默认实现检查这个 AntPathStringMatcher 的内部缓存(参见
setCachePatterns(boolean)),如果没有找到缓存副本,就创建一个新的AntPathStringMatcher实例。- 参数:
pattern- 要匹配的模式- 返回:
- 一个对应的 AntPathStringMatcher
- 另请参阅:
setCachePatterns(boolean)
-
extractPathWithinPattern
public String extractPathWithinPattern(String pattern, String path)
给定一个模式和一个完整路径,确定模式映射部分。例如:
- '
/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'
- 指定者:
extractPathWithinPattern在接口中PathMatcher- 参数:
pattern- 表达式path- 路径- 返回:
- 表达式匹配到的部分
- '
-
extractUriTemplateVariables
public Map<String,String> extractUriTemplateVariables(String pattern, String path)
从接口复制的说明:PathMatcher给定模式和完整路径,提取URI模板变量。URI模板变量通过(:xxx)表示。- 指定者:
extractUriTemplateVariables在接口中PathMatcher- 参数:
pattern- 路径模式,可能包含URI模板path- 要从中提取模板变量的完整路径- 返回:
- 映射,包含变量名作为键;变量值作为值
-
combine
public String combine(String pattern1, String pattern2)
这个实现只是简单地连接两个模式,除非第一个模式包含文件扩展名匹配(例如,.html)。 在这种情况下,第二个模式将合并到第一个模式中。否则,将抛出IllegalArgumentException。例子
Pattern 1 Pattern 2 Result nullnull/hotels null/hotels null/hotels /hotels /hotels /bookings /hotels/bookings /hotels bookings /hotels/bookings /hotels/* /bookings /hotels/bookings /hotels/** /bookings /hotels/**/bookings /hotels {hotel} /hotels/{hotel} /hotels/* {hotel} /hotels/{hotel} /hotels/** {hotel} /hotels/**/{hotel} /*.html /hotels.html /hotels.html /*.html /hotels /hotels.html /*.html /*.txt IllegalArgumentException- 指定者:
combine在接口中PathMatcher- 参数:
pattern1- 第一种模式pattern2- 第二种模式- 返回:
- 两种模式的结合
-
getPatternComparator
public Comparator<String> getPatternComparator(String path)
给定一个完整的路径,返回一个Comparator,适合按显式顺序对模式进行排序。这个
Comparator将 List.sort(Comparator) 对列表进行排序, 以便更具体的模式(没有URI模板或通配符)出现在泛型模式之前。 因此,给定一个具有以下模式的列表,返回的比较器将对该列表进行排序,以便按照指示的顺序进行排序。/hotels/new/hotels/{hotel}/hotels/*
作为参数给出的完整路径用于测试精确匹配。因此,当给定的路径是
hotels2时,模式hotels2将被排序在hotels1之前。- 指定者:
getPatternComparator在接口中PathMatcher- 参数:
path- 用于比较的完整路径- 返回:
- 一种能够按显式顺序对模式进行排序的比较器
-
-