org.thymeleaf.engine.TemplateData 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.engine;
import java.util.Set;
import org.thymeleaf.cache.ICacheEntryValidity;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresource.ITemplateResource;
/**
*
* Class containing all the data related to a template that is currently being processed.
*
*
* Objects of this class are meant to be a part of the {@link ITemplateContext} instances
* offered by the engine to processing artifacts such as processors, pre-/post-processors, etc.
*
*
* These objects contain some metadata about the template being processed, and also the template resource
* itself, coming from the {@link org.thymeleaf.templateresolver.TemplateResolution}
* produced by template resolvers ({@link org.thymeleaf.templateresolver.ITemplateResolver}).
*
*
* The template contained usually represents the template name, but can be the entire
* template contents if the template has been resolved by a
* {@link org.thymeleaf.templateresolver.StringTemplateResolver} or an analogous implementation of
* {@link org.thymeleaf.templateresolver.ITemplateResolver}.
*
*
* Objects of this class should not be considered thread-safe.
*
*
*
* @author Daniel Fernández
*
* @see org.thymeleaf.TemplateSpec
* @see org.thymeleaf.templateresolver.TemplateResolution
*
* @since 3.0.0
*
*/
public final class TemplateData {
private final String template;
private final Set templateSelectors;
private final ITemplateResource templateResource;
private final TemplateMode templateMode;
private final ICacheEntryValidity cacheValidity;
/**
*
* Builds a new TemplateData object.
*
*
* This constructor should be considered internal, as there should be no reason why
* instances of this class should be created from outside the Template Engine itself.
*
*
* @param template the template
* @param templateSelectors the template selectors
* @param templateResource the template resource
* @param templateMode the template mode
*/
TemplateData(
final String template,
final Set templateSelectors,
final ITemplateResource templateResource,
final TemplateMode templateMode,
final ICacheEntryValidity cacheValidity) {
super();
// NO VALIDATIONS OR TRANSFORMATIONS ARE PERFORMED ON DATA. This constructor is package-protected so that
// objects of this class can only be created from the engine (specifically, the TemplateManager). This
// template manager will make sure only templateSelectors and templateResolutionAttributes can be null, and
// that if they aren't they should be non-empty. Also, templateSelectors will come ordered by natural order,
// an operation that is performed at the TemplateSpec constructor itself.
this.template = template;
this.templateSelectors = templateSelectors;
this.templateResource = templateResource;
this.templateMode = templateMode;
this.cacheValidity = cacheValidity;
}
/**
*
* Returns the template (usually the template name).
*
*
* This template normally represents the template name, but can be the entire template
* contents if the template was specified as a String and resolved by a
* {@link org.thymeleaf.templateresolver.StringTemplateResolver} or equivalent.
*
*
* @return the template. Cannot be null.
*/
public String getTemplate() {
return this.template;
}
/**
*
* Returns whether this spec has template selectors specified or not.
*
*
* @return true of there are template selectors, false if not.
*/
public boolean hasTemplateSelectors() {
// Checking for null is enough, as we have already processed this in the constructor
return this.templateSelectors != null;
}
/**
*
* Returns the template selectors, if there are any.
*
*
* Template selectors allow the possibility to process only a part of the specified template, expressing
* this selection in a syntax similar to jQuery, CSS or XPath selectors. Note this is only available for
* markup template modes (HTML, XML). For more info on template selectors
* syntax, have a look at AttoParser's markup selectors
* documentation.
*
*
* @return the template selectors, or null if there are none.
*/
public Set getTemplateSelectors() {
return this.templateSelectors;
}
/**
*
* Returns the template resource.
*
*
* Template resource instances are usually created by implementations of
* {@link org.thymeleaf.templateresolver.ITemplateResolver}.
*
*
* Note that, even if this resource object will never be null, the existence of the
* resource object does not necessarily imply the existence of the resource itself unless
* the template resolver was configured for calling {@link ITemplateResource#exists()} upon
* template resolution.
*
*
* @return the template resource. Cannot be null.
*/
public ITemplateResource getTemplateResource() {
return this.templateResource;
}
/**
*
* Returns the template mode the template is being processed with.
*
*
* Most times this template mode is the one suggested by the
* {@link org.thymeleaf.templateresolver.ITemplateResolver} that resolved the template, but
* in those times that a template mode was forced by specifying it at a
* {@link org.thymeleaf.TemplateSpec} object or at a call to the {@link org.thymeleaf.engine.TemplateManager},
* these will override the template mode suggested by the template resolver.
*
*
* @return the template mode for the template. Cannot be null.
*/
public TemplateMode getTemplateMode() {
return this.templateMode;
}
/**
*
* Returns the template resolution validity.
*
*
* This validity establishes whether the template can be included in the
* template cache, and also for how long its resolution will be considered valid.
*
*
* When a cached template is not considered valid, its cache entry is discarded
* and it is resolved again.
*
*
* @return the validity object
*/
public ICacheEntryValidity getValidity() {
return this.cacheValidity;
}
}