1 18 19 package org.apache.struts.taglib.tiles; 20 21 import java.lang.reflect.InvocationTargetException ; 22 23 import javax.servlet.jsp.JspException ; 24 import javax.servlet.jsp.tagext.BodyTagSupport ; 25 26 import org.apache.commons.beanutils.PropertyUtils; 27 import org.apache.struts.taglib.tiles.util.TagUtils; 28 import org.apache.struts.tiles.AttributeDefinition; 29 import org.apache.struts.tiles.DefinitionNameAttribute; 30 import org.apache.struts.tiles.DirectStringAttribute; 31 import org.apache.struts.tiles.PathAttribute; 32 33 63 public class PutTag extends BodyTagSupport implements ComponentConstants { 64 65 66 67 70 protected String attributeName = null; 71 72 75 private Object value = null; 76 77 80 private String direct = null; 81 82 85 private String valueType = null; 86 87 90 private String beanName = null; 91 92 95 private String beanProperty = null; 96 97 100 private String beanScope = null; 101 102 105 private String role = null; 106 107 108 109 112 protected Object realValue = null; 113 114 117 protected String body = null; 118 119 122 public PutTag() { 123 super(); 124 } 125 126 129 public void release() { 130 131 super.release(); 132 133 attributeName = null; 134 valueType = null; 135 direct = null; 136 value = null; 137 beanName = null; 138 beanProperty = null; 139 beanScope = null; 140 role = null; 141 body = null; 142 } 143 144 147 protected void releaseInternal() { 148 realValue = null; 149 } 150 151 154 public void setName(String value) { 155 this.attributeName = value; 156 } 157 158 161 public String getName() { 162 return attributeName; 163 } 164 165 169 public void setValue(String value) { 170 this.value = value; 171 } 172 173 177 public String getValue() { 178 return (String ) this.value; 179 } 180 181 184 public void setValue(Object value) { 185 this.value = value; 186 } 187 188 192 public void setObjectValue(Object value) { 193 this.value = value; 194 } 195 196 200 public void setContent(String value) { 201 this.value = value; 202 } 203 204 208 public String getContent() { 209 return (String ) value; 210 } 211 212 215 public void setContent(Object value) { 216 this.value = value; 217 } 218 219 223 public void setDirect(String isDirect) { 224 this.direct = isDirect; 225 } 226 227 230 public void setType(String value) { 231 this.valueType = value; 232 } 233 234 237 public String getType() { 238 return this.valueType; 239 } 240 241 244 public void setBeanName(String value) { 245 this.beanName = value; 246 } 247 248 251 public String getBeanName() { 252 return beanName; 253 } 254 255 258 public void setBeanProperty(String value) { 259 this.beanProperty = value; 260 } 261 262 265 public String getBeanProperty() { 266 return beanProperty; 267 } 268 269 272 public void setBeanScope(String value) { 273 this.beanScope = value; 274 } 275 276 279 public String getBeanScope() { 280 return beanScope; 281 } 282 283 287 public void setRole(String role) { 288 this.role = role; 289 } 290 291 295 public String getRole() { 296 return role; 297 } 298 299 305 public Object getRealValue() throws JspException { 306 if (realValue == null) { 307 computeRealValue(); 308 } 309 310 return realValue; 311 } 312 313 317 protected void computeRealValue() throws JspException { 318 realValue = value; 320 321 if (value == null && beanName == null) { 323 if (body != null) { 325 realValue = body; 326 } else { 327 realValue = ""; 328 } 329 } 330 331 if (realValue == null && beanName != null) { 333 getRealValueFromBean(); 334 return; 335 } 336 337 if (valueType == null && direct != null) { 342 if (Boolean.valueOf(direct).booleanValue() == true) { 343 valueType = "string"; 344 } else { 345 valueType = "page"; 346 } 347 } 348 349 if (realValue != null 350 && valueType != null 351 && !(value instanceof AttributeDefinition)) { 352 353 String strValue = realValue.toString(); 354 if (valueType.equalsIgnoreCase("string")) { 355 realValue = new DirectStringAttribute(strValue); 356 357 } else if (valueType.equalsIgnoreCase("page")) { 358 realValue = new PathAttribute(strValue); 359 360 } else if (valueType.equalsIgnoreCase("template")) { 361 realValue = new PathAttribute(strValue); 362 363 } else if (valueType.equalsIgnoreCase("instance")) { 364 realValue = new DefinitionNameAttribute(strValue); 365 366 } else if (valueType.equalsIgnoreCase("definition")) { 367 realValue = new DefinitionNameAttribute(strValue); 368 369 } else { throw new JspException ( 371 "Warning - Tag put : Bad type '" + valueType + "'."); 372 } 373 } 374 375 } 376 377 381 protected void getRealValueFromBean() throws JspException { 382 try { 383 Object bean = TagUtils.retrieveBean(beanName, beanScope, pageContext); 384 if (bean != null && beanProperty != null) { 385 realValue = PropertyUtils.getProperty(bean, beanProperty); 386 } else { 387 realValue = bean; } 389 390 } catch (NoSuchMethodException ex) { 391 throw new JspException ( 392 "Error - component.PutAttributeTag : Error while retrieving value from bean '" 393 + beanName 394 + "' with property '" 395 + beanProperty 396 + "' in scope '" 397 + beanScope 398 + "'. (exception : " 399 + ex.getMessage()); 400 401 } catch (InvocationTargetException ex) { 402 throw new JspException ( 403 "Error - component.PutAttributeTag : Error while retrieving value from bean '" 404 + beanName 405 + "' with property '" 406 + beanProperty 407 + "' in scope '" 408 + beanScope 409 + "'. (exception : " 410 + ex.getMessage()); 411 412 } catch (IllegalAccessException ex) { 413 throw new JspException ( 414 "Error - component.PutAttributeTag : Error while retrieving value from bean '" 415 + beanName 416 + "' with property '" 417 + beanProperty 418 + "' in scope '" 419 + beanScope 420 + "'. (exception : " 421 + ex.getMessage()); 422 } 423 } 424 425 428 public int doStartTag() throws JspException { 429 430 body = null; 432 433 if (value == null && beanName == null) { 435 return EVAL_BODY_TAG; 436 } 437 438 return SKIP_BODY; 440 } 441 442 447 public int doAfterBody() throws JspException { 448 449 if (bodyContent != null) { 450 body = bodyContent.getString(); 451 } 452 return (SKIP_BODY); 453 454 } 455 456 459 public int doEndTag() throws JspException { 460 callParent(); 462 463 releaseInternal(); 465 466 return EVAL_PAGE; 467 } 468 469 473 protected void callParent() throws JspException { 474 PutTagParent enclosingParent = findEnclosingPutTagParent(); 476 enclosingParent.processNestedTag(this); 477 } 478 479 483 protected PutTagParent findEnclosingPutTagParent() throws JspException { 484 try { 485 PutTagParent parent = 486 (PutTagParent) findAncestorWithClass(this, PutTagParent.class); 487 488 if (parent == null) { 489 throw new JspException ("Error - tag put : enclosing tag doesn't accept 'put' tag."); 490 } 491 492 return parent; 493 494 } catch (ClassCastException ex) { 495 throw new JspException ("Error - tag put : enclosing tag doesn't accept 'put' tag."); 496 } 497 } 498 499 } 500 | Popular Tags |