KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: Matches.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 org.apache.tools.ant.BuildException;
32 import org.apache.tools.ant.Project;
33
34 import com.idaremedia.antx.FlexString;
35 import com.idaremedia.antx.StringEquality;
36 import com.idaremedia.antx.parameters.IgnoreCaseEnabled;
37 import com.idaremedia.antx.parameters.TrimEnabled;
38 import com.idaremedia.antx.parameters.ValueMatchEnabled;
39
40 /**
41  * Condition that evaluates <i>true</i> if a string value matches a known regular
42  * expression. Usually defined as &lt;matches&gt;;for example:<pre>
43  * &lt;assert ...&gt;
44  * &lt;matches pattern="(internal)|(distribution)" value="${build.type}"/&gt;
45  * &lt;matches pattern="[0-9]+" property="build.number"/&gt;
46  * &lt;matches pattern="(true)|(yes)|(okidoki)|(on)" var="_loop.haltiferror"/&gt;
47  * &lt;/assert&gt;
48  *
49  * &lt;tally trueproperty="loop.DIE"&gt;
50  * &lt;matches pattern="(true)|(yes)|(okidoki)|(on)" var="_loop.haltiferror"/&gt;
51  * &lt;/tally&gt;
52  * </pre>
53  *
54  * @since JWare/AntX 0.2
55  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
56  * @version 0.5
57  * @.safety single
58  * @.group impl,infra
59  * @see StringEquality
60  * @see com.idaremedia.antx.condition.solo.MatchesTask MatchesTask
61  **/

62
63 public final class Matches extends FlexCondition
64     implements IgnoreCaseEnabled, TrimEnabled, ValueMatchEnabled
65 {
66     /**
67      * Initializes new Matches condition.
68      **/

69     public Matches()
70     {
71         super();
72         m_impl.setOperator(StringEquality.OP_MATCHES);
73         getValueHelper().setLenient(false);
74     }
75
76
77     /**
78      * Initializes a new Matches condition with specific pattern.
79      * @param pattern regular expression to be evaluated against
80      * @param P condition's project
81      **/

82     public Matches(String JavaDoc pattern, Project P)
83     {
84         this();
85         setProject(P);
86         setPattern(pattern);
87     }
88
89
90     /**
91      * Initializes the enclosing project of this condition.
92      * Also updates this condition's helper bits.
93      **/

94     public void setProject(Project P)
95     {
96         super.setProject(P);
97         m_impl.setProject(P);
98     }
99
100
101     /**
102      * Returns the underlying value's flex string for this condition.
103      * Never returns <i>null</i>.
104      **/

105     protected final FlexString getValueHelper()
106     {
107         return m_impl.getUnknownValueGetter();
108     }
109
110
111 // ---------------------------------------------------------------------------------------
112
// Parameters:
113
// ---------------------------------------------------------------------------------------
114

115     /**
116      * Sets this condition's regular expression pattern.
117      * @param pattern pattern against which value matched (non-null)
118      **/

119     public void setPattern(String JavaDoc pattern)
120     {
121         require_(pattern!=null,"setPattern- nonzro");
122         m_impl.setKnownArg(pattern);
123     }
124
125
126     /**
127      * Returns pattern against which values matched. Returns
128      * <i>null</i> if never set.
129      **/

130     public final String JavaDoc getPattern()
131     {
132         return m_impl.getKnownArg();
133     }
134
135
136     /**
137      * Synonym for {@linkplain #setPattern setPattern()} that is
138      * more natural for some uses of a Matches condition.
139      * @since JWare/AntX 0.4
140      **/

141     public final void setMatch(String JavaDoc pattern)
142     {
143         setPattern(pattern);
144     }
145
146
147     /**
148      * Sets this condition's to-be-matched literal value.
149      * @param value value to be matched (non-null)
150      **/

151     public final void setValue(String JavaDoc value)
152     {
153         require_(value!=null,"setValue- nonzro");
154         setLiteral(value);
155     }
156
157
158
159     /**
160      * Set whether the value should be trimmed of whitespace
161      * before it's compared.
162      **/

163     public void setTrim(boolean trim)
164     {
165         getValueHelper().setTrim(trim);
166     }
167
168
169     /**
170      * Returns <i>true</i> if the value will be trimmed before
171      * it's compared.
172      **/

173     public final boolean willTrim()
174     {
175         return getValueHelper().isTrimmed();
176     }
177
178
179
180     /**
181      * Set whether the value should be lower-cased before
182      * it's compared.
183      **/

184     public void setIgnoreCase(boolean ignore)
185     {
186         getValueHelper().setIgnoreCase(ignore);
187     }
188
189
190     /**
191      * Returns <i>true</i> if the value will be lower-cased
192      * before it's compared.
193      **/

194     public final boolean isIgnoreCase()
195     {
196         return getValueHelper().isIgnoreCase();
197     }
198
199
200 // ---------------------------------------------------------------------------------------
201
// Evaluation:
202
// ---------------------------------------------------------------------------------------
203

204     /**
205      * Returns <i>true</i> if the value matches the pattern.
206      **/

207     public boolean eval()
208     {
209         verifyCanEvaluate_("eval");
210
211         return m_impl.eval();
212     }
213
214
215     /**
216      * Call to verify that this match condition is in valid project
217      * and has both its pattern and value defined.
218      * @param calr caller's identifier
219      * @throws BuildException if not in project or all bits not defined
220      **/

221     protected void verifyCanEvaluate_(String JavaDoc calr)
222     {
223         super.verifyCanEvaluate_(calr);
224
225         if (getPattern()==null) {
226             String JavaDoc ermsg = uistrs().get("task.needs.this.attr",
227                                         getTypicalName(),"pattern");
228             log(ermsg,Project.MSG_ERR);
229             throw new BuildException(ermsg);
230         }
231     }
232
233
234     private final StringEquality m_impl = new StringEquality();
235 }
236
237 /* end-of-Matches.java */
238
Popular Tags