/*
    FileExists.java - an Ant task to determine if a file or directory
    exists and set a property indicating such.
    Copyright (C) 2004 by Christopher R. Jones.  All Rights Reserved.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program 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 General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.io.File;
import org.apache.tools.ant.Task;

/**
 * Checks to see if a file exists.  Sets a property to true
 * if the file exists.
 * 
 * @author <a href="mailto:chris%40mischiefbox.com">chris&#64;mischiefbox.com</a>
 */
public class FileExists extends Task {
	/**
	 * The File that is being tested.
	 */
	File f;
	
	/**
	 * The property name containing the results of the test.
	 */
	String sProp;
	
	/**
	 *  Pass-through constructor.
	 */
	public FileExists() {
		super();
	}

	/**
	 * The filename or directory to check.
	 * 
	 * @param filename the filename or directory to check.
	 */
	public void setFilename(String filename) {
		f = new File(filename);
	}
	
	/**
	 * The property that will contain the results of the file
	 * check, either "true" or "false".
	 * 
	 * @param sProp the property name containing the results.
	 */
	public void setProperty(String sProp) {
		this.sProp = sProp;
	}
	
	/**
	 * Performs the FileExists task.
	 */
	public void execute() {
		// only set the property if the file exists
		if (f.exists()) {
			// add the results of the file check to the project properties
			getProject().setProperty(sProp, "true");
		}
	}
}
