View Javadoc

1   /**
2    * Copyright 2011-2012 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  
17  package org.apache.torque.util;
18  
19  import java.io.File;
20  import java.util.ArrayList;
21  import java.util.Collections;
22  import java.util.List;
23  
24  import org.codehaus.plexus.util.DirectoryScanner;
25  
26  /**
27   * This class provides a simple method for scanning a directory for files that match include/exclude patterns
28   */
29  public class SimpleScanner extends DirectoryScanner {
30  	private static final String FS = System.getProperty("file.separator");
31  
32  	public SimpleScanner() {
33  		this(null, getArray(null), getArray(null));
34  	}
35  
36  	public SimpleScanner(File baseDir, String include, String exclude) {
37  		this(baseDir, getArray(include), getArray(exclude));
38  	}
39  
40  	public SimpleScanner(File baseDir, String[] includes, String[] excludes) {
41  		super();
42  		setBasedir(baseDir);
43  		setIncludes(includes);
44  		setExcludes(excludes);
45  	}
46  
47  	protected static final String[] getArray(String s) {
48  		if (s == null) {
49  			return null;
50  		} else {
51  			return new String[] { s };
52  		}
53  	}
54  
55  	/**
56  	 * This method returns a sorted list of files that match an include pattern but do not match an exclude pattern
57  	 */
58  	public List<File> getFiles() {
59  		scan();
60  		String[] includedFiles = getIncludedFiles();
61  		List<File> files = new ArrayList<File>();
62  		for (String includedFile : includedFiles) {
63  			String filename = getBasedir().getAbsolutePath() + FS + includedFile;
64  			File file = new File(filename);
65  			files.add(file);
66  		}
67  		Collections.sort(files);
68  		return files;
69  	}
70  }