Beispiel #1
0
  public int doEndTag() throws JspException {
    String pattern = this.pattern;
    ResourceBundle bundle = null;

    // get pattern from bundle
    if (pattern == null) {
      String prefix = null;

      bundle = this.bundle;
      if (bundle == null) {
        BundleTag bundleTag = (BundleTag) findAncestorWithClass(this, BundleTag.class);
        if (bundleTag != null) {
          bundle = bundleTag.getBundle();
          prefix = bundleTag.getPrefix();
        }
      }
      if (bundle == null) {
        bundle = I18nUtil.findBundle(pageContext);
      }

      // get message
      try {
        pattern = bundle.getString(prefix != null ? prefix + this.key : this.key);
      } catch (Exception e) {
        pattern = "???" + this.key + "???";
      }
    }

    // format message
    String message = pattern;
    if ((this.pattern != null || bundle != null) && this.params.size() != 0) {
      // set timezone on formatters
      TimeZone timeZone = null;
      TimeZoneTag timeZoneTag = (TimeZoneTag) findAncestorWithClass(this, TimeZoneTag.class);
      if (timeZoneTag != null) timeZone = timeZoneTag.getTimeZone();
      if (timeZone == null) timeZone = I18nUtil.findTimeZone(pageContext);

      MessageFormat formatter = new MessageFormat(pattern, I18nUtil.findLocale(pageContext));
      for (Format format : formatter.getFormatsByArgumentIndex()) {
        if (format != null && format instanceof DateFormat) {
          ((DateFormat) format).setTimeZone(timeZone);
        }
      }

      // now format!
      message = formatter.format(getParams());
    }

    // output text
    if (this.var == null) {
      try {
        pageContext.getOut().print(message);
      } catch (IOException e) {
        throw new JspException(e);
      }
    }

    // store result
    else {
      pageContext.setAttribute(this.var, message, this.scope);
    }

    // clear old state
    this.pattern = null;
    this.bundle = null;
    this.key = null;
    this.var = null;
    this.scope = PageContext.PAGE_SCOPE;

    // continue with page
    return EVAL_PAGE;
  }
Beispiel #2
0
 public void setScope(String scope) {
   this.scope = I18nUtil.getScope(scope);
 }