1 16 package com.blandware.atleap.webapp.taglib.core.html; 17 18 import com.blandware.atleap.webapp.taglib.core.BaseHandlerTag; 19 import com.blandware.atleap.webapp.taglib.core.ParamParent; 20 import com.blandware.atleap.webapp.util.core.RequestUtil; 21 import com.blandware.atleap.webapp.util.core.WebappUtil; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.struts.Globals; 25 import org.apache.struts.taglib.TagUtils; 26 import org.apache.struts.taglib.html.Constants; 27 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpServletResponse ; 30 import javax.servlet.http.HttpSession ; 31 import javax.servlet.jsp.JspException ; 32 import javax.servlet.jsp.JspTagException ; 33 import javax.servlet.jsp.PageContext ; 34 import javax.servlet.jsp.tagext.JspFragment ; 35 import java.io.IOException ; 36 import java.io.StringWriter ; 37 import java.net.MalformedURLException ; 38 import java.util.HashMap ; 39 import java.util.Map ; 40 41 109 public class LinkTag extends BaseHandlerTag implements ParamParent { 110 111 protected transient final Log log = LogFactory.getLog(getClass()); 112 113 116 protected String action; 117 118 121 protected String forward; 122 123 126 protected String href; 127 128 131 protected String name; 132 133 136 protected String anchor; 137 138 141 protected String target; 142 143 146 protected Boolean transaction = Boolean.FALSE; 147 148 151 protected Boolean addLocaleSuffix = Boolean.TRUE; 152 153 156 protected Map parameters = new HashMap (); 157 158 164 protected boolean suppressValidation = false; 165 166 176 public String getAction() { 177 return action; 178 } 179 180 186 public void setAction(String action) { 187 this.action = action; 188 } 189 190 200 public String getForward() { 201 return forward; 202 } 203 204 210 public void setForward(String forward) { 211 this.forward = forward; 212 } 213 214 224 public String getHref() { 225 return href; 226 } 227 228 234 public void setHref(String href) { 235 this.href = href; 236 } 237 238 248 public String getName() { 249 return name; 250 } 251 252 258 public void setName(String name) { 259 this.name = name; 260 } 261 262 272 public String getAnchor() { 273 return anchor; 274 } 275 276 282 public void setAnchor(String anchor) { 283 this.anchor = anchor; 284 } 285 286 296 public String getTarget() { 297 return target; 298 } 299 300 306 public void setTarget(String target) { 307 this.target = target; 308 } 309 310 319 public Boolean getTransaction() { 320 return transaction; 321 } 322 323 328 public void setTransaction(Boolean transaction) { 329 this.transaction = transaction; 330 } 331 332 341 public Boolean getAddLocaleSuffix() { 342 return addLocaleSuffix; 343 } 344 345 350 public void setAddLocaleSuffix(Boolean addLocaleSuffix) { 351 this.addLocaleSuffix = addLocaleSuffix; 352 } 353 354 360 public void doTag() throws JspException , IOException { 361 362 PageContext pageContext = (PageContext ) getJspContext(); 363 HttpServletRequest request = (HttpServletRequest ) pageContext.getRequest(); 364 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 365 366 if (!suppressValidation) { 368 if ( name == null && action == null && forward == null && href == null ) { 369 JspTagException e = new JspTagException ("Because 'name' attribute is not specified, either 'action' or 'forward' or 'href' attrbute must be specified"); 370 throw e; 371 } 372 } 373 374 if ( transaction == null ) { 375 transaction = Boolean.FALSE; 376 } 377 378 if ( addLocaleSuffix == null ) { 379 addLocaleSuffix = Boolean.TRUE; 380 } 381 382 JspFragment body = getJspBody(); 384 String bodyContent = null; 385 if ( body != null ) { 386 StringWriter sw = new StringWriter (); 387 body.invoke(sw); 388 bodyContent = sw.toString(); 389 } 390 391 if ( transaction.booleanValue() ) { 393 HttpSession session = pageContext.getSession(); 394 String token = null; 395 if ( session != null ) { 396 token = (String ) session.getAttribute(Globals.TRANSACTION_TOKEN_KEY); 397 } 398 399 if ( token != null ) { 400 parameters.put(Constants.TOKEN_KEY, token); 401 } 402 } 403 404 String url = null; 406 try { 407 url = WebappUtil.computeURL(action, forward, href, anchor, parameters, request, response, true, false, addLocaleSuffix.booleanValue()); 408 } catch ( MalformedURLException e ) { 409 throw new JspTagException (e); 410 } 411 412 StringBuffer result = new StringBuffer (); 413 openElement(result); 414 appendLocation(result, url); 415 appendAttributes(result); 416 417 closeOpeningTag(result); 418 419 if ( bodyContent != null && bodyContent.trim().length() > 0 ) { 421 appendBody(result, bodyContent); 422 } 423 closeElement(result); 424 425 TagUtils.getInstance().write(pageContext, result.toString()); 427 428 } 429 430 435 protected void openElement(StringBuffer element) { 436 element.append("<a "); 437 } 438 439 445 protected void appendLocation(StringBuffer element, String location) { 446 if ( name == null ) { 447 element.append("href=\"").append(location).append("\" "); 448 } else { 449 element.append("name=\"").append(name).append("\" "); 450 } 451 } 452 453 458 protected void appendAttributes(StringBuffer element) { 459 if ( target != null ) { 460 element.append("target=\"").append(target).append("\" "); 461 } 462 element.append(prepareAttributes()); 463 } 464 465 470 protected void closeOpeningTag(StringBuffer element) { 471 element.append(">"); 472 } 473 474 479 protected void appendBody(StringBuffer element, String body) { 480 element.append(body); 481 } 482 483 487 protected void closeElement(StringBuffer element) { 488 element.append("</a>"); 489 } 490 491 494 public void addParameter(String name, Object value) { 495 parameters.put(name, RequestUtil.mergeValues(parameters.get(name), value)); 496 } 497 498 } 499 | Popular Tags |