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      /**
62       * Gets the source pattern for this matchpattern.
63       * @return the source string without Ant or Regex pattern markers.
64       * @since 3.6.0
65       */
66      public String getSource() {
67          return regexPattern == null ? source : regexPattern;
68      }
69  
70      public boolean matchPath(String str, boolean isCaseSensitive) {
71          if (regexPattern != null) {
72              return str.matches(regexPattern);
73          } else {
74              return SelectorUtils.matchAntPathPattern(this, str, separator, isCaseSensitive);
75          }
76      }
77  
78      boolean matchPath(String str, char[][] strDirs, boolean isCaseSensitive) {
79          if (regexPattern != null) {
80              return str.matches(regexPattern);
81          } else {
82              return SelectorUtils.matchAntPathPattern(getTokenizedPathChars(), strDirs, isCaseSensitive);
83          }
84      }
85  
86      public boolean matchPatternStart(String str, boolean isCaseSensitive) {
87          if (regexPattern != null) {
88              // FIXME: ICK! But we can't do partial matches for regex, so we have to reserve judgement until we have
89              // a file to deal with, or we can definitely say this is an exclusion...
90              return true;
91          } else {
92              String altStr = str.replace('\\', '/');
93  
94              return SelectorUtils.matchAntPathPatternStart(this, str, File.separator, isCaseSensitive)
95                      || SelectorUtils.matchAntPathPatternStart(this, altStr, "/", isCaseSensitive);
96          }
97      }
98  
99      public String[] getTokenizedPathString() {
100         return tokenized;
101     }
102 
103     public char[][] getTokenizedPathChars() {
104         return tokenizedChar;
105     }
106 
107     public boolean startsWith(String string) {
108         return source.startsWith(string);
109     }
110 
111     /**
112      * @since 3.6.0
113      */
114     public static String[] tokenizePathToString(String path, String separator) {
115         List<String> ret = new ArrayList<String>();
116         StringTokenizer st = new StringTokenizer(path, separator);
117         while (st.hasMoreTokens()) {
118             ret.add(st.nextToken());
119         }
120         return ret.toArray(new String[0]);
121     }
122 
123     static char[][] tokenizePathToCharArray(String path, String separator) {
124         String[] tokenizedName = tokenizePathToString(path, separator);
125         char[][] tokenizedNameChar = new char[tokenizedName.length][];
126         for (int i = 0; i < tokenizedName.length; i++) {
127             tokenizedNameChar[i] = tokenizedName[i].toCharArray();
128         }
129         return tokenizedNameChar;
130     }
131 
132     public static MatchPattern fromString(String source) {
133         return new MatchPattern(source, File.separator);
134     }
135 }