KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > condition > solo > MatchesTask


1 /**
2  * $Id: MatchesTask.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.solo;
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.AssertableTask;
37 import com.idaremedia.antx.condition.Matches;
38 import com.idaremedia.antx.helpers.Strings;
39 import com.idaremedia.antx.parameters.FlexValueSupport;
40 import com.idaremedia.antx.parameters.IgnoreCaseEnabled;
41 import com.idaremedia.antx.parameters.TrimEnabled;
42 import com.idaremedia.antx.parameters.TrueFalsePropertySetter;
43
44 /**
45  * Task version of a {@linkplain Matches} condition that lets you set another property
46  * based on the evaluation result. Usually defined &lt;matches&gt; like:<pre>
47  * &lt;<b>matches</b> trueproperty="loop.DIE"
48  * pattern="(true)|(yes)|(okidoki)|(on)" variable="_loop.haltiferror"
49  * ignorecase="true"/&gt;
50  *
51  * -OR-
52  *
53  * &lt;target name="--verify.buildenv"/&gt;
54  * &lt;<b>matches</b> falseproperty="build.type.broken"
55  * pattern="(internal)|(distribution)|(local)" value="${build.type}"/&gt;
56  * &lt;stop msgid="msg.unknown.buildtype" msgarg1="${build.type}"
57  * if="build.type.broken"/&gt;
58  * &lt;/target&gt;
59  * </pre>
60  *
61  * @since JWare/AntX 0.2
62  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
63  * @version 0.5
64  * @.safety single
65  * @.group api,infra
66  * @see Matches
67  **/

