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 this( includes, excludes, false );
46 }
47
48 public GlobSelector( String[] includes, String[] excludes, boolean useDefaultExcludes )
49 {
50 this.str = "includes = " + toString( includes ) + ", excludes = " + toString( excludes );
51 this.includes = normalizePatterns( includes );
52 this.excludes = normalizePatterns( addDefaultExcludes( excludes, useDefaultExcludes ) );
53 }
54
55 private static String toString( String[] patterns )
56 {
57 return ( patterns == null ) ? "[]" : Arrays.asList( patterns ).toString();
58 }
59
60 private static String[] addDefaultExcludes( String[] excludes, boolean useDefaultExcludes )
61 {
62 String[] defaults = DirectoryScanner.DEFAULTEXCLUDES;
63 if ( !useDefaultExcludes )
64 {
65 return excludes;
66 }
67 else if ( excludes == null || excludes.length <= 0 )
68 {
69 return defaults;
70 }
71 else
72 {
73 String[] patterns = new String[excludes.length + defaults.length];
74 System.arraycopy( excludes, 0, patterns, 0, excludes.length );
75 System.arraycopy( defaults, 0, patterns, excludes.length, defaults.length );
76 return patterns;
77 }
78 }
79
80 private static String[] normalizePatterns( String[] patterns )
81 {
82 String[] normalized;
83
84 if ( patterns != null )
85 {
86 normalized = new String[patterns.length];
87 for ( int i = patterns.length - 1; i >= 0; i-- )
88 {
89 normalized[i] = normalizePattern( patterns[i] );
90 }
91 }
92 else
93 {
94 normalized = new String[0];
95 }
96
97 return normalized;
98 }
99
100 private static String normalizePattern( String pattern )
101 {
102 String normalized = pattern.replace( ( File.separatorChar == '/' ) ? '\\' : '/', File.separatorChar );
103
104 if ( normalized.endsWith( File.separator ) )
105 {
106 normalized += "**";
107 }
108
109 return normalized;
110 }
111
112 public boolean isSelected( String pathname )
113 {
114 return ( includes.length <= 0 || isMatched( pathname, includes ) )
115 && ( excludes.length <= 0 || !isMatched( pathname, excludes ) );
116 }
117
118 private static boolean isMatched( String pathname, String[] patterns )
119 {
120 for ( int i = patterns.length - 1; i >= 0; i-- )
121 {
122 String pattern = patterns[i];
123 if ( SelectorUtils.matchPath( pattern, pathname ) )
124 {
125 return true;
126 }
127 }
128 return false;
129 }
130
131 public boolean couldHoldSelected( String pathname )
132 {
133 for ( int i = includes.length - 1; i >= 0; i-- )
134 {
135 String include = includes[i];
136 if ( SelectorUtils.matchPatternStart( include, pathname ) )
137 {
138 return true;
139 }
140 }
141 return includes.length <= 0;
142 }
143
144 public String toString()
145 {
146 return str;
147 }
148
149 }