Coverage Report - org.apache.maven.shared.utils.io.MatchPattern
 
Classes in this File Line Coverage Branch Coverage Complexity
MatchPattern
87%
21/24
50%
8/16
2.25
 
 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  167
     {
 48  167
         regexPattern = SelectorUtils.isRegexPrefixedPattern( source ) ? source.substring(
 49  
             SelectorUtils.REGEX_HANDLER_PREFIX.length(),
 50  
             source.length() - SelectorUtils.PATTERN_HANDLER_SUFFIX.length() ) : null;
 51  167
         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  167
         this.separator = separator;
 57  167
         tokenized = tokenizePathToString( this.source, separator );
 58  167
     }
 59  
 
 60  
 
 61  
     public boolean matchPath( String str, boolean isCaseSensitive )
 62  
     {
 63  1
         if ( regexPattern != null )
 64  
         {
 65  0
             return str.matches( regexPattern );
 66  
         }
 67  
         else
 68  
         {
 69  1
             return SelectorUtils.matchAntPathPattern( this, str, separator, isCaseSensitive );
 70  
         }
 71  
     }
 72  
 
 73  
     boolean matchPath( String str, String[] strDirs, boolean isCaseSensitive )
 74  
     {
 75  851
         if ( regexPattern != null )
 76  
         {
 77  0
             return str.matches( regexPattern );
 78  
         }
 79  
         else
 80  
         {
 81  851
             return SelectorUtils.matchAntPathPattern( getTokenizedPathString(), strDirs, isCaseSensitive );
 82  
         }
 83  
     }
 84  
 
 85  
     public boolean matchPatternStart( @Nonnull String str, boolean isCaseSensitive )
 86  
     {
 87  8
         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  0
             return true;
 92  
         }
 93  
         else
 94  
         {
 95  8
             String altStr = source.replace( '\\', '/' );
 96  
 
 97  8
             return SelectorUtils.matchAntPathPatternStart( this, str, File.separator, isCaseSensitive )
 98  
                 || SelectorUtils.matchAntPathPatternStart( this, altStr, "/", isCaseSensitive );
 99  
         }
 100  
     }
 101  
 
 102  
     public String[] getTokenizedPathString()
 103  
     {
 104  860
         return tokenized;
 105  
     }
 106  
 
 107  
 
 108  
     public boolean startsWith( String string )
 109  
     {
 110  9
         return source.startsWith( string );
 111  
     }
 112  
 
 113  
 
 114  
     static String[] tokenizePathToString( @Nonnull String path, @Nonnull String separator )
 115  
     {
 116  290
         List<String> ret = new ArrayList<String>();
 117  290
         StringTokenizer st = new StringTokenizer( path, separator );
 118  814
         while ( st.hasMoreTokens() )
 119  
         {
 120  524
             ret.add( st.nextToken() );
 121  
         }
 122  290
         return ret.toArray( new String[ret.size()] );
 123  
     }
 124  
 
 125  
     public static MatchPattern fromString( String source )
 126  
     {
 127  167
         return new MatchPattern( source, File.separator );
 128  
     }
 129  
 
 130  
 }