01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
22: import java.io.IOException;
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
37: .getFileUtils();
38:
39: private File src;
40: private File dest;
41: private boolean replace = true;
42:
43: /**
44: * Sets the file to be renamed.
45: * @param src the file to rename
46: */
47: public void setSrc(File 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 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 replace) {
64: this .replace = Project.toBoolean(replace);
65: }
66:
67: /**
68: * Renames the file <code>src</code> to <code>dest</code>
69: * @exception org.apache.tools.ant.BuildException The exception is
70: * thrown, if the rename operation fails.
71: */
72: public void execute() throws BuildException {
73: log("DEPRECATED - The rename task is deprecated. Use move instead.");
74:
75: if (dest == null) {
76: throw new BuildException("dest attribute is required",
77: getLocation());
78: }
79:
80: if (src == null) {
81: throw new BuildException("src attribute is required",
82: getLocation());
83: }
84:
85: if (!replace && dest.exists()) {
86: throw new BuildException(dest + " already exists.");
87: }
88:
89: try {
90: FILE_UTILS.rename(src, dest);
91: } catch (IOException e) {
92: throw new BuildException("Unable to rename " + src + " to "
93: + dest, e, getLocation());
94: }
95: }
96: }
|