View Javadoc
1   package org.apache.rat.config.exclusion.plexus;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  
20  import java.io.File;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.List;
24  import java.util.StringTokenizer;
25  
26  import org.apache.rat.utils.DefaultLog;
27  import org.apache.rat.utils.Log;
28  
29  import static java.lang.String.format;
30  @SuppressWarnings({"checkstyle:RegexpSingleLine", "checkstyle:JavadocVariable"})
31  /**
32   * <p>Describes a match target for SelectorUtils.</p>
33   *
34   * <p>Significantly more efficient than using strings, since re-evaluation and re-tokenizing is avoided.</p>
35   *
36   * <p>Based on code from plexus-utils.</p>
37   *
38   * @author Kristian Rosenvold
39   * @see <a href="https://github.com/codehaus-plexus/plexus-utils/blob/master/src/main/java/org/codehaus/plexus/util/MatchPattern.java">
40   * plexus-utils MatchPattern</a>
41   */
42  public final class MatchPattern {
43      private final String source;
44  
45      private final String regexPattern;
46  
47      private final String separator;
48  
49      private final String[] tokenized;
50  
51      private final char[][] tokenizedChar;
52  
53      public MatchPattern(final String source, final String separator) {
54          regexPattern = SelectorUtils.isRegexPrefixedPattern(source)
55                  ? source.substring(
56                  SelectorUtils.REGEX_HANDLER_PREFIX.length(),
57                  source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length())
58                  : null;
59          this.source = SelectorUtils.isAntPrefixedPattern(source)
60                  ? source.substring(
61                  SelectorUtils.ANT_HANDLER_PREFIX.length(),
62                  source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length())
63                  : source;
64          this.separator = separator;
65          tokenized = tokenizePathToString(this.source, separator);
66          tokenizedChar = new char[tokenized.length][];
67          for (int i = 0; i < tokenized.length; i++) {
68              tokenizedChar[i] = tokenized[i].toCharArray();
69          }
70      }
71  
72      public boolean matchPath(final String str, final boolean isCaseSensitive) {
73          if (regexPattern != null) {
74              return str.matches(regexPattern);
75          } else {
76              return SelectorUtils.matchAntPathPattern(this, str, separator, isCaseSensitive);
77          }
78      }
79  
80      boolean matchPath(final String str, final char[][] strDirs, final boolean isCaseSensitive) {
81          boolean result;
82          if (regexPattern != null) {
83              result = str.matches(regexPattern);
84          } else {
85              result = SelectorUtils.matchAntPathPattern(getTokenizedPathChars(), strDirs, isCaseSensitive);
86          }
87          if (result && DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) {
88              DefaultLog.getInstance().debug(format("%s match %s -> true", this, str));
89          }
90          return result;
91      }
92  
93      public boolean matchPatternStart(final String str, final boolean isCaseSensitive) {
94          if (regexPattern != null) {
95              // FIXME: ICK! But we can't do partial matches for regex, so we have to reserve judgement until we have
96              // a file to deal with, or we can definitely say this is an exclusion...
97              return true;
98          } else {
99              String altStr = str.replace('\\', '/');
100 
101             return SelectorUtils.matchAntPathPatternStart(this, str, File.separator, isCaseSensitive)
102                     || SelectorUtils.matchAntPathPatternStart(this, altStr, "/", isCaseSensitive);
103         }
104     }
105 
106     public String[] getTokenizedPathString() {
107         return tokenized;
108     }
109 
110     public char[][] getTokenizedPathChars() {
111         return tokenizedChar;
112     }
113 
114     public boolean startsWith(final String string) {
115         return source.startsWith(string);
116     }
117 
118     @Override
119     public String toString() {
120         return Arrays.asList(tokenized).toString();
121     }
122 
123     public String source() {
124         return source;
125     }
126 
127     public static String[] tokenizePathToString(final String path, final String separator) {
128         List<String> ret = new ArrayList<>();
129         StringTokenizer st = new StringTokenizer(path, separator);
130         while (st.hasMoreTokens()) {
131             ret.add(st.nextToken());
132         }
133         return ret.toArray(new String[0]);
134     }
135 
136     static char[][] tokenizePathToCharArray(final String path, final String separator) {
137         String[] tokenizedName = tokenizePathToString(path, separator);
138         char[][] tokenizedNameChar = new char[tokenizedName.length][];
139         for (int i = 0; i < tokenizedName.length; i++) {
140             tokenizedNameChar[i] = tokenizedName[i].toCharArray();
141         }
142         return tokenizedNameChar;
143     }
144 }