Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FileFinderStage |
|
| 0.0;0 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
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 | * <p>This {@link org.apache.commons.pipeline.Pipeline$Stage Stage} is used | |
30 | * to recursively find (non-directory) files that match the specified regex.</p> | |
31 | * | |
32 | * <p>File elements in the stage's queue will be recursively searched with the | |
33 | * resulting File objects placed on the subsequent stage's queue.</p> | |
34 | */ | |
35 | @ConsumedTypes({String.class, File.class}) | |
36 | @ProducedTypes(File.class) | |
37 | public class FileFinderStage extends BaseStage { | |
38 | 1 | private final Log log = LogFactory.getLog(FileFinderStage.class); |
39 | 1 | private String filePattern = ".*"; |
40 | Pattern pattern; | |
41 | ||
42 | /** Creates a new instance of FileFinder */ | |
43 | 2 | public FileFinderStage() { } |
44 | ||
45 | /** | |
46 | * Precompiles the regex pattern for matching against filenames | |
47 | */ | |
48 | public void preprocess() throws StageException { | |
49 | 1 | super.preprocess(); |
50 | 1 | this.pattern = Pattern.compile(this.filePattern); |
51 | 1 | } |
52 | ||
53 | /** | |
54 | * This method inspects a File object to determine if | |
55 | * it matches this FileFinder's filePattern property. If the File | |
56 | * represents a directory, it recursively searches that directory and | |
57 | * all subdirectories for matching files. Matched files are placed | |
58 | * on the next stage's queue. | |
59 | */ | |
60 | public void process(Object obj) { | |
61 | 531 | File file = (obj instanceof String) ? new File((String) obj) : (File) obj; |
62 | 531 | log.debug("Examining file " + file.getAbsolutePath()); |
63 | ||
64 | 531 | if (!file.exists()) { |
65 | 0 | log.info("File " + file + " does not exist."); |
66 | 531 | } else if (file.isDirectory()) { |
67 | 198 | File[] files = file.listFiles(); |
68 | 198 | log.debug(file.getName() + " is a directory, processing " + files.length + " files within."); |
69 | 727 | for (int i = 0; i < files.length; i++) { |
70 | 529 | process(files[i]); |
71 | } | |
72 | 198 | } else if (this.pattern.matcher(file.getName()).matches()){ |
73 | 80 | this.emit(file); |
74 | } | |
75 | 531 | } |
76 | ||
77 | /** Getter for property filePattern. | |
78 | * @return Value of property filePattern. | |
79 | * | |
80 | */ | |
81 | public String getFilePattern() { | |
82 | 0 | return this.filePattern; |
83 | } | |
84 | ||
85 | /** Setter for property filePattern. | |
86 | * @param pattern Value of property filePattern. | |
87 | * | |
88 | */ | |
89 | public void setFilePattern(String pattern) { | |
90 | 1 | this.filePattern = pattern; |
91 | 1 | } |
92 | } |