MessageTag.java
上传用户:jiancairen
上传日期:2007-08-27
资源大小:26458k
文件大小:6k
源码类别:

Java编程

开发平台:

Java

  1. /*
  2.  * Copyright 2002-2004 the original author or authors.
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */ 
  16. package org.springframework.web.servlet.tags;
  17. import java.io.IOException;
  18. import javax.servlet.jsp.JspException;
  19. import javax.servlet.jsp.JspTagException;
  20. import org.springframework.context.MessageSource;
  21. import org.springframework.context.NoSuchMessageException;
  22. import org.springframework.util.StringUtils;
  23. import org.springframework.web.util.ExpressionEvaluationUtils;
  24. import org.springframework.web.util.HtmlUtils;
  25. import org.springframework.web.util.JavaScriptUtils;
  26. import org.springframework.web.util.TagUtils;
  27. /**
  28.  * Custom JSP tag to look up a message in the scope of this page.
  29.  * Messages are looked up using the ApplicationContext, and thus should
  30.  * support internationalization.
  31.  *
  32.  * <p>Regards a HTML escaping setting, either on this tag instance,
  33.  * the page level, or the web.xml level. Can also apply JavaScript escaping.
  34.  *
  35.  * <p>If "code" isn't set or cannot be resolved, "text" will be used as default
  36.  * message. Thus, this tag can also be used for HTML escaping of any texts.
  37.  *
  38.  * @author Rod Johnson
  39.  * @author Juergen Hoeller
  40.  * @see #setCode
  41.  * @see #setText
  42.  * @see #setHtmlEscape
  43.  * @see #setJavaScriptEscape
  44.  * @see HtmlEscapeTag#setDefaultHtmlEscape
  45.  * @see org.springframework.web.util.WebUtils#HTML_ESCAPE_CONTEXT_PARAM
  46.  */
  47. public class MessageTag extends HtmlEscapingAwareTag {
  48. private String code;
  49. private Object arguments;
  50. private String text;
  51. private String var;
  52. private String scope = TagUtils.SCOPE_PAGE;
  53. private boolean javaScriptEscape = false;
  54. /**
  55.  * Set the message code for this tag.
  56.  */
  57. public void setCode(String code) {
  58. this.code = code;
  59. }
  60. /**
  61.  * Set optional message arguments for this tag, as a comma-delimited
  62.  * String (each String argument can contain JSP EL), an Object array
  63.  * (used as argument array), or a single Object (used as single argument).
  64.  */
  65. public void setArguments(Object arguments) {
  66. this.arguments = arguments;
  67. }
  68. /**
  69.  * Set the message text for this tag.
  70.  */
  71. public void setText(String text) {
  72. this.text = text;
  73. }
  74. /**
  75.  * Set PageContext attribute name under which to expose
  76.  * a variable that contains the resolved message.
  77.  * @see #setScope
  78.  * @see javax.servlet.jsp.PageContext#setAttribute
  79.  */
  80. public void setVar(String var) {
  81. this.var = var;
  82. }
  83. /**
  84.  * Set the scope to export the variable to.
  85.  * Default is SCOPE_PAGE ("page").
  86.  * @see #setVar
  87.  * @see org.springframework.web.util.TagUtils#SCOPE_PAGE
  88.  * @see javax.servlet.jsp.PageContext#setAttribute
  89.  */
  90. public void setScope(String scope) {
  91. this.scope = scope;
  92. }
  93. /**
  94.  * Set JavaScript escaping for this tag, as boolean value.
  95.  * Default is false.
  96.  */
  97. public void setJavaScriptEscape(String javaScriptEscape) throws JspException {
  98. this.javaScriptEscape =
  99. ExpressionEvaluationUtils.evaluateBoolean("javaScriptEscape", javaScriptEscape, pageContext);
  100. }
  101. protected final int doStartTagInternal() throws JspException, IOException {
  102. MessageSource messageSource = getMessageSource();
  103. if (messageSource == null) {
  104. throw new JspTagException("No corresponding MessageSource found");
  105. }
  106. String resolvedCode = ExpressionEvaluationUtils.evaluateString("code", this.code, pageContext);
  107. String resolvedText = ExpressionEvaluationUtils.evaluateString("text", this.text, pageContext);
  108. String resolvedVar = ExpressionEvaluationUtils.evaluateString("var", this.var, pageContext);
  109. try {
  110. String msg = null;
  111. if (resolvedCode != null) {
  112. Object[] argumentsArray = null;
  113. if (this.arguments instanceof String) {
  114. argumentsArray = StringUtils.commaDelimitedListToStringArray((String) this.arguments);
  115. for (int i = 0; i < argumentsArray.length; i++) {
  116. argumentsArray[i] =
  117.     ExpressionEvaluationUtils.evaluateString(
  118.         "argument[" + i + "]", (String) argumentsArray[i], pageContext);
  119. }
  120. }
  121. else if (this.arguments instanceof Object[]) {
  122. argumentsArray = (Object[]) this.arguments;
  123. }
  124. else {
  125. // assume a single argument object
  126. argumentsArray = new Object[] {this.arguments};
  127. }
  128. if (resolvedText != null) {
  129. msg = messageSource.getMessage(
  130. resolvedCode, argumentsArray, resolvedText, getRequestContext().getLocale());
  131. }
  132. else {
  133. msg = messageSource.getMessage(
  134. resolvedCode, argumentsArray, getRequestContext().getLocale());
  135. }
  136. }
  137. else {
  138. msg = resolvedText;
  139. }
  140. // HTML and/or JavaScript escape, if demanded
  141. msg = isHtmlEscape() ? HtmlUtils.htmlEscape(msg) : msg;
  142. msg = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(msg) : msg;
  143. // expose as variable, if demanded
  144. if (resolvedVar != null) {
  145. String resolvedScope = ExpressionEvaluationUtils.evaluateString("scope", this.scope, pageContext);
  146. pageContext.setAttribute(resolvedVar, msg, TagUtils.getScope(resolvedScope));
  147. }
  148. else {
  149. writeMessage(msg);
  150. }
  151. return EVAL_BODY_INCLUDE;
  152. }
  153. catch (NoSuchMessageException ex) {
  154. throw new JspTagException(getNoSuchMessageExceptionDescription(ex));
  155. }
  156. }
  157. /**
  158.  * Write the message to the page.
  159.  * <p>Can be overridden in subclasses, e.g. for testing purposes.
  160.  * @param msg the message to write
  161.  * @throws IOException if writing failed
  162.  */
  163. protected void writeMessage(String msg) throws IOException {
  164. pageContext.getOut().write(String.valueOf(msg));
  165. }
  166. /**
  167.  * Use the application context itself for default message resolution.
  168.  */
  169. protected MessageSource getMessageSource() {
  170. return getRequestContext().getWebApplicationContext();
  171. }
  172. /**
  173.  * Return default exception message.
  174.  */
  175. protected String getNoSuchMessageExceptionDescription(NoSuchMessageException ex) {
  176. return ex.getMessage();
  177. }
  178. }