View Javadoc
1   package org.codehaus.plexus.util;
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  import java.io.File;
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.StringTokenizer;
23  
24  /**
25   * <p>Describes a match target for SelectorUtils.</p>
26   *
27   * <p>Significantly more efficient than using strings, since re-evaluation and re-tokenizing is avoided.</p>
28   *
29   * @author Kristian Rosenvold
30   */
31  public class MatchPattern {
32      private final String source;
33  
34      private final String regexPattern;
35  
36      private final String separator;
37  
38      private final String[] tokenized;
39  
40      private final char[][] tokenizedChar;
41  
42      private MatchPattern(String source, String separator) {
43          regexPattern = SelectorUtils.isRegexPrefixedPattern(source)
44                  ? source.substring(
45                          SelectorUtils.REGEX_HANDLER_PREFIX.length(),
46                          source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length())
47                  : null;
48          this.source = SelectorUtils.isAntPrefixedPattern(source)
49                  ? source.substring(
50                          SelectorUtils.ANT_HANDLER_PREFIX.length(),
51                          source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length())
52                  : source;
53          this.separator = separator;
54          tokenized = tokenizePathToString(this.source, separator);
55          tokenizedChar = new char[tokenized.length][];
56          for (int i = 0; i < tokenized.length; i++) {
57              tokenizedChar[i] = tokenized[i].toCharArray();
58          }
59      }
60  
61      public boolean matchPath(String str, boolean isCaseSensitive) {
62          if (regexPattern != null) {
63              return str.matches(regexPattern);
64          } else {
65              return SelectorUtils.matchAntPathPattern(this, str, separator, isCaseSensitive);
66          }
67      }
68  
69      boolean matchPath(String str, char[][] strDirs, boolean isCaseSensitive) {
70          if (regexPattern != null) {
71              return str.matches(regexPattern);
72          } else {
73              return SelectorUtils.matchAntPathPattern(getTokenizedPathChars(), strDirs, isCaseSensitive);
74          }
75      }
76  
77      public boolean matchPatternStart(String str, boolean isCaseSensitive) {
78          if (regexPattern != null) {
79              // FIXME: ICK! But we can't do partial matches for regex, so we have to reserve judgement until we have
80              // a file to deal with, or we can definitely say this is an exclusion...
81              return true;
82          } else {
83              String altStr = str.replace('\\', '/');
84  
85              return SelectorUtils.matchAntPathPatternStart(this, str, File.separator, isCaseSensitive)
86                      || SelectorUtils.matchAntPathPatternStart(this, altStr, "/", isCaseSensitive);
87          }
88      }
89  
90      public String[] getTokenizedPathString() {
91          return tokenized;
92      }
93  
94      public char[][] getTokenizedPathChars() {
95          return tokenizedChar;
96      }
97  
98      public boolean startsWith(String string) {
99          return source.startsWith(string);
100     }
101 
102     static String[] tokenizePathToString(String path, String separator) {
103         List<String> ret = new ArrayList<String>();
104         StringTokenizer st = new StringTokenizer(path, separator);
105         while (st.hasMoreTokens()) {
106             ret.add(st.nextToken());
107         }
108         return ret.toArray(new String[0]);
109     }
110 
111     static char[][] tokenizePathToCharArray(String path, String separator) {
112         String[] tokenizedName = tokenizePathToString(path, separator);
113         char[][] tokenizedNameChar = new char[tokenizedName.length][];
114         for (int i = 0; i < tokenizedName.length; i++) {
115             tokenizedNameChar[i] = tokenizedName[i].toCharArray();
116         }
117         return tokenizedNameChar;
118     }
119 
120     public static MatchPattern fromString(String source) {
121         return new MatchPattern(source, File.separator);
122     }
123 }