1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.pipeline.stage;
19
20 import java.io.File;
21 import java.util.regex.Pattern;
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.commons.pipeline.StageException;
25 import org.apache.commons.pipeline.validation.ConsumedTypes;
26 import org.apache.commons.pipeline.validation.ProducedTypes;
27
28
29
30
31
32
33
34
35 @ConsumedTypes({String.class, File.class})
36 @ProducedTypes(File.class)
37 public class FileFinderStage extends BaseStage {
38 private final Log log = LogFactory.getLog(FileFinderStage.class);
39 private String filePattern = ".*";
40 Pattern pattern;
41
42
43 public FileFinderStage() { }
44
45
46
47
48 public void preprocess() throws StageException {
49 super.preprocess();
50 this.pattern = Pattern.compile(this.filePattern);
51 }
52
53
54
55
56
57
58
59
60 public void process(Object obj) {
61 File file = (obj instanceof String) ? new File((String) obj) : (File) obj;
62 log.debug("Examining file " + file.getAbsolutePath());
63
64 if (!file.exists()) {
65 log.info("File " + file + " does not exist.");
66 } else if (file.isDirectory()) {
67 File[] files = file.listFiles();
68 log.debug(file.getName() + " is a directory, processing " + files.length + " files within.");
69 for (int i = 0; i < files.length; i++) {
70 process(files[i]);
71 }
72 } else if (this.pattern.matcher(file.getName()).matches()){
73 this.emit(file);
74 }
75 }
76
77
78
79
80
81 public String getFilePattern() {
82 return this.filePattern;
83 }
84
85
86
87
88
89 public void setFilePattern(String pattern) {
90 this.filePattern = pattern;
91 }
92 }