View Javadoc
1   /*
2    * Copyright (C) 2007 the original author or authors.
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.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   * Extracts {@link ComponentDescriptor} from source files.
34   *
35   * @version $Rev$ $Date$
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  // getDefaultsByRole() seems to check for null and maven-artifact project works fine when
52  // assertions are disabled.        
53  //        assert roleDefaults != null;
54  
55          // Use a default source gleaner if none was configured
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          // Scan the sources
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          // For each class we find, try to glean off a descriptor
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 }