68
69 public final class MatchesTask extends AssertableTask
70     implements FlexValueSupport, IgnoreCaseEnabled, TrimEnabled,
71                TrueFalsePropertySetter, Condition
72 {
73     /**
74      * Initializes a new MatchesTask instance.
75      **/

76     public MatchesTask()
77     {
78         super(AntX.conditions);
79     }
80
81
82     /**
83      * Initializes the enclosing project of this task.
84      * Also updates this task's helper bits.
85      **/

86     public void setProject(Project P)
87     {
88         super.setProject(P);
89         m_impl.setProject(P);
90     }
91
92
93 // ---------------------------------------------------------------------------------------
94
// Parameters:
95
// ---------------------------------------------------------------------------------------
96

97     /**
98      * Sets this task's regular expression pattern.
99      * @param pattern pattern against which value matched (non-null)
100      **/

101     public void setPattern(String JavaDoc pattern)
102     {
103         m_impl.setPattern(pattern);
104     }
105
106
107     /**
108      * Returns pattern against which values matched. Returns
109      * <i>null</i> if never set.
110      **/

111     public String JavaDoc getPattern()
112     {
113         return m_impl.getPattern();
114     }
115
116
117     /**
118      * Sets this task's to-be-matched literal value.
119      * @param value value to be matched (non-null)
120      **/

121     public void setValue(String JavaDoc value)
122     {
123         m_impl.setValue(value);
124     }
125
126
127     /**
128      * Returns this task's to-be-matched value. Returns
129      * <i>null</i> if never set or not setup as a literal string.
130      **/

131     public String JavaDoc getValue()
132     {
133         return m_impl.getLiteral();
134     }
135
136
137     /**
138      * Sets a property whose value is to be evaluated by this
139      * task. Property read when this task is executed.
140      * @param property the property's name (non-null)
141      **/

142     public void setProperty(String JavaDoc property)
143     {
144         m_impl.setProperty(property);
145     }
146
147
148     /**
149      * Returns property's name used by this task. Returns
150      * <i>null</i> if never set or not setup for a property.
151      **/

152     public String JavaDoc getProperty()
153     {
154         return m_impl.getProperty();
155     }
156
157
158     /**
159      * Sets an exported property whose value is to be evaluated
160      * by this task. Variable read when this task is executed.
161      * @param variable the exported property's name (non-null)
162      **/

163     public void setVariable(String JavaDoc variable)
164     {
165         m_impl.setVariable(variable);
166     }
167
168
169     /**
170      * Synonym for {@linkplain #setVariable setVariable}.
171      **/

172     public void setVar(String JavaDoc variable)
173     {
174         setVariable(variable);
175     }
176
177
178     /**
179      * Returns the exported property's name used this task. Returns
180      * <i>null</i> if never set or not setup for a variable.
181      **/

182     public final String JavaDoc getVariable()
183     {
184         return m_impl.getVariable();
185     }
186
187
188     /**
189      * Sets a reference whose value is to be evaluated by this
190      * task. Reference read wen this task is executed.
191      * @param refid the reference's name (non-null)
192      **/

193     public void setReference(String JavaDoc refid)
194     {
195         m_impl.setReference(refid);
196     }
197
198
199     /**
200      * Returns the reference's name used by this task. Returns
201      * <i>null</i> if never set or not setup for a reference.
202      **/

203     public String JavaDoc getReference()
204     {
205         return m_impl.getReference();
206     }
207
208
209     /**
210      * Set whether the value should be trimmed of whitespace
211      * before it's compared.
212      **/

213     public void setTrim(boolean trim)
214     {
215         m_impl.setTrim(trim);
216     }
217
218
219     /**
220      * Returns <i>true</i> if the value will be trimmed before
221      * it's compared.
222      **/

223     public final boolean willTrim()
224     {
225         return m_impl.willTrim();
226     }
227
228
229
230     /**
231      * Set whether the value should be lower-cased before
232      * it's compared.
233      **/

234     public void setIgnoreCase(boolean ignore)
235     {
236         m_impl.setIgnoreCase(ignore);
237     }
238
239
240     /**
241      * Returns <i>true</i> if the value will be lower-cased
242      * before it's compared.
243      **/

244     public final boolean isIgnoreCase()
245     {
246         return m_impl.isIgnoreCase();
247     }
248
249
250     /**
251      * Sets the property to be created on a negative evaluation.
252      * Property will be set to the string "<i>true</i>."
253      * @param property the property to create (non-null)
254      **/

255     public void setFalseProperty(String JavaDoc property)
256     {
257         require_(property!=null,"setFalsP- nonzro nam");
258         m_falseProperty = property;
259     }
260
261
262     /**
263      * Returns the property to be created/set on a negative
264      * evaluation. Returns <i>null</i> if never set.
265      **/

266     public final String JavaDoc getFalseProperty()
267     {
268         return m_falseProperty;
269     }
270
271
272     /**
273      * Sets the property to be created on a positive evaluation.
274      * Property will be set to the string "<i>true</i>."
275      * @param property the property to create (non-null)
276      **/

277     public void setTrueProperty(String JavaDoc property)
278     {
279         require_(property!=null,"setTrueP- nonzro nam");
280         m_trueProperty = property;
281     }
282
283
284     /**
285      * Returns the property to be created/set on a positive
286      * evaluation. Returns <i>null</i> if never set.
287      **/

288     public final String JavaDoc getTrueProperty()
289     {
290         return m_trueProperty;
291     }
292
293
294     /**
295      * Returns <i>true</i> if the value matches this task's pattern.
296      * @throws BuildException if condition definition is incomplete
297      * @since JWare/AntX 0.3
298      **/

299     public boolean eval() throws BuildException
300     {
301         verifyCanExecute_("eval");
302
303         boolean istrue= m_impl.eval();
304
305         if (istrue && getTrueProperty()!=null) {
306             String JavaDoc prop = getTrueProperty();
307             log("Match was true; setting true-property '"+prop+"' property",
308                 Project.MSG_DEBUG);
309             getProject().setNewProperty(prop,Strings.TRUE);
310         }
311         if (!istrue && getFalseProperty()!=null) {
312             String JavaDoc prop = getFalseProperty();
313             log("Match was false; setting false-property '"+prop+"' property",
314                 Project.MSG_DEBUG);
315             getProject().setNewProperty(prop,Strings.TRUE);
316         }
317
318         return istrue;
319     }
320
321
322     /**
323      * Checks if the value-under-test matches this task's pattern
324      * and updates properties based on results.
325      * @throws BuildException if condition definition is incomplete
326      **/

327     public void execute() throws BuildException
328     {
329         eval();
330     }
331
332     private final Matches m_impl= new Matches();
333     private String JavaDoc m_falseProperty;
334     private String JavaDoc m_trueProperty;
335 }
336
337 /* end-of-MatchesTask.java */
338
Popular Tags