View Javadoc
1   /*
2    * Copyright (C) 2007 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  
15  package org.codehaus.plexus.metadata;
16  
17  import java.io.File;
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.List;
21  import java.util.Map;
22  
23  import com.thoughtworks.qdox.JavaProjectBuilder;
24  import com.thoughtworks.qdox.model.JavaClass;
25  import org.codehaus.plexus.component.annotations.Component;
26  import org.codehaus.plexus.component.repository.ComponentDescriptor;
27  import org.codehaus.plexus.metadata.gleaner.QDoxComponentGleaner;
28  import org.codehaus.plexus.metadata.gleaner.SourceComponentGleaner;
29  import org.codehaus.plexus.util.StringUtils;
30  
31  /**
32   * Extracts {@link ComponentDescriptor} from source files.
33   *
34   * @version $Rev$ $Date$
35   */
36  @Component(role = ComponentDescriptorExtractor.class, hint = "source")
37  public class SourceComponentDescriptorExtractor extends ComponentDescriptorExtractorSupport {
38      private SourceComponentGleaner gleaner;
39  
40      public SourceComponentDescriptorExtractor() {}
41  
42      public SourceComponentDescriptorExtractor(final SourceComponentGleaner gleaner) {
43          this.gleaner = gleaner;
44      }
45  
46      public List<ComponentDescriptor<?>> extract(
47              MetadataGenerationRequest configuration, final ComponentDescriptor<?>[] roleDefaults) throws Exception {
48          if (gleaner == null) {
49              gleaner = new QDoxComponentGleaner();
50          }
51  
52          return extract(configuration.sourceDirectories, configuration.sourceEncoding, getDefaultsByRole(roleDefaults));
53      }
54  
55      private List<ComponentDescriptor<?>> extract(
56              final List<String> sourceDirectories,
57              final String sourceEncoding,
58              final Map<String, ComponentDescriptor<?>> defaultsByRole)
59              throws Exception {
60          assert sourceDirectories != null;
61          assert defaultsByRole != null;
62  
63          List<ComponentDescriptor<?>> descriptors = new ArrayList<ComponentDescriptor<?>>();
64  
65          // Scan the sources
66          JavaProjectBuilder builder = new JavaProjectBuilder();
67  
68          if (StringUtils.isNotEmpty(sourceEncoding)) {
69              builder.setEncoding(sourceEncoding);
70          }
71  
72          for (String sourceDirectory : sourceDirectories) {
73              File dir = new File(sourceDirectory);
74  
75              builder.addSourceTree(dir);
76          }
77  
78          Collection<JavaClass> classes = builder.getClasses();
79  
80          // For each class we find, try to glean off a descriptor
81          for (JavaClass aClass : classes) {
82              ComponentDescriptor<?> descriptor = gleaner.glean(builder, aClass);
83  
84              if (descriptor != null) {
85                  applyDefaults(descriptor, defaultsByRole);
86  
87                  descriptors.add(descriptor);
88              }
89          }
90  
91          return descriptors;
92      }
93  }