KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > condition > FileNotEmpty


1 /**
2  * $Id: FileNotEmpty.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-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 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.condition;
30
31 import java.io.File JavaDoc;
32
33 import org.apache.tools.ant.Project;
34 import org.apache.tools.ant.taskdefs.condition.Condition;
35
36 import com.idaremedia.antx.AntX;
37 import com.idaremedia.antx.AssertableProjectComponent;
38 import com.idaremedia.antx.helpers.Strings;
39
40 /**
41  * Shortcut condition that checks if a file exists and is not empty (its length greater
42  * than zero). If the path defined is not an absolute path, this condition resolves it
43  * with respect to its project.
44  *
45  * @since JWare/AntX 0.2
46  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
47  * @version 0.5
48  * @.safety single
49  * @.group impl,infra
50  * @.ideas If ever important (using Available as a guide) support a
51  * 'filepath' attribute
52  **/

53
54 public class FileNotEmpty extends AssertableProjectComponent
55     implements Condition, URIable
56 {
57     /**
58      * Initializes new undefined FileNotEmpty condition.
59      **/

60     public FileNotEmpty()
61     {
62         super(AntX.rules+"filenotempty");
63     }
64
65
66     /**
67      * Initializes new fully defined FileNotEmpty condition.
68      * @param path path to be evaluated
69      * @param P condition's project
70      **/

71     public FileNotEmpty(String JavaDoc path, Project P)
72     {
73         super(AntX.rules+"filenotempty");
74         setProject(P);
75         setPath(path);
76     }
77
78
79     /**
80      * Initializes new fully defined FileNotEmpty condition.
81      * @param file file to be evaluated
82      * @param P condition's project
83      **/

84     public FileNotEmpty(File JavaDoc file, Project P)
85     {
86         super(AntX.rules+"filenotempty");
87         setProject(P);
88         setFile(file);
89     }
90
91
92     /**
93      * Sets path of file to verify. The incoming path is not
94      * resolved until this condition is evaluated.
95      * @param path path to verify (non-null)
96      **/

97     public void setPath(String JavaDoc path)
98     {
99         require_(path!=null,"setpath- nonzro");
100         m_filepath = path;
101     }
102
103
104     /**
105      * Returns path of file to verify. Returns <i>null</i>
106      * if never set.
107      **/

108     public final String JavaDoc getPath()
109     {
110         return m_filepath;
111     }
112
113
114     /**
115      * Sets file to verify. If the incoming file is not an
116      * absolute path, it is immediately resolved relative to this
117      * condition's enclosing project.
118      * @param file file to verify (non-null)
119      **/

120     public void setFile(File JavaDoc file)
121     {
122         require_(file!=null,"setfile- nonzro");
123         m_file = file;
124     }
125
126
127     /**
128      * Returns file to verify. Returns <i>null</i>
129      * if never set.
130      **/

131     public final File JavaDoc getFile()
132     {
133         return m_file;
134     }
135
136
137     /**
138      * Sets property name updated by <i>true</i> evaluation.
139      * @param property the property's name (non-null)
140      * @since JWare/AntX 0.3
141      **/

142     public void setTrueProperty(String JavaDoc property)
143     {
144         require_(property!=null,"setTrueProp- nonzro name");
145         m_updateProperty = property;
146     }
147
148
149     /**
150      * Returns property name updated by evaluation method. Returns
151      * <i>null</i> if never set.
152      * @since JWare/AntX 0.3
153      **/

154     public final String JavaDoc getTrueProperty()
155     {
156         return m_updateProperty;
157     }
158
159
160     /**
161      * Returns <i>true</i> if the file exists, is not a directory,
162      * and has a length greater than zero bytes.
163      **/

164     public boolean eval()
165     {
166         if (getTrueProperty()!=null) {
167             verifyInProject_("eval");
168         }
169
170         File JavaDoc thefile = getFile();
171
172         if (thefile==null && getPath()==null) {
173             return false;
174         }
175
176         boolean istrue=false;
177         try {
178             if (thefile==null) {
179                 if (getProject()!=null) {
180                     thefile = getProject().resolveFile(getPath());
181                 } else {
182                     thefile = new File JavaDoc(getPath());
183                 }
184             }
185             if (!thefile.isDirectory()) {
186                 istrue = thefile.length()>0L;//NB:returns 0L if doesnt-exist
187
}
188         } catch (SecurityException JavaDoc secX) {
189             log(secX.getMessage(),Project.MSG_WARN);
190         }
191
192         if (istrue && getTrueProperty()!=null) {
193             String JavaDoc prop = getTrueProperty();
194             log("FileNotEmpty was true; setting true-property '"+prop+
195                 "' property", Project.MSG_DEBUG);
196             getProject().setNewProperty(prop,Strings.TRUE);
197         }
198
199         return istrue;
200     }
201
202
203
204     /**
205      * Sets this condition's path as part of a value URI.
206      * @param fragment the value uri bits (non-null)
207      * @since JWare/AntX 0.5
208      */

209     public void xsetFromURI(String JavaDoc fragment)
210     {
211         setPath(fragment);
212     }
213
214
215     private String JavaDoc m_filepath;
216     private File JavaDoc m_file;
217     private String JavaDoc m_updateProperty;
218 }
219
220 /* end-of-FileNotEmpty.java */
221
Popular Tags