| 1 | |
package org.apache.maven.plugin.clean; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 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 | |
|
| 30 | |
|
| 31 | |
|
| 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 | 2 | this( includes, excludes, false ); |
| 46 | 2 | } |
| 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 | |
} |