KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.tools.ant.taskdefs;
19
20 import java.io.File JavaDoc;
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Task;
23 import org.apache.tools.ant.util.FileUtils;
24
25 /**
26  * This task sets a property to the name of a temporary file.
27  * Unlike {@link File#createTempFile}, this task does not actually create the
28  * temporary file, but it does guarantee that the file did not
29  * exist when the task was executed.
30  * <p>
31  * Examples
32  * <pre>&lt;tempfile property="temp.file" /&gt;</pre>
33  * create a temporary file
34  * <pre>&lt;tempfile property="temp.file" suffix=".xml" /&gt;</pre>
35  * create a temporary file with the .xml suffix.
36  * <pre>&lt;tempfile property="temp.file" destDir="build"/&gt;</pre>
37  * create a temp file in the build subdir
38  *@since Ant 1.5
39  *@ant.task
40  */

41
42 public class TempFile extends Task {
43
44     private static final FileUtils FILE_UTILS = FileUtils.getFileUtils();
45
46     /**
47      * Name of property to set.
48      */

49     private String JavaDoc property;
50
51     /**
52      * Directory to create the file in. Can be null.
53      */

54     private File JavaDoc destDir = null;
55
56     /**
57      * Prefix for the file.
58      */

59     private String JavaDoc prefix;
60
61     /**
62      * Suffix for the file.
63      */

64     private String JavaDoc suffix = "";
65
66     /** deleteOnExit flag */
67     private boolean deleteOnExit;
68
69     /**
70      * Sets the property you wish to assign the temporary file to.
71      *
72      * @param property The property to set
73      * @ant.attribute group="required"
74      */

75     public void setProperty(String JavaDoc property) {
76         this.property = property;
77     }
78
79
80     /**
81      * Sets the destination directory. If not set,
82      * the basedir directory is used instead.
83      *
84      * @param destDir The new destDir value
85      */

86     public void setDestDir(File JavaDoc destDir) {
87         this.destDir = destDir;
88     }
89
90
91     /**
92      * Sets the optional prefix string for the temp file.
93      *
94      * @param prefix string to prepend to generated string
95      */

96     public void setPrefix(String JavaDoc prefix) {
97         this.prefix = prefix;
98     }
99
100
101     /**
102      * Sets the optional suffix string for the temp file.
103      *
104      * @param suffix suffix including any "." , e.g ".xml"
105      */

106     public void setSuffix(String JavaDoc suffix) {
107         this.suffix = suffix;
108     }
109
110     /**
111      * Set whether the tempfile created by this task should be set
112      * for deletion on normal VM exit.
113      * @param deleteOnExit boolean flag.
114      */

115     public void setDeleteOnExit(boolean deleteOnExit) {
116         this.deleteOnExit = deleteOnExit;
117     }
118
119     /**
120      * Learn whether deleteOnExit is set for this tempfile task.
121      * @return boolean deleteOnExit flag.
122      */

123     public boolean isDeleteOnExit() {
124         return deleteOnExit;
125     }
126
127     /**
128      * Creates the temporary file.
129      *
130      *@exception BuildException if something goes wrong with the build
131      */

132     public void execute() throws BuildException {
133         if (property == null || property.length() == 0) {
134             throw new BuildException("no property specified");
135         }
136         if (destDir == null) {
137             destDir = getProject().resolveFile(".");
138         }
139         File JavaDoc tfile = FILE_UTILS.createTempFile(
140                 prefix, suffix, destDir, deleteOnExit);
141
142         getProject().setNewProperty(property, tfile.toString());
143     }
144 }
145
Popular Tags