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     *
009     * Cobertura is free software; you can redistribute it and/or modify
010     * it under the terms of the GNU General Public License as published
011     * by the Free Software Foundation; either version 2 of the License,
012     * or (at your option) any later version.
013     *
014     * Cobertura is distributed in the hope that it will be useful, but
015     * WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * General Public License for more details.
018     *
019     * You should have received a copy of the GNU General Public License
020     * along with Cobertura; if not, write to the Free Software
021     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
022     * USA
023     */
024    
025    package net.sourceforge.cobertura.reporting;
026    
027    import java.io.File;
028    
029    import net.sourceforge.cobertura.coveragedata.CoverageDataFileHandler;
030    import net.sourceforge.cobertura.coveragedata.ProjectData;
031    import net.sourceforge.cobertura.reporting.html.HTMLReport;
032    import net.sourceforge.cobertura.reporting.xml.XMLReport;
033    import net.sourceforge.cobertura.util.CommandLineBuilder;
034    import net.sourceforge.cobertura.util.FileFinder;
035    import net.sourceforge.cobertura.util.Header;
036    
037    import org.apache.log4j.Logger;
038    
039    public class Main {
040    
041            private static final Logger LOGGER = Logger.getLogger(Main.class);
042    
043            private String format = "html";
044            private File dataFile = null;
045            private File destinationDir = null;
046            
047            private void parseArguments(String[] args) throws Exception {
048                    FileFinder finder = new FileFinder();
049                    String baseDir = null;
050                    for (int i = 0; i < args.length; i++) {
051                            if (args[i].equals("--basedir")) {
052                                    baseDir = args[++i];
053                            } else if (args[i].equals("--datafile")) {
054                                    setDataFile( args[++i]);
055                            } else if (args[i].equals("--destination")) {
056                                    setDestination( args[++i]);
057                            } else if (args[i].equals("--format")) {
058                                    setFormat( args[++i]);
059                            } else {
060                    if( baseDir==null) {
061                            finder.addSourceDirectory( args[i]);
062                    } else {
063                            finder.addSourceFile( baseDir, args[i]);
064                    }
065                            }
066                    }
067    
068                    if (dataFile == null)
069                            dataFile = CoverageDataFileHandler.getDefaultDataFile();
070    
071                    if (destinationDir == null)
072                    {
073                            System.err.println("Error: destination directory must be set");
074                            System.exit(1);
075                    }
076    
077                    if (format == null)
078                    {
079                            System.err.println("Error: format must be set");
080                            System.exit(1);
081                    }
082                    
083                    if (LOGGER.isDebugEnabled())
084                    {
085                            LOGGER.debug("format is " + format);
086                            LOGGER.debug("dataFile is " + dataFile.getAbsolutePath());
087                            LOGGER.debug("destinationDir is "
088                                            + destinationDir.getAbsolutePath());
089                    }
090    
091                    ProjectData projectData = CoverageDataFileHandler.loadCoverageData(dataFile);
092    
093                    if (projectData == null) {
094                            System.err.println("Error: Unable to read from data file " + dataFile.getAbsolutePath());
095                            System.exit(1);
096                    }
097    
098                    ComplexityCalculator complexity = new ComplexityCalculator(finder);
099                    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    }