KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > tags > EvalTag


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * SUCH DAMAGE.
100  * ====================================================================
101  *
102  * This software consists of voluntary contributions made by many
103  * individuals on behalf of CoolServlets.com. For more information
104  * on CoolServlets.com, please see <http://www.coolservlets.com>.
105  */

106
107 package com.Yasna.forum.tags;
108
109 import java.io.*;
110 import java.util.*;
111 import javax.servlet.*;
112 import javax.servlet.jsp.*;
113 import javax.servlet.jsp.tagext.*;
114 import javax.servlet.http.*;
115 import com.Yasna.forum.tags.*;
116
117 /**
118  * JSP Tag <b>eval</b>, used to evaluate a comparison of two strings or
119  * integers.
120  * <p>
121  * Requires that attribute <b>id</b> be set to the name of a
122  * script variable for later use by the <b>true</b> or <b>false</b> tags.
123  * <p>
124  * Valid comparison operators for integers are <b>==</b> (equal), <b>!=</b>
125  * (not equal), <b>>=</b> (greater than or equal),
126  * <b><=</b> (less than or equal), <b><</b> (less than),
127  * and <b>></b> (greater than).
128  * <p>
129  * Valid comparison operators for strings include all those for an integer
130  * plus <b>|=</b> (first string is a prefix of second string),
131  * <b>|!</b> (first string is not a prefix of second string),
132  * <b>=|</b> (first string is a suffix of second string),
133  * and <b>!|</b> (first string is not a suffix of second string).
134  * <p>
135  * When comparing strings that might include white space, use the forward
136  * slash <b>\</b> at the start and end of the string.
137  * <p>
138  * The values being compared must be separated from the comparison operator
139  * by white space.
140  * <p>
141  * Any JSP tags embedded in the body of the eval tag are processed
142  * before the comparison is made.
143  * <p>
144  * The <b>true</b> and <b>false</b> tags are used to take action based on the
145  * result of the eval comparison.
146  * <p>
147  * Examples:
148  * <p>
149  * Compare the number of forums to 0.
150  * <p><pre>
151  * &lt;jf:eval id="num_forums"&gt;
152  * &lt;jsp:getProperty name="req" property="numberOfForums"/&gt; == 0
153  * &lt;/jf:eval&gt;
154  * <p></pre>
155  * Compares the prefix "Re: " to a message subject line.
156  * <p><pre>
157  * &lt;jf:eval id="sub"&gt;
158  * \Re: \ |= \&lt;jsp:getProperty name="cm" property="subject"/&gt;\
159  * &lt;/jf:eval&gt;
160  * <p></pre>
161  * JSP Tag Lib Descriptor
162  * <p><pre>
163  * &lt;name&gt;eval&lt;/name&gt;
164  * &lt;tagclass&gt;com.Yasna.forum.tags.EvalTag&lt;/tagclass&gt;
165  * &lt;bodycontent&gt;JSP&lt;/bodycontent&gt;
166  * &lt;info&gt;Compares two values, use the true and false tags to test result of eval. The values can be strings or integers.&lt;/info&gt;
167  * &lt;attribute&gt;
168  * &lt;name&gt;id&lt;/name&gt;
169  * &lt;required&gt;true&lt;/required&gt;
170  * &lt;rtexprvalue&gt;false&lt;/rtexprvalue&gt;
171  * &lt;/attribute&gt;
172  * </pre>
173  *
174  * @see TrueTag
175  * @see FalseTag
176  *
177  * @author Glenn Nielsen
178  */

