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