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.kuali.common.util;
017
018 import java.io.File;
019 import java.util.ArrayList;
020 import java.util.List;
021
022 import 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 */
027 public class SimpleScanner extends DirectoryScanner {
028 private static final String FS = File.separator;
029
030 public SimpleScanner() {
031 this((File) null, (String) null, (String) null);
032 }
033
034 public SimpleScanner(File baseDir, String include, String exclude) {
035 this(baseDir, CollectionUtils.toEmptyList(include), CollectionUtils.toEmptyList(exclude));
036 }
037
038 public SimpleScanner(File baseDir, List<String> includes, List<String> excludes) {
039 super();
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 }