KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > mktemp > MkFileTask


1 /**
2  * $Id: MkFileTask.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2003-2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.mktemp;
30
31 import java.io.File JavaDoc;
32 import java.io.IOException JavaDoc;
33
34 import org.apache.tools.ant.BuildException;
35 import org.apache.tools.ant.Project;
36 import org.apache.tools.ant.types.PropertySet;
37 import org.apache.tools.ant.types.Reference;
38 import org.apache.tools.ant.util.FileUtils;
39
40 import com.idaremedia.antx.AntX;
41 import com.idaremedia.antx.helpers.InnerString;
42 import com.idaremedia.antx.starters.StringItemListHandle;
43
44 /**
45  * Create a new simple file. Useful for creating simple, small files or test input
46  * files. Unlike the related {@linkplain MkTempFile} task, the files created by
47  * MkFileTask persist by default. The other main difference between this task and
48  * the MkTempFile task is that the caller specifies the new file's path-- it is not
49  * dynamically generated by the AntX TempLocator.
50  * <p>
51  * Prototype lines (as specified with the &lt;line&gt; or &lt;lines&gt; elements)
52  * are only applied to new empty files. If a prototype copy file (or resource) has
53  * been defined, these lines are <em>not</em> included to the new file.
54  * <p>
55  * <b>Examples:</b><pre>
56  * &lt;<b>strings</b> id="default.ignoredfiles" prefix="*."&gt;
57  * &lt;string value="last"&gt;
58  * &lt;string value="jar"&gt;
59  * &lt;string value="gz"&gt;
60  * &lt;string value="tgz"&gt;
61  * &lt;/strings&gt;
62  * &lt;<b>newfile</b> path="${work}/lib/.cvsignore"&gt;
63  * &lt;line value="classes"/&gt;
64  * &lt;lines listref="default.ignoredfiles"/&gt;
65  * &lt;/newfile&gt;
66  * </pre>
67  *
68  * @since JWare/AntX 0.3
69  * @author ssmc, &copy;2003-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
70  * @version 0.5
71  * @.safety single
72  * @.group api,helper
73  * @see MkTempFile
74  **/

