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.view.script;
018
019import java.util.Locale;
020import java.util.function.Function;
021
022import org.springframework.context.ApplicationContext;
023
024/**
025 * Context passed to {@link ScriptTemplateView} render function in order to make
026 * the application context, the locale, the template loader and the url available on
027 * scripting side.
028 *
029 * @author Sebastien Deleuze
030 * @since 5.0
031 */
032public class RenderingContext {
033
034        private final ApplicationContext applicationContext;
035
036        private final Locale locale;
037
038        private final Function<String, String> templateLoader;
039
040        private final String url;
041
042
043        /**
044         * Create a new {@code RenderingContext}.
045         *
046         * @param applicationContext the application context
047         * @param locale the locale of the rendered template
048         * @param templateLoader a function that takes a template path as input and returns
049         * the template content as a String
050         * @param url the URL of the rendered template
051         */
052        public RenderingContext(ApplicationContext applicationContext, Locale locale,
053                        Function<String, String> templateLoader, String url) {
054
055                this.applicationContext = applicationContext;
056                this.locale = locale;
057                this.templateLoader = templateLoader;
058                this.url = url;
059        }
060
061
062        /**
063         * Return the application context.
064         */
065        public ApplicationContext getApplicationContext() {
066                return this.applicationContext;
067        }
068
069        /**
070         * Return the locale of the rendered template.
071         */
072        public Locale getLocale() {
073                return this.locale;
074        }
075
076        /**
077         * Return a function that takes a template path as input and returns the template
078         * content as a String.
079         */
080        public Function<String, String> getTemplateLoader() {
081                return this.templateLoader;
082        }
083
084        /**
085         * Return the URL of the rendered template.
086         */
087        public String getUrl() {
088                return this.url;
089        }
090
091}