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   * Describes a match target for SelectorUtils.
26   * <p/>
27   * Significantly more efficient than using strings, since re-evaluation and re-tokenizing is avoided.
28   *
29   * @author Kristian Rosenvold
30   */
31  public class MatchPattern
32  {
33      private final String source;
34  
35      private final String regexPattern;
36  
37      private final String separator;
38  
39      private final String[] tokenized;
40  
41      private final char[][] tokenizedChar;
42  
43      private MatchPattern( String source, String separator )
44      {
45          regexPattern = SelectorUtils.isRegexPrefixedPattern( source )
46                          ? source.substring( SelectorUtils.REGEX_HANDLER_PREFIX.length(),
47                                              source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length() )
48                          : null;
49          this.source = SelectorUtils.isAntPrefixedPattern( source )
50                          ? source.substring( 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          {
58              tokenizedChar[i] = tokenized[i].toCharArray();
59          }
60  
61      }
62  
63      public boolean matchPath( String str, boolean isCaseSensitive )
64      {
65          if ( regexPattern != null )
66          {
67              return str.matches( regexPattern );
68          }
69          else
70          {
71              return SelectorUtils.matchAntPathPattern( this, str, separator, isCaseSensitive );
72          }
73      }
74  
75      boolean matchPath( String str, char[][] strDirs, boolean isCaseSensitive )
76      {
77          if ( regexPattern != null )
78          {
79              return str.matches( regexPattern );
80          }
81          else
82          {
83              return SelectorUtils.matchAntPathPattern( getTokenizedPathChars(), strDirs, isCaseSensitive );
84          }
85      }
86  
87      public boolean matchPatternStart( String str, boolean isCaseSensitive )
88      {
89          if ( regexPattern != null )
90          {
91              // FIXME: ICK! But we can't do partial matches for regex, so we have to reserve judgement until we have
92              // a file to deal with, or we can definitely say this is an exclusion...
93              return true;
94          }
95          else
96          {
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     public String[] getTokenizedPathString()
105     {
106         return tokenized;
107     }
108 
109     public char[][] getTokenizedPathChars()
110     {
111         return tokenizedChar;
112     }
113 
114     public boolean startsWith( String string )
115     {
116         return source.startsWith( string );
117     }
118 
119     static String[] tokenizePathToString( String path, String separator )
120     {
121         List<String> ret = new ArrayList<String>();
122         StringTokenizer st = new StringTokenizer( path, separator );
123         while ( st.hasMoreTokens() )
124         {
125             ret.add( st.nextToken() );
126         }
127         return ret.toArray( new String[ret.size()] );
128     }
129 
130     public static MatchPattern fromString( String source )
131     {
132         return new MatchPattern( source, File.separator );
133     }
134 
135 }