KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: IsAntVersion.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.condition;
30
31 import org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33 import org.apache.tools.ant.taskdefs.condition.Condition;
34
35 import com.idaremedia.antx.AntX;
36 import com.idaremedia.antx.AntXFixture;
37 import com.idaremedia.antx.AssertableProjectComponent;
38 import com.idaremedia.antx.go.IffAnt;
39 import com.idaremedia.antx.helpers.Strings;
40 import com.idaremedia.antx.helpers.Tk;
41 import com.idaremedia.antx.parameters.TrueFalsePropertySetter;
42
43 /**
44  * Condition that checks the current Ant runtime's version information
45  * against a regular expression. If you can guarantee that the project property
46  * "<span class="src">ant.version</span>" is always defined, you can get this
47  * condition's basic test by using the standard AntX {@linkplain Matches}
48  * condition. However, this condition adds the ability to set properties
49  * and use short hand (not RE) version strings.
50  * <p>
51  * Example usage:<pre>
52  * &lt;antversion like="^.*version 1\.[5-7].*"/&gt;
53  * &lt;antversion is="1.6"/&gt;
54  * &lt;antversion is="1.5.4" trueproperty="old.ant.present"/&gt;
55  * &lt;antversion is="1.6+"/&gt;
56  * &lt;antversion like="1234" property="local.ant.version"/&gt;
57  * </pre>
58  *
59  * @since JWare/AntX 0.4
60  * @author ssmc, &copy;2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
61  * @version 0.5
62  * @.safety single
63  * @.group api,infra
64  * @.caveat If this condition is part of a rule, you should not set either update
65  * property unless you only execute the rule once.
66  **/

