KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > StringEquality


1 /**
2  * $Id: StringEquality.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;
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 import org.apache.tools.ant.types.RegularExpression;
35 import org.apache.tools.ant.util.regexp.RegexpMatcher;
36
37 import com.idaremedia.antx.parameters.IgnoreCaseEnabled;
38 import com.idaremedia.antx.parameters.TrimEnabled;
39 import com.idaremedia.antx.parameters.ValueMatchEnabled;
40
41 /**
42  * Helper condition that can replace the standard Ant <span class="src">&lt;equals&gt;</span>
43  * condition with something thats works better with the default AntX &lt;assert&gt; inlined
44  * <span class="src">equals</span> and <span class="src">notequals</span> options.
45  * Note that this object is not intended as a script-facing nestable condition element;
46  * however, instances of this class can be dynamically configured with a internal
47  * <span class="src">RuntimeConfigurable</span> helper.
48  *
49  * @since JWare/AntX 0.1
50  * @author ssmc, &copy;2002-2004 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
51  * @version 0.5
52  * @.safety single
53  * @.group impl,helper
54  * @see FlexString
55  **/

56
57 public final class StringEquality extends AssertableProjectComponent
58     implements Condition, IgnoreCaseEnabled, TrimEnabled, ValueMatchEnabled
59 {
60     /** Operation name for default 'equals' condition. **/
61     public static final String JavaDoc OP_EQUALS= "equals";
62     /** Operation name for 'startswith' condition. **/
63     public static final String JavaDoc OP_STARTSWITH= "startswith";
64     /** Operation name for 'endswith' condition. **/
65     public static final String JavaDoc OP_ENDSWITH= "endswith";
66     /** Operation name for 'contains' condition. **/
67     public static final String JavaDoc OP_CONTAINS= "contains";
68     /** Operation name for 'matches' condition. **/
69     public static final String JavaDoc OP_MATCHES= "matches";
70
71
72     /**
73      * Initializes new string matching condition.
74      **/

75     public StringEquality()
76     {
77         super();
78         m_knownArg.setLenient(false);
79         m_unknownArg.setLenient(false);
80     }
81
82
83     /**
84      * Initializes new string matching condition with known
85      * argument specified.
86      **/

87     public StringEquality(String JavaDoc known)
88     {
89         this();
90         setKnownArg(known);
91     }
92
93
94     /**
95      * Initializes new string matching condition with known
96      * argument optionally specified as a property name.
97      * @param isP <i>true</i> if value is a property name
98      **/

99     public StringEquality(String JavaDoc known, boolean isP)
100     {
101         this();
102         setKnownArg(known,isP);
103     }
104
105
106     /**
107      * Initializes the enclosing project of this component.
108      * Updates the internal arguments also.
109      **/

110     public void setProject(Project P)
111     {
112         super.setProject(P);
113         m_knownArg.setProject(P);
114         m_unknownArg.setProject(P);
115     }
116
117
118     /**
119      * Returns <i>true</i> if this equality is missing either
120      * one or both arguments.
121      **/

122     public final boolean isUndefined()
123     {
124         return m_knownArg.isUndefined() || m_unknownArg.isUndefined();
125     }
126
127
128     /**
129      * Set this condition's known or first argument to as-is string.
130      **/

131     public final void setKnownArg(String JavaDoc arg1)
132     {
133         setKnownArg(arg1,false);
134     }
135
136
137     /**
138      * Set this condition's known or first argument with option to
139      * specify value is a property name (which is determined at
140      * evaluation time).
141      * @param isP <i>true</i> if is a property name
142      **/

143     public void setKnownArg(String JavaDoc arg1, boolean isP)
144     {
145         m_knownArg.set(arg1);
146         m_knownArg.setIsLiteral();
147         m_knownArg.setIsProperty(isP);
148     }
149
150
151     /**
152      * Returns this condition's known argument; returns <i>null</i>
153      * if never set.
154      * @see #getKnownValue
155      **/

156     public final String JavaDoc getKnownArg()
157     {
158         return m_knownArg.get();
159     }
160
161
162     /**
163      * Returns the processed known argument's value as it's used in
164      * comparision. This value has all the pre-processing instructions
165      * applied (like trimming, lower-casing, etc.)
166      **/

167     public final String JavaDoc getKnownValue()
168     {
169         return m_knownArg.getValue();
170     }
171
172
173     /**
174      * Script-facing delegatee for {@linkplain #setKnownArg
175      * setKnownArg(String)}.
176      * @since JWare/AntX 0.4
177      **/

178     public final void setMatch(String JavaDoc pattern)
179     {
180         setKnownArg(pattern,false);
181         setOperator(OP_MATCHES);
182     }
183
184
185     /**
186      * Set this condition's unknown or second argument to as-is string.
187      **/

188     public final void setUnknownArg(String JavaDoc arg2)
189     {
190         setUnknownArg(arg2, false);
191     }
192
193
194     /**
195      * Set this condition's unknown or second argument with option to
196      * specify value is a property name (which is determined at
197      * evaluation time).
198      * @param isP <i>true</i> if is a property name
199      **/

200     public void setUnknownArg(String JavaDoc arg1, boolean isP)
201     {
202         m_unknownArg.set(arg1);
203         m_unknownArg.setIsLiteral();
204         m_unknownArg.setIsProperty(isP);
205     }
206
207
208     /**
209      * Returns this condition's unknown argument; returns <i>null</i>
210      * if never set.
211      * @see #getUnknownValue
212      **/

213     public final String JavaDoc getUnknownArg()
214     {
215         return m_unknownArg.get();
216     }
217
218
219     /**
220      * Returns the processed unknown argument's value as it's used in
221      * comparision. This value has all the pre-processing instructions
222      * applied (like trimming, lower-casing, etc.)
223      **/

224     protected final String JavaDoc getUnknownValue()
225     {
226         return m_unknownArg.getValue();
227     }
228
229
230     /**
231      * Script-facing delegatee for {@linkplain #setUnknownArg
232      * setUnknownArg(String)}.
233      * @since JWare/AntX 0.4
234      **/

235     public final void setValue(String JavaDoc value)
236     {
237         setUnknownArg(value);
238     }
239
240
241     /**
242      * Sets the main operator for this condition. Must be one of
243      * the declared "OP_" constants.
244      **/

245     public final void setOperator(String JavaDoc op)
246     {
247         require_(op!=null,"setOp- nonzro op");
248         if (OP_EQUALS.equals(op)) { m_Op=0; }
249         else if (OP_CONTAINS.equals(op)) { m_Op=1; }
250         else if (OP_STARTSWITH.equals(op)) { m_Op=2; }
251         else if (OP_MATCHES.equals(op)) { m_Op=3; }
252         else if (OP_ENDSWITH.equals(op)) { m_Op=4; }
253         else { require_(false, "setOp- known operator"); }
254     }
255
256
257     /**
258      * Set whether both arguments should be trimmed before being
259      * compared.
260      **/

261     public void setTrim(boolean trim)
262     {
263         m_knownArg.setTrim(trim);
264         m_unknownArg.setTrim(trim);
265     }
266
267
268     /**
269      * Returns <i>true</i> if both arguments will be trimmed before
270      * comparision.
271      **/

272     public boolean willTrim()
273     {
274         return m_knownArg.isTrimmed();
275     }
276
277
278     /**
279      * Set whether the string comparision should be case-insensitive.
280      * Defaults to case-sensitive comparisions.
281      * @param b <i>true</i> if case sensitive (default)
282      **/

283     public void setIgnoreCase(boolean b)
284     {
285         m_knownArg.setIgnoreCase(b);
286         m_unknownArg.setIgnoreCase(b);
287     }
288
289
290     /**
291      * Returns <i>true</i> if case insensitive string comparision
292      * will be done. Defaults to case-sensitive comparisions.
293      **/

294     public boolean isIgnoreCase()
295     {
296         return m_knownArg.isIgnoreCase();
297     }
298
299
300     /**
301      * Set whether this comparision is for inequality. Shortcut for
302      * embedding &lt;not&gt; in inlined condition attributes.
303      **/

304     public void setNegate(boolean b)
305     {
306         m_Not = b;
307     }
308
309
310     /**
311      * Returns <i>true</i> if this is a not-a-match comparision
312      * condition.
313      **/

314     public boolean isNegate()
315     {
316         return m_Not;
317     }
318
319
320     /**
321      * Converts the known RE pattern string into the Regexp object
322      * used for matching.
323      **/

324     private RegexpMatcher getRE()
325     {
326         if (m_RE==null) {
327             RegularExpression redt= new RegularExpression();
328             redt.setPattern(getKnownValue());
329             m_RE= redt.getRegexp(getProject());
330         }
331         return m_RE;
332     }
333
334
335     /**
336      * Compares two strings for some kind-of match.
337      * @see #setOperator
338      **/

339     public boolean eval() throws BuildException
340     {
341         if (isUndefined()) {
342             String JavaDoc ermsg = AntX.uistrs().get("brul.assert.need.2args");
343             if (getProject()!=null) {
344                 log(ermsg,Project.MSG_ERR);
345             }
346             throw new BuildException(ermsg);
347         }
348
349         String JavaDoc unknown = getUnknownValue();
350         String JavaDoc known = getKnownValue();
351
352         if (known==null || unknown==null) {//=> a missing property!
353
return false;
354         }
355
356         boolean hit = true;
357         switch(m_Op) {
358             case 0: {
359                 hit = known.equals(unknown);
360                 break;
361             }
362             case 1: {
363                 hit = unknown.indexOf(known)>=0;
364                 break;
365             }
366             case 2: {
367                 hit = unknown.startsWith(known);
368                 break;
369             }
370             case 3: {
371                 hit = getRE().matches(unknown);
372                 break;
373             }
374             case 4: {
375                 hit = unknown.endsWith(known);
376                 break;
377             }
378         }
379
380         return isNegate() ? !hit : hit;
381     }
382
383
384     /**
385      * Retuns the known argument's underlying flexstring so
386      * users can customize individually. Never returns <i>null</i>.
387      **/

388     public final FlexString getKnownValueGetter()
389     {
390         return m_knownArg;
391     }
392
393
394     /**
395      * Returns the unknown argument's underlying flexstring so
396      * users can customize individually. Never returns <i>null</i>.
397      **/

398     public final FlexString getUnknownValueGetter()
399     {
400         return m_unknownArg;
401     }
402
403     private FlexString m_knownArg = new FlexString();
404     private FlexString m_unknownArg = new FlexString();
405     private boolean m_Not;
406     private int m_Op = 0;//equals
407
private RegexpMatcher m_RE;
408 }
409
410 /* end-of-StringEquality.java */
411
Popular Tags