1 16 17 package org.apache.taglibs.standard.tag.common.fmt; 18 19 import java.io.IOException ; 20 import java.text.MessageFormat ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 import java.util.Locale ; 24 import java.util.MissingResourceException ; 25 import java.util.ResourceBundle ; 26 27 import javax.servlet.jsp.JspException ; 28 import javax.servlet.jsp.JspTagException ; 29 import javax.servlet.jsp.PageContext ; 30 import javax.servlet.jsp.jstl.fmt.LocalizationContext; 31 import javax.servlet.jsp.tagext.BodyTagSupport ; 32 import javax.servlet.jsp.tagext.Tag ; 33 34 import org.apache.taglibs.standard.tag.common.core.Util; 35 36 42 43 public abstract class MessageSupport extends BodyTagSupport { 44 45 48 public static final String UNDEFINED_KEY = "???"; 49 50 51 54 protected String keyAttrValue; protected boolean keySpecified; protected LocalizationContext bundleAttrValue; protected boolean bundleSpecified; 59 60 63 private String var; private int scope; private List params; 66 67 68 71 public MessageSupport() { 72 super(); 73 params = new ArrayList (); 74 init(); 75 } 76 77 private void init() { 78 var = null; 79 scope = PageContext.PAGE_SCOPE; 80 keyAttrValue = null; 81 keySpecified = false; 82 bundleAttrValue = null; 83 bundleSpecified = false; 84 } 85 86 87 90 public void setVar(String var) { 91 this.var = var; 92 } 93 94 public void setScope(String scope) { 95 this.scope = Util.getScope(scope); 96 } 97 98 99 102 107 public void addParam(Object arg) { 108 params.add(arg); 109 } 110 111 112 115 public int doStartTag() throws JspException { 116 params.clear(); 117 return EVAL_BODY_BUFFERED; 118 } 119 120 public int doEndTag() throws JspException { 121 122 String key = null; 123 LocalizationContext locCtxt = null; 124 125 if (keySpecified) { 127 key = keyAttrValue; 129 } else { 130 if (bodyContent != null && bodyContent.getString() != null) 132 key = bodyContent.getString().trim(); 133 } 134 135 if ((key == null) || key.equals("")) { 136 try { 137 pageContext.getOut().print("??????"); 138 } catch (IOException ioe) { 139 throw new JspTagException (ioe.toString(), ioe); 140 } 141 return EVAL_PAGE; 142 } 143 144 String prefix = null; 145 if (!bundleSpecified) { 146 Tag t = findAncestorWithClass(this, BundleSupport.class); 147 if (t != null) { 148 BundleSupport parent = (BundleSupport) t; 150 locCtxt = parent.getLocalizationContext(); 151 prefix = parent.getPrefix(); 152 } else { 153 locCtxt = BundleSupport.getLocalizationContext(pageContext); 154 } 155 } else { 156 locCtxt = bundleAttrValue; 158 if (locCtxt.getLocale() != null) { 159 SetLocaleSupport.setResponseLocale(pageContext, 160 locCtxt.getLocale()); 161 } 162 } 163 164 String message = UNDEFINED_KEY + key + UNDEFINED_KEY; 165 if (locCtxt != null) { 166 ResourceBundle bundle = locCtxt.getResourceBundle(); 167 if (bundle != null) { 168 try { 169 if (prefix != null) 171 key = prefix + key; 172 message = bundle.getString(key); 173 if (!params.isEmpty()) { 175 Object [] messageArgs = params.toArray(); 176 MessageFormat formatter = new MessageFormat (""); if (locCtxt.getLocale() != null) { 178 formatter.setLocale(locCtxt.getLocale()); 179 } else { 180 Locale locale = SetLocaleSupport.getFormattingLocale(pageContext); 185 if (locale != null) { 187 formatter.setLocale(locale); 188 } 189 } 190 formatter.applyPattern(message); 191 message = formatter.format(messageArgs); 192 } 193 } catch (MissingResourceException mre) { 194 message = UNDEFINED_KEY + key + UNDEFINED_KEY; 195 } 196 } 197 } 198 199 if (var != null) { 200 pageContext.setAttribute(var, message, scope); 201 } else { 202 try { 203 pageContext.getOut().print(message); 204 } catch (IOException ioe) { 205 throw new JspTagException (ioe.toString(), ioe); 206 } 207 } 208 209 return EVAL_PAGE; 210 } 211 212 public void release() { 214 init(); 215 } 216 } 217 | Popular Tags |