KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > savant > ant > taskdefs > UncopyTask


1 /*
2  * Copyright (c) 2003-2004, Inversoft, All Rights Reserved
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.savant.ant.taskdefs;
8
9
10 import java.io.File JavaDoc;
11 import java.util.Enumeration JavaDoc;
12
13 import org.apache.tools.ant.BuildException;
14 import org.apache.tools.ant.Project;
15 import org.apache.tools.ant.taskdefs.Copy;
16 import org.apache.tools.ant.types.FilterChain;
17 import org.apache.tools.ant.types.FilterSet;
18
19
20 /**
21  * <p>
22  * This class is a sub-class of the Copy ant task that
23  * is used to un-copy all the files that normally would
24  * have been copied
25  * </p>
26  *
27  * <p>
28  * This version of the uncopy task only works with ant
29  * version 1.6.1
30  * </p>
31  *
32  * @author Brian Pontarelli
33  */

34 public class UncopyTask extends Copy {
35
36
37     /**
38      * Constructs a new <code>UncopyTask</code>
39      */

40     public UncopyTask() {
41         forceOverwrite = true;
42     }
43
44
45     /**
46      * Un-support for un-copy, throws BuildException
47      */

48     public void setOverwrite(String JavaDoc overwrite) {
49         throw new BuildException("overwrite not supported for the un-copy task");
50     }
51
52     /**
53      * Un-support for un-copy, throws BuildException
54      */

55     public FilterChain createFilterChain() {
56         throw new BuildException("filterchain not supported for the un-copy task");
57     }
58
59     /**
60      * Un-support for un-copy, throws BuildException
61      */

62     public FilterSet createFilterSet() {
63         throw new BuildException("filterset not supported for the un-copy task");
64     }
65
66     /**
67      * Overrides the doFileOperations from the Copy task. Rather than doing the
68      * copy here, this version deletes the destination file instead. Hence, un-
69      * copying the file.
70      */

71     protected void doFileOperations() {
72
73         if (fileCopyMap.size() > 0) {
74             log("Un-copying " + fileCopyMap.size()
75                 + " file" + (fileCopyMap.size() == 1 ? "" : "s")
76                 + " to " + destDir.getAbsolutePath());
77
78             Enumeration JavaDoc e = fileCopyMap.keys();
79             while (e.hasMoreElements()) {
80                 String JavaDoc fromFile = (String JavaDoc) e.nextElement();
81                 String JavaDoc[] toFiles = (String JavaDoc[]) fileCopyMap.get(fromFile);
82
83                 for (int i = 0; i < toFiles.length; i++) {
84                     if (fromFile.equals(toFiles[i])) {
85                         log("Skipping self-un-copy of " + fromFile, Project.MSG_DEBUG);
86                         continue;
87                     }
88
89                     log("Un-copying " + fromFile + " to " + toFiles[i], Project.MSG_DEBUG);
90
91                     File JavaDoc remove = new File JavaDoc(toFiles[i]);
92                     if (remove.exists()) {
93                         if (!remove.delete()) {
94                             throw new BuildException("Failed to un-copy " +
95                                 fromFile + " to " + toFiles[i]);
96                         }
97                     }
98                 }
99             }
100         }
101
102         if (includeEmpty) {
103             Enumeration JavaDoc e = dirCopyMap.elements();
104             int count = 0;
105             while (e.hasMoreElements()) {
106                 String JavaDoc[] dirs = (String JavaDoc[]) e.nextElement();
107                 for (int i = 0; i < dirs.length; i++) {
108                     File JavaDoc dir = new File JavaDoc(dirs[i]);
109                     if (dir.exists()) {
110                         dir.delete();
111                         count++;
112                     }
113                 }
114             }
115
116             if (count > 0) {
117                 log("Un-copied " + count + " empty director" + (count == 1 ? "y" : "ies") +
118                     " to " + destDir.getAbsolutePath());
119             }
120         }
121     }
122 }
Popular Tags