org.thymeleaf.expression.ExecutionInfo 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.expression;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.thymeleaf.context.ITemplateContext;
import org.thymeleaf.engine.TemplateData;
import org.thymeleaf.templatemode.TemplateMode;
/**
*
* Expression Object providing useful information about the template being processed inside Thymeleaf Standard
* Expressions.
*
*
* An object of this class is usually available in variable evaluation expressions with the name
* #execInfo.
*
*
* @author Daniel Fernández
*
* @since 3.0.0
*
*/
public final class ExecutionInfo {
private final ITemplateContext context;
private final Calendar now;
public ExecutionInfo(final ITemplateContext context) {
super();
this.context = context;
this.now = Calendar.getInstance(context.getLocale());
}
/**
*
* Returns the template name (of the leaf template).
*
*
* Note that the template name returned here corresponds with origin of the elements or nodes being
* currently processed. This is, if a processor is being executed for an element inserted from an external
* template (via a th:insert, for example), then this method will return the template mode
* for the template in which the inserted fragment lives, not the one it was inserted into.
*
*
* @return the template name
*/
public String getTemplateName() {
return this.context.getTemplateData().getTemplate();
}
/**
*
* Returns the template mode ({@link TemplateMode}) (of the leaf template).
*
*
* Note that the {@link TemplateMode} returned here corresponds with origin of the elements or nodes being
* currently processed. This is, if a processor is being executed for an element inserted from an external
* template (via a th:insert, for example), then this method will return the template mode
* for the template in which the inserted fragment lives, not the one it was inserted into.
*
*
* @return the template mode
*/
public TemplateMode getTemplateMode() {
return this.context.getTemplateData().getTemplateMode();
}
/**
*
* Returns the template name of the first-level template.
*
*
* Note this template name refers to the first-level one, the one used to call the TemplateEngine itself, even
* if by the moment this method is called the engine is processing a fragment inserted from the first-level
* template (or at any other level in the hierarchy).
*
*
* @return the template name
*/
public String getProcessedTemplateName() {
return this.context.getTemplateStack().get(0).getTemplate();
}
/**
*
* Returns the template mode ({@link TemplateMode}) of the first-level template.
*
*
* Note this template mode refers to the first-level one, the one used to call the TemplateEngine itself, even
* if by the moment this method is called the engine is processing a fragment inserted from the first-level
* template (or at any other level in the hierarchy).
*
*
* @return the template mode
*/
public TemplateMode getProcessedTemplateMode() {
return this.context.getTemplateStack().get(0).getTemplateMode();
}
/**
*
* Returns the names of all the stack of templates appliable to the current point
* of execution. This will depend on which templates are inserted inside wich.
*
*
* The first-level template will appear first, and the most specific template will appear last.
*
*
* @return the stack of template names
*/
public List getTemplateNames() {
final List templateStack = this.context.getTemplateStack();
final List templateNameStack = new ArrayList(templateStack.size());
for (final TemplateData templateData : templateStack) {
templateNameStack.add(templateData.getTemplate());
}
return templateNameStack;
}
/**
*
* Returns the {@link TemplateMode}s of all the stack of templates appliable to the current point
* of execution. This will depend on which templates are inserted inside wich.
*
*
* The first-level template will appear first, and the most specific template will appear last.
*
*
* @return the stack of template modes
*/
public List getTemplateModes() {
final List templateStack = this.context.getTemplateStack();
final List templateModeStack = new ArrayList(templateStack.size());
for (final TemplateData templateData : templateStack) {
templateModeStack.add(templateData.getTemplateMode());
}
return templateModeStack;
}
/**
*
* Returns the template stack, containing the metadata for the first-level template being
* processed and also any fragments that might have been nested up to the current execution point.
*
*
* The result of this method actually corresponds to the result of {@link ITemplateContext#getTemplateStack()}.
*
*
* @return the stack of {@link TemplateData} objects
*/
public List getTemplateStack() {
return this.context.getTemplateStack();
}
/**
*
* Returns the current date and time (from the moment of template execution).
*
*
* @return the current date and time, as a Calendar
*/
public Calendar getNow() {
return this.now;
}
}