Removed the ability of allowing pluggable log levels. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@270409 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -11,10 +11,8 @@ import java.io.File; | |||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| import org.apache.tools.ant.DirectoryScanner; | import org.apache.tools.ant.DirectoryScanner; | ||||
| import org.apache.tools.ant.Project; | |||||
| import org.apache.tools.ant.taskdefs.MatchingTask; | |||||
| import org.apache.tools.ant.Task; | |||||
| import org.apache.tools.ant.types.FileSet; | import org.apache.tools.ant.types.FileSet; | ||||
| import org.apache.tools.ant.types.PatternSet; | |||||
| /** | /** | ||||
| * Deletes a file or directory, or set of files defined by a fileset. The | * Deletes a file or directory, or set of files defined by a fileset. The | ||||
| @@ -34,29 +32,12 @@ import org.apache.tools.ant.types.PatternSet; | |||||
| * @author Jon S. Stevens <a href="mailto:jon@latchkey.com">jon@latchkey.com</a> | * @author Jon S. Stevens <a href="mailto:jon@latchkey.com">jon@latchkey.com</a> | ||||
| */ | */ | ||||
| public class Delete | public class Delete | ||||
| extends MatchingTask | |||||
| extends Task | |||||
| { | { | ||||
| protected File file = null; | |||||
| protected File dir = null; | |||||
| protected ArrayList filesets = new ArrayList(); | |||||
| protected boolean usedMatchingTask = false; | |||||
| protected boolean includeEmpty = false;// by default, remove matching empty dirs | |||||
| private int verbosity = Project.MSG_VERBOSE; | |||||
| private boolean quiet = false; | |||||
| private boolean failonerror = true; | |||||
| /** | |||||
| * Sets whether default exclusions should be used or not. | |||||
| * | |||||
| * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions | |||||
| * should be used, "false"|"off"|"no" when they shouldn't be used. | |||||
| */ | |||||
| public void setDefaultexcludes( boolean useDefaultExcludes ) | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| super.setDefaultexcludes( useDefaultExcludes ); | |||||
| } | |||||
| private final ArrayList filesets = new ArrayList(); | |||||
| private File m_dir; | |||||
| private File m_file; | |||||
| private boolean includeEmpty;// by default, remove matching empty dirs | |||||
| /** | /** | ||||
| * Set the directory from which files are to be deleted | * Set the directory from which files are to be deleted | ||||
| @@ -65,43 +46,7 @@ public class Delete | |||||
| */ | */ | ||||
| public void setDir( File dir ) | public void setDir( File dir ) | ||||
| { | { | ||||
| this.dir = dir; | |||||
| } | |||||
| /** | |||||
| * Sets the set of exclude patterns. Patterns may be separated by a comma or | |||||
| * a space. | |||||
| * | |||||
| * @param excludes the string containing the exclude patterns | |||||
| */ | |||||
| public void setExcludes( String excludes ) | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| super.setExcludes( excludes ); | |||||
| } | |||||
| /** | |||||
| * Sets the name of the file containing the includes patterns. | |||||
| * | |||||
| * @param excludesfile A string containing the filename to fetch the include | |||||
| * patterns from. | |||||
| */ | |||||
| public void setExcludesfile( File excludesfile ) | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| super.setExcludesfile( excludesfile ); | |||||
| } | |||||
| /** | |||||
| * this flag means 'note errors to the output, but keep going' | |||||
| * | |||||
| * @param failonerror true or false | |||||
| */ | |||||
| public void setFailOnError( boolean failonerror ) | |||||
| { | |||||
| this.failonerror = failonerror; | |||||
| m_dir = dir; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -111,78 +56,7 @@ public class Delete | |||||
| */ | */ | ||||
| public void setFile( File file ) | public void setFile( File file ) | ||||
| { | { | ||||
| this.file = file; | |||||
| } | |||||
| /** | |||||
| * Used to delete empty directories. | |||||
| * | |||||
| * @param includeEmpty The new IncludeEmptyDirs value | |||||
| */ | |||||
| public void setIncludeEmptyDirs( boolean includeEmpty ) | |||||
| { | |||||
| this.includeEmpty = includeEmpty; | |||||
| } | |||||
| /** | |||||
| * Sets the set of include patterns. Patterns may be separated by a comma or | |||||
| * a space. | |||||
| * | |||||
| * @param includes the string containing the include patterns | |||||
| */ | |||||
| public void setIncludes( String includes ) | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| super.setIncludes( includes ); | |||||
| } | |||||
| /** | |||||
| * Sets the name of the file containing the includes patterns. | |||||
| * | |||||
| * @param includesfile A string containing the filename to fetch the include | |||||
| * patterns from. | |||||
| */ | |||||
| public void setIncludesfile( File includesfile ) | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| super.setIncludesfile( includesfile ); | |||||
| } | |||||
| /** | |||||
| * If the file does not exist, do not display a diagnostic message or modify | |||||
| * the exit status to reflect an error. This means that if a file or | |||||
| * directory cannot be deleted, then no error is reported. This setting | |||||
| * emulates the -f option to the Unix "rm" command. Default is | |||||
| * false meaning things are "noisy" | |||||
| * | |||||
| * @param quiet "true" or "on" | |||||
| */ | |||||
| public void setQuiet( boolean quiet ) | |||||
| { | |||||
| this.quiet = quiet; | |||||
| if( quiet ) | |||||
| { | |||||
| this.failonerror = false; | |||||
| } | |||||
| } | |||||
| /** | |||||
| * Used to force listing of all names of deleted files. | |||||
| * | |||||
| * @param verbose "true" or "on" | |||||
| */ | |||||
| public void setVerbose( boolean verbose ) | |||||
| { | |||||
| if( verbose ) | |||||
| { | |||||
| this.verbosity = Project.MSG_INFO; | |||||
| } | |||||
| else | |||||
| { | |||||
| this.verbosity = Project.MSG_VERBOSE; | |||||
| } | |||||
| m_file = file; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -195,42 +69,6 @@ public class Delete | |||||
| filesets.add( set ); | filesets.add( set ); | ||||
| } | } | ||||
| /** | |||||
| * add a name entry on the exclude list | |||||
| * | |||||
| * @return Description of the Returned Value | |||||
| */ | |||||
| public PatternSet.NameEntry createExclude() | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| return super.createExclude(); | |||||
| } | |||||
| /** | |||||
| * add a name entry on the include list | |||||
| * | |||||
| * @return Description of the Returned Value | |||||
| */ | |||||
| public PatternSet.NameEntry createInclude() | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| return super.createInclude(); | |||||
| } | |||||
| /** | |||||
| * add a set of patterns | |||||
| * | |||||
| * @return Description of the Returned Value | |||||
| */ | |||||
| public PatternSet createPatternSet() | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| return super.createPatternSet(); | |||||
| } | |||||
| /** | /** | ||||
| * Delete the file(s). | * Delete the file(s). | ||||
| * | * | ||||
| @@ -239,65 +77,45 @@ public class Delete | |||||
| public void execute() | public void execute() | ||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| if( usedMatchingTask ) | |||||
| { | |||||
| getLogger().info( "DEPRECATED - Use of the implicit FileSet is deprecated. Use a nested fileset element instead." ); | |||||
| } | |||||
| if( file == null && dir == null && filesets.size() == 0 ) | |||||
| if( m_file == null && m_dir == null && filesets.size() == 0 ) | |||||
| { | { | ||||
| throw new TaskException( "At least one of the file or dir attributes, or a fileset element, must be set." ); | |||||
| } | |||||
| if( quiet && failonerror ) | |||||
| { | |||||
| throw new TaskException( "quiet and failonerror cannot both be set to true" ); | |||||
| final String message = "At least one of the file or dir attributes, " + | |||||
| "or a fileset element, must be set."; | |||||
| throw new TaskException( message ); | |||||
| } | } | ||||
| // delete the single file | // delete the single file | ||||
| if( file != null ) | |||||
| if( null != m_file ) | |||||
| { | { | ||||
| if( file.exists() ) | |||||
| if( m_file.exists() ) | |||||
| { | { | ||||
| if( file.isDirectory() ) | |||||
| if( m_file.isDirectory() ) | |||||
| { | { | ||||
| getLogger().info( "Directory " + file.getAbsolutePath() + " cannot be removed using the file attribute. Use dir instead." ); | |||||
| final String message = "Directory " + m_file.getAbsolutePath() + | |||||
| " cannot be removed using the file attribute. Use dir instead."; | |||||
| getLogger().info( message ); | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| getLogger().info( "Deleting: " + file.getAbsolutePath() ); | |||||
| if( !file.delete() ) | |||||
| getLogger().info( "Deleting: " + m_file.getAbsolutePath() ); | |||||
| if( !m_file.delete() ) | |||||
| { | { | ||||
| String message = "Unable to delete file " + file.getAbsolutePath(); | |||||
| if( failonerror ) | |||||
| throw new TaskException( message ); | |||||
| else | |||||
| log( message, | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| String message = "Unable to delete file " + m_file.getAbsolutePath(); | |||||
| throw new TaskException( message ); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| getLogger().debug( "Could not find file " + file.getAbsolutePath() + " to delete." ); | |||||
| getLogger().debug( "Could not find file " + m_file.getAbsolutePath() + " to delete." ); | |||||
| } | } | ||||
| } | } | ||||
| // delete the directory | // delete the directory | ||||
| if( dir != null && dir.exists() && dir.isDirectory() && !usedMatchingTask ) | |||||
| if( m_dir != null && m_dir.exists() && m_dir.isDirectory() ) | |||||
| { | { | ||||
| /* | |||||
| * If verbosity is MSG_VERBOSE, that mean we are doing regular logging | |||||
| * (backwards as that sounds). In that case, we want to print one | |||||
| * message about deleting the top of the directory tree. Otherwise, | |||||
| * the removeDir method will handle messages for _all_ directories. | |||||
| */ | |||||
| if( verbosity == Project.MSG_VERBOSE ) | |||||
| { | |||||
| getLogger().info( "Deleting directory " + dir.getAbsolutePath() ); | |||||
| } | |||||
| removeDir( dir ); | |||||
| getLogger().info( "Deleting directory " + m_dir.getAbsolutePath() ); | |||||
| removeDir( m_dir ); | |||||
| } | } | ||||
| // delete the files in the filesets | // delete the files in the filesets | ||||
| @@ -314,40 +132,7 @@ public class Delete | |||||
| catch( TaskException be ) | catch( TaskException be ) | ||||
| { | { | ||||
| // directory doesn't exist or is not readable | // directory doesn't exist or is not readable | ||||
| if( failonerror ) | |||||
| { | |||||
| throw be; | |||||
| } | |||||
| else | |||||
| { | |||||
| log( be.getMessage(), | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| } | |||||
| } | |||||
| } | |||||
| // delete the files from the default fileset | |||||
| if( usedMatchingTask && dir != null ) | |||||
| { | |||||
| try | |||||
| { | |||||
| DirectoryScanner ds = super.getDirectoryScanner( dir ); | |||||
| String[] files = ds.getIncludedFiles(); | |||||
| String[] dirs = ds.getIncludedDirectories(); | |||||
| removeFiles( dir, files, dirs ); | |||||
| } | |||||
| catch( TaskException be ) | |||||
| { | |||||
| // directory doesn't exist or is not readable | |||||
| if( failonerror ) | |||||
| { | |||||
| throw be; | |||||
| } | |||||
| else | |||||
| { | |||||
| log( be.getMessage(), | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| } | |||||
| throw be; | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -356,44 +141,42 @@ public class Delete | |||||
| // protected and private methods | // protected and private methods | ||||
| //************************************************************************ | //************************************************************************ | ||||
| protected void removeDir( File d ) | |||||
| protected void removeDir( final File baseDir ) | |||||
| throws TaskException | |||||
| { | |||||
| final File[] list = baseDir.listFiles(); | |||||
| if( list != null ) | |||||
| { | |||||
| deleteFiles( list ); | |||||
| } | |||||
| getLogger().debug( "Deleting directory " + baseDir.getAbsolutePath() ); | |||||
| if( !baseDir.delete() ) | |||||
| { | |||||
| String message = "Unable to delete directory " + m_dir.getAbsolutePath(); | |||||
| throw new TaskException( message ); | |||||
| } | |||||
| } | |||||
| private void deleteFiles( final File[] list ) | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| String[] list = d.list(); | |||||
| if( list == null ) | |||||
| list = new String[ 0 ]; | |||||
| for( int i = 0; i < list.length; i++ ) | for( int i = 0; i < list.length; i++ ) | ||||
| { | { | ||||
| String s = list[ i ]; | |||||
| File f = new File( d, s ); | |||||
| if( f.isDirectory() ) | |||||
| final File file = list[ i ]; | |||||
| if( file.isDirectory() ) | |||||
| { | { | ||||
| removeDir( f ); | |||||
| removeDir( file ); | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| log( "Deleting " + f.getAbsolutePath(), verbosity ); | |||||
| if( !f.delete() ) | |||||
| getLogger().debug( "Deleting " + file.getAbsolutePath() ); | |||||
| if( !file.delete() ) | |||||
| { | { | ||||
| String message = "Unable to delete file " + f.getAbsolutePath(); | |||||
| if( failonerror ) | |||||
| throw new TaskException( message ); | |||||
| else | |||||
| log( message, | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| String message = "Unable to delete file " + file.getAbsolutePath(); | |||||
| throw new TaskException( message ); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| log( "Deleting directory " + d.getAbsolutePath(), verbosity ); | |||||
| if( !d.delete() ) | |||||
| { | |||||
| String message = "Unable to delete directory " + dir.getAbsolutePath(); | |||||
| if( failonerror ) | |||||
| throw new TaskException( message ); | |||||
| else | |||||
| log( message, | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| } | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -404,24 +187,23 @@ public class Delete | |||||
| * @param files array of files to delete; can be of zero length | * @param files array of files to delete; can be of zero length | ||||
| * @param dirs array of directories to delete; can of zero length | * @param dirs array of directories to delete; can of zero length | ||||
| */ | */ | ||||
| protected void removeFiles( File d, String[] files, String[] dirs ) | |||||
| protected void removeFiles( final File baseDir, | |||||
| final String[] files, | |||||
| final String[] dirs ) | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| if( files.length > 0 ) | if( files.length > 0 ) | ||||
| { | { | ||||
| getLogger().info( "Deleting " + files.length + " files from " + d.getAbsolutePath() ); | |||||
| for( int j = 0; j < files.length; j++ ) | |||||
| final String message = "Deleting " + files.length + " files from " + baseDir.getAbsolutePath(); | |||||
| getLogger().info( message ); | |||||
| for( int i = 0; i < files.length; i++ ) | |||||
| { | { | ||||
| File f = new File( d, files[ j ] ); | |||||
| log( "Deleting " + f.getAbsolutePath(), verbosity ); | |||||
| if( !f.delete() ) | |||||
| final File file = new File( baseDir, files[ i ] ); | |||||
| getLogger().debug( "Deleting " + file.getAbsolutePath() ); | |||||
| if( !file.delete() ) | |||||
| { | { | ||||
| String message = "Unable to delete file " + f.getAbsolutePath(); | |||||
| if( failonerror ) | |||||
| throw new TaskException( message ); | |||||
| else | |||||
| log( message, | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| String message2 = "Unable to delete file " + file.getAbsolutePath(); | |||||
| throw new TaskException( message2 ); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -431,20 +213,16 @@ public class Delete | |||||
| int dirCount = 0; | int dirCount = 0; | ||||
| for( int j = dirs.length - 1; j >= 0; j-- ) | for( int j = dirs.length - 1; j >= 0; j-- ) | ||||
| { | { | ||||
| File dir = new File( d, dirs[ j ] ); | |||||
| File dir = new File( baseDir, dirs[ j ] ); | |||||
| String[] dirFiles = dir.list(); | String[] dirFiles = dir.list(); | ||||
| if( dirFiles == null || dirFiles.length == 0 ) | if( dirFiles == null || dirFiles.length == 0 ) | ||||
| { | { | ||||
| log( "Deleting " + dir.getAbsolutePath(), verbosity ); | |||||
| getLogger().debug( "Deleting " + dir.getAbsolutePath() ); | |||||
| if( !dir.delete() ) | if( !dir.delete() ) | ||||
| { | { | ||||
| String message = "Unable to delete directory " | |||||
| + dir.getAbsolutePath(); | |||||
| if( failonerror ) | |||||
| throw new TaskException( message ); | |||||
| else | |||||
| log( message, | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| final String message = | |||||
| "Unable to delete directory " + dir.getAbsolutePath(); | |||||
| throw new TaskException( message ); | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| @@ -455,9 +233,9 @@ public class Delete | |||||
| if( dirCount > 0 ) | if( dirCount > 0 ) | ||||
| { | { | ||||
| getLogger().info( "Deleted " + dirCount + " director" + | |||||
| ( dirCount == 1 ? "y" : "ies" ) + | |||||
| " from " + d.getAbsolutePath() ); | |||||
| final String message = "Deleted " + dirCount + " director" + | |||||
| ( dirCount == 1 ? "y" : "ies" ) + " from " + baseDir.getAbsolutePath(); | |||||
| getLogger().info( message ); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -11,10 +11,8 @@ import java.io.File; | |||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||
| import org.apache.myrmidon.api.TaskException; | import org.apache.myrmidon.api.TaskException; | ||||
| import org.apache.tools.ant.DirectoryScanner; | import org.apache.tools.ant.DirectoryScanner; | ||||
| import org.apache.tools.ant.Project; | |||||
| import org.apache.tools.ant.taskdefs.MatchingTask; | |||||
| import org.apache.tools.ant.Task; | |||||
| import org.apache.tools.ant.types.FileSet; | import org.apache.tools.ant.types.FileSet; | ||||
| import org.apache.tools.ant.types.PatternSet; | |||||
| /** | /** | ||||
| * Deletes a file or directory, or set of files defined by a fileset. The | * Deletes a file or directory, or set of files defined by a fileset. The | ||||
| @@ -34,29 +32,12 @@ import org.apache.tools.ant.types.PatternSet; | |||||
| * @author Jon S. Stevens <a href="mailto:jon@latchkey.com">jon@latchkey.com</a> | * @author Jon S. Stevens <a href="mailto:jon@latchkey.com">jon@latchkey.com</a> | ||||
| */ | */ | ||||
| public class Delete | public class Delete | ||||
| extends MatchingTask | |||||
| extends Task | |||||
| { | { | ||||
| protected File file = null; | |||||
| protected File dir = null; | |||||
| protected ArrayList filesets = new ArrayList(); | |||||
| protected boolean usedMatchingTask = false; | |||||
| protected boolean includeEmpty = false;// by default, remove matching empty dirs | |||||
| private int verbosity = Project.MSG_VERBOSE; | |||||
| private boolean quiet = false; | |||||
| private boolean failonerror = true; | |||||
| /** | |||||
| * Sets whether default exclusions should be used or not. | |||||
| * | |||||
| * @param useDefaultExcludes "true"|"on"|"yes" when default exclusions | |||||
| * should be used, "false"|"off"|"no" when they shouldn't be used. | |||||
| */ | |||||
| public void setDefaultexcludes( boolean useDefaultExcludes ) | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| super.setDefaultexcludes( useDefaultExcludes ); | |||||
| } | |||||
| private final ArrayList filesets = new ArrayList(); | |||||
| private File m_dir; | |||||
| private File m_file; | |||||
| private boolean includeEmpty;// by default, remove matching empty dirs | |||||
| /** | /** | ||||
| * Set the directory from which files are to be deleted | * Set the directory from which files are to be deleted | ||||
| @@ -65,43 +46,7 @@ public class Delete | |||||
| */ | */ | ||||
| public void setDir( File dir ) | public void setDir( File dir ) | ||||
| { | { | ||||
| this.dir = dir; | |||||
| } | |||||
| /** | |||||
| * Sets the set of exclude patterns. Patterns may be separated by a comma or | |||||
| * a space. | |||||
| * | |||||
| * @param excludes the string containing the exclude patterns | |||||
| */ | |||||
| public void setExcludes( String excludes ) | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| super.setExcludes( excludes ); | |||||
| } | |||||
| /** | |||||
| * Sets the name of the file containing the includes patterns. | |||||
| * | |||||
| * @param excludesfile A string containing the filename to fetch the include | |||||
| * patterns from. | |||||
| */ | |||||
| public void setExcludesfile( File excludesfile ) | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| super.setExcludesfile( excludesfile ); | |||||
| } | |||||
| /** | |||||
| * this flag means 'note errors to the output, but keep going' | |||||
| * | |||||
| * @param failonerror true or false | |||||
| */ | |||||
| public void setFailOnError( boolean failonerror ) | |||||
| { | |||||
| this.failonerror = failonerror; | |||||
| m_dir = dir; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -111,78 +56,7 @@ public class Delete | |||||
| */ | */ | ||||
| public void setFile( File file ) | public void setFile( File file ) | ||||
| { | { | ||||
| this.file = file; | |||||
| } | |||||
| /** | |||||
| * Used to delete empty directories. | |||||
| * | |||||
| * @param includeEmpty The new IncludeEmptyDirs value | |||||
| */ | |||||
| public void setIncludeEmptyDirs( boolean includeEmpty ) | |||||
| { | |||||
| this.includeEmpty = includeEmpty; | |||||
| } | |||||
| /** | |||||
| * Sets the set of include patterns. Patterns may be separated by a comma or | |||||
| * a space. | |||||
| * | |||||
| * @param includes the string containing the include patterns | |||||
| */ | |||||
| public void setIncludes( String includes ) | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| super.setIncludes( includes ); | |||||
| } | |||||
| /** | |||||
| * Sets the name of the file containing the includes patterns. | |||||
| * | |||||
| * @param includesfile A string containing the filename to fetch the include | |||||
| * patterns from. | |||||
| */ | |||||
| public void setIncludesfile( File includesfile ) | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| super.setIncludesfile( includesfile ); | |||||
| } | |||||
| /** | |||||
| * If the file does not exist, do not display a diagnostic message or modify | |||||
| * the exit status to reflect an error. This means that if a file or | |||||
| * directory cannot be deleted, then no error is reported. This setting | |||||
| * emulates the -f option to the Unix "rm" command. Default is | |||||
| * false meaning things are "noisy" | |||||
| * | |||||
| * @param quiet "true" or "on" | |||||
| */ | |||||
| public void setQuiet( boolean quiet ) | |||||
| { | |||||
| this.quiet = quiet; | |||||
| if( quiet ) | |||||
| { | |||||
| this.failonerror = false; | |||||
| } | |||||
| } | |||||
| /** | |||||
| * Used to force listing of all names of deleted files. | |||||
| * | |||||
| * @param verbose "true" or "on" | |||||
| */ | |||||
| public void setVerbose( boolean verbose ) | |||||
| { | |||||
| if( verbose ) | |||||
| { | |||||
| this.verbosity = Project.MSG_INFO; | |||||
| } | |||||
| else | |||||
| { | |||||
| this.verbosity = Project.MSG_VERBOSE; | |||||
| } | |||||
| m_file = file; | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -195,42 +69,6 @@ public class Delete | |||||
| filesets.add( set ); | filesets.add( set ); | ||||
| } | } | ||||
| /** | |||||
| * add a name entry on the exclude list | |||||
| * | |||||
| * @return Description of the Returned Value | |||||
| */ | |||||
| public PatternSet.NameEntry createExclude() | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| return super.createExclude(); | |||||
| } | |||||
| /** | |||||
| * add a name entry on the include list | |||||
| * | |||||
| * @return Description of the Returned Value | |||||
| */ | |||||
| public PatternSet.NameEntry createInclude() | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| return super.createInclude(); | |||||
| } | |||||
| /** | |||||
| * add a set of patterns | |||||
| * | |||||
| * @return Description of the Returned Value | |||||
| */ | |||||
| public PatternSet createPatternSet() | |||||
| throws TaskException | |||||
| { | |||||
| usedMatchingTask = true; | |||||
| return super.createPatternSet(); | |||||
| } | |||||
| /** | /** | ||||
| * Delete the file(s). | * Delete the file(s). | ||||
| * | * | ||||
| @@ -239,65 +77,45 @@ public class Delete | |||||
| public void execute() | public void execute() | ||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| if( usedMatchingTask ) | |||||
| { | |||||
| getLogger().info( "DEPRECATED - Use of the implicit FileSet is deprecated. Use a nested fileset element instead." ); | |||||
| } | |||||
| if( file == null && dir == null && filesets.size() == 0 ) | |||||
| if( m_file == null && m_dir == null && filesets.size() == 0 ) | |||||
| { | { | ||||
| throw new TaskException( "At least one of the file or dir attributes, or a fileset element, must be set." ); | |||||
| } | |||||
| if( quiet && failonerror ) | |||||
| { | |||||
| throw new TaskException( "quiet and failonerror cannot both be set to true" ); | |||||
| final String message = "At least one of the file or dir attributes, " + | |||||
| "or a fileset element, must be set."; | |||||
| throw new TaskException( message ); | |||||
| } | } | ||||
| // delete the single file | // delete the single file | ||||
| if( file != null ) | |||||
| if( null != m_file ) | |||||
| { | { | ||||
| if( file.exists() ) | |||||
| if( m_file.exists() ) | |||||
| { | { | ||||
| if( file.isDirectory() ) | |||||
| if( m_file.isDirectory() ) | |||||
| { | { | ||||
| getLogger().info( "Directory " + file.getAbsolutePath() + " cannot be removed using the file attribute. Use dir instead." ); | |||||
| final String message = "Directory " + m_file.getAbsolutePath() + | |||||
| " cannot be removed using the file attribute. Use dir instead."; | |||||
| getLogger().info( message ); | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| getLogger().info( "Deleting: " + file.getAbsolutePath() ); | |||||
| if( !file.delete() ) | |||||
| getLogger().info( "Deleting: " + m_file.getAbsolutePath() ); | |||||
| if( !m_file.delete() ) | |||||
| { | { | ||||
| String message = "Unable to delete file " + file.getAbsolutePath(); | |||||
| if( failonerror ) | |||||
| throw new TaskException( message ); | |||||
| else | |||||
| log( message, | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| String message = "Unable to delete file " + m_file.getAbsolutePath(); | |||||
| throw new TaskException( message ); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| getLogger().debug( "Could not find file " + file.getAbsolutePath() + " to delete." ); | |||||
| getLogger().debug( "Could not find file " + m_file.getAbsolutePath() + " to delete." ); | |||||
| } | } | ||||
| } | } | ||||
| // delete the directory | // delete the directory | ||||
| if( dir != null && dir.exists() && dir.isDirectory() && !usedMatchingTask ) | |||||
| if( m_dir != null && m_dir.exists() && m_dir.isDirectory() ) | |||||
| { | { | ||||
| /* | |||||
| * If verbosity is MSG_VERBOSE, that mean we are doing regular logging | |||||
| * (backwards as that sounds). In that case, we want to print one | |||||
| * message about deleting the top of the directory tree. Otherwise, | |||||
| * the removeDir method will handle messages for _all_ directories. | |||||
| */ | |||||
| if( verbosity == Project.MSG_VERBOSE ) | |||||
| { | |||||
| getLogger().info( "Deleting directory " + dir.getAbsolutePath() ); | |||||
| } | |||||
| removeDir( dir ); | |||||
| getLogger().info( "Deleting directory " + m_dir.getAbsolutePath() ); | |||||
| removeDir( m_dir ); | |||||
| } | } | ||||
| // delete the files in the filesets | // delete the files in the filesets | ||||
| @@ -314,40 +132,7 @@ public class Delete | |||||
| catch( TaskException be ) | catch( TaskException be ) | ||||
| { | { | ||||
| // directory doesn't exist or is not readable | // directory doesn't exist or is not readable | ||||
| if( failonerror ) | |||||
| { | |||||
| throw be; | |||||
| } | |||||
| else | |||||
| { | |||||
| log( be.getMessage(), | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| } | |||||
| } | |||||
| } | |||||
| // delete the files from the default fileset | |||||
| if( usedMatchingTask && dir != null ) | |||||
| { | |||||
| try | |||||
| { | |||||
| DirectoryScanner ds = super.getDirectoryScanner( dir ); | |||||
| String[] files = ds.getIncludedFiles(); | |||||
| String[] dirs = ds.getIncludedDirectories(); | |||||
| removeFiles( dir, files, dirs ); | |||||
| } | |||||
| catch( TaskException be ) | |||||
| { | |||||
| // directory doesn't exist or is not readable | |||||
| if( failonerror ) | |||||
| { | |||||
| throw be; | |||||
| } | |||||
| else | |||||
| { | |||||
| log( be.getMessage(), | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| } | |||||
| throw be; | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -356,44 +141,42 @@ public class Delete | |||||
| // protected and private methods | // protected and private methods | ||||
| //************************************************************************ | //************************************************************************ | ||||
| protected void removeDir( File d ) | |||||
| protected void removeDir( final File baseDir ) | |||||
| throws TaskException | |||||
| { | |||||
| final File[] list = baseDir.listFiles(); | |||||
| if( list != null ) | |||||
| { | |||||
| deleteFiles( list ); | |||||
| } | |||||
| getLogger().debug( "Deleting directory " + baseDir.getAbsolutePath() ); | |||||
| if( !baseDir.delete() ) | |||||
| { | |||||
| String message = "Unable to delete directory " + m_dir.getAbsolutePath(); | |||||
| throw new TaskException( message ); | |||||
| } | |||||
| } | |||||
| private void deleteFiles( final File[] list ) | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| String[] list = d.list(); | |||||
| if( list == null ) | |||||
| list = new String[ 0 ]; | |||||
| for( int i = 0; i < list.length; i++ ) | for( int i = 0; i < list.length; i++ ) | ||||
| { | { | ||||
| String s = list[ i ]; | |||||
| File f = new File( d, s ); | |||||
| if( f.isDirectory() ) | |||||
| final File file = list[ i ]; | |||||
| if( file.isDirectory() ) | |||||
| { | { | ||||
| removeDir( f ); | |||||
| removeDir( file ); | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| log( "Deleting " + f.getAbsolutePath(), verbosity ); | |||||
| if( !f.delete() ) | |||||
| getLogger().debug( "Deleting " + file.getAbsolutePath() ); | |||||
| if( !file.delete() ) | |||||
| { | { | ||||
| String message = "Unable to delete file " + f.getAbsolutePath(); | |||||
| if( failonerror ) | |||||
| throw new TaskException( message ); | |||||
| else | |||||
| log( message, | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| String message = "Unable to delete file " + file.getAbsolutePath(); | |||||
| throw new TaskException( message ); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| log( "Deleting directory " + d.getAbsolutePath(), verbosity ); | |||||
| if( !d.delete() ) | |||||
| { | |||||
| String message = "Unable to delete directory " + dir.getAbsolutePath(); | |||||
| if( failonerror ) | |||||
| throw new TaskException( message ); | |||||
| else | |||||
| log( message, | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| } | |||||
| } | } | ||||
| /** | /** | ||||
| @@ -404,24 +187,23 @@ public class Delete | |||||
| * @param files array of files to delete; can be of zero length | * @param files array of files to delete; can be of zero length | ||||
| * @param dirs array of directories to delete; can of zero length | * @param dirs array of directories to delete; can of zero length | ||||
| */ | */ | ||||
| protected void removeFiles( File d, String[] files, String[] dirs ) | |||||
| protected void removeFiles( final File baseDir, | |||||
| final String[] files, | |||||
| final String[] dirs ) | |||||
| throws TaskException | throws TaskException | ||||
| { | { | ||||
| if( files.length > 0 ) | if( files.length > 0 ) | ||||
| { | { | ||||
| getLogger().info( "Deleting " + files.length + " files from " + d.getAbsolutePath() ); | |||||
| for( int j = 0; j < files.length; j++ ) | |||||
| final String message = "Deleting " + files.length + " files from " + baseDir.getAbsolutePath(); | |||||
| getLogger().info( message ); | |||||
| for( int i = 0; i < files.length; i++ ) | |||||
| { | { | ||||
| File f = new File( d, files[ j ] ); | |||||
| log( "Deleting " + f.getAbsolutePath(), verbosity ); | |||||
| if( !f.delete() ) | |||||
| final File file = new File( baseDir, files[ i ] ); | |||||
| getLogger().debug( "Deleting " + file.getAbsolutePath() ); | |||||
| if( !file.delete() ) | |||||
| { | { | ||||
| String message = "Unable to delete file " + f.getAbsolutePath(); | |||||
| if( failonerror ) | |||||
| throw new TaskException( message ); | |||||
| else | |||||
| log( message, | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| String message2 = "Unable to delete file " + file.getAbsolutePath(); | |||||
| throw new TaskException( message2 ); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| @@ -431,20 +213,16 @@ public class Delete | |||||
| int dirCount = 0; | int dirCount = 0; | ||||
| for( int j = dirs.length - 1; j >= 0; j-- ) | for( int j = dirs.length - 1; j >= 0; j-- ) | ||||
| { | { | ||||
| File dir = new File( d, dirs[ j ] ); | |||||
| File dir = new File( baseDir, dirs[ j ] ); | |||||
| String[] dirFiles = dir.list(); | String[] dirFiles = dir.list(); | ||||
| if( dirFiles == null || dirFiles.length == 0 ) | if( dirFiles == null || dirFiles.length == 0 ) | ||||
| { | { | ||||
| log( "Deleting " + dir.getAbsolutePath(), verbosity ); | |||||
| getLogger().debug( "Deleting " + dir.getAbsolutePath() ); | |||||
| if( !dir.delete() ) | if( !dir.delete() ) | ||||
| { | { | ||||
| String message = "Unable to delete directory " | |||||
| + dir.getAbsolutePath(); | |||||
| if( failonerror ) | |||||
| throw new TaskException( message ); | |||||
| else | |||||
| log( message, | |||||
| quiet ? Project.MSG_VERBOSE : Project.MSG_WARN ); | |||||
| final String message = | |||||
| "Unable to delete directory " + dir.getAbsolutePath(); | |||||
| throw new TaskException( message ); | |||||
| } | } | ||||
| else | else | ||||
| { | { | ||||
| @@ -455,9 +233,9 @@ public class Delete | |||||
| if( dirCount > 0 ) | if( dirCount > 0 ) | ||||
| { | { | ||||
| getLogger().info( "Deleted " + dirCount + " director" + | |||||
| ( dirCount == 1 ? "y" : "ies" ) + | |||||
| " from " + d.getAbsolutePath() ); | |||||
| final String message = "Deleted " + dirCount + " director" + | |||||
| ( dirCount == 1 ? "y" : "ies" ) + " from " + baseDir.getAbsolutePath(); | |||||
| getLogger().info( message ); | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||