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 145 146 public class CalcTag extends BodyTagSupport 147 { 148 long res = 0; 150 151 156 public final int doStartTag() throws JspException 157 { 158 return EVAL_BODY_TAG; 159 } 160 161 167 public final int doAfterBody() throws JspException 168 { 169 BodyContent body = getBodyContent(); 171 String s = body.getString(); 172 body.clearBody(); 174 175 List l = new LinkedList(); 177 int beg = 0,end=0; 178 int len = s.length(); 179 char v; 180 boolean digit = false; 181 boolean operator = false; 182 boolean negated = false; 183 184 for( end=0; end < len; end++ ) { 186 v = s.charAt(end); 187 if( Character.isWhitespace(v) ) { 189 if( digit ) { 190 l.add((Object )Long.valueOf(s.substring(beg,end))); 192 } else if( operator ) { 193 l.add((Object )new StringBuffer (s.substring(beg,end))); 195 } 196 digit = negated = operator = false; 197 beg = end; 198 } else if( Character.isDigit(v) ) { 199 if( !operator || negated ) { 200 if( !digit && !negated )beg = end; 201 digit = true; 202 operator = negated = false; 203 } 204 } else { 205 if( !operator && !digit) { 206 beg = end; 207 if( v == '-' ) 208 negated = true; 209 } else { 210 negated = false; 211 } 212 operator = true; 213 digit = false; 214 } 215 } 216 217 if( (end - beg) > 0 ) { 218 if( digit ) { 219 l.add((Object )Long.valueOf(s.substring(beg,end))); 221 } else if( operator ) { 222 l.add((Object )new StringBuffer (s.substring(beg,end))); 224 } 225 } 226 res = calculate((ListIterator)l.iterator()); 228 return SKIP_BODY; 230 } 231 232 237 238 public final int doEndTag() throws JspException 239 { 240 try { 241 pageContext.getOut().write("" + res); 242 } catch(Exception e) { 243 throw new JspException("IO Error: " + e.getMessage()); 244 } 245 return EVAL_PAGE; 246 } 247 248 253 private long calculate(ListIterator li) throws JspException 254 { 255 long a = 0; 256 long res = 0; 257 StringBuffer sb = null; 258 Object o; 259 Long v; 260 boolean aset = false; 261 262 while(li.hasNext()) { 265 o = li.next(); 266 if( o.getClass().getName().equals("java.lang.Long") ) { 267 v = (Long )o; 268 if( aset ) { 270 if( sb == null ) { 271 throw new JspException("Jive tag calc missing operator."); 272 } 273 if( sb.toString().equals("*") ) 275 a *= v.longValue(); 276 else if( sb.toString().equals("+") ) 277 a += v.longValue(); 278 else if( sb.toString().equals("/") ) 279 a /= v.longValue(); 280 else if( sb.toString().equals("-") ) 281 a -= v.longValue(); 282 else 283 throw new JspException("Jive tag calc invalid operator \"" + 284 sb + "\""); 285 } else { 286 aset = true; 287 a = v.longValue(); 288 } 289 } else { 290 sb = (StringBuffer )o; 291 if( !aset ) { 292 throw new JspException("Jive tag calc must start with a number."); 293 } 294 } 295 } 296 return a; 297 } 298 299 } 300 | Popular Tags |