Coverage Report - net.sourceforge.cobertura.reporting.Main
 
Classes in this File Line Coverage Branch Coverage Complexity
Main
76%
56/74
71%
27/38
4.6
 
 1  
 /*
 2  
  * Cobertura - http://cobertura.sourceforge.net/
 3  
  *
 4  
  * Copyright (C) 2003 jcoverage ltd.
 5  
  * Copyright (C) 2005 Mark Doliner
 6  
  * Copyright (C) 2005 Jeremy Thomerson
 7  
  * Copyright (C) 2005 Grzegorz Lukasik
 8  
  *
 9  
  * Cobertura is free software; you can redistribute it and/or modify
 10  
  * it under the terms of the GNU General Public License as published
 11  
  * by the Free Software Foundation; either version 2 of the License,
 12  
  * or (at your option) any later version.
 13  
  *
 14  
  * Cobertura is distributed in the hope that it will be useful, but
 15  
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 16  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 17  
  * General Public License for more details.
 18  
  *
 19  
  * You should have received a copy of the GNU General Public License
 20  
  * along with Cobertura; if not, write to the Free Software
 21  
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 22  
  * USA
 23  
  */
 24  
 
 25  
 package net.sourceforge.cobertura.reporting;
 26  
 
 27  
 import java.io.File;
 28  
 
 29  
 import net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler;
 30  
 import net.sourceforge.cobertura.coveragedata.ProjectData;
 31  
 import net.sourceforge.cobertura.reporting.html.HTMLReport;
 32  
 import net.sourceforge.cobertura.reporting.xml.XMLReport;
 33  
 import net.sourceforge.cobertura.util.CommandLineBuilder;
 34  
 import net.sourceforge.cobertura.util.FileFinder;
 35  
 import net.sourceforge.cobertura.util.Header;
 36  
 
 37  
 import org.apache.log4j.Logger;
 38  
 
 39  8
 public class Main {
 40  
 
 41  8
         private static final Logger LOGGER = Logger.getLogger(Main.class);
 42  
 
 43  8
         private String format = "html";
 44  8
         private File dataFile = null;
 45  8
         private File destinationDir = null;
 46  
         
 47  
         private void parseArguments(String[] args) throws Exception {
 48  8
                 FileFinder finder = new FileFinder();
 49  8
                 String baseDir = null;
 50  63
                 for (int i = 0; i < args.length; i++) {
 51  55
                         if (args[i].equals("--basedir")) {
 52  4
                                 baseDir = args[++i];
 53  4
                         } else if (args[i].equals("--datafile")) {
 54  8
                                 setDataFile( args[++i]);
 55  8
                         } else if (args[i].equals("--destination")) {
 56  8
                                 setDestination( args[++i]);
 57  8
                         } else if (args[i].equals("--format")) {
 58  8
                                 setFormat( args[++i]);
 59  8
                         } else {
 60  27
                     if( baseDir==null) {
 61  4
                             finder.addSourceDirectory( args[i]);
 62  4
                     } else {
 63  23
                             finder.addSourceFile( baseDir, args[i]);
 64  
                     }
 65  
                         }
 66  
                 }
 67  
 
 68  8
                 if (dataFile == null)
 69  0
                         dataFile = CoverageDataFileHandler.getDefaultDataFile();
 70  
 
 71  8
                 if (destinationDir == null)
 72  
                 {
 73  0
                         System.err.println("Error: destination directory must be set");
 74  0
                         System.exit(1);
 75  
                 }
 76  
 
 77  8
                 if (format == null)
 78  
                 {
 79  0
                         System.err.println("Error: format must be set");
 80  0
                         System.exit(1);
 81  
                 }
 82  
                 
 83  8
                 if (LOGGER.isDebugEnabled())
 84  
                 {
 85  8
                         LOGGER.debug("format is " + format);
 86  8
                         LOGGER.debug("dataFile is " + dataFile.getAbsolutePath());
 87  8
                         LOGGER.debug("destinationDir is "
 88  
                                         + destinationDir.getAbsolutePath());
 89  
                 }
 90  
 
 91  8
                 ProjectData projectData = CoverageDataFileHandler.loadCoverageData(dataFile);
 92  
 
 93  8
                 if (projectData == null) {
 94  0
                         System.err.println("Error: Unable to read from data file " + dataFile.getAbsolutePath());
 95  0
                         System.exit(1);
 96  
                 }
 97  
 
 98  8
                 ComplexityCalculator complexity = new ComplexityCalculator(finder);
 99  8
                 if (format.equalsIgnoreCase("html")) {
 100  4
                         new HTMLReport(projectData, destinationDir, finder, complexity);
 101  4
                 } else if (format.equalsIgnoreCase("xml")) {
 102  4
                         new XMLReport(projectData, destinationDir, finder, complexity);
 103  
                 }
 104  8
         }
 105  
         
 106  
         private void setFormat(String value) 
 107  
         {
 108  8
                 format = value;
 109  8
                 if (!format.equalsIgnoreCase("html") && !format.equalsIgnoreCase("xml")) {
 110  0
                         System.err.println("" +
 111  
                                         "Error: format \"" +
 112  
                                         format + "\" is invalid. Must be either html or xml"
 113  
                                         );
 114  0
                         System.exit(1);
 115  
                 }
 116  8
         }
 117  
 
 118  
         private void setDataFile(String value) 
 119  
         {
 120  8
                 dataFile = new File(value);
 121  8
                 if (!dataFile.exists())
 122  
                 {
 123  0
                         System.err.println("Error: data file " + dataFile.getAbsolutePath()
 124  
                                         + " does not exist");
 125  0
                         System.exit(1);
 126  
                 }
 127  8
                 if (!dataFile.isFile())
 128  
                 {
 129  0
                         System.err.println("Error: data file " + dataFile.getAbsolutePath()
 130  
                                         + " must be a regular file");
 131  0
                         System.exit(1);
 132  
                 }
 133  8
         }
 134  
 
 135  
         private void setDestination(String value) 
 136  
         {
 137  8
                 destinationDir = new File(value);
 138  8
                 if (destinationDir.exists() && !destinationDir.isDirectory())
 139  
                 {
 140  0
                         System.err.println("Error: destination directory " + destinationDir
 141  
                                         + " already exists but is not a directory");
 142  0
                         System.exit(1);
 143  
                 }
 144  8
                 destinationDir.mkdirs();
 145  8
         }
 146  
         
 147  
         public static void main(String[] args) throws Exception {
 148  8
                 Header.print(System.out);
 149  
 
 150  8
                 long startTime = System.currentTimeMillis();
 151  
 
 152  8
                 Main main = new Main();
 153  
 
 154  
                 try {
 155  8
                         args = CommandLineBuilder.preprocessCommandLineArguments( args);
 156  0
                 } catch( Exception ex) {
 157  0
                         System.err.println( "Error: Cannot process arguments: " + ex.getMessage());
 158  0
                         System.exit(1);
 159  8
                 }
 160  
                 
 161  8
                 main.parseArguments(args);
 162  
 
 163  8
                 long stopTime = System.currentTimeMillis();
 164  8
                 System.out.println("Report time: " + (stopTime - startTime) + "ms");
 165  8
         }
 166  
 
 167  
 }