1 16 17 package org.springframework.web.servlet.tags; 18 19 import java.io.IOException ; 20 import java.util.Collection ; 21 22 import javax.servlet.jsp.JspException ; 23 import javax.servlet.jsp.JspTagException ; 24 25 import org.springframework.context.MessageSource; 26 import org.springframework.context.MessageSourceResolvable; 27 import org.springframework.context.NoSuchMessageException; 28 import org.springframework.util.ObjectUtils; 29 import org.springframework.util.StringUtils; 30 import org.springframework.web.util.ExpressionEvaluationUtils; 31 import org.springframework.web.util.HtmlUtils; 32 import org.springframework.web.util.JavaScriptUtils; 33 import org.springframework.web.util.TagUtils; 34 35 55 public class MessageTag extends HtmlEscapingAwareTag { 56 57 60 public static final String DEFAULT_ARGUMENT_SEPARATOR = ","; 61 62 63 private Object message; 64 65 private String code; 66 67 private Object arguments; 68 69 private String argumentSeparator = DEFAULT_ARGUMENT_SEPARATOR; 70 71 private String text; 72 73 private String var; 74 75 private String scope = TagUtils.SCOPE_PAGE; 76 77 private boolean javaScriptEscape = false; 78 79 80 87 public void setMessage(Object message) { 88 this.message = message; 89 } 90 91 94 public void setCode(String code) { 95 this.code = code; 96 } 97 98 103 public void setArguments(Object arguments) { 104 this.arguments = arguments; 105 } 106 107 112 public void setArgumentSeparator(String argumentSeparator) { 113 this.argumentSeparator = argumentSeparator; 114 } 115 116 119 public void setText(String text) { 120 this.text = text; 121 } 122 123 129 public void setVar(String var) { 130 this.var = var; 131 } 132 133 140 public void setScope(String scope) { 141 this.scope = scope; 142 } 143 144 148 public void setJavaScriptEscape(String javaScriptEscape) throws JspException { 149 this.javaScriptEscape = 150 ExpressionEvaluationUtils.evaluateBoolean("javaScriptEscape", javaScriptEscape, pageContext); 151 } 152 153 154 162 protected final int doStartTagInternal() throws JspException , IOException { 163 try { 164 String msg = resolveMessage(); 166 167 msg = isHtmlEscape() ? HtmlUtils.htmlEscape(msg) : msg; 169 msg = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(msg) : msg; 170 171 String resolvedVar = ExpressionEvaluationUtils.evaluateString("var", this.var, pageContext); 173 if (resolvedVar != null) { 174 String resolvedScope = ExpressionEvaluationUtils.evaluateString("scope", this.scope, pageContext); 175 pageContext.setAttribute(resolvedVar, msg, TagUtils.getScope(resolvedScope)); 176 } 177 else { 178 writeMessage(msg); 179 } 180 181 return EVAL_BODY_INCLUDE; 182 } 183 catch (NoSuchMessageException ex) { 184 throw new JspTagException (getNoSuchMessageExceptionDescription(ex)); 185 } 186 } 187 188 192 protected String resolveMessage() throws JspException , NoSuchMessageException { 193 MessageSource messageSource = getMessageSource(); 194 if (messageSource == null) { 195 throw new JspTagException ("No corresponding MessageSource found"); 196 } 197 198 MessageSourceResolvable resolvedMessage = null; 200 if (this.message instanceof MessageSourceResolvable) { 201 resolvedMessage = (MessageSourceResolvable) this.message; 202 } 203 else if (this.message != null) { 204 String expr = this.message.toString(); 205 resolvedMessage = (MessageSourceResolvable) 206 ExpressionEvaluationUtils.evaluate("message", expr, MessageSourceResolvable.class, pageContext); 207 } 208 209 if (resolvedMessage != null) { 210 return messageSource.getMessage(resolvedMessage, getRequestContext().getLocale()); 212 } 213 214 String resolvedCode = ExpressionEvaluationUtils.evaluateString("code", this.code, pageContext); 215 String resolvedText = ExpressionEvaluationUtils.evaluateString("text", this.text, pageContext); 216 217 if (resolvedCode != null) { 218 Object [] argumentsArray = resolveArguments(this.arguments); 220 if (resolvedText != null) { 221 return messageSource.getMessage( 223 resolvedCode, argumentsArray, resolvedText, getRequestContext().getLocale()); 224 } 225 else { 226 return messageSource.getMessage( 228 resolvedCode, argumentsArray, getRequestContext().getLocale()); 229 } 230 } 231 232 return resolvedText; 234 } 235 236 243 protected Object [] resolveArguments(Object arguments) throws JspException { 244 if (arguments instanceof String ) { 245 String [] stringArray = 246 StringUtils.delimitedListToStringArray((String ) arguments, this.argumentSeparator); 247 if (stringArray.length == 1) { 248 Object argument = ExpressionEvaluationUtils.evaluate("argument", stringArray[0], pageContext); 249 if (argument != null && argument.getClass().isArray()) { 250 return ObjectUtils.toObjectArray(argument); 251 } 252 else { 253 return new Object [] {argument}; 254 } 255 } 256 else { 257 Object [] argumentsArray = new Object [stringArray.length]; 258 for (int i = 0; i < stringArray.length; i++) { 259 argumentsArray[i] = 260 ExpressionEvaluationUtils.evaluate("argument[" + i + "]", stringArray[i], pageContext); 261 } 262 return argumentsArray; 263 } 264 } 265 else if (arguments instanceof Object []) { 266 return (Object []) arguments; 267 } 268 else if (arguments instanceof Collection ) { 269 return ((Collection ) arguments).toArray(); 270 } 271 else if (arguments != null) { 272 return new Object [] {arguments}; 274 } 275 else { 276 return null; 277 } 278 } 279 280 286 protected void writeMessage(String msg) throws IOException { 287 pageContext.getOut().write(String.valueOf(msg)); 288 } 289 290 293 protected MessageSource getMessageSource() { 294 return getRequestContext().getWebApplicationContext(); 295 } 296 297 300 protected String getNoSuchMessageExceptionDescription(NoSuchMessageException ex) { 301 return ex.getMessage(); 302 } 303 304 } 305 | Popular Tags |