KickJava   Java API By Example, From Geeks To Geeks.

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


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.Project;
25 import org.apache.tools.ant.Task;
26 import org.apache.tools.ant.util.FileUtils;
27
28 /**
29  * Renames a file.
30  *
31  * @deprecated The rename task is deprecated since Ant 1.2. Use move instead.
32  * @since Ant 1.1
33  */

34 public class Rename extends Task {
35
36     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
37
38     private File JavaDoc src;
39     private File JavaDoc dest;
40     private boolean replace = true;
41
42
43     /**
44      * Sets the file to be renamed.
45      * @param src the file to rename
46      */

47     public void setSrc(File JavaDoc src) {
48         this.src = src;
49     }
50
51     /**
52      * Sets the new name of the file.
53      * @param dest the new name of the file.
54      */

55     public void setDest(File JavaDoc dest) {
56         this.dest = dest;
57     }
58
59     /**
60      * Sets whether an existing file should be replaced.
61      * @param replace <code>on</code>, if an existing file should be replaced.
62      */

63     public void setReplace(String JavaDoc replace) {
64         this.replace = Project.toBoolean(replace);
65     }
66
67
68     /**
69      * Renames the file <code>src</code> to <code>dest</code>
70      * @exception org.apache.tools.ant.BuildException The exception is
71      * thrown, if the rename operation fails.
72      */

73     public void execute() throws BuildException {
74         log("DEPRECATED - The rename task is deprecated. Use move instead.");
75
76         if (dest == null) {
77             throw new BuildException("dest attribute is required", getLocation());
78         }
79
80         if (src == null) {
81             throw new BuildException("src attribute is required", getLocation());
82         }
83
84         if (!replace && dest.exists()) {
85             throw new BuildException(dest + " already exists.");
86         }
87
88         try {
89             FILE_UTILS.rename(src, dest);
90         } catch (IOException JavaDoc e) {
91             throw new BuildException("Unable to rename " + src + " to "
92                 + dest, e, getLocation());
93         }
94     }
95 }
96
Popular Tags