1 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 public class Main { 40 41 private static final Logger LOGGER = Logger.getLogger(Main.class); 42 43 private String format = "html"; 44 private File dataFile = null; 45 private File destinationDir = null; 46 47 private void parseArguments(String [] args) throws Exception { 48 FileFinder finder = new FileFinder(); 49 String baseDir = null; 50 for (int i = 0; i < args.length; i++) { 51 if (args[i].equals("--basedir")) { 52 baseDir = args[++i]; 53 } else if (args[i].equals("--datafile")) { 54 setDataFile( args[++i]); 55 } else if (args[i].equals("--destination")) { 56 setDestination( args[++i]); 57 } else if (args[i].equals("--format")) { 58 setFormat( args[++i]); 59 } else { 60 if( baseDir==null) { 61 finder.addSourceDirectory( args[i]); 62 } else { 63 finder.addSourceFile( baseDir, args[i]); 64 } 65 } 66 } 67 68 if (dataFile == null) 69 dataFile = CoverageDataFileHandler.getDefaultDataFile(); 70 71 if (destinationDir == null) 72 { 73 System.err.println("Error: destination directory must be set"); 74 System.exit(1); 75 } 76 77 if (format == null) 78 { 79 System.err.println("Error: format must be set"); 80 System.exit(1); 81 } 82 83 if (LOGGER.isDebugEnabled()) 84 { 85 LOGGER.debug("format is " + format); 86 LOGGER.debug("dataFile is " + dataFile.getAbsolutePath()); 87 LOGGER.debug("destinationDir is " 88 + destinationDir.getAbsolutePath()); 89 } 90 91 ProjectData projectData = CoverageDataFileHandler.loadCoverageData(dataFile); 92 93 if (projectData == null) { 94 System.err.println("Error: Unable to read from data file " + dataFile.getAbsolutePath()); 95 System.exit(1); 96 } 97 98 ComplexityCalculator complexity = new ComplexityCalculator(finder); 99 if (format.equalsIgnoreCase("html")) { 100 new HTMLReport(projectData, destinationDir, finder, complexity); 101 } else if (format.equalsIgnoreCase("xml")) { 102 new XMLReport(projectData, destinationDir, finder, complexity); 103 } 104 } 105 106 private void setFormat(String value) 107 { 108 format = value; 109 if (!format.equalsIgnoreCase("html") && !format.equalsIgnoreCase("xml")) { 110 System.err.println("" + 111 "Error: format \"" + 112 format + "\" is invalid. Must be either html or xml" 113 ); 114 System.exit(1); 115 } 116 } 117 118 private void setDataFile(String value) 119 { 120 dataFile = new File (value); 121 if (!dataFile.exists()) 122 { 123 System.err.println("Error: data file " + dataFile.getAbsolutePath() 124 + " does not exist"); 125 System.exit(1); 126 } 127 if (!dataFile.isFile()) 128 { 129 System.err.println("Error: data file " + dataFile.getAbsolutePath() 130 + " must be a regular file"); 131 System.exit(1); 132 } 133 } 134 135 private void setDestination(String value) 136 { 137 destinationDir = new File (value); 138 if (destinationDir.exists() && !destinationDir.isDirectory()) 139 { 140 System.err.println("Error: destination directory " + destinationDir 141 + " already exists but is not a directory"); 142 System.exit(1); 143 } 144 destinationDir.mkdirs(); 145 } 146 147 public static void main(String [] args) throws Exception { 148 Header.print(System.out); 149 150 long startTime = System.currentTimeMillis(); 151 152 Main main = new Main(); 153 154 try { 155 args = CommandLineBuilder.preprocessCommandLineArguments( args); 156 } catch( Exception ex) { 157 System.err.println( "Error: Cannot process arguments: " + ex.getMessage()); 158 System.exit(1); 159 } 160 161 main.parseArguments(args); 162 163 long stopTime = System.currentTimeMillis(); 164 System.out.println("Report time: " + (stopTime - startTime) + "ms"); 165 } 166 167 } 168 | Popular Tags |