e.g. Calendar Search Help
You must enter a value before pressing Search
ant

Class: org.apache.tools.ant.taskdefs.Deltree   ©

 OK to copy?
001 /*
002  * Copyright  2000,2002,2004 The Apache Software Foundation
003  *
004  *  Licensed under the Apache License, Version 2.0 (the "License");
005  *  you may not use this file except in compliance with the License.
006  *  You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  *  Unless required by applicable law or agreed to in writing, software
011  *  distributed under the License is distributed on an "AS IS" BASIS,
012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  *  See the License for the specific language governing permissions and
014  *  limitations under the License.
015  *
016  */
017 
018 package org.apache.tools.ant.taskdefs;
019 
020 import java.io.File;
021 import java.io.IOException;
022 import org.apache.tools.ant.BuildException;
023 import org.apache.tools.ant.Task;
024 
025 /**
026  *
027  *
028  *
029  * @since Ant 1.1
030  *
031  * @deprecated The deltree task is deprecated since Ant 1.2.  Use
032  * delete instead.
033  */
034 
035 public class Deltree extends Task {
036 
037     private File dir;
038 
039     /**
040      * Set the directory to be deleted
041      *
042      * @param dir the root of the tree to be removed.
043      */
044     public void setDir(File dir) {
045         this.dir = dir;
046     }
047 
048     /**
049      * Do the work.
050      *
051      * @exception BuildException if the task is not configured correctly or
052      * the tree cannot be removed.
053      */
054     public void execute() throws BuildException {
055         log("DEPRECATED - The deltree task is deprecated.  "
056             + "Use delete instead.");
057 
058         if (dir == null) {
059             throw new BuildException("dir attribute must be set!", getLocation());
060         }
061 
062         if (dir.exists()) {
063             if (!dir.isDirectory()) {
064                 if (!dir.delete()) {
065                     throw new BuildException("Unable to delete directory "
066                                              + dir.getAbsolutePath(),
067                                              getLocation());
068                 }
069                 return;
070             }
071 
072             log("Deleting: " + dir.getAbsolutePath());
073 
074             try {
075                 removeDir(dir);
076             } catch (IOException ioe) {
077                 String msg = "Unable to delete " + dir.getAbsolutePath();
078                 throw new BuildException(msg, getLocation());
079             }
080         }
081     }
082 
083     private void removeDir(File dir) throws IOException {
084 
085         // check to make sure that the given dir isn't a symlink
086         // the comparison of absolute path and canonical path
087         // catches this
088 
089         //        if (dir.getCanonicalPath().equals(dir.getAbsolutePath())) {
090         // (costin) It will not work if /home/costin is symlink to
091         // /da0/home/costin ( taz for example )
Rate092         String[] list = dir.list();
093         for (int i = 0; i < list.length; i++) {
094             String s = list[i];
095             File f = new File(dir, s);
096             if (f.isDirectory()) {
097                 removeDir(f);
098             } else {
099                 if (!f.delete()) {
100                     throw new BuildException("Unable to delete file "
101                                              + f.getAbsolutePath());
102                 }
103             }
104         }
105         if (!dir.delete()) {
106             throw new BuildException("Unable to delete directory "
107                                      + dir.getAbsolutePath());
108         }
109     }
110 }


            
All Examples in File:
Example
Line
Rating (found
useful by...)
92 0% of 0