Coverage Report - org.apache.maven.plugin.clean.GlobSelector
 
Classes in this File Line Coverage Branch Coverage Complexity
GlobSelector
73 %
30/41
58 %
21/36
3,1
 
 1  
 package org.apache.maven.plugin.clean;
 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.Arrays;
 24  
 
 25  
 import org.codehaus.plexus.util.DirectoryScanner;
 26  
 import org.codehaus.plexus.util.SelectorUtils;
 27  
 
 28  
 /**
 29  
  * Selects paths based on Ant-like glob patterns.
 30  
  * 
 31  
  * @author Benjamin Bentmann
 32  
  */
 33  
 class GlobSelector
 34  
     implements Selector
 35  
 {
 36  
 
 37  
     private final String[] includes;
 38  
 
 39  
     private final String[] excludes;
 40  
 
 41  
     private final String str;
 42  
 
 43  
     public GlobSelector( String[] includes, String[] excludes )
 44  
     {
 45  0
         this( includes, excludes, false );
 46  0
     }
 47  
 
 48  
     public GlobSelector( String[] includes, String[] excludes, boolean useDefaultExcludes )
 49  2
     {
 50  2
         this.str = "includes = " + toString( includes ) + ", excludes = " + toString( excludes );
 51  2
         this.includes = normalizePatterns( includes );
 52  2
         this.excludes = normalizePatterns( addDefaultExcludes( excludes, useDefaultExcludes ) );
 53  2
     }
 54  
 
 55  
     private static String toString( String[] patterns )
 56  
     {
 57  4
         return ( patterns == null ) ? "[]" : Arrays.asList( patterns ).toString();
 58  
     }
 59  
 
 60  
     private static String[] addDefaultExcludes( String[] excludes, boolean useDefaultExcludes )
 61  
     {
 62  2
         String[] defaults = DirectoryScanner.DEFAULTEXCLUDES;
 63  2
         if ( !useDefaultExcludes )
 64  
         {
 65  2
             return excludes;
 66  
         }
 67  0
         else if ( excludes == null || excludes.length <= 0 )
 68  
         {
 69  0
             return defaults;
 70  
         }
 71  
         else
 72  
         {
 73  0
             String[] patterns = new String[excludes.length + defaults.length];
 74  0
             System.arraycopy( excludes, 0, patterns, 0, excludes.length );
 75  0
             System.arraycopy( defaults, 0, patterns, excludes.length, defaults.length );
 76  0
             return patterns;
 77  
         }
 78  
     }
 79  
 
 80  
     private static String[] normalizePatterns( String[] patterns )
 81  
     {
 82  
         String[] normalized;
 83  
 
 84  4
         if ( patterns != null )
 85  
         {
 86  4
             normalized = new String[patterns.length];
 87  9
             for ( int i = patterns.length - 1; i >= 0; i-- )
 88  
             {
 89  5
                 normalized[i] = normalizePattern( patterns[i] );
 90  
             }
 91  
         }
 92  
         else
 93  
         {
 94  0
             normalized = new String[0];
 95  
         }
 96  
 
 97  4
         return normalized;
 98  
     }
 99  
 
 100  
     private static String normalizePattern( String pattern )
 101  
     {
 102  5
         if ( pattern == null )
 103  
         {
 104  1
             return "";
 105  
         }
 106  
 
 107  4
         String normalized = pattern.replace( ( File.separatorChar == '/' ) ? '\\' : '/', File.separatorChar );
 108  
 
 109  4
         if ( normalized.endsWith( File.separator ) )
 110  
         {
 111  0
             normalized += "**";
 112  
         }
 113  
 
 114  4
         return normalized;
 115  
     }
 116  
 
 117  
     public boolean isSelected( String pathname )
 118  
     {
 119  8
         return ( includes.length <= 0 || isMatched( pathname, includes ) )
 120  
             && ( excludes.length <= 0 || !isMatched( pathname, excludes ) );
 121  
     }
 122  
 
 123  
     private static boolean isMatched( String pathname, String[] patterns )
 124  
     {
 125  25
         for ( int i = patterns.length - 1; i >= 0; i-- )
 126  
         {
 127  19
             String pattern = patterns[i];
 128  19
             if ( SelectorUtils.matchPath( pattern, pathname ) )
 129  
             {
 130  9
                 return true;
 131  
             }
 132  
         }
 133  6
         return false;
 134  
     }
 135  
 
 136  
     public boolean couldHoldSelected( String pathname )
 137  
     {
 138  5
         for ( int i = includes.length - 1; i >= 0; i-- )
 139  
         {
 140  5
             String include = includes[i];
 141  5
             if ( SelectorUtils.matchPatternStart( include, pathname ) )
 142  
             {
 143  5
                 return true;
 144  
             }
 145  
         }
 146  0
         return includes.length <= 0;
 147  
     }
 148  
 
 149  
     public String toString()
 150  
     {
 151  2
         return str;
 152  
     }
 153  
 
 154  
 }