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   */
37  public class MatchPattern
38  {
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      {
51          regexPattern = SelectorUtils.isRegexPrefixedPattern( source ) ? source.substring(
52              SelectorUtils.REGEX_HANDLER_PREFIX.length(),
53              source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length() ) : null;
54          regexPatternRegex = regexPattern != null ? Pattern.compile( regexPattern ) : null;
55          this.source = SelectorUtils.isAntPrefixedPattern( source ) ? source.substring(
56              SelectorUtils.ANT_HANDLER_PREFIX.length(),
57              source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length() ) : source;
58          this.separator = separator;
59          tokenized = tokenizePathToString( this.source, separator );
60      }
61  
62  
63      /**
64       * @param str The string to match for.
65       * @param isCaseSensitive case sensitive true false otherwise.
66       * @return true if matches false otherwise.
67       */
68      public boolean matchPath( String str, boolean isCaseSensitive )
69      {
70          if ( regexPattern != null )
71          {
72              return regexPatternRegex.matcher( str ).matches();
73          }
74          else
75          {
76              return SelectorUtils.matchAntPathPattern( this, str, separator, isCaseSensitive );
77          }
78      }
79  
80      boolean matchPath( String str, String[] strDirs, boolean isCaseSensitive )
81      {
82          if ( regexPattern != null )
83          {
84              return regexPatternRegex.matcher( str ).matches();
85          }
86          else
87          {
88              return SelectorUtils.matchAntPathPattern( getTokenizedPathString(), strDirs, isCaseSensitive );
89          }
90      }
91  
92      /**
93       * @param str The string to check.
94       * @param isCaseSensitive Check case sensitive or not.
95       * @return true in case of matching pattern.
96       */
97      public boolean matchPatternStart( @Nonnull String str, boolean isCaseSensitive )
98      {
99          if ( regexPattern != null )
100         {
101             // FIXME: ICK! But we can't do partial matches for regex, so we have to reserve judgement until we have
102             // a file to deal with, or we can definitely say this is an exclusion...
103             return true;
104         }
105         else
106         {
107             String altStr = source.replace( '\\', '/' );
108 
109             return SelectorUtils.matchAntPathPatternStart( this, str, File.separator, isCaseSensitive )
110                 || SelectorUtils.matchAntPathPatternStart( this, altStr, "/", isCaseSensitive );
111         }
112     }
113 
114     /**
115      * @return Tokenized string.
116      */
117     public String[] getTokenizedPathString()
118     {
119         return tokenized;
120     }
121 
122 
123     /**
124      * @param string The part which will be checked to start with.
125      * @return true in case of starting with the string false otherwise.
126      */
127     public boolean startsWith( String string )
128     {
129         return source.startsWith( string );
130     }
131 
132 
133     static String[] tokenizePathToString( @Nonnull String path, @Nonnull String separator )
134     {
135         List<String> ret = new ArrayList<String>();
136         StringTokenizer st = new StringTokenizer( path, separator );
137         while ( st.hasMoreTokens() )
138         {
139             ret.add( st.nextToken() );
140         }
141         return ret.toArray( new String[ret.size()] );
142     }
143 
144     /**
145      * @param source The source.
146      * @return The match pattern.
147      */
148     public static MatchPattern fromString( String source )
149     {
150         return new MatchPattern( source, File.separator );
151     }
152 
153 }