001package org.jadira.scanner.file.projector;
002
003import java.io.File;
004import java.util.ArrayList;
005import java.util.List;
006
007import org.jadira.scanner.core.api.Projector;
008import org.jadira.scanner.core.exception.FileAccessException;
009import org.jadira.scanner.core.helper.FileUtils;
010import org.jadira.scanner.core.helper.filenamefilter.AntPathFilter;
011
012public class AntPathProjector implements Projector<File> {
013
014        private String path;
015        
016        public AntPathProjector(String path) {
017                this.path = path;
018        }
019        
020        @Override
021        public List<File> project(File segment) {
022
023                final List<File> files;
024                
025                AntPathFilter antPathMatcher = new AntPathFilter(path);
026                if (antPathMatcher.isPatterned()) {
027                        files = findFilesForPatternPath(segment, path);
028                } else {
029                        files = findFilesForActualPath(segment, path);
030                }
031                return files;
032        }
033
034        private List<File> findFilesForPatternPath(File parentFile, String pattern) throws FileAccessException {
035
036                final List<File> files = new ArrayList<File>();
037
038                AntPathFilter antPathMatcher = new AntPathFilter(pattern);
039                if (antPathMatcher.match(AntPathFilter.PATH_SEPARATOR) || antPathMatcher.match("")) {
040                        files.add(parentFile);
041                } else {
042                        findFilesForPatternRecursively(pattern, files, parentFile, parentFile);
043                }
044                return files;
045        }
046
047        private List<File> findFilesForActualPath(File parentFile, String path) {
048
049                final List<File> files = new ArrayList<File>();
050                File nextFile = FileUtils.getFileForPathName(path, parentFile);
051                if ((nextFile != null) && nextFile.isFile()) {
052                        files.add(nextFile);
053                }
054                return files;
055        }
056
057        private void findFilesForPatternRecursively(String pattern, final List<File> resultsHolder, File root, File currentParent) {
058
059                if (currentParent.isDirectory()) {
060                        File[] childFiles = currentParent.listFiles();
061                        for (File next : childFiles) {
062                                String currentPath = next.getPath().substring(root.getPath().length());
063                                if (next.isDirectory() && (!currentPath.endsWith(AntPathFilter.PATH_SEPARATOR))) {
064                                        currentPath = currentPath + AntPathFilter.PATH_SEPARATOR;
065                                }
066                                AntPathFilter antPathMatcher = new AntPathFilter(pattern);
067                                if (antPathMatcher.match(currentPath)) {
068                                        resultsHolder.add(next);
069                                } else if (antPathMatcher.matchStart(currentPath)) {
070                                        findFilesForPatternRecursively(pattern, resultsHolder, root, currentParent);
071                                }
072                        }
073                }
074        }
075}