Coverage Report - org.codehaus.plexus.metadata.SourceComponentDescriptorExtractor
 
Classes in this File Line Coverage Branch Coverage Complexity
SourceComponentDescriptorExtractor
0 %
0/27
0 %
0/18
2,25
 
 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  0
 public class SourceComponentDescriptorExtractor
 37  
     extends ComponentDescriptorExtractorSupport
 38  
 {
 39  
     private SourceComponentGleaner gleaner;
 40  
 
 41  
     public SourceComponentDescriptorExtractor()
 42  0
     {
 43  0
     }
 44  
 
 45  
     public SourceComponentDescriptorExtractor( final SourceComponentGleaner gleaner )
 46  0
     {
 47  0
         this.gleaner = gleaner;
 48  0
     }
 49  
 
 50  
     public List<ComponentDescriptor<?>> extract( MetadataGenerationRequest configuration, final ComponentDescriptor<?>[] roleDefaults )
 51  
         throws Exception
 52  
     {
 53  0
         if ( gleaner == null )
 54  
         {
 55  0
             gleaner = new QDoxComponentGleaner();
 56  
         }
 57  
 
 58  0
         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  0
         assert sourceDirectories != null;
 65  0
         assert defaultsByRole != null;
 66  
 
 67  0
         List<ComponentDescriptor<?>> descriptors = new ArrayList<ComponentDescriptor<?>>();
 68  
 
 69  
         // Scan the sources
 70  0
         JavaProjectBuilder builder = new JavaProjectBuilder();
 71  
 
 72  0
         if ( StringUtils.isNotEmpty( sourceEncoding ) )
 73  
         {
 74  0
             builder.setEncoding( sourceEncoding );
 75  
         }
 76  
 
 77  0
         for ( String sourceDirectory : sourceDirectories )
 78  
         {
 79  0
             File dir = new File( sourceDirectory );
 80  
 
 81  0
             builder.addSourceTree( dir );
 82  0
         }
 83  
 
 84  0
         Collection<JavaClass> classes = builder.getClasses();
 85  
 
 86  
         // For each class we find, try to glean off a descriptor
 87  0
         for (JavaClass aClass : classes) {
 88  0
             ComponentDescriptor<?> descriptor = gleaner.glean(builder, aClass);
 89  
 
 90  0
             if (descriptor != null) {
 91  0
                 applyDefaults(descriptor, defaultsByRole);
 92  
 
 93  0
                 descriptors.add(descriptor);
 94  
             }
 95  0
         }
 96  
 
 97  0
         return descriptors;
 98  
     }
 99  
 }