1 package org.apache.beehive.netui.tags.html; 2 3 import org.apache.beehive.netui.pageflow.scoping.ScopedServletUtils; 4 import org.apache.beehive.netui.tags.ByRef; 5 import org.apache.beehive.netui.tags.HtmlUtils; 6 import org.apache.beehive.netui.tags.internal.PageFlowTagUtils; 7 import org.apache.beehive.netui.tags.javascript.CoreScriptFeature; 8 import org.apache.beehive.netui.tags.javascript.IScriptReporter; 9 import org.apache.beehive.netui.tags.javascript.ScriptRequestState; 10 import org.apache.beehive.netui.tags.rendering.*; 11 import org.apache.beehive.netui.util.Bundle; 12 import org.apache.beehive.netui.util.ParamHelper; 13 import org.apache.beehive.netui.util.internal.InternalStringBuilder; 14 import org.apache.beehive.netui.util.logging.Logger; 15 import org.apache.struts.taglib.html.Constants; 16 17 import javax.servlet.ServletRequest ; 18 import javax.servlet.http.HttpServletRequest ; 19 import javax.servlet.http.HttpServletResponse ; 20 import javax.servlet.jsp.JspException ; 21 import java.net.URISyntaxException ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 31 abstract public class AnchorBase extends HtmlBaseTag 32 implements IUrlParams, IHasPopupSupport 33 { 34 protected static final String REQUIRED_ATTR = "href, action, linkName, clientAction"; 35 private static final Logger logger = Logger.getInstance(AnchorBase.class); 36 37 protected AnchorTag.State _state = new AnchorTag.State(); 38 protected String _linkName; protected String _clientAction; 41 private String _action; 42 private String _href; 43 private String _targetScope; private String _location; private Map _params; private Form _form; private boolean _formSubmit = false; private PopupSupport _popupSupport = null; private boolean _disableSecondClick = false; 51 59 public void setAttribute(String name, String value, String facet) 60 throws JspException 61 { 62 if (name != null && name.equals(HREF)) { 63 String s = Bundle.getString("Tags_AttributeMayNotBeSet", new Object []{name}); 64 registerTagError(s, null); 65 } 66 super.setAttribute(name, value, facet); 67 } 68 69 78 public void setShape(String shape) 79 { 80 _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, SHAPE, shape); 81 } 82 83 92 public void setCoords(String coords) 93 { 94 _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, COORDS, coords); 95 } 96 97 107 public void setAction(String action) 108 throws JspException 109 { 110 _action = setRequiredValueAttribute(action, "action"); 111 } 112 113 123 public void setHref(String href) 124 throws JspException 125 { 126 _href = href; 127 } 128 129 138 public void setLocation(String location) 139 { 140 _location = setNonEmptyValueAttribute(location); 141 } 142 143 153 public void setTargetScope(String targetScope) 154 { 155 _targetScope = targetScope; 156 } 157 158 173 public void setFormSubmit(boolean formSubmit) 174 { 175 _formSubmit = formSubmit; 176 } 177 178 188 public void setPopup(boolean popup) 189 { 190 _popupSupport = (popup ? new PopupSupport() : null); 191 } 192 193 194 204 public void setDisableSecondClick(boolean disableSecondClick) 205 { 206 _disableSecondClick = disableSecondClick; 207 } 208 209 210 221 public void setTabindex(int tabindex) 222 { 223 _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, TABINDEX, Integer.toString(tabindex)); 224 } 225 226 227 245 public void setAccessKey(char accessKey) 246 { 247 if (accessKey == 0x00) 248 return; 249 _state.registerAttribute(AbstractHtmlState.ATTR_GENERAL, ACCESSKEY, Character.toString(accessKey)); 250 } 251 252 261 public void setOnBlur(String onblur) 262 { 263 _state.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONBLUR, onblur); 264 } 265 266 275 public void setOnFocus(String onfocus) 276 { 277 _state.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONFOCUS, onfocus); 278 } 279 280 282 288 public void addParameter(String name, Object value, String facet) throws JspException 289 { 290 assert(name != null) : "Parameter 'name' must not be null"; 291 292 if (_params == null) { 293 _params = new HashMap (); 294 } 295 ParamHelper.addParam(_params, name, value); 296 } 297 298 306 protected final boolean createAnchorBeginTag(HttpServletRequest request, ByRef scriptRef, 307 TagRenderingBase trb, AbstractRenderAppender writer, String requiredAttrs) 308 throws JspException 309 { 310 int have = 0; 311 String formAction = null; 312 String idScript = null; 313 314 if (_formSubmit) 315 _form = getNearestForm(); 316 317 ScriptRequestState srs = ScriptRequestState.getScriptRequestState(request); 318 319 if (_href != null) have++; 321 if (_action != null) have++; 322 if (_clientAction != null) have++; 323 if (_linkName != null) have++; 324 325 String tagId = getTagId(); 326 327 if (have == 0 && !_formSubmit && tagId != null) { 329 return createNameAnchor(request, trb); 330 } 331 332 if (_formSubmit == true) { 335 formAction = getFormAction(); 336 if ((formAction != null) && (have == 0)) { 337 have++; 338 } 339 if (_action == null) { 340 _action = formAction; 341 _location = getFormLocation(); 342 } 343 } 344 345 if (have == 0 || have > 1) { 348 String s = Bundle.getString("Tags_Anchor_InvalidAnchorURI", 349 new Object []{getTagName(), requiredAttrs}); 350 registerTagError(s, null); 351 return false; 352 } 353 354 if (_linkName != null) { 355 return createPageAnchor(request, trb); 356 } 357 358 if (_action != null) { 359 if (!PageFlowTagUtils.isAction(request, _action)) { 361 String s = null; 362 if (_action.equals("")) { 363 s = Bundle.getString("Tags_NullBadAction", null); 364 } 365 else { 366 s = Bundle.getString("Tags_BadAction", _action); 367 } 368 registerTagError(s, null); 369 } 370 else { 371 String token = PageFlowTagUtils.getToken(request, _action); 376 if (token != null) { 377 addParamInternal(Constants.TOKEN_KEY, token); 378 } 379 } 380 } 381 382 if (tagId != null) { 385 _state.id = tagId; 386 idScript = renderNameAndId(request, _state, null); 387 } 388 389 if (_clientAction != null) { 391 _state.href = ""; 392 } 393 else { 394 if (_targetScope != null) { 396 addParamInternal(ScopedServletUtils.SCOPE_ID_PARAM, _targetScope); 397 } 398 399 if (_popupSupport != null) { 400 _popupSupport.addParams(this, request); 401 } 402 403 String uri = null; 405 try { 406 if (_action != null) { 407 uri = PageFlowTagUtils.rewriteActionURL(pageContext, _action, _params, _location); 408 } 409 else if (_href != null) { 410 uri = PageFlowTagUtils.rewriteHrefURL(pageContext, _href, _params, _location); 411 } 412 } 413 catch (URISyntaxException e) { 414 logger.error(Bundle.getString("Tags_URISyntaxException")); 416 String s = Bundle.getString("Tags_Anchor_URLException", 417 new Object []{e.getMessage()}); 418 registerTagError(s, e); 419 } 420 421 if (uri == null) { 422 if (hasErrors()) 423 return false; 424 } 425 else { 426 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 427 _state.href = response.encodeURL(uri); 428 } 429 } 430 431 IScriptReporter sr = getScriptReporter(); 433 if (_clientAction != null && srs.isFeatureWritten(CoreScriptFeature.DYNAMIC_INIT)) { 434 String action = HtmlUtils.escapeEscapes(_clientAction); 436 String entry = ScriptRequestState.getString("netuiAction", 437 new Object []{action}); 438 _state.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONCLICK, entry); 439 } 442 443 String onclick = _state.getAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONCLICK); 445 if (onclick == null) { 446 if (_formSubmit && formAction != null) { 447 String realFormName = getFormId(); 448 String key = (_disableSecondClick ? "anchorDisableAndSubmitFormAction" : "anchorFormSubmitAction"); 449 String entry = ScriptRequestState.getString(key, new Object []{realFormName, _state.href}); 450 _state.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONCLICK, entry); 451 if (_form != null) 455 _form.insureRealId(); 456 } 457 else if (_disableSecondClick) { 458 String entry = ScriptRequestState.getString("anchorDisableAction", null); 459 _state.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONCLICK, entry); 460 } 461 else if (_popupSupport != null) { 462 _state.registerAttribute(AbstractHtmlState.ATTR_JAVASCRIPT, ONCLICK, _popupSupport.getOnClick(request,_state.href)); 463 } 466 } 467 468 if (hasErrors()) 469 return false; 470 471 trb.doStartTag(writer, _state); 472 473 if (_formSubmit && formAction != null || idScript != null || _popupSupport != null) { 475 InternalStringBuilder script = new InternalStringBuilder(32); 476 StringBuilderRenderAppender scriptWriter = new StringBuilderRenderAppender(script); 477 478 if (_formSubmit && formAction != null) 479 srs.writeFeature(sr, scriptWriter, CoreScriptFeature.ANCHOR_SUBMIT, true, false, 480 null); 481 if (_popupSupport != null) 482 _popupSupport.writeScript(request, srs, getScriptReporter(), scriptWriter); 483 if (idScript != null) 484 scriptWriter.append(idScript); 485 scriptRef.setRef(script.toString()); 486 } 487 488 return true; 490 } 491 492 private void addParamInternal(String paramName, String paramVal) 493 { 494 if (_params == null) { 495 _params = new HashMap (); 496 } 497 _params.put(paramName, paramVal); 498 } 499 500 505 private boolean createNameAnchor(ServletRequest req, TagRenderingBase trb) 506 { 507 assert(_state.id != null) : "tagId must not be nulll"; 508 509 WriteRenderAppender writer = new WriteRenderAppender(pageContext); 510 511 _state.id = getIdForTagId(_state.id); 515 String script = renderDefaultNameAndId((HttpServletRequest ) req, _state, _state.id, null); 516 517 _state.name = _state.id; 519 520 trb = TagRenderingBase.Factory.getRendering(TagRenderingBase.ANCHOR_TAG, req); 522 trb.doStartTag(writer, _state); 523 return !hasErrors(); 524 } 525 526 535 private boolean createPageAnchor(ServletRequest req, TagRenderingBase trb) 536 { 537 _linkName = _linkName.trim(); 541 if (_linkName.charAt(0) != '#') { 542 _state.href = "#" + getIdForTagId(_linkName); 543 } 544 else { 545 _state.href = _linkName; 546 } 547 548 if (_state.id != null) { 550 _state.name = _state.id; 552 renderNameAndId((HttpServletRequest ) req, _state,null); 553 } 555 556 WriteRenderAppender writer = new WriteRenderAppender(pageContext); 558 trb = TagRenderingBase.Factory.getRendering(TagRenderingBase.ANCHOR_TAG, req); 559 trb.doStartTag(writer, _state); 560 return !hasErrors(); 561 } 562 563 567 private String getFormAction() 568 { 569 if (_form != null) 570 return _form.getAction(); 571 return null; 572 } 573 574 private String getFormLocation() 575 { 576 if (_form != null) 577 return _form.getLocation(); 578 return null; 579 } 580 581 586 private String getFormId() 587 { 588 if (_form != null) { 589 return _form.getRealFormId(); 590 } 591 return null; 592 } 593 594 597 protected void localRelease() 598 { 599 super.localRelease(); 600 601 _state.clear(); 602 _linkName = null; 603 _clientAction = null; 604 605 _action = null; 606 _href = null; 607 _targetScope = null; 608 _location = null; 609 _params = null; 610 _form = null; 611 _formSubmit = false; 612 _popupSupport = null; 613 _disableSecondClick = false; 614 } 615 616 public PopupSupport getPopupSupport() 617 { 618 return _popupSupport; 619 } 620 } 621 | Popular Tags |