1 | /* |
2 | * Copyright 2006 Eric Ballet Baz |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | package org.apache.maven.author; |
18 | |
19 | import java.io.File; |
20 | import java.util.Iterator; |
21 | import java.util.List; |
22 | import java.util.Map; |
23 | import java.util.SortedMap; |
24 | import java.util.SortedSet; |
25 | import java.util.TreeMap; |
26 | import java.util.TreeSet; |
27 | |
28 | import org.apache.commons.jelly.tags.ant.FileScanner; |
29 | import org.apache.maven.author.resolver.AuthorResolver; |
30 | |
31 | /** |
32 | * This class is the main entry point to resolve the authors of each file to process. |
33 | * The same file can have multiples authors (multiple author javadoc tag for example). |
34 | * |
35 | * @author Eric Ballet Baz |
36 | */ |
37 | public final class FilesByAuthorProvider { |
38 | |
39 | /** |
40 | * Private constructor : do not allow instanciation of this class. |
41 | */ |
42 | private FilesByAuthorProvider() { |
43 | } |
44 | |
45 | /** |
46 | * This method is the main entry point to resolve the authors of each file to process. |
47 | * The same file can have multiples authors (multiple author javadoc tags for example). |
48 | * |
49 | * @param filesToProcess List of files to process. |
50 | * @param authorResolverClassNames List of AuthorResolver's class names. |
51 | * @return Sorted map where key is the author and value is a list of filenames. The Map is sorted on author. |
52 | * |
53 | * @throws ClassNotFoundException If a class of a resolver is not found. |
54 | * @throws InstantiationException If a class of a resolver cannot be instanciate. |
55 | * @throws IllegalAccessException If a constructor of a resolver is not accessible. |
56 | */ |
57 | public static SortedMap resolveAuthors(final FileScanner filesToProcess, final List authorResolverClassNames) |
58 | throws ClassNotFoundException, InstantiationException, IllegalAccessException { |
59 | |
60 | // Check arguments |
61 | if (filesToProcess == null) { |
62 | throw new IllegalArgumentException("Argument [filesToProcess] should not be null"); |
63 | } |
64 | if (authorResolverClassNames == null) { |
65 | throw new IllegalArgumentException("Argument [authorResolverClassNames] should not be null"); |
66 | } |
67 | |
68 | // Instanciate the list of resolvers |
69 | AuthorResolver[] authorResolvers = instanciateAuthorResolvers(authorResolverClassNames); |
70 | |
71 | // Process each file, and build a Map where key is the author and value is a list of filenames |
72 | // The Map will be sorted on author |
73 | SortedMap filesByAuthorMap = new TreeMap(); |
74 | Iterator it = filesToProcess.iterator(); |
75 | while (it.hasNext()) { |
76 | File file = (File) it.next(); |
77 | |
78 | // Ask resolvers to find the author(s) of the current file |
79 | String[] authors = resolveAuthorsForFile(file, authorResolvers); |
80 | |
81 | // Add map entry(ies) for this file and the author(s) found |
82 | addAuthorsForFileToMap(filesByAuthorMap, authors, file.getPath()); |
83 | } |
84 | return filesByAuthorMap; |
85 | } |
86 | |
87 | /** |
88 | * Resolve the authors of the specified file. |
89 | * |
90 | * @param file File to resolve authors for. |
91 | * @param authorResolvers Array of AuthorResolvers. |
92 | * @return Array of authors. If no author is found an array with an empty string as the unique element is returned. |
93 | */ |
94 | private static String[] resolveAuthorsForFile(final File file, final AuthorResolver[] authorResolvers) { |
95 | // Ask each resolvers to find the author(s) of this file |
96 | for (int i = 0; i < authorResolvers.length; i++) { |
97 | String[] currentAuthors = authorResolvers[i].resolveAuthors(file); |
98 | |
99 | // At least an author has been found by this resolver => exit loop |
100 | if (currentAuthors != null && currentAuthors.length > 0) { |
101 | return currentAuthors; |
102 | } |
103 | } |
104 | |
105 | // No author has been found |
106 | return new String[] {""}; |
107 | } |
108 | |
109 | /** |
110 | * Add entry(ies) in the specified Map, where key is an author and value is a sorted set of filenames. |
111 | * |
112 | * @param filesByAuthorMap Map to add entry(ies) into. |
113 | * @param authors Array of authors for the specified filename. |
114 | * @param filename Filename to add. |
115 | */ |
116 | private static void addAuthorsForFileToMap(final Map filesByAuthorMap, final String[] authors, final String filename) { |
117 | for (int i = 0; i < authors.length; i++) { |
118 | SortedSet set = (SortedSet) filesByAuthorMap.get(authors[i]); |
119 | if (set == null) { |
120 | set = new TreeSet(); |
121 | filesByAuthorMap.put(authors[i], set); |
122 | } |
123 | set.add(filename); |
124 | } |
125 | } |
126 | |
127 | /** |
128 | * Instanciate the list of AuthorResolver from their class names. |
129 | * |
130 | * @param authorResolverClassNames List of AuthorResolver's class names. |
131 | * @return List of AuthorResolver. |
132 | * |
133 | * @throws ClassNotFoundException If a class is not found. |
134 | * @throws InstantiationException If a class cannot be instanciate. |
135 | * @throws IllegalAccessException If a constructor is not accessible. |
136 | */ |
137 | private static AuthorResolver[] instanciateAuthorResolvers(final List authorResolverClassNames) |
138 | throws ClassNotFoundException, InstantiationException, IllegalAccessException { |
139 | |
140 | AuthorResolver[] resolvers = new AuthorResolver[authorResolverClassNames.size()]; |
141 | for (int i = 0; i < authorResolverClassNames.size(); i++) { |
142 | Class cl = Class.forName(((String) authorResolverClassNames.get(i)).trim()); |
143 | resolvers[i] = (AuthorResolver) cl.newInstance(); |
144 | } |
145 | return resolvers; |
146 | } |
147 | } |