1 package org.codehaus.plexus.components.io.fileselectors;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import javax.annotation.Nonnull;
20 import javax.annotation.Nullable;
21 import javax.inject.Named;
22
23 import java.io.File;
24
25 import org.codehaus.plexus.util.FileUtils;
26 import org.codehaus.plexus.util.MatchPatterns;
27 import org.codehaus.plexus.util.SelectorUtils;
28
29
30
31
32
33 @Named(IncludeExcludeFileSelector.ROLE_HINT)
34 public class IncludeExcludeFileSelector implements FileSelector {
35
36
37
38 public static final String ROLE_HINT = "standard";
39
40 private static final MatchPatterns ALL_INCLUDES = MatchPatterns.from(getCanonicalName("**/*"));
41
42 private static final MatchPatterns ZERO_EXCLUDES = MatchPatterns.from();
43
44 private boolean isCaseSensitive = true;
45
46 private boolean useDefaultExcludes = true;
47
48 private String[] includes;
49
50 private String[] excludes;
51
52 private MatchPatterns computedIncludes = ALL_INCLUDES;
53
54 private MatchPatterns computedExcludes = ZERO_EXCLUDES;
55
56
57
58
59
60
61
62
63
64 protected boolean isExcluded(@Nonnull String name) {
65 return computedExcludes.matches(name, isCaseSensitive);
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81 public void setIncludes(@Nullable String[] includes) {
82 this.includes = includes;
83 if (includes == null) {
84 computedIncludes = ALL_INCLUDES;
85 } else {
86 String[] cleaned;
87 cleaned = new String[includes.length];
88 for (int i = 0; i < includes.length; i++) {
89 cleaned[i] = asPattern(includes[i]);
90 }
91 computedIncludes = MatchPatterns.from(cleaned);
92 }
93 }
94
95 private static @Nonnull String getCanonicalName(@Nonnull String pName) {
96 return pName.replace('/', File.separatorChar).replace('\\', File.separatorChar);
97 }
98
99 private String asPattern(@Nonnull String pPattern) {
100 String pattern = getCanonicalName(pPattern.trim());
101 if (pattern.endsWith(File.separator)) {
102 pattern += "**";
103 }
104 return pattern;
105 }
106
107
108
109
110
111
112
113
114
115
116 public @Nullable String[] getIncludes() {
117 return includes;
118 }
119
120
121
122
123
124
125
126
127
128
129
130
131
132 public void setExcludes(@Nullable String[] excludes) {
133 this.excludes = excludes;
134 final String[] defaultExcludes = useDefaultExcludes ? FileUtils.getDefaultExcludes() : new String[] {};
135 if (excludes == null) {
136 computedExcludes = MatchPatterns.from(defaultExcludes);
137 } else {
138 String[] temp = new String[excludes.length + defaultExcludes.length];
139 for (int i = 0; i < excludes.length; i++) {
140 temp[i] = asPattern(excludes[i]);
141 }
142
143 if (defaultExcludes.length > 0) {
144 System.arraycopy(defaultExcludes, 0, temp, excludes.length, defaultExcludes.length);
145 }
146 computedExcludes = MatchPatterns.from(temp);
147 }
148 }
149
150
151
152
153
154
155
156
157
158 public @Nullable String[] getExcludes() {
159 return excludes;
160 }
161
162
163
164
165
166
167
168
169 protected boolean matchPath(@Nonnull String pattern, @Nonnull String name, boolean isCaseSensitive) {
170 return SelectorUtils.matchPath(pattern, name, isCaseSensitive);
171 }
172
173
174
175
176
177
178
179
180
181 protected boolean isIncluded(@Nonnull String name) {
182 return computedIncludes.matches(name, isCaseSensitive);
183 }
184
185 public boolean isSelected(@Nonnull FileInfo fileInfo) {
186 final String name = getCanonicalName(fileInfo.getName());
187 return isIncluded(name) && !isExcluded(name);
188 }
189
190
191
192
193
194 public boolean isCaseSensitive() {
195 return isCaseSensitive;
196 }
197
198
199
200
201
202 public void setCaseSensitive(boolean caseSensitive) {
203 isCaseSensitive = caseSensitive;
204 }
205
206
207
208
209
210 public boolean isUseDefaultExcludes() {
211 return useDefaultExcludes;
212 }
213
214
215
216
217
218 public void setUseDefaultExcludes(boolean pUseDefaultExcludes) {
219 useDefaultExcludes = pUseDefaultExcludes;
220 setExcludes(excludes);
221 }
222 }