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