67
68 public class IsAntVersion extends AssertableProjectComponent
69     implements Condition, TrueFalsePropertySetter, URIable
70 {
71     /**
72      * Initializes a new antversion condition.
73      **/

74     public IsAntVersion()
75     {
76         super(AntX.conditions);
77     }
78
79
80     /**
81      * Initializes a new antversion condition bound to a
82      * project and pattern.
83      **/

84     public IsAntVersion(Project P, String JavaDoc pattern)
85     {
86         super(AntX.conditions);
87         setProject(P);
88         setLike(pattern);
89     }
90
91 // ---------------------------------------------------------------------------------------
92
// Parameters:
93
// ---------------------------------------------------------------------------------------
94

95     private static final String JavaDoc VERSION_RE_0= "^.*version ";
96     private static final String JavaDoc VERSION_RE_N= " compiled .*$";
97
98
99     /**
100      * Sets this condition's regular expression pattern.
101      * @param pattern pattern against which value matched (non-null)
102      **/

103     public void setLike(String JavaDoc pattern)
104     {
105         require_(pattern!=null,"setLike- nonzro pattern");
106         m_pattern = pattern;
107     }
108
109
110     /**
111      * Sets this condition's version string as a simple
112      * <em>exact</em> number string like:
113      * "<span class="src">1.6.1</span>". This method basically
114      * builds the match pattern for caller.
115      * @param version exact version to look for (non-null)
116      **/

117     public final void setIs(String JavaDoc version)
118     {
119         require_(!Tk.isWhitespace(version),"setIs- nonzro version");
120
121         StringBuffer JavaDoc sb = AntXFixture.newStringBuffer();
122         sb.append(VERSION_RE_0);
123
124         int N= version.length();
125         for (int i=0;i<N;i++) {
126             char c = version.charAt(i);
127             if (c=='.') {
128                 sb.append("\\.");
129             } else if (c=='+' && i==N-1) {
130                 sb.append("(\\.[0-9]+)?(beta[1-9]+)?");
131             } else {
132                 sb.append(c);
133             }
134         }
135
136         sb.append(VERSION_RE_N);
137         setLike(sb.substring(0));
138
139         log("[antversion] looking for version pattern='"+m_pattern+"'",
140             Project.MSG_DEBUG);
141         sb = null;
142     }
143
144
145     /**
146      * Returns pattern against which values matched. Returns
147      * <i>null</i> if never set.
148      **/

149     public final String JavaDoc getPattern()
150     {
151         return m_pattern;
152     }
153
154
155     /**
156      * Sets the property to be created on a negative evaluation.
157      * Property will be set to the string "<i>true</i>."
158      * @param property the property to create (non-null)
159      **/

160     public void setFalseProperty(String JavaDoc property)
161     {
162         require_(property!=null,"setFalsP- nonzro nam");
163         m_falseProperty = property;
164     }
165
166
167     /**
168      * Returns the property to be created/set on a negative
169      * evaluation. Returns <i>null</i> if never set.
170      **/

171     public final String JavaDoc getFalseProperty()
172     {
173         return m_falseProperty;
174     }
175
176
177     /**
178      * Sets the property to be created on a positive evaluation.
179      * Property will be set to the string "<i>true</i>."
180      * @param property the property to create (non-null)
181      **/

182     public void setTrueProperty(String JavaDoc property)
183     {
184         require_(property!=null,"setTrueP- nonzro nam");
185         m_trueProperty = property;
186     }
187
188
189     /**
190      * Returns the property to be created/set on a positive
191      * evaluation. Returns <i>null</i> if never set.
192      **/

193     public final String JavaDoc getTrueProperty()
194     {
195         return m_trueProperty;
196     }
197
198
199     /**
200      * Sets a custom source property for the version string. This
201      * parameter is useful for test scripts and other situations
202      * where you want to read version string from source other than
203      * default Ant property.
204      * @param property name of property containing version string
205      **/

206     public void setProperty(String JavaDoc property)
207     {
208         m_versionProperty = property;
209     }
210
211
212
213     /**
214      * Sets this condition's pattern (like) as part of a value URI.
215      * @param fragment the value uri bits (non-null)
216      * @since JWare/AntX 0.5
217      */

218     public void xsetFromURI(String JavaDoc fragment)
219     {
220         setLike(fragment);
221     }
222
223
224 // ---------------------------------------------------------------------------------------
225
// Evaluation:
226
// ---------------------------------------------------------------------------------------
227

228     /**
229      * Returns <i>true</i> if the Ant version matches the pattern.
230      * @.sideeffect Will update true and/or false properties
231      * if defined.
232      **/

233     public boolean eval()
234     {
235         verifyCanEvaluate_("eval");
236
237         boolean istrue;
238
239         if (m_versionProperty==null) {
240             istrue = IffAnt.pass(getPattern(),getProject(),false);
241         } else {
242             istrue = IffAnt.pass(getPattern(),m_versionProperty,getProject(),false);
243         }
244
245         if (m_trueProperty!=null && istrue) {
246             log("[antversion] was true; setting true-property '"+
247                 m_trueProperty+
248                 "' property", Project.MSG_DEBUG);
249             getProject().setNewProperty(m_trueProperty,Strings.TRUE);
250         }
251
252         if (m_falseProperty!=null && !istrue) {
253             log("[antversion] was false; setting false-property '"+
254                 m_falseProperty+
255                 "' property", Project.MSG_DEBUG);
256             getProject().setNewProperty(m_falseProperty,Strings.TRUE);
257         }
258
259         return istrue;
260     }
261
262
263     /**
264      * Call to verify that this condition is in valid project
265      * and has its pattern defined.
266      * @param calr caller's identifier
267      * @throws BuildException if not in project or all bits not defined
268      **/

269     protected void verifyCanEvaluate_(String JavaDoc calr)
270     {
271         super.verifyInProject_(calr);
272
273         if (getPattern()==null) {
274             String JavaDoc ermsg = uistrs().get("type.needs.this.attr",
275                                         "antversion", "pattern|is");
276             log(ermsg,Project.MSG_ERR);
277             throw new BuildException(ermsg);
278         }
279     }
280
281
282     private String JavaDoc m_falseProperty;
283     private String JavaDoc m_trueProperty;
284     private String JavaDoc m_pattern;
285     private String JavaDoc m_versionProperty;
286 }
287
288 /* end-of-IsAntVersion.java */
289
Popular Tags