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