1 package org.codehaus.mojo.wagon.shared;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Iterator;
21 import java.util.List;
22
23 import org.apache.maven.plugin.logging.Log;
24 import org.apache.maven.wagon.Wagon;
25 import org.apache.maven.wagon.WagonException;
26 import org.codehaus.plexus.util.StringUtils;
27
28 public class WagonDirectoryScanner {
29 private final static String[] NOT_DIRECTORIES = new String[] { ".jar", ".zip", ".md5", ".sha1", ".pom", ".xml",
30 ".war" };
31
32
33
34
35
36 public static final String[] DEFAULTEXCLUDES = org.codehaus.plexus.util.DirectoryScanner.DEFAULTEXCLUDES;
37
38
39
40
41 private Wagon wagon;
42
43
44
45
46 private String directory;
47
48
49 private String[] includes;
50
51
52 private String[] excludes;
53
54
55
56
57 private boolean isCaseSensitive = true;
58
59
60
61
62 private List<String> filesIncluded = new ArrayList<String>();
63
64 private Log logger;
65
66
67
68
69
70
71
72
73
74
75
76 public void setIncludes(String[] includes) {
77 if (includes == null) {
78 this.includes = null;
79 } else {
80 this.includes = new String[includes.length];
81 for (int i = 0; i < includes.length; i++) {
82 String pattern = includes[i].trim();
83
84 if (pattern.endsWith("/")) {
85 pattern += "**";
86 }
87 this.includes[i] = pattern;
88 }
89 }
90 }
91
92
93
94
95
96
97
98
99
100
101 public void setExcludes(String[] excludes) {
102 if (excludes == null) {
103 this.excludes = null;
104 } else {
105 this.excludes = new String[excludes.length];
106 for (int i = 0; i < excludes.length; i++) {
107 String pattern = excludes[i].trim();
108
109 if (pattern.endsWith("/")) {
110 pattern += "**";
111 }
112 this.excludes[i] = pattern;
113 }
114 }
115 }
116
117
118
119
120
121
122
123
124
125 private boolean isIncluded(String name) {
126 for (int i = 0; i < includes.length; i++) {
127 if (matchPath(includes[i], name, isCaseSensitive)) {
128 return true;
129 }
130 }
131 return false;
132 }
133
134
135
136
137
138
139
140
141
142 protected boolean isExcluded(String name) {
143 for (int i = 0; i < excludes.length; i++) {
144 if (matchPath(excludes[i], name, isCaseSensitive)) {
145 return true;
146 }
147 }
148 return false;
149 }
150
151
152
153
154
155
156
157
158
159 protected boolean couldHoldIncluded(String name) {
160 for (int i = 0; i < includes.length; i++) {
161 if (matchPatternStart(includes[i], name, isCaseSensitive)) {
162 return true;
163 }
164 }
165 return false;
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183 protected static boolean matchPatternStart(String pattern, String str, boolean isCaseSensitive) {
184 return SelectorUtils.matchPatternStart(pattern, str, isCaseSensitive);
185 }
186
187
188
189
190
191
192
193
194
195
196
197
198
199 private static boolean matchPath(String pattern, String str, boolean isCaseSensitive) {
200 return SelectorUtils.matchPath(pattern, str, isCaseSensitive);
201 }
202
203 public void scan() throws WagonException {
204 if (wagon == null) {
205 throw new IllegalStateException("No wagon set");
206 }
207
208 if (StringUtils.isBlank(directory)) {
209 directory = "";
210 }
211
212 if (includes == null) {
213
214 includes = new String[1];
215 includes[0] = "**";
216 }
217
218 if (excludes == null) {
219 excludes = new String[0];
220 }
221
222 filesIncluded = new ArrayList<String>();
223
224 scandir(directory, "");
225
226 Collections.sort(filesIncluded);
227
228 }
229
230
231
232
233 public void addDefaultExcludes() {
234 int excludesLength = excludes == null ? 0 : excludes.length;
235 String[] newExcludes;
236 newExcludes = new String[excludesLength + DEFAULTEXCLUDES.length];
237 if (excludesLength > 0) {
238 System.arraycopy(excludes, 0, newExcludes, 0, excludesLength);
239 }
240 for (int i = 0; i < DEFAULTEXCLUDES.length; i++) {
241 newExcludes[i + excludesLength] = DEFAULTEXCLUDES[i];
242 }
243 excludes = newExcludes;
244 }
245
246
247
248
249
250
251
252
253
254
255 private boolean isRidiculousFile(String fileName) {
256 return fileName.endsWith(".") || fileName.contains("*") || fileName.startsWith("?") || fileName.startsWith("#");
257 }
258
259
260
261
262
263
264
265
266
267
268 private void scandir(String dir, String vpath) throws WagonException {
269 logger.debug("scandir: dir: " + dir + " vpath: " + vpath);
270 List<?> files = wagon.getFileList(dir);
271
272 for (Iterator<?> iterator = files.iterator(); iterator.hasNext();) {
273 String fileName = (String) iterator.next();
274
275 if (isRidiculousFile(fileName)) {
276 continue;
277 }
278
279 String file = fileName;
280
281 if (!StringUtils.isBlank(dir)) {
282 if (dir.endsWith("/")) {
283 file = dir + fileName;
284 } else {
285 file = dir + "/" + fileName;
286 }
287 }
288
289 String name = vpath + fileName;
290
291 if (this.isDirectory(file)) {
292
293 if (!name.endsWith("/")) {
294 name += "/";
295 }
296
297 if (isIncluded(name)) {
298 if (!isExcluded(name)) {
299 scandir(file, name);
300 } else {
301 if (couldHoldIncluded(name)) {
302 scandir(file, name);
303 }
304 }
305 } else {
306 if (couldHoldIncluded(name)) {
307 scandir(file, name);
308 }
309 }
310
311 } else {
312
313 if (isIncluded(name)) {
314 if (!isExcluded(name)) {
315 filesIncluded.add(name);
316 }
317 }
318 }
319 }
320 }
321
322 private boolean isDirectory(String existedRemotePath) throws WagonException {
323 for (int x = 0; x < NOT_DIRECTORIES.length; x++) {
324 if (existedRemotePath.endsWith(NOT_DIRECTORIES[x])) {
325 return false;
326 }
327 }
328 if (existedRemotePath.endsWith("/")) {
329 return true;
330 }
331
332 return wagon.resourceExists(existedRemotePath + "/");
333 }
334
335
336 public List<String> getFilesIncluded() {
337 return filesIncluded;
338 }
339
340 public void setWagon(Wagon wagon) {
341 this.wagon = wagon;
342 }
343
344 public void setCaseSensitive(boolean isCaseSensitive) {
345 this.isCaseSensitive = isCaseSensitive;
346 }
347
348 public void setDirectory(String basePath) {
349 this.directory = basePath;
350 }
351
352 public Log getLogger() {
353 return logger;
354 }
355
356 public void setLogger(Log logger) {
357 this.logger = logger;
358 }
359
360 }