Coverage report

  %line %branch
org.apache.maven.author.FilesByAuthorProvider
100% 
100% 

 1  
 /*
 2  
  * Copyright 2006 Eric Ballet Baz
 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.apache.maven.author;
 18  
 
 19  
 import java.io.File;
 20  
 import java.util.Iterator;
 21  
 import java.util.List;
 22  
 import java.util.Map;
 23  
 import java.util.SortedMap;
 24  
 import java.util.SortedSet;
 25  
 import java.util.TreeMap;
 26  
 import java.util.TreeSet;
 27  
 
 28  
 import org.apache.commons.jelly.tags.ant.FileScanner;
 29  
 import org.apache.maven.author.resolver.AuthorResolver;
 30  
 
 31  
 /**
 32  
  * This class is the main entry point to resolve the authors of each file to process.
 33  
  * The same file can have multiples authors (multiple author javadoc tag for example).
 34  
  *
 35  
  * @author Eric Ballet Baz
 36  
  */
 37  
 public final class FilesByAuthorProvider {
 38  
 
 39  
     /**
 40  
      * Private constructor : do not allow instanciation of this class.
 41  
      */
 42  1
     private FilesByAuthorProvider() {
 43  1
     }
 44  
 
 45  
     /**
 46  
      * This method is the main entry point to resolve the authors of each file to process.
 47  
      * The same file can have multiples authors (multiple author javadoc tags for example).
 48  
      *
 49  
      * @param filesToProcess List of files to process.
 50  
      * @param authorResolverClassNames List of AuthorResolver's class names.
 51  
      * @return Sorted map where key is the author and value is a list of filenames. The Map is sorted on author.
 52  
      *
 53  
      * @throws ClassNotFoundException If a class of a resolver is not found.
 54  
      * @throws InstantiationException If a class of a resolver cannot be instanciate.
 55  
      * @throws IllegalAccessException If a constructor of a resolver is not accessible.
 56  
      */
 57  
     public static SortedMap resolveAuthors(final FileScanner filesToProcess, class="keyword">final List authorResolverClassNames)
 58  
         throws ClassNotFoundException, InstantiationException, IllegalAccessException {
 59  
 
 60  
         // Check arguments
 61  8
         if (filesToProcess == null) {
 62  2
             throw new IllegalArgumentException("Argument [filesToProcess] should not be null");
 63  
         }
 64  6
         if (authorResolverClassNames == null) {
 65  1
             throw new IllegalArgumentException("Argument [authorResolverClassNames] should not be null");
 66  
         }
 67  
 
 68  
         // Instanciate the list of resolvers
 69  5
         AuthorResolver[] authorResolvers = instanciateAuthorResolvers(authorResolverClassNames);
 70  
 
 71  
         // Process each file, and build a Map where key is the author and value is a list of filenames
 72  
         // The Map will be sorted on author
 73  5
         SortedMap filesByAuthorMap = new TreeMap();
 74  5
         Iterator it = filesToProcess.iterator();
 75  16
         while (it.hasNext()) {
 76  6
             File file = (File) it.next();
 77  
 
 78  
             // Ask resolvers to find the author(s) of the current file
 79  6
             String[] authors = resolveAuthorsForFile(file, authorResolvers);
 80  
 
 81  
             // Add map entry(ies) for this file and the author(s) found
 82  6
             addAuthorsForFileToMap(filesByAuthorMap, authors, file.getPath());
 83  
         }
 84  5
         return filesByAuthorMap;
 85  
     }
 86  
 
 87  
     /**
 88  
      * Resolve the authors of the specified file.
 89  
      *
 90  
      * @param file File to resolve authors for.
 91  
      * @param authorResolvers Array of AuthorResolvers.
 92  
      * @return Array of authors. If no author is found an array with an empty string as the unique element is returned.
 93  
      */
 94  
     private static String[] resolveAuthorsForFile(final File file, class="keyword">final AuthorResolver[] authorResolvers) {
 95  
         // Ask each resolvers to find the author(s) of this file
 96  10
         for (int i = 0; i < authorResolvers.length; i++) {
 97  8
             String[] currentAuthors = authorResolvers[i].resolveAuthors(file);
 98  
 
 99  
             // At least an author has been found by this resolver => exit loop
 100  8
             if (currentAuthors != null && currentAuthors.length > 0) {
 101  4
                 return currentAuthors;
 102  
             }
 103  
         }
 104  
 
 105  
         // No author has been found
 106  2
         return new String[] {""};
 107  
     }
 108  
 
 109  
     /**
 110  
      * Add entry(ies) in the specified Map, where key is an author and value is a sorted set of filenames.
 111  
      *
 112  
      * @param filesByAuthorMap Map to add entry(ies) into.
 113  
      * @param authors Array of authors for the specified filename.
 114  
      * @param filename Filename to add.
 115  
      */
 116  
     private static void addAuthorsForFileToMap(final Map filesByAuthorMap, class="keyword">final String[] authors, class="keyword">final String filename) {
 117  16
         for (int i = 0; i < authors.length; i++) {
 118  10
             SortedSet set = (SortedSet) filesByAuthorMap.get(authors[i]);
 119  10
             if (set == null) {
 120  9
                 set = new TreeSet();
 121  9
                 filesByAuthorMap.put(authors[i], set);
 122  
             }
 123  10
             set.add(filename);
 124  
         }
 125  6
     }
 126  
 
 127  
     /**
 128  
      * Instanciate the list of AuthorResolver from their class names.
 129  
      *
 130  
      * @param authorResolverClassNames List of AuthorResolver's class names.
 131  
      * @return List of AuthorResolver.
 132  
      *
 133  
      * @throws ClassNotFoundException If a class is not found.
 134  
      * @throws InstantiationException If a class cannot be instanciate.
 135  
      * @throws IllegalAccessException If a constructor is not accessible.
 136  
      */
 137  
     private static AuthorResolver[] instanciateAuthorResolvers(final List authorResolverClassNames)
 138  
         throws ClassNotFoundException, InstantiationException, IllegalAccessException {
 139  
 
 140  5
         AuthorResolver[] resolvers = new AuthorResolver[authorResolverClassNames.size()];
 141  13
         for (int i = 0; i < authorResolverClassNames.size(); i++) {
 142  8
             Class cl = Class.forName(((String) authorResolverClassNames.get(i)).trim());
 143  8
             resolvers[i] = (AuthorResolver) cl.newInstance();
 144  
         }
 145  5
         return resolvers;
 146  
     }
 147  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.