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.resolver; |
18 | |
19 | import java.io.File; |
20 | import java.util.List; |
21 | |
22 | import xjavadoc.SourceSet; |
23 | import xjavadoc.XClass; |
24 | import xjavadoc.XDoc; |
25 | import xjavadoc.XJavaDoc; |
26 | import xjavadoc.XTag; |
27 | import xjavadoc.filesystem.FileSourceSet; |
28 | |
29 | /** |
30 | * AuthorResolver based on javadoc. |
31 | * This resolver uses author's javadoc tags to resolve the author(s). |
32 | * |
33 | * @author Eric Ballet Baz |
34 | */ |
35 | public class JavadocAuthorResolver implements AuthorResolver { |
36 | |
37 | /** Javadoc tag used to declare the author(s) of a file. */ |
38 | private static final String AUTHOR_TAG_NAME = "author"; |
39 | |
40 | /** |
41 | * @see org.apache.maven.author.resolver.AuthorResolver#resolveAuthors(java.io.File) |
42 | */ |
43 | public final String[] resolveAuthors(final File file) { |
44 | // Check argument |
45 | if (file == null) { |
46 | throw new IllegalArgumentException("Argument [file] should not be null"); |
47 | } |
48 | if (file.isDirectory()) { |
49 | throw new IllegalArgumentException("Argument [file] should not be a directory"); |
50 | } |
51 | |
52 | // Prepare resources used to inspect the javadoc of this file |
53 | XJavaDoc xJavaDoc = new XJavaDoc(); |
54 | SourceSet fileSourceSet = new FileSourceSet(file); |
55 | xJavaDoc.addSourceSet(fileSourceSet); |
56 | |
57 | // Find values of author's javadoc tags for this file |
58 | XClass xClass = xJavaDoc.getXClass(fileSourceSet.getQualifiedName(0)); |
59 | |
60 | // This should never happen |
61 | if (xClass == null) { |
62 | throw new IllegalStateException( |
63 | "xjavadoc.XClass.getXClass(String qualifiedName) has returned a null value. Check version of xjavadoc used"); |
64 | } |
65 | |
66 | // Inspect javadoc to resolver authors |
67 | XDoc xDoc = xClass.getDoc(); |
68 | if (xDoc != null) { |
69 | List authorTags = xDoc.getTags(AUTHOR_TAG_NAME); |
70 | if (authorTags != null && authorTags.size() > 0) { |
71 | String[] authors = new String[authorTags.size()]; |
72 | for (int i = 0; i < authorTags.size(); i++) { |
73 | authors[i] = ((XTag) authorTags.get(i)).getValue(); |
74 | } |
75 | return authors; |
76 | } |
77 | } |
78 | |
79 | // No author has been found |
80 | return new String[0]; |
81 | } |
82 | } |