/*
 * FindBugs - Find Bugs in Java programs
 * Copyright (C) 2003-2007 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.anttask;

import java.io.File;

import org.apache.tools.ant.BuildException;

/**
 * Ant task to invoke the SetBugDatabaseInfo program in the
 * workflow package
 * 
 * @author David Hovemeyer
 * @author Ben Langmead
 */
public class SetBugDatabaseInfoTask extends AbstractFindBugsTask {
	
	private File outputFile;
    private String name;
	private String timestamp;
	private String source;
    private String findSource;
    private String suppress;
    private String withMessages;
    private String resetSource;
	private DataFile inputFile;

	public SetBugDatabaseInfoTask() {
    	super("edu.umd.cs.findbugs.workflow.SetBugDatabaseInfo");
    	setFailOnError(true);
    }
	
	public DataFile createDataFile() {
		if (inputFile != null) {
			throw new BuildException("only one dataFile element is allowed", getLocation());
		}
		inputFile = new DataFile();
		return inputFile;
	}
	
    public void setName(String arg) {
        this.name = arg;
    }

    public void setTimestamp(String arg) {
        this.timestamp = arg;
    }

    public void setOutput(File output) {
		this.outputFile = output;
	}
	
	public void setInput(String input) {
		this.inputFile = new DataFile();
		this.inputFile.name = input;
	}
            
    public void setSuppress(String arg) {
    	this.suppress = arg;
    }
    
    public void setSource(String arg) {
    	this.source = arg;
    }

    public void setFindSource(String arg) {
        this.findSource = arg;
    }

    public void setWithMessages(String arg) {
        this.withMessages = arg;
    }

    public void setResetSource(String arg) {
        this.resetSource = arg;
    }

    private void checkBoolean(String attrVal, String attrName) {
    	if (attrVal == null) {
    		return;
    	}
    	attrVal = attrVal.toLowerCase();
    	if (!attrVal.equals("true") && !attrVal.equals("false")) {
    		throw new BuildException("attribute " + attrName + " requires boolean value", getLocation());
    	}
    }
    
    /* (non-Javadoc)
     * @see edu.umd.cs.findbugs.anttask.AbstractFindBugsTask#checkParameters()
     */
    @Override
    protected void checkParameters() {
    	super.checkParameters();
    	
    	if (outputFile == null) {
    		throw new BuildException("output attribute is required", getLocation());
    	}
    	
    	if (inputFile == null) {
    		throw new BuildException("inputFile element is required");
    	}
    	
    	checkBoolean(withMessages, "withMessages");
    	checkBoolean(resetSource, "resetSource");
    }
    
    private void addOption(String name, String value) {
    	if (value != null) {
    		addArg(name);
    		addArg(value);
    	}
    }
    
    public void addBoolOption(String option, String value) {
    	if (value != null) {
    		addArg(option + ":" + value);
    	}
    }

	/* (non-Javadoc)
	 * @see edu.umd.cs.findbugs.anttask.AbstractFindBugsTask#configureFindbugsEngine()
	 */
	@Override
	protected void configureFindbugsEngine() {
        addOption("-name", name);
        addOption("-timestamp", timestamp);
		addOption("-source", source);
		addOption("-findSource", findSource);
		addOption("-suppress", suppress);
		addBoolOption("-withMessages", withMessages);
		if(resetSource != null && resetSource.equals("true")) {
            addArg("-resetSource");
		}
		addArg(inputFile.getName());
		if(outputFile != null) {
			// Don't use .getName() because it discards path
	        addArg(outputFile.getAbsolutePath());
		}
	}

	/* (non-Javadoc)
	 * @see edu.umd.cs.findbugs.anttask.AbstractFindBugsTask#beforeExecuteJavaProcess()
	 */
	@Override
	protected void beforeExecuteJavaProcess() {
		log("running setBugDatabaseInfo...");
	}

	/* (non-Javadoc)
	 * @see edu.umd.cs.findbugs.anttask.AbstractFindBugsTask#afterExecuteJavaProcess(int)
	 */
	@Override
	protected void afterExecuteJavaProcess(int rc) {
		if (rc != 0) {
			throw new BuildException("execution of " + getTaskName() + " failed");
		}
	}

}

@@ -1160,20 +1160,22 @@
 private 
-File
+String
  outputF
@@ -1389,24 +1389,22 @@
 private 
-DataFile
+String
  inputFi
@@ -1544,213 +1544,8 @@
  %7D%0A%09
-%0A%09public DataFile createDataFile() %7B%0A%09%09if (inputFile != null) %7B%0A%09%09%09throw new BuildException(%22only one dataFile element is allowed%22, getLocation());%0A%09%09%7D%0A%09%09inputFile = new DataFile();%0A%09%09return inputFile;%0A%09%7D%0A
 %09%0A  
@@ -1718,20 +1718,22 @@
 tOutput(
-File
+String
  output)
@@ -1802,24 +1802,24 @@
 ng input) %7B%0A
+
 %09%09this.input
@@ -1828,48 +1828,8 @@
 le =
- new DataFile();%0A%09%09this.inputFile.name =
  inp
@@ -3913,21 +3913,11 @@
 File
-.getName()
 );%0A
+
 %09%09if
@@ -3944,109 +3944,108 @@
  %7B%0A%09
-%09%09// Don't use .getName() because it discards path%0A%09        addArg(outputFile.getAbsolutePath());%0A%09%09%7D
+        addArg(outputFile);%0A%09%09%7D%0A%09%09System.out.println(%22XXX %22 + getFindbugsEngine().getCommandLine());
 %0A%09%7D%0A

Fix some strangeness in the ant task. [ 2073871 ] Findbugs datamining: setBugDatabaseInfo.output ignores Path