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 <thekingant@users.sourceforge.net>
008     * Copyright (C) 2005 Joakim Erdfelt <joakim@erdfelt.net>
009     *
010     * Redistribution and use in source and binary forms, with or without
011     * modification, are permitted provided that the following conditions
012     * are met:
013     *
014     * 1. Redistributions of source code must retain the above copyright
015     *    notice, this list of conditions and the following disclaimer.
016     *
017     * 2. Redistributions in binary form must reproduce the above copyright
018     *    notice, this list of conditions and the following disclaimer in
019     *    the documentation and/or other materials provided with the
020     *    distribution.
021     *
022     * 3. The end-user documentation included with the redistribution, if
023     *    any, must include the following acknowlegement:
024     *       "This product includes software developed by the
025     *        Apache Software Foundation (http://www.apache.org/)."
026     *    Alternately, this acknowlegement may appear in the software itself,
027     *    if and wherever such third-party acknowlegements normally appear.
028     *
029     * 4. The names "Ant" and "Apache Software
030     *    Foundation" must not be used to endorse or promote products derived
031     *    from this software without prior written permission. For written
032     *    permission, please contact apache@apache.org.
033     *
034     * 5. Products derived from this software may not be called "Apache"
035     *    nor may "Apache" appear in their names without prior written
036     *    permission of the Apache Group.
037     *
038     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
039     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
040     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
041     * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
042     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
045     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
046     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
047     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
048     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
049     * SUCH DAMAGE.
050     * ====================================================================
051     *
052     * This software consists of voluntary contributions made by many
053     * individuals on behalf of the Apache Software Foundation.  For more
054     * information on the Apache Software Foundation, please see
055     * <http://www.apache.org/>.
056     */
057    
058    package net.sourceforge.cobertura.ant;
059    
060    import java.util.Arrays;
061    import java.util.HashSet;
062    import java.util.Iterator;
063    import java.util.Set;
064    
065    import org.apache.tools.ant.BuildException;
066    import org.apache.tools.ant.Project;
067    import org.apache.tools.ant.types.FileSet;
068    
069    public class MergeTask extends CommonMatchingTask
070    {
071    
072            public MergeTask()
073            {
074                    super("net.sourceforge.cobertura.merge.Main");
075            }
076    
077            void merge(String[] filenames)
078            {
079                    if (filenames.length == 0)
080                    {
081                            return;
082                    }
083    
084                    for (int i = 0; i < filenames.length; i++)
085                    {
086                            getProject().log("Adding " + filenames[i] + " to list",
087                                            Project.MSG_VERBOSE);
088                            addArg("-i");
089                            addArg(filenames[i]);
090                    }
091    
092                    saveArgs();
093    
094                    if (getJava().executeJava() != 0)
095                    {
096                            throw new BuildException();
097                    }
098            }
099    
100            public void execute() throws BuildException
101            {
102                    initArgs();
103                    if (toDir != null)
104                    {
105                            addArg("-o");
106                            addArg(toDir.toString());
107                    }
108    
109                    Set filenames = new HashSet();
110                    Iterator i = fileSets.iterator();
111                    while (i.hasNext())
112                    {
113                            FileSet fileSet = (FileSet)i.next();
114                            filenames.addAll(Arrays.asList(getFilenames(fileSet)));
115                    }
116    
117                    merge((String[])filenames.toArray(new String[filenames.size()]));
118    
119                    unInitArgs();
120            }
121    }