All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.thymeleaf.spring4.expression.Themes Maven / Gradle / Ivy

/*
 * =============================================================================
 * 
 *   Copyright (c) 2011-2016, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */

package org.thymeleaf.spring4.expression;

import java.util.Locale;

import org.springframework.ui.context.Theme;
import org.springframework.web.servlet.support.RequestContext;
import org.thymeleaf.context.IExpressionContext;
import org.thymeleaf.exceptions.TemplateProcessingException;
import org.thymeleaf.spring4.naming.SpringContextVariableNames;

/**
 * A utility object, accessed in Thymeleaf templates by the #themes
 * expression, that provides the same features as the Spring
 * <spring:theme JSP tag.
 * 
 * @author Emanuel Rabina
 */
public class Themes {

    private final Theme theme;
    private final Locale locale;

    /**
     * Constructor, obtains the current theme and locale from the processing
     * context for code lookups later.
     * 
     * @param context the processing context being used
     */
    public Themes(final IExpressionContext context) {

        super();
        this.locale = context.getLocale();
        final RequestContext requestContext =
                (RequestContext) context.getVariable(SpringContextVariableNames.SPRING_REQUEST_CONTEXT);
        this.theme = requestContext != null ? requestContext.getTheme() : null;
    }

    /**
     * Looks up and returns the value of the given key in the properties file of
     * the currently-selected theme.
     * 
     * @param code Key to look up in the theme properties file.
     * @return The value of the code in the current theme properties file, or an
     * 		   empty string if the code could not be resolved.
     */
    public String code(final String code) {
        if (this.theme == null) {
            throw new TemplateProcessingException("Theme cannot be resolved because RequestContext was not found. "
                + "Are you using a Context object without a RequestContext variable?");
        }
        return this.theme.getMessageSource().getMessage(code, null, "", this.locale);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy