 
 package org.apache.tools.ant.taskdefs;
 
 import org.apache.tools.ant.*;
 import org.apache.tools.ant.types.*;
 import java.io.*;
 import java.util.*;
 
 /**
  * Delete a single associated file or a set of associated files. For example, if you provide a fileset of .jsp files, and supply .java as the associated file type this class will for each .jsp file delete the corresponding .java file of the same name.
  *
  * @author Richard Hansen
  */
  
 public class DeleteAssociated extends MatchingTask
 {
 	private String associatedFileType = "";
 	private int verbosity = Project.MSG_VERBOSE;
 	private Vector filesets = new Vector();
 	
 	/**
 	 * Set the filetype to delete.
 	 *
 	 * @param Filetype The corresponding associated file type to delete.
 	 */
 	public void setFiletype(String Filetype)
 	{
 		associatedFileType = Filetype;
 	}
 	
 	/**
	 * Adds a set of files (nested fileset attribute).
	 */
	public void addFileset(FileSet set) {
		filesets.addElement(set);
    }
    
    /**
	 * Used to force listing of all names of deleted files.
	 * 
	 * @param verbose "true" or "on"
	 */
	public void setVerbose(String verbose) {
		if ("true".equalsIgnoreCase(verbose.trim()) || "on".equalsIgnoreCase(verbose.trim())) {
			this.verbosity = Project.MSG_INFO;
		} else {
			this.verbosity = Project.MSG_VERBOSE;
		} 
    } 
 	
 	/**
 	 * Delete the associated files
 	 *
 	 * @throws BuildException
 	 */
 	public void execute() throws BuildException
 	{
 		try
 		{
 		log("inside the execute method of DeleteAssociated "+String.valueOf(filesets.size()),verbosity);
 		for (int k=0;k<filesets.size();k++)
 		{
 			FileSet fs = (FileSet) filesets.elementAt(k);
 			DirectoryScanner ds = fs.getDirectoryScanner(project);
 			String[] files = ds.getIncludedFiles();
 			
 			if (files.length > 0)
 			{
 				for(int i=0;i<files.length; i++)
 				{
 					File f = new File(fs.getDir(project),files[i]);
 					int dotIndex = files[i].indexOf('.');
 					String newFileName = ((dotIndex != -1)?files[i].substring(0,dotIndex):files[i])+associatedFileType;
 					
 					File associatedFile = new File(f.getParent(),newFileName);
 					if (associatedFile.exists())
 					{
 						if (associatedFile.delete())
 							log("deleting "+associatedFile.getAbsolutePath(),verbosity);
 					}
 					f = null;
 					associatedFile = null;
 				}
 			}
 		}
 		}catch(Exception e)
 		{}
 	}
 }


