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