1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.codehaus.plexus.maven.plugin;
18
19 import java.io.File;
20 import java.util.ArrayList;
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.Map;
24
25 import com.thoughtworks.qdox.JavaDocBuilder;
26 import com.thoughtworks.qdox.model.JavaClass;
27 import org.apache.maven.project.MavenProject;
28 import org.codehaus.plexus.cdc.gleaner.QDoxComponentGleaner;
29 import org.codehaus.plexus.cdc.gleaner.SourceComponentGleaner;
30 import org.codehaus.plexus.component.repository.cdc.ComponentDescriptor;
31
32
33
34
35
36
37 public class SourceComponentDescriptorExtractor
38 extends ComponentDescriptorExtractorSupport
39 {
40 private SourceComponentGleaner gleaner;
41
42 public SourceComponentDescriptorExtractor(final SourceComponentGleaner gleaner) {
43 this.gleaner = gleaner;
44 }
45
46 public SourceComponentDescriptorExtractor() {}
47
48 public List extract(final MavenProject project, final String scope, final ComponentDescriptor[] roleDefaults) throws Exception {
49 assert project != null;
50 assert scope != null;
51
52
53
54
55
56 if (gleaner == null) {
57 gleaner = new QDoxComponentGleaner();
58 }
59
60 log.debug("Gleaner: " + gleaner + ", scope: " + scope);
61
62 List roots;
63
64 if (COMPILE_SCOPE.equals(scope)) {
65 roots = project.getCompileSourceRoots();
66 }
67 else if (TEST_SCOPE.equals(scope)) {
68 roots = project.getTestCompileSourceRoots();
69 }
70 else {
71 throw new IllegalArgumentException("Invalid scope: " + scope);
72 }
73
74 return extract(roots, getDefaultsByRole(roleDefaults));
75 }
76
77 private List extract(final List sourceDirectories, final Map defaultsByRole) throws Exception {
78 assert sourceDirectories != null;
79 assert defaultsByRole != null;
80
81 List descriptors = new ArrayList();
82
83
84 JavaDocBuilder builder = new JavaDocBuilder();
85
86 for (Iterator iter = sourceDirectories.iterator(); iter.hasNext();) {
87 File dir = new File((String)iter.next());
88
89 log.debug("Adding source directory: " + dir);
90
91 builder.addSourceTree(dir);
92 }
93
94 JavaClass[] classes = builder.getClasses();
95
96
97 for (int i = 0; i < classes.length; i++) {
98 log.debug("Gleaning from: " + classes[i].getFullyQualifiedName());
99
100 ComponentDescriptor descriptor = gleaner.glean(builder, classes[i]);
101
102 if (descriptor != null) {
103 applyDefaults(descriptor, defaultsByRole);
104
105 descriptors.add(descriptor);
106 }
107 }
108
109 log.debug("Extracted " + descriptors.size() + " descriptor(s)");
110
111 return descriptors;
112 }
113 }