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      /**
084       * @since 2.4
085       */
086      public static boolean match(WildcardPattern[] patterns, String value) {
087        for (WildcardPattern pattern : patterns) {
088          if (pattern.match(value)) {
089            return true;
090          }
091        }
092        return false;
093      }
094    
095      public static WildcardPattern create(String pattern) {
096        return create(pattern, "/");
097      }
098    
099      public static WildcardPattern[] create(String[] patterns) {
100        if (patterns == null) {
101          return new WildcardPattern[0];
102        }
103        WildcardPattern[] exclusionPAtterns = new WildcardPattern[patterns.length];
104        for (int i = 0; i < patterns.length; i++) {
105          exclusionPAtterns[i] = create(patterns[i]);
106        }
107        return exclusionPAtterns;
108      }
109    
110      public static WildcardPattern create(String pattern, String directorySeparator) {
111        String key = pattern + directorySeparator;
112        WildcardPattern wildcardPattern = patterns.get(key);
113        if (wildcardPattern == null) {
114          wildcardPattern = new WildcardPattern(pattern, directorySeparator);
115          patterns.put(key, wildcardPattern);
116        }
117        return wildcardPattern;
118      }
119    }