KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Deltree


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Task;
25
26 /**
27  *
28  *
29  * @since Ant 1.1
30  *
31  * @deprecated The deltree task is deprecated since Ant 1.2. Use
32  * delete instead.
33  */

34
35 public class Deltree extends Task {
36
37     private File JavaDoc dir;
38
39     /**
40      * Set the directory to be deleted
41      *
42      * @param dir the root of the tree to be removed.
43      */

44     public void setDir(File JavaDoc dir) {
45         this.dir = dir;
46     }
47
48     /**
49      * Do the work.
50      *
51      * @exception BuildException if the task is not configured correctly or
52      * the tree cannot be removed.
53      */

54     public void execute() throws BuildException {
55         log("DEPRECATED - The deltree task is deprecated. "
56             + "Use delete instead.");
57
58         if (dir == null) {
59             throw new BuildException("dir attribute must be set!", getLocation());
60         }
61
62         if (dir.exists()) {
63             if (!dir.isDirectory()) {
64                 if (!dir.delete()) {
65                     throw new BuildException("Unable to delete directory "
66                                              + dir.getAbsolutePath(),
67                                              getLocation());
68                 }
69                 return;
70             }
71
72             log("Deleting: " + dir.getAbsolutePath());
73
74             try {
75                 removeDir(dir);
76             } catch (IOException JavaDoc ioe) {
77                 String JavaDoc msg = "Unable to delete " + dir.getAbsolutePath();
78                 throw new BuildException(msg, getLocation());
79             }
80         }
81     }
82
83     private void removeDir(File JavaDoc dir) throws IOException JavaDoc {
84
85         // check to make sure that the given dir isn't a symlink
86
// the comparison of absolute path and canonical path
87
// catches this
88

89         // if (dir.getCanonicalPath().equals(dir.getAbsolutePath())) {
90
// (costin) It will not work if /home/costin is symlink to
91
// /da0/home/costin ( taz for example )
92
String JavaDoc[] list = dir.list();
93         for (int i = 0; i < list.length; i++) {
94             String JavaDoc s = list[i];
95             File JavaDoc f = new File JavaDoc(dir, s);
96             if (f.isDirectory()) {
97                 removeDir(f);
98             } else {
99                 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 }
111
112
Popular Tags