KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.DirectoryScanner;
27 import org.apache.tools.ant.Project;
28
29 /**
30  * Copies a directory.
31  *
32  * @since Ant 1.1
33  *
34  * @deprecated The copydir task is deprecated since Ant 1.2. Use copy instead.
35  */

36
37 public class Copydir extends MatchingTask {
38
39     private File JavaDoc srcDir;
40     private File JavaDoc destDir;
41     private boolean filtering = false;
42     private boolean flatten = false;
43     private boolean forceOverwrite = false;
44     private Hashtable JavaDoc filecopyList = new Hashtable JavaDoc();
45
46     /**
47      * The src attribute
48      *
49      * @param src the source file
50      */

51     public void setSrc(File JavaDoc src) {
52         srcDir = src;
53     }
54
55     /**
56      * The dest attribute
57      *
58      * @param dest the destination file
59      */

60     public void setDest(File JavaDoc dest) {
61         destDir = dest;
62     }
63
64     /**
65      * The filtering attribute.
66      * Default is false.
67      * @param filter if true use filtering
68      */

69     public void setFiltering(boolean filter) {
70         filtering = filter;
71     }
72
73     /**
74      * The flattening attribute.
75      * Default is false.
76      * @param flatten if true use flattening
77      */

78     public void setFlatten(boolean flatten) {
79         this.flatten = flatten;
80     }
81
82     /**
83      * The forceoverwrite attribute.
84      * Default is false.
85      * @param force if true overwrite even if the destination file
86      * is newer that the source file
87      */

88     public void setForceoverwrite(boolean force) {
89         forceOverwrite = force;
90     }
91
92     /**
93      * Execute the task.
94      * @throws BuildException on error
95      */

96     public void execute() throws BuildException {
97         log("DEPRECATED - The copydir task is deprecated. Use copy instead.");
98
99         if (srcDir == null) {
100             throw new BuildException("src attribute must be set!",
101                                      getLocation());
102         }
103
104         if (!srcDir.exists()) {
105             throw new BuildException("srcdir " + srcDir.toString()
106                                      + " does not exist!", getLocation());
107         }
108
109         if (destDir == null) {
110             throw new BuildException("The dest attribute must be set.",
111                                      getLocation());
112         }
113
114         if (srcDir.equals(destDir)) {
115             log("Warning: src == dest", Project.MSG_WARN);
116         }
117
118         DirectoryScanner ds = super.getDirectoryScanner(srcDir);
119
120         try {
121             String JavaDoc[] files = ds.getIncludedFiles();
122             scanDir(srcDir, destDir, files);
123             if (filecopyList.size() > 0) {
124                 log("Copying " + filecopyList.size() + " file"
125                     + (filecopyList.size() == 1 ? "" : "s")
126                     + " to " + destDir.getAbsolutePath());
127                 Enumeration JavaDoc e = filecopyList.keys();
128                 while (e.hasMoreElements()) {
129                     String JavaDoc fromFile = (String JavaDoc) e.nextElement();
130                     String JavaDoc toFile = (String JavaDoc) filecopyList.get(fromFile);
131                     try {
132                         getProject().copyFile(fromFile, toFile, filtering,
133                                          forceOverwrite);
134                     } catch (IOException JavaDoc ioe) {
135                         String JavaDoc msg = "Failed to copy " + fromFile + " to "
136                             + toFile + " due to " + ioe.getMessage();
137                         throw new BuildException(msg, ioe, getLocation());
138                     }
139                 }
140             }
141         } finally {
142             filecopyList.clear();
143         }
144     }
145
146     private void scanDir(File JavaDoc from, File JavaDoc to, String JavaDoc[] files) {
147         for (int i = 0; i < files.length; i++) {
148             String JavaDoc filename = files[i];
149             File JavaDoc srcFile = new File JavaDoc(from, filename);
150             File JavaDoc destFile;
151             if (flatten) {
152                 destFile = new File JavaDoc(to, new File JavaDoc(filename).getName());
153             } else {
154                 destFile = new File JavaDoc(to, filename);
155             }
156             if (forceOverwrite
157                 || (srcFile.lastModified() > destFile.lastModified())) {
158                 filecopyList.put(srcFile.getAbsolutePath(),
159                                  destFile.getAbsolutePath());
160             }
161         }
162     }
163 }
164
Popular Tags