KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > reporting > Main


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 JavaDoc;
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 JavaDoc format = "html";
44     private File JavaDoc dataFile = null;
45     private File JavaDoc destinationDir = null;
46     
47     private void parseArguments(String JavaDoc[] args) throws Exception JavaDoc {
48         FileFinder finder = new FileFinder();
49         String JavaDoc 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 JavaDoc 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 JavaDoc value)
119     {
120         dataFile = new File JavaDoc(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 JavaDoc value)
136     {
137         destinationDir = new File JavaDoc(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 JavaDoc[] args) throws Exception JavaDoc {
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 JavaDoc 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