1 package fr.improve.struts.taglib.layout; 2 3 import java.util.HashMap ; 4 import java.util.Map ; 5 6 import javax.servlet.jsp.JspException ; 7 import javax.servlet.jsp.PageContext ; 8 9 import org.apache.struts.taglib.html.Constants; 10 import org.apache.struts.taglib.logic.IterateTag; 11 import org.apache.struts.util.MessageResources; 12 13 import fr.improve.struts.taglib.layout.el.Expression; 14 import fr.improve.struts.taglib.layout.event.EndLayoutEvent; 15 import fr.improve.struts.taglib.layout.event.LayoutEventListener; 16 import fr.improve.struts.taglib.layout.event.StartLayoutEvent; 17 import fr.improve.struts.taglib.layout.policy.AbstractPolicy; 18 import fr.improve.struts.taglib.layout.skin.Skin; 19 import fr.improve.struts.taglib.layout.util.FormUtilsInterface; 20 import fr.improve.struts.taglib.layout.util.LayoutUtils; 21 import fr.improve.struts.taglib.layout.util.ParentFinder; 22 import fr.improve.struts.taglib.layout.util.TagUtils; 23 24 31 32 public class LinkTag extends org.apache.struts.taglib.html.BaseHandlerTag implements LayoutTag, LayoutEventListener { 33 34 35 37 38 41 protected String text = null; 42 43 44 46 47 50 protected String anchor = null; 51 52 public String getAnchor() { 53 return (this.anchor); 54 } 55 56 public void setAnchor(String anchor) { 57 this.anchor = anchor; 58 } 59 60 61 64 protected String forward = null; 65 66 public String getForward() { 67 return (this.forward); 68 } 69 70 public void setForward(String forward) { 71 this.forward = forward; 72 } 73 74 75 78 protected String href = null; 79 protected String jspHref; 80 81 public String getHref() { 82 return (this.href); 83 } 84 85 public void setHref(String href) { 86 this.href = href; 87 } 88 89 90 93 protected String linkName = null; 94 95 public String getLinkName() { 96 return (this.linkName); 97 } 98 99 public void setLinkName(String linkName) { 100 this.linkName = linkName; 101 } 102 103 104 107 protected static MessageResources messages = 108 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 109 110 111 114 protected String name = null; 115 116 public String getName() { 117 return (this.name); 118 } 119 120 public void setName(String name) { 121 this.name = name; 122 } 123 124 125 129 protected String page = null; 130 protected String action; 131 protected String module; 132 133 public String getPage() { 134 return (this.page); 135 } 136 137 public void setPage(String page) { 138 this.page = page; 139 } 140 141 142 145 protected String paramId = null; 146 147 public String getParamId() { 148 return (this.paramId); 149 } 150 151 public void setParamId(String paramId) { 152 this.paramId = paramId; 153 } 154 155 156 159 protected String paramName = null; 160 161 public String getParamName() { 162 return (this.paramName); 163 } 164 165 public void setParamName(String paramName) { 166 this.paramName = paramName; 167 } 168 169 170 173 protected String paramProperty = null; 174 175 public String getParamProperty() { 176 return (this.paramProperty); 177 } 178 179 public void setParamProperty(String paramProperty) { 180 this.paramProperty = paramProperty; 181 } 182 183 184 187 protected String paramScope = null; 188 189 public String getParamScope() { 190 return (this.paramScope); 191 } 192 193 public void setParamScope(String paramScope) { 194 this.paramScope = paramScope; 195 } 196 197 198 201 protected String property = null; 202 203 public String getProperty() { 204 return (this.property); 205 } 206 207 public void setProperty(String property) { 208 this.property = property; 209 } 210 211 212 215 protected String scope = null; 216 217 public String getScope() { 218 return (this.scope); 219 } 220 221 public void setScope(String scope) { 222 this.scope = scope; 223 } 224 225 226 229 protected String target = null; 230 231 public String getTarget() { 232 return (this.target); 233 } 234 235 public void setTarget(String target) { 236 this.target = target; 237 } 238 239 240 243 protected boolean transaction = false; 244 245 public boolean getTransaction() { 246 return (this.transaction); 247 } 248 249 public void setTransaction(boolean transaction) { 250 this.transaction = transaction; 251 } 252 253 256 protected String indexId = null; 257 258 public String getIndexId() { 259 return (this.indexId); 260 } 261 262 public void setIndexId(String indexId) { 263 this.indexId = indexId; 264 } 265 266 protected boolean layout = true; 267 268 public void setLayout(boolean in_layout) { 269 layout = in_layout; 270 } 271 public boolean isLayout() { 272 return layout; 273 } 274 275 protected String scheme; 276 277 public String getScheme() { 278 return scheme; 279 } 280 281 public void setScheme(String scheme) { 282 this.scheme = scheme; 283 } 284 285 287 public final PageContext getPageContext() { 288 return pageContext; 289 } 290 291 public final int doStartTag() throws JspException { 292 ParentFinder.registerTag(pageContext, this); 293 initDynamicValues(); 294 return doStartLayoutTag(); 295 } 296 297 public final int doEndTag() throws JspException { 298 try { 299 return doEndLayoutTag(); 300 } finally { 301 reset(); 302 ParentFinder.deregisterTag(pageContext); 303 } 304 } 305 306 309 protected void initDynamicValues() { 310 jspHref = href; 311 href = Expression.evaluate(jspHref, pageContext); 312 } 313 314 320 public int doStartLayoutTag() throws JspException { 321 322 if (policy != null) { 324 Skin lc_currentSkin = LayoutUtils.getSkin(pageContext.getSession()); 325 AbstractPolicy lc_policy = lc_currentSkin.getPolicy(); 326 String lc_action = null; 327 if (getHref() != null) { 328 lc_action = getHref(); 329 } else if (getPage() != null) { 330 lc_action = getPage(); 331 } else if (getForward() != null) { 332 lc_action = getForward(); 333 } else { 334 throw new JspException ("You must specify exactly one of the following attributes for the Link tag: href, page or forward."); 335 } 336 switch (lc_policy.getAuthorizedDisplayMode(getPolicy(), lc_action, getProperty(), pageContext)) { 337 case AbstractPolicy.MODE_EDIT: 338 break; 339 case AbstractPolicy.MODE_NODISPLAY: 340 display = false; 341 break; 342 case AbstractPolicy.MODE_INSPECT: 343 showLink = false; 344 break; 345 default: 346 throw new IllegalStateException (lc_policy.getClass().getName() + " returns an illegal value"); 347 } 348 } 349 350 if (!display) 352 return SKIP_BODY; 353 354 if (isLayout()) { 355 StringBuffer lc_buffer = new StringBuffer (); 356 beginLinkLayout(lc_buffer); 357 new StartLayoutEvent(this, lc_buffer.toString()).send(); 358 } 359 360 if (linkName != null) { 362 StringBuffer results = new StringBuffer ("<a name=\""); 363 results.append(linkName); 364 results.append("\">"); 365 TagUtils.write(pageContext, results.toString()); 366 return (EVAL_BODY_TAG); 367 } 368 369 String url = computeURL(); 371 372 StringBuffer results = new StringBuffer ("<a "); 374 375 if (showLink) { 376 results.append("href=\""); 377 results.append(url); 378 results.append("\""); 379 if (target != null) { 380 results.append(" target=\""); 381 results.append(target); 382 results.append("\""); 383 } 384 } 385 results.append(prepareStyles()); 386 results.append(prepareEventHandlers()); 387 results.append(">"); 388 389 TagUtils.write(pageContext, results.toString()); 391 392 this.text = null; 394 return (EVAL_BODY_TAG); 395 396 } 397 398 protected String computeURL() throws JspException { 399 Map params = LayoutUtils.computeParameters 401 (pageContext, paramId, paramName, paramProperty, paramScope, 402 name, property, scope, transaction); 403 404 if( indexId!=null ) { 407 IterateTag iterateTag = (IterateTag) findAncestorWithClass(this, IterateTag.class); 409 if (iterateTag == null) { 410 JspException e = new JspException (messages.getMessage("indexed.noEnclosingIterate")); 412 TagUtils.saveException(pageContext, e); 413 throw e; 414 } 415 416 if (params == null) { 418 params = new HashMap (); } 420 params.put(indexId, Integer.toString(iterateTag.getIndex())); 421 } 422 423 String url = null; 424 url = LayoutUtils.computeURL(pageContext, forward, href, 425 page, action, module, params, anchor, false, target, 426 Expression.evaluate(scheme, pageContext)); 427 return url; 428 } 429 430 431 432 437 public int doAfterBody() throws JspException { 438 439 if (bodyContent != null) { 440 String value = bodyContent.getString().trim(); 441 if (value.length() > 0) 442 text = value; 443 } 444 return (SKIP_BODY); 445 446 } 447 448 449 454 public int doEndLayoutTag() throws JspException { 455 if (!display) { 457 display = true; 458 return EVAL_PAGE; 459 } 460 461 StringBuffer results = new StringBuffer (); 463 if (text != null) 464 results.append(text); 465 results.append("</a>"); 466 467 468 TagUtils.write(pageContext, results.toString()); 470 471 if (isLayout()) { 472 results.setLength(0); 473 endLinkLayout(results); 474 new EndLayoutEvent(this, results.toString()).send(); 475 } 476 477 return (EVAL_PAGE); 479 480 } 481 482 protected void reset() { 483 href = jspHref; 484 jspHref = null; 485 486 showLink = true; 487 } 488 489 492 protected void beginLinkLayout(StringBuffer in_buffer) throws JspException { 493 in_buffer.append("<td> </td><td"); 494 in_buffer.append(">"); 500 } 501 502 505 protected void endLinkLayout(StringBuffer in_buffer) { 506 in_buffer.append("</td>"); 507 } 508 509 512 public void release() { 513 514 super.release(); 515 anchor = null; 516 forward = null; 517 href = null; 518 action = null; 519 module = null; 520 linkName = null; 521 name = null; 522 page = null; 523 paramId = null; 524 paramName = null; 525 paramProperty = null; 526 paramScope = null; 527 property = null; 528 scope = null; 529 target = null; 530 text = null; 531 transaction = false; 532 policy = null; 533 display = true; 534 layout = true; 535 scheme = null; 536 } 537 538 539 protected boolean display = true; 540 protected boolean showLink = true; 541 protected String policy = null; 542 543 547 public void setMode(String in_mode) { 548 if (in_mode == null || in_mode.length() != 5) { 549 throw new IllegalArgumentException ( 550 "The specified mode" + in_mode + " is invalid"); 551 } 552 int lc_formMode = LayoutUtils.getSkin(pageContext.getSession()).getFormUtils().getFormDisplayMode(pageContext); 553 char lc_displayMode; 554 switch (lc_formMode) { 555 case FormUtilsInterface.CREATE_MODE : 556 lc_displayMode = in_mode.charAt(0); 557 break; 558 case FormUtilsInterface.EDIT_MODE : 559 lc_displayMode = in_mode.charAt(2); 560 break; 561 case FormUtilsInterface.INSPECT_MODE : 562 lc_displayMode = in_mode.charAt(4); 563 break; 564 default : 565 lc_displayMode = 'D'; 566 } 567 display = lc_displayMode == 'D' || lc_displayMode == 'd'; 568 } 569 570 573 public String getPolicy() 574 { 575 return policy; 576 } 577 578 581 public void setPolicy(String policy) 582 { 583 this.policy = policy; 584 } 585 586 public Object processEndLayoutEvent(EndLayoutEvent in_event) throws JspException { 587 return Boolean.FALSE; 588 } 589 public Object processStartLayoutEvent(StartLayoutEvent in_event) throws JspException { 590 return Boolean.FALSE; 591 } 592 public String getAction() { 593 return action; 594 } 595 public void setAction(String action) { 596 this.action = action; 597 } 598 599 public String getModule() { 600 return module; 601 } 602 603 public void setModule(String module) { 604 this.module = module; 605 } 606 } | Popular Tags |