001    /**
002     * Copyright 2004-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.torque.util;
017    
018    import java.io.File;
019    import java.util.ArrayList;
020    import java.util.Collections;
021    import java.util.List;
022    
023    import org.codehaus.plexus.util.DirectoryScanner;
024    
025    /**
026     * This class provides a simple method for scanning a directory for files that match include/exclude patterns
027     */
028    public class SimpleScanner extends DirectoryScanner {
029            private static final String FS = System.getProperty("file.separator");
030    
031            public SimpleScanner() {
032                    this(null, getArray(null), getArray(null));
033            }
034    
035            public SimpleScanner(File baseDir, String include, String exclude) {
036                    this(baseDir, getArray(include), getArray(exclude));
037            }
038    
039            public SimpleScanner(File baseDir, String[] includes, String[] excludes) {
040                    super();
041                    setBasedir(baseDir);
042                    setIncludes(includes);
043                    setExcludes(excludes);
044            }
045    
046            protected static final String[] getArray(String s) {
047                    if (s == null) {
048                            return null;
049                    } else {
050                            return new String[] { s };
051                    }
052            }
053    
054            /**
055             * This method returns a sorted list of files that match an include pattern but do not match an exclude pattern
056             */
057            public List<File> getFiles() {
058                    scan();
059                    String[] includedFiles = getIncludedFiles();
060                    List<File> files = new ArrayList<File>();
061                    for (String includedFile : includedFiles) {
062                            String filename = getBasedir().getAbsolutePath() + FS + includedFile;
063                            File file = new File(filename);
064                            files.add(file);
065                    }
066                    Collections.sort(files);
067                    return files;
068            }
069    }