001/*
002 * Cobertura - http://cobertura.sourceforge.net/
003 *
004 * Copyright (C) 2003 jcoverage ltd.
005 * Copyright (C) 2005 Mark Doliner
006 * Copyright (C) 2005 Jeremy Thomerson
007 * Copyright (C) 2005 Grzegorz Lukasik
008 * Copyright (C) 2006 Dan Godfrey
009 *
010 * Cobertura is free software; you can redistribute it and/or modify
011 * it under the terms of the GNU General Public License as published
012 * by the Free Software Foundation; either version 2 of the License,
013 * or (at your option) any later version.
014 *
015 * Cobertura is distributed in the hope that it will be useful, but
016 * WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018 * General Public License for more details.
019 *
020 * You should have received a copy of the GNU General Public License
021 * along with Cobertura; if not, write to the Free Software
022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
023 * USA
024 */
025
026package net.sourceforge.cobertura.reporting;
027
028import java.io.File;
029
030import net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler;
031import net.sourceforge.cobertura.coveragedata.ProjectData;
032import net.sourceforge.cobertura.reporting.html.HTMLReport;
033import net.sourceforge.cobertura.reporting.xml.SummaryXMLReport;
034import net.sourceforge.cobertura.reporting.xml.XMLReport;
035import net.sourceforge.cobertura.util.CommandLineBuilder;
036import net.sourceforge.cobertura.util.FileFinder;
037import net.sourceforge.cobertura.util.Header;
038
039import org.apache.log4j.Logger;
040
041public class Main {
042
043        private static final Logger LOGGER = Logger.getLogger(Main.class);
044
045        private String format = "html";
046        private File dataFile = null;
047        private File destinationDir = null;
048        private String encoding = "UTF-8";
049        
050        private void parseArguments(String[] args) throws Exception {
051                FileFinder finder = new FileFinder();
052                String baseDir = null;
053                for (int i = 0; i < args.length; i++) {
054                        if (args[i].equals("--basedir")) {
055                                baseDir = args[++i];
056                        } else if (args[i].equals("--datafile")) {
057                                setDataFile( args[++i]);
058                        } else if (args[i].equals("--destination")) {
059                                setDestination( args[++i]);
060                        } else if (args[i].equals("--format")) {
061                                setFormat( args[++i]);
062                        } else if (args[i].equals("--encoding")) {
063                                setEncoding( args[++i]);
064                        } else {
065                                if( baseDir==null) {
066                                        finder.addSourceDirectory( args[i]);
067                                } else {
068                                        finder.addSourceFile( baseDir, args[i]);
069                                }
070                        }
071                }
072
073                if (dataFile == null)
074                        dataFile = CoverageDataFileHandler.getDefaultDataFile();
075
076                if (destinationDir == null)
077                {
078                        System.err.println("Error: destination directory must be set");
079                        System.exit(1);
080                }
081
082                if (format == null)
083                {
084                        System.err.println("Error: format must be set");
085                        System.exit(1);
086                }
087                
088                if (LOGGER.isDebugEnabled())
089                {
090                        LOGGER.debug("format is " + format + " encoding is " + encoding);
091                        LOGGER.debug("dataFile is " + dataFile.getAbsolutePath());
092                        LOGGER.debug("destinationDir is "
093                                        + destinationDir.getAbsolutePath());
094                }
095
096                ProjectData projectData = CoverageDataFileHandler.loadCoverageData(dataFile);
097
098                if (projectData == null) {
099                        System.err.println("Error: Unable to read from data file " + dataFile.getAbsolutePath());
100                        System.exit(1);
101                }
102
103                ComplexityCalculator complexity = new ComplexityCalculator(finder);
104                if (format.equalsIgnoreCase("html")) {
105                        new HTMLReport(projectData, destinationDir, finder, complexity, encoding);
106                } else if (format.equalsIgnoreCase("xml")) {
107                        new XMLReport(projectData, destinationDir, finder, complexity);
108                } else if (format.equalsIgnoreCase("summaryXml")) {
109                        new SummaryXMLReport(projectData, destinationDir, finder, complexity);
110                }
111        }
112        
113        private void setFormat(String value) 
114        {
115                format = value;
116                if (!format.equalsIgnoreCase("html") 
117                                && !format.equalsIgnoreCase("xml")
118                                && !format.equalsIgnoreCase("summaryXml")) {
119                        System.err.println("" +
120                                        "Error: format \"" +
121                                        format + "\" is invalid. Must be either html or xml or summaryXml"
122                                        );
123                        System.exit(1);
124                }
125        }
126
127        private void setDataFile(String value) 
128        {
129                dataFile = new File(value);
130                if (!dataFile.exists())
131                {
132                        System.err.println("Error: data file " + dataFile.getAbsolutePath()
133                                        + " does not exist");
134                        System.exit(1);
135                }
136                if (!dataFile.isFile())
137                {
138                        System.err.println("Error: data file " + dataFile.getAbsolutePath()
139                                        + " must be a regular file");
140                        System.exit(1);
141                }
142        }
143
144        private void setDestination(String value) 
145        {
146                destinationDir = new File(value);
147                if (destinationDir.exists() && !destinationDir.isDirectory())
148                {
149                        System.err.println("Error: destination directory " + destinationDir
150                                        + " already exists but is not a directory");
151                        System.exit(1);
152                }
153                destinationDir.mkdirs();
154        }
155
156        private void setEncoding(String encoding){
157                this.encoding = encoding;
158        }
159        
160        public static void main(String[] args) throws Exception {
161                Header.print(System.out);
162
163                long startTime = System.currentTimeMillis();
164
165                Main main = new Main();
166
167                try {
168                        args = CommandLineBuilder.preprocessCommandLineArguments( args);
169                } catch( Exception ex) {
170                        System.err.println( "Error: Cannot process arguments: " + ex.getMessage());
171                        System.exit(1);
172                }
173                
174                main.parseArguments(args);
175
176                long stopTime = System.currentTimeMillis();
177                System.out.println("Report time: " + (stopTime - startTime) + "ms");
178        }
179
180}