/*
 * FindBugs - Find bugs in Java programs
 * Copyright (C) 2004, University of Maryland
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package edu.umd.cs.findbugs.workflow;

import java.io.IOException;
import java.util.Iterator;

import org.dom4j.DocumentException;

import edu.umd.cs.findbugs.AnalysisError;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.DetectorFactoryCollection;
import edu.umd.cs.findbugs.FindBugs;
import edu.umd.cs.findbugs.Project;
import edu.umd.cs.findbugs.ProjectStats;
import edu.umd.cs.findbugs.SortedBugCollection;
import edu.umd.cs.findbugs.config.CommandLine;

/**
 * Compute the union of two sets of bug results, preserving annotations.
 */
public class UnionResults {

    static class UnionResultsCommandLine extends CommandLine {
        public String outputFile;

        boolean withMessages;

        UnionResultsCommandLine() {
            addSwitch("-withMessages", "Generated XML should contain msgs for external processing");
            addOption("-output", "outputFile", "File in which to store combined results");
        }

        /*
         * (non-Javadoc)
         * 
         * @see
         * edu.umd.cs.findbugs.config.CommandLine#handleOption(java.lang.String,
         * java.lang.String)
         */
        @Override
        protected void handleOption(String option, String optionExtraPart) throws IOException {
            if (option.equals("-withMessages"))
                withMessages = true;
            else
                throw new IllegalArgumentException("Unknown option : " + option);
        }

        /*
         * (non-Javadoc)
         * 
         * @see
         * edu.umd.cs.findbugs.config.CommandLine#handleOptionWithArgument(java
         * .lang.String, java.lang.String)
         */
        @Override
        protected void handleOptionWithArgument(String option, String argument) throws IOException {
            if (option.equals("-output"))
                outputFile = argument;
            else
                throw new IllegalArgumentException("Unknown option : " + option);
        }

    }

    static {
        DetectorFactoryCollection.instance(); // as a side effect, loads
                                              // detector plugins
    }

    static public SortedBugCollection union(SortedBugCollection origCollection, SortedBugCollection newCollection) {

        SortedBugCollection result = origCollection.duplicate();

        for (Iterator<BugInstance> i = newCollection.iterator(); i.hasNext();) {
            BugInstance bugInstance = i.next();
            result.add(bugInstance);
        }
        ProjectStats stats = result.getProjectStats();
        ProjectStats stats2 = newCollection.getProjectStats();
        stats.addStats(stats2);

        Project project = result.getProject();
        Project project2 = newCollection.getProject();
        project.add(project2);
        
        for(AnalysisError error : newCollection.getErrors())
            result.addError(error);

        return result;
    }

    public static void main(String[] argv) throws IOException {

        FindBugs.setNoAnalysis();
        final UnionResultsCommandLine commandLine = new UnionResultsCommandLine();

        int argCount = commandLine.parse(argv, 2, Integer.MAX_VALUE, "Usage: " + UnionResults.class.getName()
                + " [options] [<results1> <results2> ... <resultsn>] ");

        SortedBugCollection results = null;
        for (int i = argCount; i < argv.length; i++) {
            try {
                SortedBugCollection more = new SortedBugCollection();

                more.readXML(argv[i]);
                
                if (results != null) {
                    results = union(results, more);
                } else {
                    results = more;
                }
            } catch (IOException e) {
                System.err.println("Trouble reading/parsing " + argv[i]);
            } catch (DocumentException e) {
                System.err.println("Trouble reading/parsing " + argv[i]);
            }
        }

        if (results == null) {
            System.err.println("No files successfully read");
            System.exit(1);
            return;
        }
        results.setWithMessages(commandLine.withMessages);
        if (commandLine.outputFile == null)
            results.writeXML(System.out);
        else
            results.writeXML(commandLine.outputFile);
    }

}

// vim:ts=3

@@ -890,24 +890,50 @@
 OException;%0A
+import java.util.HashSet;%0A
 import java.
@@ -3096,17 +3096,16 @@
 tion) %7B%0A
-%0A
         
@@ -3161,25 +3161,24 @@
 cate();%0A
-%0A
         
 for (Ite
@@ -3173,90 +3173,189 @@
     
-for (Iterator%3CBugInstance%3E i = newCollection.iterator(); i.hasNext();
+merge(null, result, newCollection);%0A        return result;%0A    %7D%0A    static public void merge(HashSet%3CString%3E hashes, SortedBugCollection into, SortedBugCollection from
 ) %7B%0A
- 
+%0A
         
    B
@@ -3350,19 +3350,21 @@
         
-   
+for (
 BugInsta
@@ -3383,38 +3383,131 @@
 nce 
-= i.next();%0A            result
+: from.getCollection()) %7B%0A            if (hashes == null %7C%7C hashes.add(bugInstance.getInstanceHash()))%0A                into
 .add
@@ -3560,22 +3560,20 @@
 stats = 
-result
+into
 .getProj
@@ -3610,37 +3610,28 @@
 ts stats2 = 
-newCollection
+from
 .getProjectS
@@ -3697,22 +3697,20 @@
 oject = 
-result
+into
 .getProj
@@ -3743,29 +3743,20 @@
 ject2 = 
-newCollection
+from
 .getProj
@@ -3836,29 +3836,20 @@
 error : 
-newCollection
+from
 .getErro
@@ -3866,22 +3866,20 @@
         
-result
+into
 .addErro
@@ -3895,39 +3895,32 @@
 %0A%0A        return
- result
 ;%0A    %7D%0A%0A    pub
@@ -4323,16 +4323,81 @@
 = null;%0A
+        HashSet%3CString%3E hashes = new HashSet%3CString%3E();%0A        %0A
         
@@ -4651,24 +4651,22 @@
     
-results = union(
+merge(hashes, 
 resu
@@ -4738,16 +4738,135 @@
 = more;%0A
+                    for(BugInstance b : more.getCollection())%0A                        hashes.add(b.getInstanceHash());%0A
         

Handle duplicates