Search in sources :

Example 1 with EqualsExpression

use of org.thymeleaf.standard.expression.EqualsExpression in project thymeleaf by thymeleaf.

the class StandardCaseTagProcessor method isVisible.

@Override
protected boolean isVisible(final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue) {
    /*
         * Note the th:case processors must admit the concept of SHORTCUT inside the enclosing th:switch, which means
         * that once one th:case has evaluated to true, no other th:case should be evaluated at all. It is because
         * of this that this class should not extend from any other that evaluates the attributeValue before calling
         * this code.
         */
    final StandardSwitchTagProcessor.SwitchStructure switchStructure = (StandardSwitchTagProcessor.SwitchStructure) context.getVariable(StandardSwitchTagProcessor.SWITCH_VARIABLE_NAME);
    if (switchStructure == null) {
        throw new TemplateProcessingException("Cannot specify a \"" + attributeName + "\" attribute in an environment where no " + "switch operator has been defined before.");
    }
    if (switchStructure.isExecuted()) {
        return false;
    }
    if (attributeValue != null && attributeValue.trim().equals(CASE_DEFAULT_ATTRIBUTE_VALUE)) {
        if (this.logger.isTraceEnabled()) {
            this.logger.trace("[THYMELEAF][{}][{}] Case expression \"{}\" in attribute \"{}\" has been evaluated as: \"{}\"", new Object[] { TemplateEngine.threadIndex(), LoggingUtils.loggifyTemplateName(context.getTemplateData().getTemplate()), attributeValue, attributeName, attributeValue, Boolean.TRUE });
        }
        switchStructure.setExecuted(true);
        return true;
    }
    final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(context.getConfiguration());
    final IStandardExpression caseExpression = expressionParser.parseExpression(context, attributeValue);
    final EqualsExpression equalsExpression = new EqualsExpression(switchStructure.getExpression(), caseExpression);
    final Object value = equalsExpression.execute(context);
    final boolean visible = EvaluationUtils.evaluateAsBoolean(value);
    if (this.logger.isTraceEnabled()) {
        this.logger.trace("[THYMELEAF][{}][{}] Case expression \"{}\" in attribute \"{}\" has been evaluated as: \"{}\"", new Object[] { TemplateEngine.threadIndex(), LoggingUtils.loggifyTemplateName(context.getTemplateData().getTemplate()), attributeValue, attributeName, attributeValue, Boolean.valueOf(visible) });
    }
    if (visible) {
        switchStructure.setExecuted(true);
    }
    return visible;
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) IStandardExpressionParser(org.thymeleaf.standard.expression.IStandardExpressionParser) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) EqualsExpression(org.thymeleaf.standard.expression.EqualsExpression)

Aggregations

TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)1 EqualsExpression (org.thymeleaf.standard.expression.EqualsExpression)1 IStandardExpression (org.thymeleaf.standard.expression.IStandardExpression)1 IStandardExpressionParser (org.thymeleaf.standard.expression.IStandardExpressionParser)1