1 10 package org.mmbase.bridge.jsp.taglib; 11 12 import javax.servlet.http.HttpServletRequest ; 13 import javax.servlet.jsp.*; 14 import javax.servlet.jsp.tagext.*; 15 import javax.servlet.jsp.jstl.core.*; 16 17 import java.io.*; 18 19 import org.mmbase.bridge.jsp.taglib.util.Attribute; 20 import org.mmbase.bridge.jsp.taglib.containers.QueryContainer; 21 import org.mmbase.util.Casting; 22 import org.mmbase.util.logging.*; 23 import org.mmbase.util.functions.Parameter; 24 import org.mmbase.util.functions.Parameters; 25 26 import java.util.*; 27 28 38 39 public abstract class ContextReferrerTag extends BodyTagSupport implements TryCatchFinally { 40 41 48 public static int EVAL_BODY = -1; 49 53 54 private static final Logger log = Logging.getLoggerInstance(ContextReferrerTag.class); 55 56 private static final Logger pageLog = Logging.getLoggerInstance(Logging.PAGE_CATEGORY); 57 58 59 60 protected static String getTaglibVersion() { 61 try { 62 ClassLoader cl = ContextReferrerTag.class.getClassLoader(); 63 InputStream is = cl.getResourceAsStream("org/mmbase/taglib/version"); 64 if (is == null) { 65 return "1.1"; 66 } 67 BufferedReader r = new BufferedReader(new InputStreamReader(is)); 68 return r.readLine(); 69 } catch (IOException io) { 70 return "1.0"; 72 } 73 } 74 75 76 77 protected ContextTag pageContextTag = null; 78 79 protected Attribute contextId = Attribute.NULL; protected Attribute referid = Attribute.NULL; 81 82 protected Attribute id = Attribute.NULL; 84 private String thisPage = null; 85 86 void setPageContextOnly(PageContext pc) { 87 super.setPageContext(pc); 88 } 90 91 92 public PageContext getPageContext() { 93 return pageContext; 94 } 95 96 99 protected ContextTag getPageContextTag() { 100 if (pageContextTag == null) { 101 102 pageContextTag = (ContextTag) pageContext.getAttribute(ContextTag.CONTEXTTAG_KEY); 103 104 105 if (pageContextTag == null) { log.debug("No pageContextTag found in pagecontext, creating.."); 107 if (pageLog.isServiceEnabled()) { 108 HttpServletRequest request = ((HttpServletRequest )pageContext.getRequest()); 109 String queryString = ((HttpServletRequest )pageContext.getRequest()).getQueryString(); 111 String includedPage = (String ) request.getAttribute("javax.servlet.include.servlet_path"); 112 thisPage = (includedPage == null ? "" : includedPage + " for ") + request.getRequestURI(); 113 pageLog.service("Parsing JSP page: " + thisPage + 114 (queryString != null ? "?" + queryString : "")); 115 } 116 pageContextTag = new ContextTag(); 117 pageContextTag.setId(null); 118 119 123 pageContextTag.setPageContextOnly(pageContext); 124 125 pageContextTag.createContainer(null); 129 pageContextTag.pageContextTag = pageContextTag; pageContext.setAttribute(ContextTag.CONTEXTTAG_KEY, pageContextTag); 131 132 } 137 } 138 return pageContextTag; 139 } 140 141 public void setPageContext(PageContext pc) { 142 if (EVAL_BODY == -1) { EVAL_BODY = "true".equals(pc.getServletContext().getInitParameter("mmbase.taglib.eval_body_include")) ? 144 EVAL_BODY_INCLUDE : EVAL_BODY_BUFFERED; 145 log.info("Using " + (EVAL_BODY == EVAL_BODY_BUFFERED ? " EVAL_BODY_BUFFERED (If you use a modern app-server, which supports it, you prefer EVAL_BODY_INCLUDE. See web.xml)" : "EVAL_BODY_INCLUDE")); 146 } 147 148 if (log.isDebugEnabled()) { 149 log.debug("setting page context: " + this.getClass().getName()); 150 } 151 setPageContextOnly(pc); pageContextTag = null; 153 getPageContextTag(); 154 155 } 156 157 163 164 public void setReferid(String r) throws JspTagException { 165 referid = getAttribute(r); 166 } 167 168 public void setId(String i) { 169 try { 170 if ("_".equals(i)) { 171 throw new RuntimeException ("'_' is not a valid id (it is reserved for the 'current writer')"); 172 } 173 id = getAttribute(i); 174 } catch (JspTagException j) { 175 throw new RuntimeException (j); 176 } 177 } 178 179 public String getId() { 180 try { 181 return (String ) id.getValue(this); 182 } catch (JspTagException j) { 183 throw new RuntimeException (j); 184 } 185 } 186 187 protected String getReferid() throws JspTagException { 188 return (String ) referid.getValue(this); 189 } 190 191 196 199 protected Attribute writerid = Attribute.NULL; 200 201 202 208 public Writer findWriter() throws JspTagException { 209 return findWriter(true); 210 211 } 212 215 public Writer findWriter(boolean th) throws JspTagException { 216 Writer w = (Writer) findParentTag(Writer.class, (String ) writerid.getValue(this), th); 217 if (w != null) { 218 w.haveBody(); 219 } 220 return w; 221 } 222 223 224 225 228 public void setWriter(String w) throws JspTagException { 229 writerid = getAttribute(w); 230 231 } 232 233 public int doEndTag() throws JspTagException { 234 return EVAL_PAGE; 235 } 236 237 public void doFinally() { 238 helper.doFinally(); 239 thisPage = null; 240 pageContextTag = null; 241 writerid = Attribute.NULL; 242 } 243 244 public void doCatch(Throwable e) throws Throwable { 245 log.debug("Caught throwable: " + e.getMessage()); 246 throw e; 247 } 248 249 252 public void release() { 253 super.release(); 254 if (log.isDebugEnabled()) { 255 log.debug("releasing context-referrer " + this.getClass().getName()); 256 } 257 if (thisPage != null) { 258 pageLog.debug("END Parsing JSP page: " + thisPage); 259 thisPage = null; 260 } 261 pageContextTag = null; 262 267 } 268 269 270 277 278 public void setContext(String c) throws JspTagException { 279 contextId = getAttribute(c); 280 } 281 282 300 301 public String getAttributeValue(String attribute) throws JspTagException { 302 if (attribute == null) return null; 303 return getAttribute(attribute).getString(this); 304 } 305 308 public Attribute getAttribute(String attribute) throws JspTagException { 309 return Attribute.getAttribute(attribute); 310 } 311 312 316 317 protected Boolean getAttributeBoolean(String b) throws JspTagException { 318 String r = getAttributeValue(b).toLowerCase(); 319 if ("true".equals(r)) { 320 return Boolean.TRUE; 321 } else if ("false".equals(r)) { 322 return Boolean.FALSE; 323 } else { 324 throw new JspTagException("'" + r + "' cannot be converted to a boolean"); 325 } 326 } 327 332 333 protected Integer getAttributeInteger(String i) throws JspTagException { 334 return getAttributeInteger(i, 0); 335 } 336 protected Integer getAttributeInteger(String i, int def) throws JspTagException { 337 try { 338 i = getAttributeValue(i); 339 if (i.equals("")) return new Integer (def); 340 return new Integer (i); 341 } catch (NumberFormatException e) { try { 343 return new Integer (new java.math.BigDecimal (i).intValue()); 344 } catch (NumberFormatException e2) { 345 throw new TaglibException(i + " is not an integer value ", e2); 346 } 347 } 348 } 349 350 354 355 final protected TagSupport findParentTag(String classname, String id, boolean exception) throws JspTagException { 356 Class clazz ; 357 try { 358 clazz = Class.forName(classname); 359 } catch (java.lang.ClassNotFoundException e) { 360 throw new TaglibException ("Could not find " + classname + " class", e); 361 } 362 return findParentTag(clazz, id, exception); 363 } 364 365 375 376 final public TagSupport findParentTag(Class clazz, String tagId, boolean exception) throws JspTagException { 377 TagSupport cTag = (TagSupport) findAncestorWithClass(this, clazz); 378 if (cTag == null) { 379 if (exception) { 380 throw new JspTagException ("Could not find parent of type " + clazz.getName()); 381 } else { 382 if (log.isDebugEnabled()) { 383 log.debug("Could not find parent of type " + clazz.getName()); 384 } 385 return null; 386 } 387 } 388 389 if (tagId != null) { if (log.isDebugEnabled()) { 391 log.debug(" with id (" + tagId + ")"); 392 } 393 while (! tagId.equals(cTag.getId())) { 394 cTag = (TagSupport) findAncestorWithClass(cTag, clazz); 395 if (cTag == null) { 396 if (exception) { 397 throw new JspTagException ("Could not find parent Tag of type " + clazz.getName() + " with id " + tagId); 398 } else { 399 return null; 400 } 401 } 402 } 403 } 404 return cTag; 405 406 } 407 408 411 public Tag findLoopOrQuery(String tagId, boolean exception) throws JspTagException { 412 Tag tag = getParent(); 413 while (tag != null) { 414 if (tag instanceof LoopTag) { 415 if (tagId == null) return tag; 416 } else if (tag instanceof QueryContainer) { 417 if (tagId == null || ((QueryContainer) tag).getId().equals(tagId)) return tag; 418 } 419 tag = tag.getParent(); 420 } 421 throw new JspTagException("Cloud not find parent Tag of LoopTag or QueryContainer type"); 422 } 423 426 final protected TagSupport findParentTag(String classname, String id) throws JspTagException { 427 return findParentTag(classname, id, true); 428 } 429 432 final protected TagSupport findParentTag(Class clazz, String id) throws JspTagException { 433 return findParentTag(clazz, id, true); 434 } 435 436 440 441 public ContextProvider getContextProvider() throws JspTagException { 442 return getContextProvider((String ) contextId.getValue(this), ContextProvider.class); 443 } 444 445 450 451 public ContextTag getContextTag() throws JspTagException { 452 return (ContextTag) getContextProvider((String ) contextId.getValue(this), ContextTag.class); 453 } 454 455 460 461 private ContextProvider getContextProvider(String contextid, Class cl) throws JspTagException { 462 463 if(log.isDebugEnabled()) { 464 log.debug("Searching context " + contextid); 465 } 466 ContextProvider contextTag = (ContextProvider) findParentTag(cl, contextid, false); 467 if (contextTag == null || 468 contextTag.getContextContainer().getPageContext() != pageContext) { 472 473 log.debug("Didn't find one, take the pageContextTag"); 474 contextTag = getPageContextTag(); 475 if (contextTag == null) { 476 throw new RuntimeException ("Did not find pageContextTag!"); 477 } 478 if (contextid != null) { 479 if(! contextid.equals(contextTag.getId())) { 480 throw new JspTagException("Could not find context tag with id " + contextid + " (page context has id " + contextTag.getId() + ")"); 481 } 482 } 483 } 484 if (log.isDebugEnabled()) { 485 log.debug("found a context with ID= " + contextTag.getId()); 486 } 487 return contextTag; 488 } 489 490 494 public Object getObject(String key) throws JspTagException { 495 if (log.isDebugEnabled()) { 497 log.debug("Getting object '" + key + "' from '" + getContextProvider().getId() + "'"); 498 } 499 Object r = getContextProvider().getContextContainer().getObject(key); 500 if (r == null) { 501 log.debug("Not found, returning empty string"); 502 return ""; 503 } else { 504 if (log.isDebugEnabled()) log.debug("found: '" + r + "'"); 505 } 506 return r; 507 } 508 509 514 public Object getObjectConditional(String key) throws JspTagException { 515 if (key.endsWith("?")) { 516 key = key.substring(0, key.length() - 1); 517 return getContextProvider().getContextContainer().get(key); 518 } else { 519 return getObject(key); 520 } 521 } 522 530 531 protected String getString(String key) throws JspTagException { 532 return Casting.toString(getObject(key)); 533 } 534 535 540 541 public ContentTag getContentTag() throws JspTagException { 542 ContentTag ct = (ContentTag) findParentTag(ContentTag.class, null, false); 543 if (ct == null) { 544 return ContentTag.DEFAULT; 545 } else { 546 return ct; 547 } 548 } 549 550 557 public Locale getLocale() throws JspTagException { 558 Locale locale = getLocaleFromContext(); 559 if (locale == null) { 560 locale = getDefaultLocale(); 561 } 562 return locale; 563 } 564 565 572 public Locale getLocaleFromContext() throws JspTagException { 573 LocaleTag localeTag = (LocaleTag)findParentTag(LocaleTag.class, null, false); 575 if (localeTag != null) { 576 Locale locale = localeTag.getLocale(); 577 if (locale != null) { 578 return locale; 579 } 580 } 581 ContextReferrerTag contextReferrerTag = (ContextReferrerTag)findParentTag(ContextReferrerTag.class, null, false); 582 if (contextReferrerTag != null) { 583 Locale locale = contextReferrerTag.getLocale(); 584 if (locale != null) { 585 return locale; 586 } 587 } 588 { 589 Locale locale = (Locale) pageContext.getAttribute(LocaleTag.KEY, LocaleTag.SCOPE); 590 if (locale != null) { 591 return locale; 592 } 593 } 594 return null; 595 } 596 597 602 public Locale getDefaultLocale() { 603 return org.mmbase.bridge.ContextProvider.getDefaultCloudContext().getDefaultLocale(); 604 } 605 606 609 public TimeZone getTimeZone() { 610 TimeZone timeZone = (TimeZone) pageContext.getAttribute(LocaleTag.TZ_KEY, LocaleTag.SCOPE); 611 if (timeZone != null) return timeZone; 612 return org.mmbase.bridge.ContextProvider.getDefaultCloudContext().getDefaultTimeZone(); 613 } 614 617 protected void fillStandardParameters(Parameters p) throws JspTagException { 618 log.debug("Filling standard parameters"); 619 p.setIfDefined(Parameter.RESPONSE, pageContext.getResponse()); 620 p.setIfDefined(Parameter.REQUEST, pageContext.getRequest()); 621 java.util.Locale locale = getLocale(); 623 if (locale != null) { 624 p.setIfDefined(Parameter.LANGUAGE, locale.getLanguage()); 625 p.setIfDefined(Parameter.LOCALE, locale); 626 } 627 } 628 629 630 631 634 637 638 final protected WriterHelper helper = new WriterHelper(this); 639 641 public void setVartype(String t) throws JspTagException { 642 helper.setVartype(t); 643 } 644 647 final public void setListdelimiter(String l) throws JspTagException { 648 helper.setListdelimiter(getAttribute(l)); 649 } 650 public void setJspvar(String j) { 651 helper.setJspvar(j); 652 } 653 final public void setWrite(String w) throws JspTagException { 654 helper.setWrite(getAttribute(w)); 655 } 656 657 final public void setEscape(String e) throws JspTagException { 658 helper.setEscape(getAttribute(e)); 659 } 660 661 final public Object getWriterValue() { 662 return helper.getValue(); 663 } 664 final public void haveBody() { helper.haveBody(); } 665 666 670 protected Object getEscapedValue(Object value) throws JspTagException { 671 if (helper.getEscape() == null) { 672 return value; 673 } else { 674 org.mmbase.util.transformers.CharTransformer ct = ContentTag.getCharTransformer(helper.getEscape(), this); 675 if (ct != null) { 676 return ct.transform(Casting.toString(value)); 677 } else { 678 return value; 679 } 680 } 681 } 682 683 684 } 685 | Popular Tags |