1 25 package org.ofbiz.webapp.taglib; 26 27 import java.io.IOException ; 28 import java.lang.reflect.Method ; 29 import java.util.Collection ; 30 import javax.servlet.jsp.JspTagException ; 31 import javax.servlet.jsp.JspWriter ; 32 import javax.servlet.jsp.tagext.BodyContent ; 33 import javax.servlet.jsp.tagext.BodyTagSupport ; 34 35 import org.ofbiz.base.util.Debug; 36 37 45 public class IfTag extends BodyTagSupport { 46 47 public static final String module = IfTag.class.getName(); 48 49 private String name = null; 50 private String value = null; 51 private String type = null; 52 private Integer size = null; 53 54 public void setName(String name) { 55 this.name = name; 56 } 57 58 public void setValue(String value) { 59 this.value = value; 60 } 61 62 public void setType(String type) { 63 this.type = type; 64 } 65 66 public void setSize(String size) throws NumberFormatException { 67 this.size = Integer.valueOf(size); 68 } 69 70 public String getName() { 71 return name; 72 } 73 74 public String getValue() { 75 return value; 76 } 77 78 public String getType() { 79 return type; 80 } 81 82 public String getSize() { 83 if (size == null) 84 return null; 85 return size.toString(); 86 } 87 88 public int doStartTag() throws JspTagException { 89 Object object = null; 90 91 try { 92 object = pageContext.findAttribute(name); 93 if (object == null) { 94 object = pageContext.getRequest().getParameter(name); 95 } 96 if (object == null) { 97 return SKIP_BODY; 98 } 99 } catch (RuntimeException e) { 100 Debug.logError(e, module); 101 return SKIP_BODY; 102 } 103 104 if (size != null) { 105 int localSize = size.intValue(); 106 107 try { 108 if (object instanceof Collection ) { 109 if (((Collection ) object).size() > localSize) 111 return EVAL_BODY_AGAIN; 112 } else if (object instanceof String ) { 113 if (((String ) object).length() > localSize) 115 return EVAL_BODY_AGAIN; 116 } else { 117 try { 119 Method sizeMethod = object.getClass().getMethod("size", (Class []) null); 120 int objectSize = ((Integer ) sizeMethod.invoke(object, (Object []) null)).intValue(); 121 122 if (objectSize > localSize) 123 return EVAL_BODY_AGAIN; 124 } catch (Exception e) { 125 Debug.logError(e, module); 126 return SKIP_BODY; 127 } 128 } 129 } catch (RuntimeException e) { 130 Debug.logError(e, module); 131 return SKIP_BODY; 132 } 133 } else if (object instanceof Boolean || "Boolean".equalsIgnoreCase(type)) { 134 try { 136 Boolean b = (Boolean ) object; 137 138 if (value != null) { 139 Boolean v = new Boolean (value); 140 141 if (b.equals(v)) 142 return EVAL_BODY_AGAIN; 143 } else { 144 if (b.booleanValue()) 145 return EVAL_BODY_AGAIN; 146 } 147 } catch (RuntimeException e) { 148 Debug.logError(e, module); 149 return SKIP_BODY; 150 } 151 } else if (value != null) { 152 if (object instanceof String || "String".equalsIgnoreCase(type)) { 153 try { 155 String s = (String ) object; 156 157 if (s.equals(value)) 158 return EVAL_BODY_AGAIN; 159 } catch (RuntimeException e) { 160 Debug.logError(e, module); 161 return SKIP_BODY; 162 } 163 } else if (object instanceof Integer || "Integer".equalsIgnoreCase(type)) { 164 try { 166 Integer i = (Integer ) object; 167 Integer v = Integer.valueOf(value); 168 169 if (i.equals(v)) 170 return EVAL_BODY_AGAIN; 171 } catch (RuntimeException e) { 172 Debug.logError(e, module); 173 return SKIP_BODY; 174 } 175 } else if (object instanceof Long || "Long".equalsIgnoreCase(type)) { 176 try { 178 Long i = (Long ) object; 179 Long v = Long.valueOf(value); 180 181 if (i.equals(v)) 182 return EVAL_BODY_AGAIN; 183 } catch (RuntimeException e) { 184 Debug.logError(e, module); 185 return SKIP_BODY; 186 } 187 } else if (object instanceof Float || "Float".equalsIgnoreCase(type)) { 188 try { 190 Float d = (Float ) object; 191 Float v = Float.valueOf(value); 192 193 if (d.equals(v)) 194 return EVAL_BODY_AGAIN; 195 } catch (RuntimeException e) { 196 Debug.logError(e, module); 197 return SKIP_BODY; 198 } 199 } else if (object instanceof Double || "Double".equalsIgnoreCase(type)) { 200 try { 202 Double d = (Double ) object; 203 Double v = Double.valueOf(value); 204 205 if (d.equals(v)) 206 return EVAL_BODY_AGAIN; 207 } catch (RuntimeException e) { 208 Debug.logError(e, module); 209 return SKIP_BODY; 210 } 211 } else { 212 Object valueObject = null; 214 215 try { 216 valueObject = pageContext.findAttribute(value); 217 if (valueObject != null && valueObject.equals(object)) 218 return EVAL_BODY_AGAIN; 219 } catch (RuntimeException e) { 220 Debug.logError(e, module); 221 return SKIP_BODY; 222 } 223 } 224 } else { 225 return EVAL_BODY_AGAIN; 229 } 230 231 return SKIP_BODY; 232 } 233 234 public int doAfterBody() { 235 return SKIP_BODY; 236 } 237 238 public int doEndTag() { 239 try { 240 BodyContent body = getBodyContent(); 241 242 if (body != null) { 243 JspWriter out = body.getEnclosingWriter(); 244 String bodyString = body.getString(); 245 body.clearBody(); 246 out.print(bodyString); 248 } 249 } catch (IOException e) { 250 Debug.logError(e, "IfTag Error.", module); 251 } 252 return EVAL_PAGE; 253 } 254 } 255 256 | Popular Tags |