/* * SonarQube * Copyright (C) 2009-2016 SonarSource SA * mailto:contact AT sonarsource DOT com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 3 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ package org.sonar.api.utils; import java.util.HashMap; import java.util.Map; import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; /** * Implementation of Ant-style matching patterns. * Contrary to other implementations (like AntPathMatcher from Spring Framework) it is based on {@link Pattern Java Regular Expressions}. * To increase performance it holds an internal cache of all processed patterns. *
* Following rules are applied: *
* Some examples of patterns: *
org/T?st.java
- matches org/Test.java
and also org/Tost.java
org/*.java
- matches all .java
files in the org
directory,
* e.g. org/Foo.java
or org/Bar.java
org/**
- matches all files underneath the org
directory,
* e.g. org/Foo.java
or org/foo/bar.jsp
org/**/Test.java
- matches all Test.java
files underneath the org
directory,
* e.g. org/Test.java
or org/foo/Test.java
or org/foo/bar/Test.java
org/**/*.java
- matches all .java
files underneath the org
directory,
* e.g. org/Foo.java
or org/foo/Bar.java
or org/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
*/
public class WildcardPattern {
private static final Map
* This is used to match Java-classes, i.e.
* Also note that no matter whether forward or backward slashes were used in the org.foo.Bar
against org/**
.
* However usage of character other than "/" as a directory separator is misleading and should be avoided,
* so method {@link #create(String)} is preferred over this one.
*
* antPattern
* the returned pattern will use directorySeparator
.
* Thus to match Windows-style path "dir\file.ext" against pattern "dir/file.ext" normalization should be performed.
*
*/
public static WildcardPattern create(String pattern, String directorySeparator) {
String key = pattern + directorySeparator;
WildcardPattern wildcardPattern = CACHE.get(key);
if (wildcardPattern == null) {
wildcardPattern = new WildcardPattern(pattern, directorySeparator);
CACHE.put(key, wildcardPattern);
}
return wildcardPattern;
}
}