Class GlobMatcher
Java NIO PathMatcher only works for file-system
paths. This class provides lightweight glob matching for arbitrary
text strings using two wildcards:
*— matches any sequence of characters (including empty)?— matches exactly one character
Use case: match DocBook namespace with trailing wildcard
GlobMatcher.match("http://docbook.org/ns/docbook*", "http://docbook.org/ns/docbook"); // true
GlobMatcher.match("http://docbook.org/ns/docbook*", "http://docbook.org/ns/docbook/5.1"); // true
Use case: match any MyBatis DTD version
GlobMatcher.match("-//mybatis.org//DTD Mapper*", "-//mybatis.org//DTD Mapper 3.0//EN"); // true
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanMatches a glob pattern against a text string.static booleanChecks if any glob pattern in the list matches the given value using full-text matching (match(String, String)).static booleanPath-aware glob matching.static booleanmatchPathAny(List<String> patterns, String value) Checks if any glob pattern in the list matches the given value using path-aware matching (matchPath(String, String)).
-
Method Details
-
match
Matches a glob pattern against a text string.Supports path-glob
**/sequences: when**appears as a complete path component (at the start of the pattern or after/), it matches zero or more path segments. Any non-empty prefix consumed by**must end at a/boundary.Use case:
**/my-schema.dtdmatchesmy-schema.dtdandpath/to/my-schema.dtd**/my-schema.dtddoes not matchother-my-schema.dtd
- Parameters:
pattern- the glob pattern (supports*,?, and**/).text- the text to match.- Returns:
trueif the text matches the pattern.
-
matchPath
Path-aware glob matching. Behaves likematch(String, String)but also tries matching against the filename portion of the text (after the last/) when the pattern contains no/.Use this for values that are typically paths or URIs with path structure — such as DOCTYPE system IDs, grammar URIs, and xml-model hrefs — where a bare filename pattern like
my-schema.dtdshould matchpath/to/my-schema.dtd.Do not use this for namespace URIs or public IDs, where a pattern like
docbookmust not accidentally matchhttp://example.com/ns/docbook.- Parameters:
pattern- the glob pattern.text- the text to match.- Returns:
trueif the text matches the pattern.
-
matchAny
Checks if any glob pattern in the list matches the given value using full-text matching (match(String, String)).Use case: check if a document's namespace URI or public ID matches any of the configured patterns (OR semantics).
- Parameters:
patterns- the list of glob patterns.value- the value to match, ornull.- Returns:
trueif any pattern matches the value;falseif the value isnullor no pattern matches.
-
matchPathAny
Checks if any glob pattern in the list matches the given value using path-aware matching (matchPath(String, String)).Use case: check if a document's system ID or grammar URI matches any of the configured patterns (OR semantics), allowing bare filename patterns to match path-like values.
- Parameters:
patterns- the list of glob patterns.value- the value to match, ornull.- Returns:
trueif any pattern matches the value;falseif the value isnullor no pattern matches.
-