001/**
002 * Copyright 2010-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 */
016package org.kuali.common.util;
017
018import java.io.File;
019import java.util.ArrayList;
020import java.util.List;
021
022import org.codehaus.plexus.util.DirectoryScanner;
023
024/**
025 * This class provides a simple method for scanning a directory for files that match include/exclude patterns
026 */
027public class SimpleScanner extends DirectoryScanner {
028
029        private static final String FS = File.separator;
030
031        public SimpleScanner() {
032                this((File) null, (String) null, (String) null);
033        }
034
035        public SimpleScanner(File baseDir, String include, String exclude) {
036                this(baseDir, CollectionUtils.toEmptyList(include), CollectionUtils.toEmptyList(exclude));
037        }
038
039        public SimpleScanner(File baseDir, List<String> includes, List<String> excludes) {
040                if (baseDir != null) {
041                        setBasedir(baseDir);
042                }
043                if (!CollectionUtils.isEmpty(includes)) {
044                        setIncludes(CollectionUtils.toStringArray(includes));
045                }
046                if (!CollectionUtils.isEmpty(excludes)) {
047                        setExcludes(CollectionUtils.toStringArray(excludes));
048                }
049        }
050
051        /**
052         * This method scans the file system starting at <code>basedir</code> and returns files matching the provided include/exclude patterns
053         */
054        public List<File> getFiles() {
055                scan();
056                String[] includedFiles = getIncludedFiles();
057                List<File> files = new ArrayList<File>();
058                for (String includedFile : includedFiles) {
059                        String filename = getBasedir().getAbsolutePath() + FS + includedFile;
060                        File file = new File(filename);
061                        files.add(file);
062                }
063                return files;
064        }
065
066    /**
067     * This method scans the file system starting at <code>basedir</code> and returns directory names matching the provided include/exclude patterns
068     */
069    public List<String> getDirectories() {
070        scan();
071        String[] includedDirectories = getIncludedDirectories();
072        List<String> directories = new ArrayList<String>();
073        for (String includedDirectory : includedDirectories) {
074            directories.add(includedDirectory);
075        }
076        return directories;
077    }
078}