001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.pipeline.stage;
019
020 import java.io.File;
021 import java.util.regex.Pattern;
022 import org.apache.commons.logging.Log;
023 import org.apache.commons.logging.LogFactory;
024 import org.apache.commons.pipeline.StageException;
025 import org.apache.commons.pipeline.validation.ConsumedTypes;
026 import org.apache.commons.pipeline.validation.ProducedTypes;
027
028 /**
029 * <p>This {@link org.apache.commons.pipeline.Pipeline$Stage Stage} is used
030 * to recursively find (non-directory) files that match the specified regex.</p>
031 *
032 * <p>File elements in the stage's queue will be recursively searched with the
033 * resulting File objects placed on the subsequent stage's queue.</p>
034 */
035 @ConsumedTypes({String.class, File.class})
036 @ProducedTypes(File.class)
037 public class FileFinderStage extends BaseStage {
038 private final Log log = LogFactory.getLog(FileFinderStage.class);
039 private String filePattern = ".*";
040 Pattern pattern;
041
042 /** Creates a new instance of FileFinder */
043 public FileFinderStage() { }
044
045 /**
046 * Precompiles the regex pattern for matching against filenames
047 */
048 public void preprocess() throws StageException {
049 super.preprocess();
050 this.pattern = Pattern.compile(this.filePattern);
051 }
052
053 /**
054 * This method inspects a File object to determine if
055 * it matches this FileFinder's filePattern property. If the File
056 * represents a directory, it recursively searches that directory and
057 * all subdirectories for matching files. Matched files are placed
058 * on the next stage's queue.
059 */
060 public void process(Object obj) {
061 File file = (obj instanceof String) ? new File((String) obj) : (File) obj;
062 log.debug("Examining file " + file.getAbsolutePath());
063
064 if (!file.exists()) {
065 log.info("File " + file + " does not exist.");
066 } else if (file.isDirectory()) {
067 File[] files = file.listFiles();
068 log.debug(file.getName() + " is a directory, processing " + files.length + " files within.");
069 for (int i = 0; i < files.length; i++) {
070 process(files[i]);
071 }
072 } else if (this.pattern.matcher(file.getName()).matches()){
073 this.emit(file);
074 }
075 }
076
077 /** Getter for property filePattern.
078 * @return Value of property filePattern.
079 *
080 */
081 public String getFilePattern() {
082 return this.filePattern;
083 }
084
085 /** Setter for property filePattern.
086 * @param pattern Value of property filePattern.
087 *
088 */
089 public void setFilePattern(String pattern) {
090 this.filePattern = pattern;
091 }
092 }