Package org.sonar.api.utils
Class WildcardPattern
- java.lang.Object
-
- org.sonar.api.utils.WildcardPattern
-
@ThreadSafe public class WildcardPattern extends java.lang.ObjectImplementation of Ant-style matching patterns. Contrary to other implementations (like AntPathMatcher from Spring Framework) it is based onJava Regular Expressions. To increase performance it holds an internal cache of all processed patterns.Following rules are applied:
- ? matches single character
- * matches zero or more characters
- ** matches zero or more 'directories'
Some examples of patterns:
org/T?st.java- matchesorg/Test.javaand alsoorg/Tost.javaorg/*.java- matches all.javafiles in theorgdirectory, e.g.org/Foo.javaororg/Bar.javaorg/**- matches all files underneath theorgdirectory, e.g.org/Foo.javaororg/foo/bar.jsporg/**/Test.java- matches allTest.javafiles underneath theorgdirectory, e.g.org/Test.javaororg/foo/Test.javaororg/foo/bar/Test.javaorg/**/*.java- matches all.javafiles underneath theorgdirectory, e.g.org/Foo.javaororg/foo/Bar.javaororg/foo/bar/Baz.java
Another implementation, which is also based on Java Regular Expressions, can be found in FileUtil from IntelliJ OpenAPI.
- Since:
- 1.10
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedWildcardPattern(java.lang.String pattern, java.lang.String directorySeparator)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static WildcardPatterncreate(java.lang.String pattern)Creates pattern with "/" as a directory separator.static WildcardPattern[]create(java.lang.String[] patterns)Creates array of patterns with "/" as a directory separator.static WildcardPatterncreate(java.lang.String pattern, java.lang.String directorySeparator)Creates pattern with specified separator for directories.booleanmatch(java.lang.String value)Returns true if specified value matches this pattern.static booleanmatch(WildcardPattern[] patterns, java.lang.String value)Returns true if specified value matches one of specified patterns.java.lang.StringtoString()Returns string representation of this pattern.
-
-
-
Method Detail
-
toString
public java.lang.String toString()
Returns string representation of this pattern.- Overrides:
toStringin classjava.lang.Object- Since:
- 2.5
-
match
public boolean match(java.lang.String value)
Returns true if specified value matches this pattern.
-
match
public static boolean match(WildcardPattern[] patterns, java.lang.String value)
Returns true if specified value matches one of specified patterns.- Since:
- 2.4
-
create
public static WildcardPattern create(java.lang.String pattern)
Creates pattern with "/" as a directory separator.- See Also:
create(String, String)
-
create
public static WildcardPattern[] create(@Nullable java.lang.String[] patterns)
Creates array of patterns with "/" as a directory separator.- See Also:
create(String, String)
-
create
public static WildcardPattern create(java.lang.String pattern, java.lang.String directorySeparator)
Creates pattern with specified separator for directories.This is used to match Java-classes, i.e.
org.foo.Baragainstorg/**. However usage of character other than "/" as a directory separator is misleading and should be avoided, so methodcreate(String)is preferred over this one.Also note that no matter whether forward or backward slashes were used in the
antPatternthe returned pattern will usedirectorySeparator. Thus to match Windows-style path "dir\file.ext" against pattern "dir/file.ext" normalization should be performed.
-
-