org.thymeleaf.templateresolver.StringTemplateResolver Maven / Gradle / Ivy
Show all versions of thymeleaf Show documentation
/*
* =============================================================================
*
* 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.templateresolver;
import java.util.Map;
import org.thymeleaf.IEngineConfiguration;
import org.thymeleaf.cache.AlwaysValidCacheEntryValidity;
import org.thymeleaf.cache.ICacheEntryValidity;
import org.thymeleaf.cache.NonCacheableCacheEntryValidity;
import org.thymeleaf.cache.TTLCacheEntryValidity;
import org.thymeleaf.exceptions.ConfigurationException;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresource.ITemplateResource;
import org.thymeleaf.templateresource.StringTemplateResource;
import org.thymeleaf.util.Validate;
/**
*
* Implementation of {@link ITemplateResolver} that extends {@link AbstractTemplateResolver}
* and creates {@link StringTemplateResource} instances for template resources.
*
*
* This template resolvers will consider the template being resolved as the template itself,
* this is, its contents. No external file or resource will be therefore accessed.
*
*
* This template resolver will consider its resolved templates non-cacheable by default,
* given its nature of being used for resolving arbitrary String objects.
*
*
* Also, the {@link TemplateMode#HTML} template mode will be used by default.
*
*
* Note this is the default template resolver that {@link org.thymeleaf.TemplateEngine}
* instances will use if no other resolvers are configured.
*
*
* @author Daniel Fernández
*
* @since 3.0.0
*
*/
public class StringTemplateResolver extends AbstractTemplateResolver {
/**
*
* Default template mode: {@link TemplateMode#HTML}
*
*/
public static final TemplateMode DEFAULT_TEMPLATE_MODE = TemplateMode.HTML;
/**
*
* Default value for the cacheable flag: false.
*
*/
public static final boolean DEFAULT_CACHEABLE = false;
/**
*
* Default value for the cache TTL: null. This means the parsed template will live in
* cache until removed by LRU (because of being the oldest entry).
*
*/
public static final Long DEFAULT_CACHE_TTL_MS = null;
private TemplateMode templateMode = DEFAULT_TEMPLATE_MODE;
private boolean cacheable = DEFAULT_CACHEABLE;
private Long cacheTTLMs = DEFAULT_CACHE_TTL_MS;
/**
*
* Creates a new instance of this template resolver.
*
*/
public StringTemplateResolver() {
super();
}
/**
*
* Returns the template mode to be applied to templates resolved by
* this template resolver.
*
*
* @return the template mode to be used.
*/
public final TemplateMode getTemplateMode() {
return this.templateMode;
}
/**
*
* Sets the template mode to be applied to templates resolved by this resolver.
*
*
* @param templateMode the template mode.
*/
public final void setTemplateMode(final TemplateMode templateMode) {
Validate.notNull(templateMode, "Cannot set a null template mode value");
// We re-parse the specified template mode so that we make sure we get rid of deprecated values
this.templateMode = TemplateMode.parse(templateMode.toString());
}
/**
*
* Sets the template mode to be applied to templates resolved by this resolver.
*
*
* Allowed templates modes are defined by the {@link TemplateMode} class.
*
*
* @param templateMode the template mode.
*/
public final void setTemplateMode(final String templateMode) {
// Setter overload actually goes against the JavaBeans spec, but having this one is good for legacy
// compatibility reasons. Besides, given the getter returns TemplateMode, intelligent frameworks like
// Spring will recognized the property as TemplateMode-typed and simply ignore this setter.
Validate.notNull(templateMode, "Cannot set a null template mode value");
this.templateMode = TemplateMode.parse(templateMode);
}
/**
*
* Returns whether templates resolved by this resolver have to be considered
* cacheable or not.
*
*
* @return whether templates resolved are cacheable or not.
*/
public final boolean isCacheable() {
return this.cacheable;
}
/**
*
* Sets a new value for the cacheable flag.
*
*
* @param cacheable whether resolved patterns should be considered cacheable or not.
*/
public final void setCacheable(final boolean cacheable) {
this.cacheable = cacheable;
}
/**
*
* Returns the TTL (Time To Live) in cache of templates resolved by this
* resolver.
*
*
* If a template is resolved as cacheable but cache TTL is null,
* this means the template will live in cache until evicted by LRU
* (Least Recently Used) algorithm for being the oldest entry in cache.
*
*
* @return the cache TTL for resolved templates.
*/
public final Long getCacheTTLMs() {
return this.cacheTTLMs;
}
/**
*
* Sets a new value for the cache TTL for resolved templates.
*
*
* If a template is resolved as cacheable but cache TTL is null,
* this means the template will live in cache until evicted by LRU
* (Least Recently Used) algorithm for being the oldest entry in cache.
*
*
* @param cacheTTLMs the new cache TTL, or null for using natural LRU eviction.
*/
public final void setCacheTTLMs(final Long cacheTTLMs) {
this.cacheTTLMs = cacheTTLMs;
}
@Override
public void setUseDecoupledLogic(final boolean useDecoupledLogic) {
if (useDecoupledLogic) {
throw new ConfigurationException("The 'useDecoupledLogic' flag is not allowed for String template resolution");
}
super.setUseDecoupledLogic(useDecoupledLogic);
}
@Override
protected ITemplateResource computeTemplateResource(final IEngineConfiguration configuration, final String ownerTemplate, final String template, final Map templateResolutionAttributes) {
return new StringTemplateResource(template);
}
@Override
protected TemplateMode computeTemplateMode(final IEngineConfiguration configuration, final String ownerTemplate, final String template, final Map templateResolutionAttributes) {
return this.templateMode;
}
@Override
protected ICacheEntryValidity computeValidity(final IEngineConfiguration configuration, final String ownerTemplate, final String template, final Map templateResolutionAttributes) {
if (isCacheable()) {
if (this.cacheTTLMs != null) {
return new TTLCacheEntryValidity(this.cacheTTLMs.longValue());
}
return AlwaysValidCacheEntryValidity.INSTANCE;
}
return NonCacheableCacheEntryValidity.INSTANCE;
}
}