1 18 19 20 package org.apache.struts.taglib.logic; 21 22 23 import java.lang.reflect.InvocationTargetException ; 24 import javax.servlet.http.Cookie ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.jsp.JspException ; 27 import org.apache.commons.beanutils.PropertyUtils; 28 import org.apache.struts.util.MessageResources; 29 import org.apache.struts.taglib.TagUtils; 30 31 32 38 39 public abstract class CompareTagBase extends ConditionalTagBase { 40 41 42 44 45 48 protected static final int DOUBLE_COMPARE = 0; 49 50 51 54 protected static final int LONG_COMPARE = 1; 55 56 57 60 protected static final int STRING_COMPARE = 2; 61 62 63 66 protected static MessageResources messages = 67 MessageResources.getMessageResources 68 ("org.apache.struts.taglib.logic.LocalStrings"); 69 70 71 73 74 78 public String value = null; 79 80 public String getValue() { 81 return (this.value); 82 } 83 84 public void setValue(String value) { 85 this.value = value; 86 } 87 88 89 91 92 95 public void release() { 96 97 super.release(); 98 value = null; 99 100 } 101 102 103 105 106 114 protected abstract boolean condition() throws JspException ; 115 116 117 128 protected boolean condition(int desired1, int desired2) 129 throws JspException { 130 131 int type = -1; 133 double doubleValue = 0.0; 134 long longValue = 0; 135 if ((type < 0) && (value.length() > 0)) { 136 try { 137 doubleValue = Double.parseDouble(value); 138 type = DOUBLE_COMPARE; 139 } catch (NumberFormatException e) { 140 ; 141 } 142 } 143 if ((type < 0) && (value.length() > 0)) { 144 try { 145 longValue = Long.parseLong(value); 146 type = LONG_COMPARE; 147 } catch (NumberFormatException e) { 148 ; 149 } 150 } 151 if (type < 0) { 152 type = STRING_COMPARE; 153 } 154 155 Object variable = null; 157 if (cookie != null) { 158 Cookie cookies[] = 159 ((HttpServletRequest ) pageContext.getRequest()). 160 getCookies(); 161 if (cookies == null) 162 cookies = new Cookie [0]; 163 for (int i = 0; i < cookies.length; i++) { 164 if (cookie.equals(cookies[i].getName())) { 165 variable = cookies[i].getValue(); 166 break; 167 } 168 } 169 } else if (header != null) { 170 variable = 171 ((HttpServletRequest ) pageContext.getRequest()). 172 getHeader(header); 173 } else if (name != null) { 174 Object bean = TagUtils.getInstance().lookup(pageContext, name, scope); 175 if (property != null) { 176 if (bean == null) { 177 JspException e = new JspException 178 (messages.getMessage("logic.bean", name)); 179 TagUtils.getInstance().saveException(pageContext, e); 180 throw e; 181 } 182 try { 183 variable = PropertyUtils.getProperty(bean, property); 184 } catch (InvocationTargetException e) { 185 Throwable t = e.getTargetException(); 186 if (t == null) 187 t = e; 188 TagUtils.getInstance().saveException(pageContext, t); 189 throw new JspException 190 (messages.getMessage("logic.property", name, property, 191 t.toString())); 192 } catch (Throwable t) { 193 TagUtils.getInstance().saveException(pageContext, t); 194 throw new JspException 195 (messages.getMessage("logic.property", name, property, 196 t.toString())); 197 } 198 } else { 199 variable = bean; 200 } 201 } else if (parameter != null) { 202 variable = 203 pageContext.getRequest().getParameter(parameter); 204 } else { 205 JspException e = new JspException 206 (messages.getMessage("logic.selector")); 207 TagUtils.getInstance().saveException(pageContext, e); 208 throw e; 209 } 210 if (variable == null) { 211 variable = ""; } 213 214 int result = 0; 216 if (type == DOUBLE_COMPARE) { 217 try { 218 double doubleVariable = 219 Double.parseDouble(variable.toString()); 220 if (doubleVariable < doubleValue) 221 result = -1; 222 else if (doubleVariable > doubleValue) 223 result = +1; 224 } catch (NumberFormatException e) { 225 result = variable.toString().compareTo(value); 226 } 227 } else if (type == LONG_COMPARE) { 228 try { 229 long longVariable = Long.parseLong(variable.toString()); 230 if (longVariable < longValue) 231 result = -1; 232 else if (longVariable > longValue) 233 result = +1; 234 } catch (NumberFormatException e) { 235 result = variable.toString().compareTo(value); 236 } 237 } else { 238 result = variable.toString().compareTo(value); 239 } 240 241 if (result < 0) 243 result = -1; 244 else if (result > 0) 245 result = +1; 246 247 return ((result == desired1) || (result == desired2)); 249 250 } 251 252 253 } 254 | Popular Tags |