001/*
002 * The Apache Software License, Version 1.1
003 *
004 * Copyright (C) 2000-2002 The Apache Software Foundation.  All rights
005 * reserved.
006 * Copyright (C) 2003 jcoverage ltd.
007 * Copyright (C) 2005 Mark Doliner
008 * Copyright (C) 2005 Jeremy Thomerson
009 * Copyright (C) 2005 Grzegorz Lukasik
010 *
011 * Redistribution and use in source and binary forms, with or without
012 * modification, are permitted provided that the following conditions
013 * are met:
014 *
015 * 1. Redistributions of source code must retain the above copyright
016 *    notice, this list of conditions and the following disclaimer.
017 *
018 * 2. Redistributions in binary form must reproduce the above copyright
019 *    notice, this list of conditions and the following disclaimer in
020 *    the documentation and/or other materials provided with the
021 *    distribution.
022 *
023 * 3. The end-user documentation included with the redistribution, if
024 *    any, must include the following acknowlegement:
025 *       "This product includes software developed by the
026 *        Apache Software Foundation (http://www.apache.org/)."
027 *    Alternately, this acknowlegement may appear in the software itself,
028 *    if and wherever such third-party acknowlegements normally appear.
029 *
030 * 4. The names "Ant" and "Apache Software
031 *    Foundation" must not be used to endorse or promote products derived
032 *    from this software without prior written permission. For written
033 *    permission, please contact apache@apache.org.
034 *
035 * 5. Products derived from this software may not be called "Apache"
036 *    nor may "Apache" appear in their names without prior written
037 *    permission of the Apache Group.
038 *
039 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
043 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
044 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
045 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050 * SUCH DAMAGE.
051 * ====================================================================
052 *
053 * This software consists of voluntary contributions made by many
054 * individuals on behalf of the Apache Software Foundation.  For more
055 * information on the Apache Software Foundation, please see
056 * <http://www.apache.org/>.
057 */
058
059package net.sourceforge.cobertura.ant;
060
061import java.io.File;
062import java.io.IOException;
063
064import net.sourceforge.cobertura.util.CommandLineBuilder;
065
066import org.apache.tools.ant.BuildException;
067import org.apache.tools.ant.Project;
068
069/**
070 * Generate a coverage report based on coverage data generated 
071 * by instrumented classes.
072 */
073public class ReportTask extends CommonMatchingTask
074{
075
076        private String dataFile = null;
077        private String format = "html";
078        private File destDir;
079        private String srcDir;
080
081        public ReportTask() {
082                super("net.sourceforge.cobertura.reporting.Main");
083        }
084        
085        public void execute() throws BuildException {
086                CommandLineBuilder builder = null;
087                try {
088                        builder = new CommandLineBuilder();
089                        if (dataFile != null)
090                                builder.addArg("--datafile", dataFile);
091                        if (destDir != null)
092                                builder.addArg("--destination", destDir.getAbsolutePath());
093                        if (format != null)
094                                builder.addArg("--format", format);
095                        if (srcDir != null)
096                                builder.addArg(srcDir);
097
098                        createArgumentsForFilesets(builder);
099
100                        builder.saveArgs();
101                } catch (IOException ioe) {
102                        getProject().log("Error creating commands file.", Project.MSG_ERR);
103                        throw new BuildException("Unable to create the commands file.", ioe);
104                }
105
106                // Execute GPL licensed code in separate virtual machine
107                getJava().createArg().setValue("--commandsfile");
108                getJava().createArg().setValue(builder.getCommandLineFile());
109                AntUtil.transferCoberturaDataFileProperty(getJava());
110                if (getJava().executeJava() != 0) {
111                        throw new BuildException(
112                                        "Error running reports. See messages above.");
113                }
114
115                builder.dispose();
116        }
117
118        public void setDataFile(String dataFile) {
119                this.dataFile = dataFile;
120        }
121
122        public void setDestDir(File destDir) {
123                this.destDir = destDir;
124        }
125
126        public void setFormat(String format) {
127                this.format = format;
128        }
129        
130        public void setSrcDir(String dir) {
131                srcDir = dir;
132        }
133}