179
180 public class EvalTag extends BodyTagSupport
181 {
182   // Result of comparison
183
private Boolean JavaDoc result = new Boolean JavaDoc(false);
184
185   /**
186    * Allow the body of the calc tag to be processed.
187    *
188    * @return EVAL_BODY_TAG
189    */

190   public final int doStartTag() throws JspException
191   {
192     return EVAL_BODY_TAG;
193   }
194
195   /**
196    * Read the body of the eval tag to obtain the integer or string values
197    * comparison operator, then perform eval comparison.
198    *
199    * @return SKIP_BODY
200    */

201   public final int doAfterBody() throws JspException
202   {
203     // Use the body of the tag as input for the comparison
204
BodyContent body = getBodyContent();
205     String JavaDoc s = body.getString();
206     // Clear the body since we only used it as input for the comparison
207
body.clearBody();
208
209     // Parse the body extracting integers or strings and an operator
210
List l = new LinkedList();
211     int beg = 0,end;
212     int len = s.length();
213     char v;
214     boolean digit = false;
215     boolean operator = false;
216     boolean slashed = false;
217     boolean was_slashed = false;
218     boolean negated = false;
219
220     for( end=0; end < len; end++ ) {
221       v = s.charAt(end);
222 // System.out.println("eval char \"" + v + "\" negated=" + negated);
223
if( !slashed && Character.isWhitespace(v) ) {
224         if( digit ) {
225 // System.out.println("Eval digit \"" + s.substring(beg,end) + "\"");
226
l.add((Object JavaDoc)Long.valueOf(s.substring(beg,end)));
227         } else if( operator ) {
228 // System.out.println("Eval op \"" + s.substring(beg,end) + "\"");
229
if( was_slashed ) {
230             beg++;
231             char ec = s.charAt(end-1);
232             if( ec == '\\' )
233               end--;
234           }
235           l.add((Object JavaDoc)new StringBuffer JavaDoc(s.substring(beg,end)));
236         }
237     was_slashed = digit = negated = operator = false;
238     beg = end;
239       } else if( Character.isDigit(v) ) {
240     if( !operator || negated ) {
241           if( !digit && !negated )beg = end;
242           digit = true;
243           operator = negated = false;
244         }
245       } else {
246         if( !operator && !digit) {
247           beg = end;
248           if( v == '-' )
249             negated = true;
250       if( v == '\\' )
251         was_slashed = slashed = true;
252         } else {
253           negated = false;
254       if( v == '\\' )
255         slashed = false;
256         }
257         operator = true;
258     digit = false;
259       }
260     }
261
262     if( (end - beg) > 0 ) {
263       if( digit ) {
264 // System.out.println("Eval digit \"" + s.substring(beg,end) + "\"");
265
l.add((Object JavaDoc)Long.valueOf(s.substring(beg,end)));
266       } else if( operator ) {
267 // System.out.println("Eval op \"" + s.substring(beg,end) + "\"");
268
if( was_slashed ) {
269           beg++;
270           char ec = s.charAt(end-1);
271           if( ec == '\\' )
272             end--;
273         }
274         l.add((Object JavaDoc)new StringBuffer JavaDoc(s.substring(beg,end)));
275       }
276     }
277
278     if( l.size() != 3 ) {
279       throw new JspException("Jive tag eval must have two values and an operator.");
280     }
281     // Evaluate the comparison
282
result = new Boolean JavaDoc(evaluate(l));
283     // Save the script variable for later use by true or false tags
284
pageContext.setAttribute(id,result,PageContext.PAGE_SCOPE);
285     return SKIP_BODY;
286   }
287
288   /**
289    * Do the comparison
290    *
291    * @return String - result of the calculation as a boolean
292    */

293   private boolean evaluate(List l) throws JspException
294   {
295     boolean res = false;
296     StringBuffer JavaDoc op = null;
297     Long JavaDoc v;
298     boolean aset = false;
299     boolean bset = false;
300
301 // System.out.println("EvalTag a \"" + l.get(0) + "\"");
302
// System.out.println("EvalTag op \"" + l.get(1) + "\"");
303
// System.out.println("EvalTag b \"" + l.get(2) + "\"");
304

305     op = (StringBuffer JavaDoc)l.get(1);
306     if( l.get(0).getClass().getName().equals("java.lang.Long") &&
307     l.get(2).getClass().getName().equals("java.lang.Long") ) {
308       // Do an integer comparison
309
long a = ((Long JavaDoc)l.get(0)).longValue();
310       long b = ((Long JavaDoc)l.get(2)).longValue();
311       if( op.toString().equals("<") ) {
312         res = (a < b);
313       } else if( op.toString().equals(">") ) {
314         res = (a > b);
315       } else if( op.toString().equals("==") ) {
316         res = (a == b);
317       } else if( op.toString().equals("<=") ) {
318         res = (a <= b);
319       } else if( op.toString().equals(">=") ) {
320         res = (a >= b);
321       } else if( op.toString().equals("!=") ) {
322         res = (a != b);
323       } else {
324         throw new JspException("Jive tag eval invalid comparison operator \"" +
325       op + "\".");
326       }
327     } else {
328       // Do a string comparison
329
String JavaDoc a = "" + l.get(0);
330       String JavaDoc b = "" + l.get(2);
331       int comp = a.compareTo(b);
332       if( op.toString().equals("<") ) {
333         if( comp < 0 )res=true;
334       } else if( op.toString().equals(">") ) {
335         if( comp > 0 )res=true;
336       } else if( op.toString().equals("==") ) {
337         if( comp == 0 )res=true;
338       } else if( op.toString().equals("<=") ) {
339         if( comp <= 0 )res=true;
340       } else if( op.toString().equals(">=") ) {
341     if( comp >= 0 )res=true;
342       } else if( op.toString().equals("!=") ) {
343     if( comp != 0 )res=true;
344       } else if( op.toString().equals("|=") ) {
345         res = b.startsWith(a);
346       } else if( op.toString().equals("|!") ) {
347         res = !b.startsWith(a);
348       } else if( op.toString().equals("=|") ) {
349         res = b.endsWith(a);
350       } else if( op.toString().equals("!|") ) {
351         res = !b.endsWith(a);
352       } else {
353         throw new JspException("Jive tag eval invalid comparison operator \"" +
354           op + "\".");
355       }
356     }
357     return res;
358   }
359
360 }
361
Popular Tags