001/*
002 * Copyright 2002-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.web.servlet.tags;
018
019import org.springframework.context.MessageSource;
020import org.springframework.context.NoSuchMessageException;
021
022/**
023 * The {@code <theme>} tag looks up a theme message in the scope of this page.
024 * Messages are looked up using the ApplicationContext's ThemeSource,
025 * and thus should support internationalization.
026 *
027 * <p>Regards a HTML escaping setting, either on this tag instance,
028 * the page level, or the web.xml level.
029 *
030 * <p>If "code" isn't set or cannot be resolved, "text" will be used
031 * as default message.
032 *
033 * <p>Message arguments can be specified via the {@link #setArguments(Object)
034 * arguments} attribute or by using nested {@code <spring:argument>} tags.
035 *
036 * <table>
037 * <caption>Attribute Summary</caption>
038 * <thead>
039 * <tr>
040 * <th>Attribute</th>
041 * <th>Required?</th>
042 * <th>Runtime Expression?</th>
043 * <th>Description</th>
044 * </tr>
045 * </thead>
046 * <tbody>
047 * <tr>
048 * <td>arguments</td>
049 * <td>false</td>
050 * <td>true</td>
051 * <td>Set optional message arguments for this tag, as a (comma-)delimited
052 * String (each String argument can contain JSP EL), an Object array (used as
053 * argument array), or a single Object (used as single argument).</td>
054 * </tr>
055 * <tr>
056 * <td>argumentSeparator</td>
057 * <td>false</td>
058 * <td>true</td>
059 * <td>The separator character to be used for splitting the arguments string
060 * value; defaults to a 'comma' (',').</td>
061 * </tr>
062 * <tr>
063 * <td>code</td>
064 * <td>false</td>
065 * <td>true</td>
066 * <td>The code (key) to use when looking up the message. If code is not
067 * provided, the text attribute will be used.</td>
068 * </tr>
069 * <tr>
070 * <td>htmlEscape</td>
071 * <td>false</td>
072 * <td>true</td>
073 * <td>Set HTML escaping for this tag, as boolean value. Overrides the default
074 * HTML escaping setting for the current page.</td>
075 * </tr>
076 * <tr>
077 * <td>javaScriptEscape</td>
078 * <td>false</td>
079 * <td>true</td>
080 * <td>Set JavaScript escaping for this tag, as boolean value.
081 * Default is false.</td>
082 * </tr>
083 * <tr>
084 * <td>message</td>
085 * <td>false</td>
086 * <td>true</td>
087 * <td>A MessageSourceResolvable argument (direct or through JSP EL).</td>
088 * </tr>
089 * <tr>
090 * <td>scope</td>
091 * <td>false</td>
092 * <td>true</td>
093 * <td>The scope to use when exporting the result to a variable. This attribute
094 * is only used when var is also set. Possible values are page, request, session
095 * and application.</td>
096 * </tr>
097 * <tr>
098 * <td>text</td>
099 * <td>false</td>
100 * <td>true</td>
101 * <td>Default text to output when a message for the given code could not be
102 * found. If both text and code are not set, the tag will output null.</td>
103 * </tr>
104 * <tr>
105 * <td>var</td>
106 * <td>false</td>
107 * <td>true</td>
108 * <td>The string to use when binding the result to the page, request, session
109 * or application scope. If not specified, the result gets outputted to the
110 * writer (i.e. typically directly to the JSP).</td>
111 * </tr>
112 * </tbody>
113 * </table>
114 *
115 * @author Jean-Pierre Pawlak
116 * @author Juergen Hoeller
117 * @see org.springframework.ui.context.Theme
118 * @see org.springframework.ui.context.ThemeSource
119 * @see #setCode
120 * @see #setText
121 * @see #setHtmlEscape
122 * @see HtmlEscapeTag#setDefaultHtmlEscape
123 * @see org.springframework.web.util.WebUtils#HTML_ESCAPE_CONTEXT_PARAM
124 * @see ArgumentTag
125 */
126@SuppressWarnings("serial")
127public class ThemeTag extends MessageTag {
128
129        /**
130         * Use the theme MessageSource for theme message resolution.
131         */
132        @Override
133        protected MessageSource getMessageSource() {
134                return getRequestContext().getTheme().getMessageSource();
135        }
136
137        /**
138         * Return exception message that indicates the current theme.
139         */
140        @Override
141        protected String getNoSuchMessageExceptionDescription(NoSuchMessageException ex) {
142                return "Theme '" + getRequestContext().getTheme().getName() + "': " + ex.getMessage();
143        }
144
145}