001    /**
002     * Copyright 2011-2012 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    
017    package org.apache.torque.util;
018    
019    import java.io.File;
020    import java.util.ArrayList;
021    import java.util.Collections;
022    import java.util.List;
023    
024    import org.codehaus.plexus.util.DirectoryScanner;
025    
026    /**
027     * This class provides a simple method for scanning a directory for files that match include/exclude patterns
028     */
029    public class SimpleScanner extends DirectoryScanner {
030            private static final String FS = System.getProperty("file.separator");
031    
032            public SimpleScanner() {
033                    this(null, getArray(null), getArray(null));
034            }
035    
036            public SimpleScanner(File baseDir, String include, String exclude) {
037                    this(baseDir, getArray(include), getArray(exclude));
038            }
039    
040            public SimpleScanner(File baseDir, String[] includes, String[] excludes) {
041                    super();
042                    setBasedir(baseDir);
043                    setIncludes(includes);
044                    setExcludes(excludes);
045            }
046    
047            protected static final String[] getArray(String s) {
048                    if (s == null) {
049                            return null;
050                    } else {
051                            return new String[] { s };
052                    }
053            }
054    
055            /**
056             * This method returns a sorted list of files that match an include pattern but do not match an exclude pattern
057             */
058            public List<File> getFiles() {
059                    scan();
060                    String[] includedFiles = getIncludedFiles();
061                    List<File> files = new ArrayList<File>();
062                    for (String includedFile : includedFiles) {
063                            String filename = getBasedir().getAbsolutePath() + FS + includedFile;
064                            File file = new File(filename);
065                            files.add(file);
066                    }
067                    Collections.sort(files);
068                    return files;
069            }
070    }