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