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 org.codehaus.plexus.component.annotations.Component;
24  import org.codehaus.plexus.component.repository.ComponentDescriptor;
25  import org.codehaus.plexus.metadata.gleaner.QDoxComponentGleaner;
26  import org.codehaus.plexus.metadata.gleaner.SourceComponentGleaner;
27  import org.codehaus.plexus.util.StringUtils;
28  
29  import com.thoughtworks.qdox.JavaProjectBuilder;
30  import com.thoughtworks.qdox.model.JavaClass;
31  
32  /**
33   * Extracts {@link ComponentDescriptor} from source files.
34   * 
35   * @version $Rev$ $Date$
36   */
37  @Component( role = ComponentDescriptorExtractor.class, hint = "source" )
38  public class SourceComponentDescriptorExtractor
39      extends ComponentDescriptorExtractorSupport
40  {
41      private SourceComponentGleaner gleaner;
42  
43      public SourceComponentDescriptorExtractor()
44      {
45      }
46  
47      public SourceComponentDescriptorExtractor( final SourceComponentGleaner gleaner )
48      {
49          this.gleaner = gleaner;
50      }
51  
52      public List<ComponentDescriptor<?>> extract( MetadataGenerationRequest configuration, final ComponentDescriptor<?>[] roleDefaults )
53          throws Exception
54      {
55          if ( gleaner == null )
56          {
57              gleaner = new QDoxComponentGleaner();
58          }
59  
60          return extract( configuration.sourceDirectories, configuration.sourceEncoding, getDefaultsByRole( roleDefaults ) );
61      }
62  
63      private List<ComponentDescriptor<?>> extract( final List<String> sourceDirectories, final String sourceEncoding, final Map<String, ComponentDescriptor<?>> defaultsByRole )
64          throws Exception
65      {
66          assert sourceDirectories != null;
67          assert defaultsByRole != null;
68  
69          List<ComponentDescriptor<?>> descriptors = new ArrayList<ComponentDescriptor<?>>();
70  
71          // Scan the sources
72          JavaProjectBuilder builder = new JavaProjectBuilder();
73  
74          if ( StringUtils.isNotEmpty( sourceEncoding ) )
75          {
76              builder.setEncoding( sourceEncoding );
77          }
78  
79          for ( String sourceDirectory : sourceDirectories )
80          {
81              File dir = new File( sourceDirectory );
82  
83              builder.addSourceTree( dir );
84          }
85  
86          Collection<JavaClass> classes = builder.getClasses();
87  
88          // For each class we find, try to glean off a descriptor
89          for (JavaClass aClass : classes) {
90              ComponentDescriptor<?> descriptor = gleaner.glean(builder, aClass);
91  
92              if (descriptor != null) {
93                  applyDefaults(descriptor, defaultsByRole);
94  
95                  descriptors.add(descriptor);
96              }
97          }
98  
99          return descriptors;
100     }
101 }