KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: ParentDirTask.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004 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 (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any 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 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 GNU 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
33 import org.apache.tools.ant.BuildException;
34 import org.apache.tools.ant.Project;
35
36 import com.idaremedia.antx.AntX;
37 import com.idaremedia.antx.AntXFixture;
38 import com.idaremedia.antx.AssertableTask;
39 import com.idaremedia.antx.apis.AntLibFriendly;
40 import com.idaremedia.antx.helpers.Tk;
41 import com.idaremedia.antx.ownhelpers.LocalTk;
42 import com.idaremedia.antx.parameters.FlexExistenceEnabled;
43
44 /**
45  * Task that extracts and saves the parent directory of a file system object. Currently
46  * the &lt;parentdir&gt; task handles files and file URLs as well as two internal
47  * pseudo-URL types: <span class="src">resource:</span> and <span class="src">tmp:</span>.
48  * See next section for some examples.
49  * <p>
50  * If both the "<span class="src">mustexist</span>" and "<span class="src">append</span>"
51  * parameters are specified, the existence check is performed on the calculated
52  * parent directory <em>before</em> the subpath bits are appended.
53  * <p>
54  * <b>Example Usage:</b><pre>
55  * &lt;tempdir urlproperty="TMP"/&gt;
56  * &lt;<b>parentdir</b> of="${TMP}" urlproperty="TMP.."/&gt;
57  *
58  * &lt;<b>parentdir</b> pop="2" pathproperty="basedir/../.."/&gt;
59  * &lt;<b>parentdir</b> of="resource:antx/oofs/test.properties" pathproperty="ETC"/&gt;
60  * &lt;<b>parentdir</b> of="${nextbuild.file}" mustexist="yes" pathproperty="nextroot"/&gt;
61  *
62  * &lt;<b>parentdir</b> of="${basedir}" pop="2" pathproperty="etc.dir" append="etc"/>
63  * </pre>
64  *
65  * @since JWare/AntX 0.5
66  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
67  * @version 0.5
68  * @.safety single
69  * @.group api,helper
70  **/

71
72 public final class ParentDirTask extends AssertableTask
73     implements FlexExistenceEnabled, AntLibFriendly
74 {
75     /**
76      * Initializes a new parent dir task.
77      **/

78     public ParentDirTask()
79     {
80         super(AntX.mktemp+"ParentDirTask:");
81     }
82
83
84     /**
85      * Sets the file (by path or by URL) that we'd like the
86      * parent of. File URLs are converted to file system-dependent
87      * paths before being processed.
88      * @param fileref the file reference (non-null)
89      **/

90     public void setOf(String JavaDoc fileref)
91     {
92         require_(fileref!=null,"setOf- nonzro path descriptor");
93         m_fileref = fileref;
94     }
95
96
97
98     /**
99      * Returns the file (path or URL) that we'll try to get the
100      * parent of. Will return <i>null</i> if never set explicitly.
101      **/

102     public String JavaDoc getOf()
103     {
104         return m_fileref;
105     }
106
107
108
109     /**
110      * Sets the property to be updated with the parent directory's
111      * full path.
112      * @param property property to be updated (non-null)
113      **/

114     public void setPathProperty(String JavaDoc property)
115     {
116         require_(property!=null,"setProp- nonzro nam");
117         m_updateProperty = property;
118     }
119
120
121
122     /**
123      * Returns the property updated with the parent directory's
124      * full path. Returns <i>null</i> if never set.
125      **/

126     public String JavaDoc getPathProperty()
127     {
128         return m_updateProperty;
129     }
130
131
132
133     /**
134      * Sets the property into which a URL representation of the
135      * parent directory's path is copied.
136      * @param urlproperty the property's name (non-null)
137      **/

138     public void setUrlProperty(String JavaDoc urlproperty)
139     {
140         require_(urlproperty!=null,"setUrlProp- nonzro name");
141         m_updateUrlProperty = urlproperty;
142     }
143
144
145
146     /**
147      * Returns the name of the property into which a URL representation
148      * of the parent directory's path will be stored. Returns <i>null</i>
149      * if never set explicitly.
150      **/

151     public final String JavaDoc getUrlPathProperty()
152     {
153         return m_updateUrlProperty;
154     }
155
156
157
158     /**
159      * Tells this task whether the parent directory must exist or not.
160      * @param mustExist <i>true</i> if parent must exist.
161      *
162      */

163     public void setMustExist(boolean mustExist)
164     {
165         m_mustExist = mustExist;
166     }
167
168
169
170     /**
171      * Returns <i>true</i> if the calculated parent directory must
172      * already exist. Defaults to <i>false</i>.
173      */

174     public boolean getMustExist()
175     {
176         return m_mustExist;
177     }
178
179
180
181     /**
182      * Gives this task a specific number of "pop" level to create
183      * parent or grand-parent operation.
184      * @param levelDesc levels to pop up (>="1")
185      **/

186     public void setPop(String JavaDoc levelDesc)
187     {
188         require_(levelDesc!=null,"setPop- nonzro level descriptor");
189         m_popLevelDesc = levelDesc;
190     }
191  
192  
193  
194     /**
195      * Gives this task a subpath to append to the calculated parent
196      * directory. This subpath is appended after the parent has been
197      * verified; any leading slashes are stripped.
198      * @param subpath sub path (non-null)
199      **/

200     public void setAppend(String JavaDoc subpath)
201     {
202         require_(subpath!=null,"setAppend- nonzro subpath");
203         final int N= subpath.length();
204         int i=0;
205         while (i<N) {
206             if (subpath.charAt(0)==File.separatorChar) {
207                 subpath= subpath.substring(1);
208             }
209             i++;
210         }
211         m_subpath = subpath;
212     }
213
214
215
216
217     /**
218      * Returns the subpath that this task will append to the calculated
219      * parent directory. Will return <i>null</i> if never set.
220      **/

221     public final String JavaDoc getAppendedSubpath()
222     {
223         return m_subpath;
224     }
225
226
227
228     /**
229      * Ensures we have at least one update property and a source file
230      * reference. If no file reference was named, we assume we should
231      * check our project's <span class="src">basedir</span> property.
232      * @param calr calling function (non-null)
233      * @throws BuildException if missing required update property.
234      */

235     protected void verifyCanExecute_(String JavaDoc calr)
236     {
237         verifyInProject_(calr);
238
239         if (getPathProperty()==null && getUrlPathProperty()==null) {
240             String JavaDoc e = getAntXMsg("task.needs.this.attr", getTaskName(),
241                 "pathproperty|urlproperty");
242             log(e, Project.MSG_ERR);
243             throw new BuildException(e,getLocation());
244         }
245         
246         if (getOf()==null) {
247             m_fileref = getProject().getProperty("basedir");
248             verify_(m_fileref!=null,calr+"- Ant's basedir defined");
249         }
250     }
251
252
253
254     private File JavaDoc theParent(File JavaDoc f)
255     {
256         File JavaDoc pf = f;
257         int i = Tk.integerFrom(m_popLevelDesc,-1);
258         while (pf!=null && i>0) {
259             pf = pf.getParentFile();
260             i--;
261         }
262         return pf;
263     }
264
265
266
267     /**
268      * Tries to determine the parent directory of named file object.
269      * If the object has no parent directory (is root), the update
270      * property is set to the platform path string for root.
271      * @throws BuildException if missing required update property.
272      * @.impl Lots of calls to Project.resolvePath are necessary to deal
273      * with the mixing of platform dependent and unix-only path
274      * strings. The final path must be normalized to runtime's OS.
275      */

276     public void execute()
277     {
278         verifyCanExecute_("exec");
279
280         File JavaDoc f= null;
281         String JavaDoc filepath= getOf();
282
283         if (filepath.startsWith("file:")) {
284             filepath = AntXFixture.fileUtils().fromURI(filepath);
285             f = getProject().resolveFile(filepath);
286         }
287         else if (filepath.startsWith("resource:")) {
288             f = LocalTk.getSystemResourceSource(filepath.substring(9),getProject());
289         }
290         else if (filepath.startsWith("tmp:")) {
291             File JavaDoc tmp = TempLocator.getSystemTempDir();
292             filepath = filepath.substring(4);
293             if (filepath.startsWith("/")) {
294                 filepath = filepath.substring(1);
295             }
296             filepath = tmp.getPath() + File.separator + filepath;
297             f = getProject().resolveFile(filepath);
298         } else {
299             f = getProject().resolveFile(filepath);
300         }
301
302         File JavaDoc pf = theParent(f);
303         if (getMustExist() && (pf==null || !pf.canRead())) {
304             String JavaDoc e = getAntXMsg("mktemp.parent.notfound",m_popLevelDesc,getOf());
305             log(e, Project.MSG_ERR);
306             throw new BuildException(e,getLocation());
307         }
308
309         String JavaDoc parentPath = "";
310         if (pf!=null) {
311             parentPath = pf.getPath();
312         } else if (f!=null) {
313             //Find whatever OS seez is "root" path!
314
parentPath = getProject().resolveFile("/").getPath();
315         }
316         if (!Tk.isWhitespace(m_subpath)) {
317             parentPath += File.separator + m_subpath;
318             parentPath = getProject().resolveFile(parentPath).getPath();
319         }
320         saveFinalPath(parentPath,true);
321     }
322
323
324
325     /**
326      * Apply any common update properties to final file system object.
327      * @param finalPath file system object's reference (non-null)
328      * @param strict <i>true</i> if should fail if unable to set update properties
329      **/

330     private void saveFinalPath(String JavaDoc finalPath, boolean strict)
331     {
332         if (getPathProperty()!=null) {
333             checkIfProperty_(getPathProperty(),!strict);
334             getProject().setNewProperty(getPathProperty(),finalPath);
335         }
336         if (getUrlPathProperty()!=null) {
337             checkIfProperty_(getUrlPathProperty(),!strict);
338             String JavaDoc urlPath = AntXFixture.fileUtils().toURI(finalPath);
339             getProject().setNewProperty(getUrlPathProperty(),urlPath);
340         }
341     }
342
343
344     private String JavaDoc m_fileref;
345     private String JavaDoc m_updateProperty;
346     private String JavaDoc m_updateUrlProperty;
347     private boolean m_mustExist;
348     private String JavaDoc m_popLevelDesc= "1";//=> .. => parent
349
private String JavaDoc m_subpath;//=> none
350
}
351
352
353 /* end-of-ParentDirTask.java */
Popular Tags