75
76 public class MkFileTask extends MkNewObject
77 {
78     /**
79      * Initializes a new MkFileTask instance.
80      **/

81     public MkFileTask()
82     {
83         super(AntX.mktemp);
84     }
85
86
87     /**
88      * Initializes a new CV-labeled MkFileTask instance.
89      **/

90     protected MkFileTask(String JavaDoc iam)
91     {
92         super(iam);
93     }
94
95
96     /**
97      * Defines the new file's path. If the path is relative the
98      * new file will be relative to the project's base directory.
99      * @param fp the file with full path (non-null)
100      **/

101     public void setPath(File JavaDoc fp)
102     {
103         require_(fp!=null,"setPath- nonzro path");
104         m_filepath = fp;
105     }
106
107
108     /**
109      * Returns file path of new files created by this task. Will
110      * return <i>null</i> if never set. Required before this task
111      * is executed.
112      **/

113     public File JavaDoc getPath()
114     {
115         return m_filepath;
116     }
117
118
119     /**
120      * Returns absolute file path of new files created by this task.
121      * Resolves the script-specified {@linkplain #setPath path} against
122      * this task's project's base directory if necessary. Will return
123      * <i>null</i> if this task's path has not been defined.
124      **/

125     public File JavaDoc getEffectivePath()
126     {
127         if (getPath()==null || getProject()==null) {
128             return null;
129         }
130         return getProject().resolveFile(getPath().getPath());
131     }
132
133
134     /**
135      * Defines whether this task will mark all new files as
136      * delete-on-exit. Is <i>false</i> by default.
137      * @param persist <i>false</i> if items should be deleted
138      * @see MkTempFile
139      **/

140     public void setPersist(boolean persist)
141     {
142         m_autoDelete = !persist;
143     }
144
145
146     /**
147      * Returns <i>true</i> if this task will try to mark all
148      * temporary objects (files and directories) as delete-on-exit.
149      **/

150     public boolean isAutoDelete()
151     {
152         return m_autoDelete;
153     }
154
155
156     /**
157      * Tells this tasks whether existing files should be replaced
158      * with fresh copies.
159      * @param stompOnOlder <i>true</i> if existing files will be overwritten
160      **/

161     public void setOverwrite(boolean stompOnOlder)
162     {
163         m_overWrite = stompOnOlder;
164     }
165
166
167     /**
168      * Returns <i>true</i> if existing files will be overwritten with
169      * new (fresh) copies. Defaults to <i>true</i>.
170      **/

171     public boolean willOverwrite()
172     {
173         return m_overWrite;
174     }
175
176
177     /**
178      * Grunt work of creating the new file in specified location.
179      * If a prototype source file/resource is defined, its contents
180      * are copied to new file.
181      * @param newFile the new file's location (absolute path)
182      * @throws BuildException if unable to create new file
183      **/

184     protected final void createFile(File JavaDoc newFile) throws BuildException
185     {
186         require_(newFile!=null,"newFile- nonzro fil");
187         FileUtils fsu= getFileUtils();
188
189         try {
190             if (getPrototypePlainFile()!=null) {
191                 fsu.copyFile(getPrototypePlainFile(),newFile,
192                              getCopyFilters(),willOverwrite());
193             }
194             else if (getPrototypeResourceFile()!=null) {
195                 fsu.copyFile(getPrototypeResourceFile(),newFile,
196                              getCopyFilters(),willOverwrite());
197             } else {
198                 if (willOverwrite() || !newFile.exists()) {
199                     newFile.createNewFile();
200                     copyPrototypeLines(newFile,false);
201                 }
202             }
203         } catch(IOException JavaDoc iox) {
204             throw new BuildException(iox, getLocation());
205         }
206
207         if (isAutoDelete()) {
208             newFile.deleteOnExit();
209         }
210     }
211
212
213     /**
214      * Adds a new prototype line to include in new file.
215      * @param line the line information (non-null)
216      **/

217     public void addLine(InnerString line)
218     {
219         require_(line!=null,"addLn- nonzro ln");
220         getPrototypeLinesNoNull().add(line);
221     }
222
223
224     /**
225      * Adds a reference to prototype lines to include in new file.
226      * @param lines the line information (non-null)
227      **/

228     public void addLines(StringItemListHandle lines)
229     {
230         require_(lines!=null,"addLns- nonzro lns");
231         getPrototypeLinesNoNull().add(lines);
232     }
233
234
235     /**
236      * Shortcut for {@linkplain #addLines addLines()}. The
237      * refered-to list is <em>added</em> to this task's prototype
238      * lines.
239      * @param ref the itemlist's reference (non-null)
240      **/

241     public final void setLines(Reference ref)
242     {
243         StringItemListHandle lines = new StringItemListHandle(ref);
244         getPrototypeLinesNoNull().add(lines);
245     }
246
247
248     /**
249      * Adds a new property set to include in new file.
250      * @param propertyset new propertyset line (non-null)
251      * @since JWare/AntX 0.4
252      **/

253     public void addConfiguredPropertySet(PropertySet propertyset)
254     {
255         require_(propertyset!=null,"addPropSet- nonzro pset");
256         getPrototypeLinesNoNull().add(propertyset);
257     }
258
259
260     /**
261      * Tries to create a new file in script-specified location.
262      * @throws BuildException if unable to create or overwrite file
263      **/

264     public void execute()
265     {
266         verifyCanExecute_("execute");
267         File JavaDoc newFile = getEffectivePath();
268         createFile(newFile);
269         saveFinalPath(newFile,false);
270     }
271
272
273     /**
274      * Ensures this task is part of a valid project and that the
275      * new file's path has been specified.
276      * @throws BuildException if verification fails
277      **/

278     protected void verifyCanExecute_(String JavaDoc calr)
279     {
280         verifyInProject_(calr);
281         if (getPath()==null) {
282             String JavaDoc error = uistrs().get("task.needs.this.attr",getTaskName(),"path");
283             log(error,Project.MSG_ERR);
284             throw new BuildException(error,getLocation());
285         }
286     }
287
288
289     private File JavaDoc m_filepath;
290     private boolean m_overWrite=true;//NB:stomps-by-default
291
private boolean m_autoDelete;//NB:persists-by-default
292
}
293
294 /* end-of-MkFileTask.java */
295
Popular Tags