View Javadoc

1   /**
2    * Copyright 2004-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.List;
21  
22  import org.codehaus.plexus.util.DirectoryScanner;
23  
24  /**
25   * This class provides a simple method for scanning a directory for files that match include/exclude patterns
26   */
27  public class SimpleScanner extends DirectoryScanner {
28  	private static final String FS = File.separator;
29  
30  	public SimpleScanner() {
31  		this((File) null, (String) null, (String) null);
32  	}
33  
34  	public SimpleScanner(File baseDir, String include, String exclude) {
35  		this(baseDir, CollectionUtils.toEmptyList(include), CollectionUtils.toEmptyList(exclude));
36  	}
37  
38  	public SimpleScanner(File baseDir, List<String> includes, List<String> excludes) {
39  		super();
40  		if (baseDir != null) {
41  			setBasedir(baseDir);
42  		}
43  		if (!CollectionUtils.isEmpty(includes)) {
44  			setIncludes(CollectionUtils.toStringArray(includes));
45  		}
46  		if (!CollectionUtils.isEmpty(excludes)) {
47  			setExcludes(CollectionUtils.toStringArray(excludes));
48  		}
49  	}
50  
51  	/**
52  	 * This method scans the file system starting at <code>basedir</code> and returns files matching the provided include/exclude patterns
53  	 */
54  	public List<File> getFiles() {
55  		scan();
56  		String[] includedFiles = getIncludedFiles();
57  		List<File> files = new ArrayList<File>();
58  		for (String includedFile : includedFiles) {
59  			String filename = getBasedir().getAbsolutePath() + FS + includedFile;
60  			File file = new File(filename);
61  			files.add(file);
62  		}
63  		return files;
64  	}
65  }