| 1 | |
package org.jbehave.core.io; |
| 2 | |
|
| 3 | |
import static java.util.Arrays.asList; |
| 4 | |
|
| 5 | |
import java.io.File; |
| 6 | |
import java.net.URL; |
| 7 | |
import java.util.ArrayList; |
| 8 | |
import java.util.Collections; |
| 9 | |
import java.util.Comparator; |
| 10 | |
import java.util.List; |
| 11 | |
|
| 12 | |
import org.apache.commons.collections.CollectionUtils; |
| 13 | |
import org.apache.commons.collections.Transformer; |
| 14 | |
import org.apache.commons.lang.StringUtils; |
| 15 | |
import org.codehaus.plexus.util.DirectoryScanner; |
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
public class StoryFinder { |
| 22 | |
|
| 23 | |
private static final String JAVA = ".java"; |
| 24 | |
private final String classNameExtension; |
| 25 | |
private final Comparator<? super String> sortingComparator; |
| 26 | |
|
| 27 | |
public StoryFinder() { |
| 28 | 11 | this(JAVA); |
| 29 | 11 | } |
| 30 | |
|
| 31 | |
public StoryFinder(String classNameExtension) { |
| 32 | 12 | this(classNameExtension, null); |
| 33 | 12 | } |
| 34 | |
|
| 35 | |
public StoryFinder(Comparator<? super String> sortingComparator) { |
| 36 | 1 | this(JAVA, sortingComparator); |
| 37 | 1 | } |
| 38 | |
|
| 39 | 13 | private StoryFinder(String classNameExtension, Comparator<? super String> sortingComparator) { |
| 40 | 13 | this.classNameExtension = classNameExtension; |
| 41 | 13 | this.sortingComparator = sortingComparator; |
| 42 | 13 | } |
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
public List<String> findClassNames(String searchInDirectory, List<String> includes, List<String> excludes) { |
| 57 | 4 | return classNames(normalise(sort(scan(searchInDirectory, includes, excludes)))); |
| 58 | |
} |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
public List<String> findPaths(URL searchInURL, String include, String exclude) { |
| 73 | 0 | return findPaths(searchInURL.getFile(), asList(include), asList(exclude)); |
| 74 | |
} |
| 75 | |
|
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
public List<String> findPaths(String searchInDirectory, List<String> includes, List<String> excludes) { |
| 89 | 4 | return normalise(sort(scan(searchInDirectory, includes, excludes))); |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
public List<String> findPaths(String searchInDirectory, List<String> includes, List<String> excludes, |
| 110 | |
String prefixWith) { |
| 111 | 2 | return normalise(prefix(prefixWith, sort(scan(searchInDirectory, includes, excludes)))); |
| 112 | |
} |
| 113 | |
|
| 114 | |
protected List<String> normalise(List<String> paths) { |
| 115 | 12 | List<String> transformed = new ArrayList<String>(paths); |
| 116 | 12 | CollectionUtils.transform(transformed, new Transformer() { |
| 117 | |
public Object transform(Object input) { |
| 118 | 105 | String path = (String) input; |
| 119 | 105 | return path.replace('\\', '/'); |
| 120 | |
} |
| 121 | |
}); |
| 122 | 12 | return transformed; |
| 123 | |
} |
| 124 | |
|
| 125 | |
protected List<String> prefix(final String prefixWith, List<String> paths) { |
| 126 | 2 | if (StringUtils.isBlank(prefixWith)) { |
| 127 | 1 | return paths; |
| 128 | |
} |
| 129 | 1 | List<String> transformed = new ArrayList<String>(paths); |
| 130 | 1 | CollectionUtils.transform(transformed, new Transformer() { |
| 131 | |
public Object transform(Object input) { |
| 132 | 4 | String path = (String) input; |
| 133 | 4 | return prefixWith + path; |
| 134 | |
} |
| 135 | |
}); |
| 136 | 1 | return transformed; |
| 137 | |
} |
| 138 | |
|
| 139 | |
protected List<String> classNames(List<String> paths) { |
| 140 | 4 | List<String> trasformed = new ArrayList<String>(paths); |
| 141 | 4 | CollectionUtils.transform(trasformed, new Transformer() { |
| 142 | |
public Object transform(Object input) { |
| 143 | 8 | String path = (String) input; |
| 144 | 8 | if (!StringUtils.endsWithIgnoreCase(path, classNameExtension())) { |
| 145 | 1 | return input; |
| 146 | |
} |
| 147 | 7 | return StringUtils.removeEndIgnoreCase(path, classNameExtension()).replace('/', '.'); |
| 148 | |
} |
| 149 | |
}); |
| 150 | 4 | return trasformed; |
| 151 | |
} |
| 152 | |
|
| 153 | |
protected String classNameExtension() { |
| 154 | 15 | return classNameExtension; |
| 155 | |
} |
| 156 | |
|
| 157 | |
protected List<String> sort(List<String> input) { |
| 158 | 10 | List<String> sorted = new ArrayList<String>(input); |
| 159 | 10 | Collections.sort(sorted, sortingComparator()); |
| 160 | 10 | return sorted; |
| 161 | |
} |
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
protected Comparator<? super String> sortingComparator() { |
| 170 | 10 | return sortingComparator; |
| 171 | |
} |
| 172 | |
|
| 173 | |
protected List<String> scan(String basedir, List<String> includes, List<String> excludes) { |
| 174 | 10 | DirectoryScanner scanner = new DirectoryScanner(); |
| 175 | 10 | if (!new File(basedir).exists()) { |
| 176 | 1 | return new ArrayList<String>(); |
| 177 | |
} |
| 178 | 9 | scanner.setBasedir(basedir); |
| 179 | 9 | if (includes != null) { |
| 180 | 8 | scanner.setIncludes(includes.toArray(new String[includes.size()])); |
| 181 | |
} |
| 182 | 9 | if (excludes != null) { |
| 183 | 8 | scanner.setExcludes(excludes.toArray(new String[excludes.size()])); |
| 184 | |
} |
| 185 | 9 | scanner.scan(); |
| 186 | 9 | return asList(scanner.getIncludedFiles()); |
| 187 | |
} |
| 188 | |
|
| 189 | |
} |