1 53 54 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 179 180 public class EvalTag extends BodyTagSupport 181 { 182 private Boolean result = new Boolean (false); 184 185 190 public final int doStartTag() throws JspException 191 { 192 return EVAL_BODY_TAG; 193 } 194 195 201 public final int doAfterBody() throws JspException 202 { 203 BodyContent body = getBodyContent(); 205 String s = body.getString(); 206 body.clearBody(); 208 209 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 if( !slashed && Character.isWhitespace(v) ) { 224 if( digit ) { 225 l.add((Object )Long.valueOf(s.substring(beg,end))); 227 } else if( operator ) { 228 if( was_slashed ) { 230 beg++; 231 char ec = s.charAt(end-1); 232 if( ec == '\\' ) 233 end--; 234 } 235 l.add((Object )new StringBuffer (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 l.add((Object )Long.valueOf(s.substring(beg,end))); 266 } else if( operator ) { 267 if( was_slashed ) { 269 beg++; 270 char ec = s.charAt(end-1); 271 if( ec == '\\' ) 272 end--; 273 } 274 l.add((Object )new StringBuffer (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 result = new Boolean (evaluate(l)); 283 pageContext.setAttribute(id,result,PageContext.PAGE_SCOPE); 285 return SKIP_BODY; 286 } 287 288 293 private boolean evaluate(List l) throws JspException 294 { 295 boolean res = false; 296 StringBuffer op = null; 297 Long v; 298 boolean aset = false; 299 boolean bset = false; 300 301 305 op = (StringBuffer )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 long a = ((Long )l.get(0)).longValue(); 310 long b = ((Long )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 String a = "" + l.get(0); 330 String 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 |