View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.utils.io;
20  
21  import javax.annotation.Nonnull;
22  
23  import java.io.File;
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.StringTokenizer;
27  import java.util.regex.Pattern;
28  
29  /**
30   * <p>Describes a match target for SelectorUtils.</p>
31   * <p>
32   * Significantly more efficient than using strings, since re-evaluation and re-tokenizing is avoided.</p>
33   *
34   * @author Kristian Rosenvold
35   * @deprecated use {@code java.nio.file.DirectoryStream.Filter<T>} and related classes
36   */
37  @Deprecated
38  public class MatchPattern {
39      private final String source;
40  
41      private final String regexPattern;
42  
43      private final Pattern regexPatternRegex;
44  
45      private final String separator;
46  
47      private final String[] tokenized;
48  
49      private MatchPattern(@Nonnull String source, @Nonnull String separator) {
50          regexPattern = SelectorUtils.isRegexPrefixedPattern(source)
51                  ? source.substring(
52                          SelectorUtils.REGEX_HANDLER_PREFIX.length(),
53                          source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length())
54                  : null;
55          regexPatternRegex = regexPattern != null ? Pattern.compile(regexPattern) : null;
56          this.source = SelectorUtils.isAntPrefixedPattern(source)
57                  ? source.substring(
58                          SelectorUtils.ANT_HANDLER_PREFIX.length(),
59                          source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length())
60                  : source;
61          this.separator = separator;
62          tokenized = tokenizePathToString(this.source, separator);
63      }
64  
65      /**
66       * @param str The string to match for.
67       * @param isCaseSensitive case sensitive true false otherwise.
68       * @return true if matches false otherwise.
69       */
70      public boolean matchPath(String str, boolean isCaseSensitive) {
71          if (regexPattern != null) {
72              return regexPatternRegex.matcher(str).matches();
73          } else {
74              return SelectorUtils.matchAntPathPattern(this, str, separator, isCaseSensitive);
75          }
76      }
77  
78      boolean matchPath(String str, String[] strDirs, boolean isCaseSensitive) {
79          if (regexPattern != null) {
80              return regexPatternRegex.matcher(str).matches();
81          } else {
82              return SelectorUtils.matchAntPathPattern(getTokenizedPathString(), strDirs, isCaseSensitive);
83          }
84      }
85  
86      /**
87       * @param str The string to check.
88       * @param isCaseSensitive Check case sensitive or not.
89       * @return true in case of matching pattern.
90       */
91      public boolean matchPatternStart(@Nonnull String str, boolean isCaseSensitive) {
92          if (regexPattern != null) {
93              // FIXME: ICK! But we can't do partial matches for regex, so we have to reserve judgment until we have
94              // a file to deal with, or we can definitely say this is an exclusion...
95              return true;
96          } else {
97              String altStr = source.replace('\\', '/');
98  
99              return SelectorUtils.matchAntPathPatternStart(this, str, File.separator, isCaseSensitive)
100                     || SelectorUtils.matchAntPathPatternStart(this, altStr, "/", isCaseSensitive);
101         }
102     }
103 
104     /**
105      * @return Tokenized string.
106      */
107     public String[] getTokenizedPathString() {
108         return tokenized;
109     }
110 
111     /**
112      * @param string The part which will be checked to start with.
113      * @return true in case of starting with the string false otherwise.
114      */
115     public boolean startsWith(String string) {
116         return source.startsWith(string);
117     }
118 
119     static String[] tokenizePathToString(@Nonnull String path, @Nonnull String separator) {
120         List<String> ret = new ArrayList<String>();
121         StringTokenizer st = new StringTokenizer(path, separator);
122         while (st.hasMoreTokens()) {
123             ret.add(st.nextToken());
124         }
125         return ret.toArray(new String[ret.size()]);
126     }
127 
128     /**
129      * @param source The source.
130      * @return The match pattern.
131      */
132     public static MatchPattern fromString(String source) {
133         return new MatchPattern(source, File.separator);
134     }
135 }