Class AntPathMatcher

java.lang.Object
org.miaixz.bus.core.text.AntPathMatcher

public class AntPathMatcher extends Object
Ant风格的路径匹配器。 来自Spring-core和Ant

匹配URL的规则如下:

  • ? 匹配单个字符
  • * 匹配0个或多个字符
  • ** 0个或多个路径中的目录节点
  • {bus:[a-z]+} 匹配以"bus"命名的正则 [a-z]+

示例:

  • com/t?st.jsp — 匹配 com/test.jspcom/tast.jspcom/txst.jsp
  • com/*.jsp — 匹配com目录下全部 .jsp文件
  • com/**/test.jsp — 匹配com目录下全部 test.jsp文件
  • org/bus/**/*.jsp — 匹配org/bus路径下全部.jsp 文件
  • org/**/servlet/bla.jsp — 匹配org/bus/servlet/bla.jsporg/bus/testing/servlet/bla.jsporg/servlet/bla.jsp
  • com/{filename:\\w+}.jsp 匹配 com/test.jsp 并将 test 关联到 filename 变量

注意: 表达式和路径必须都为绝对路径或都为相对路径。

Since:
Java 17+
Author:
Kimi Liu
  • Field Details

    • DEFAULT_PATH_SEPARATOR

      public static final String DEFAULT_PATH_SEPARATOR
      Default path separator: "/".
      See Also:
  • Constructor Details

    • AntPathMatcher

      public AntPathMatcher()
      使用 DEFAULT_PATH_SEPARATOR 作为分隔符构造
    • AntPathMatcher

      public AntPathMatcher(String pathSeparator)
      使用自定义的分隔符构造
      Parameters:
      pathSeparator - the path separator to use, must not be null.
  • Method Details

    • setPathSeparator

      public AntPathMatcher setPathSeparator(String pathSeparator)
      设置路径分隔符
      Parameters:
      pathSeparator - 分隔符,null表示使用默认分隔符DEFAULT_PATH_SEPARATOR
      Returns:
      this
    • setCaseSensitive

      public AntPathMatcher setCaseSensitive(boolean caseSensitive)
      设置是否大小写敏感,默认为true
      Parameters:
      caseSensitive - 是否大小写敏感
      Returns:
      this
    • setTrimTokens

      public AntPathMatcher setTrimTokens(boolean trimTokens)
      设置是否去除路径节点两边的空白符,默认为false
      Parameters:
      trimTokens - 是否去除路径节点两边的空白符
      Returns:
      this
    • setCachePatterns

      public AntPathMatcher 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. 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.

      Parameters:
      cachePatterns - 是否缓存表达式
      Returns:
      this
      See Also:
    • isPattern

      public boolean isPattern(String path)
      判断给定路径是否是表达式
      Parameters:
      path - 路径
      Returns:
      是否为表达式
    • match

      public boolean match(String pattern, String path)
      给定路径是否匹配表达式
      Parameters:
      pattern - 表达式
      path - 路径
      Returns:
      是否匹配
    • matchStart

      public boolean matchStart(String pattern, String path)
      前置部分匹配
      Parameters:
      pattern - 表达式
      path - 路径
      Returns:
      是否匹配
    • doMatch

      protected boolean doMatch(String pattern, String path, boolean fullMatch, Map<String,String> uriTemplateVariables)
      执行匹配,判断给定的path是否匹配pattern
      Parameters:
      pattern - 表达式
      path - 路径
      fullMatch - 是否全匹配。true 表示全路径匹配,false表示只匹配开始
      uriTemplateVariables - 变量映射
      Returns:
      true 表示提供的 path 匹配, false 表示不匹配
    • tokenizePattern

      protected String[] tokenizePattern(String pattern)
      Tokenize the given path pattern into parts, based on this matcher's settings.

      Performs caching based on setCachePatterns(boolean), delegating to tokenizePath(String) for the actual tokenization algorithm.

      Parameters:
      pattern - the pattern to tokenize
      Returns:
      the tokenized pattern parts
    • tokenizePath

      protected String[] tokenizePath(String path)
      Tokenize the given path into parts, based on this matcher's settings.
      Parameters:
      path - the path to tokenize
      Returns:
      the tokenized path parts
    • getStringMatcher

      protected AntPathMatcher.AntPathStringMatcher getStringMatcher(String pattern)
      Build or retrieve an 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 copier 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.

      Parameters:
      pattern - the pattern to match against (never null)
      Returns:
      a corresponding AntPathStringMatcher (never null)
      See Also:
    • extractPathWithinPattern

      public String extractPathWithinPattern(String pattern, String path)
      Given a pattern and a full path, determine the pattern-mapped part.

      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'
      • '/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'

      Assumes that match(java.lang.String, java.lang.String) returns true for 'pattern' and 'path', but does not enforce this.

      Parameters:
      pattern - 表达式
      path - 路径
      Returns:
      表达式匹配到的部分
    • extractUriTemplateVariables

      public Map<String,String> extractUriTemplateVariables(String pattern, String path)
      提取参数
      Parameters:
      pattern - 模式
      path - 路径
      Returns:
      参数
    • combine

      public String combine(String pattern1, String pattern2)
      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., *.html). In that case, the second pattern will be merged into the first. Otherwise, an IllegalArgumentException will be thrown.
      Parameters:
      pattern1 - the first pattern
      pattern2 - the second pattern
      Returns:
      the combination of the two patterns
      Throws:
      IllegalArgumentException - if the two patterns cannot be combined
    • getPatternComparator

      public Comparator<String> getPatternComparator(String path)
      Given a full path, returns a Comparator suitable for sorting patterns in order of explicitness.

      This Comparator will sort a list so that more specific patterns (without URI templates or wild cards) come before generic patterns. So given a list with the following patterns, the returned compare will sort this list so that the order will be as indicated.

      1. /hotels/new
      2. /hotels/{hotel}
      3. /hotels/*

      The full path given as parameter is used to test for exact matches. So when the given path is /hotels/2, the pattern /hotels/2 will be sorted before /hotels/1.

      Parameters:
      path - the full path to use for comparison
      Returns:
      a compare capable of sorting patterns in order of explicitness