001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2009 SonarSource SA
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.api.utils;
021
022 import org.apache.commons.lang.StringUtils;
023
024 import java.util.HashMap;
025 import java.util.Map;
026 import java.util.regex.Pattern;
027
028 /**
029 * @since 1.10
030 */
031 public class WildcardPattern {
032
033 private static final Map<String, WildcardPattern> patterns = new HashMap<String, WildcardPattern>();
034
035 private Pattern pattern;
036
037 protected WildcardPattern(String pattern, String directorySeparator) {
038 this.pattern = Pattern.compile(toRegexp(pattern, directorySeparator));
039 }
040
041 public boolean match(String value) {
042 return pattern.matcher(removeSlahesToIgnore(value)).matches();
043 }
044
045 private String toRegexp(String wildcardPattern, String directorySeparator) {
046 String patternStr = removeSlahesToIgnore(wildcardPattern);
047 patternStr = StringUtils.replace(patternStr, "**/**", "**");
048 patternStr = StringUtils.replace(patternStr, "**/", "|");
049 patternStr = StringUtils.replace(patternStr, "/**", "|");
050 patternStr = StringUtils.replace(patternStr, "**", "|");
051 StringBuilder sb = new StringBuilder();
052
053 for (char c : patternStr.toCharArray()) {
054 switch (c) {
055 case '|':
056 sb.append(".*");
057 break;
058 case '*':
059 sb.append("[^\\").append(directorySeparator).append("]*");
060 break;
061 case '?':
062 sb.append("[^\\").append(directorySeparator).append("]");
063 break;
064 case '.':
065 sb.append("\\.");
066 break;
067 case '/':
068 sb.append("\\").append(directorySeparator);
069 break;
070 default:
071 sb.append(c);
072 }
073 }
074
075 return sb.toString();
076 }
077
078 private String removeSlahesToIgnore(String wildcardPattern) {
079 String patternStr = StringUtils.removeStart(wildcardPattern, "/");
080 return StringUtils.removeEnd(patternStr, "/");
081 }
082
083 public static WildcardPattern create(String pattern) {
084 return create(pattern, "/");
085 }
086
087 public static WildcardPattern[] create(String[] patterns) {
088 if (patterns==null) {
089 return new WildcardPattern[0];
090 }
091 WildcardPattern[] exclusionPAtterns = new WildcardPattern[patterns.length];
092 for (int i = 0; i < patterns.length; i++) {
093 exclusionPAtterns[i] = create(patterns[i]);
094 }
095 return exclusionPAtterns;
096 }
097
098 public static WildcardPattern create(String pattern, String directorySeparator) {
099 String key = pattern + directorySeparator;
100 WildcardPattern wildcardPattern = patterns.get(key);
101 if (wildcardPattern == null) {
102 wildcardPattern = new WildcardPattern(pattern, directorySeparator);
103 patterns.put(key, wildcardPattern);
104 }
105 return wildcardPattern;
106 }
107 }