Search in sources :

Example 1 with StandardExpressionExecutionContext

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

the class AbstractStandardMultipleAttributeModifierTagProcessor method doProcess.

@Override
protected final void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue, final IElementTagStructureHandler structureHandler) {
    final AssignationSequence assignations = AssignationUtils.parseAssignationSequence(context, attributeValue, false);
    if (assignations == null) {
        throw new TemplateProcessingException("Could not parse value as attribute assignations: \"" + attributeValue + "\"");
    }
    // Compute the required execution context depending on whether execution should be restricted or not
    final StandardExpressionExecutionContext expCtx = (this.restrictedExpressionExecution ? StandardExpressionExecutionContext.RESTRICTED : StandardExpressionExecutionContext.NORMAL);
    final List<Assignation> assignationValues = assignations.getAssignations();
    final int assignationValuesLen = assignationValues.size();
    for (int i = 0; i < assignationValuesLen; i++) {
        final Assignation assignation = assignationValues.get(i);
        final IStandardExpression leftExpr = assignation.getLeft();
        final Object leftValue = leftExpr.execute(context, expCtx);
        final IStandardExpression rightExpr = assignation.getRight();
        final Object rightValue = rightExpr.execute(context, expCtx);
        if (rightValue == NoOpToken.VALUE) {
            // No changes to be done for this attribute
            continue;
        }
        final String newAttributeName = (leftValue == null ? null : leftValue.toString());
        if (StringUtils.isEmptyOrWhitespace(newAttributeName)) {
            throw new TemplateProcessingException("Attribute name expression evaluated as null or empty: \"" + leftExpr + "\"");
        }
        if (getTemplateMode() == TemplateMode.HTML && this.modificationType == ModificationType.SUBSTITUTION && ArrayUtils.contains(StandardConditionalFixedValueTagProcessor.ATTR_NAMES, newAttributeName)) {
            if (EvaluationUtils.evaluateAsBoolean(rightValue)) {
                structureHandler.setAttribute(newAttributeName, newAttributeName);
            } else {
                structureHandler.removeAttribute(newAttributeName);
            }
        } else {
            // Attribute is a "normal" attribute, not a fixed-value conditional one - or we are not just replacing
            final String newAttributeValue = EscapedAttributeUtils.escapeAttribute(getTemplateMode(), rightValue == null ? null : rightValue.toString());
            if (newAttributeValue == null || newAttributeValue.length() == 0) {
                if (this.modificationType == ModificationType.SUBSTITUTION) {
                    // Substituting by a no-value will be equivalent to simply removing
                    structureHandler.removeAttribute(newAttributeName);
                }
            // Prepend and append simply ignored in this case
            } else {
                if (this.modificationType == ModificationType.SUBSTITUTION || !tag.hasAttribute(newAttributeName) || tag.getAttributeValue(newAttributeName).length() == 0) {
                    // Normal value replace
                    structureHandler.setAttribute(newAttributeName, newAttributeValue);
                } else {
                    String currentValue = tag.getAttributeValue(newAttributeName);
                    if (this.modificationType == ModificationType.APPEND) {
                        structureHandler.setAttribute(newAttributeName, currentValue + newAttributeValue);
                    } else if (this.modificationType == ModificationType.APPEND_WITH_SPACE) {
                        structureHandler.setAttribute(newAttributeName, currentValue + ' ' + newAttributeValue);
                    } else if (this.modificationType == ModificationType.PREPEND) {
                        structureHandler.setAttribute(newAttributeName, newAttributeValue + currentValue);
                    } else {
                        // modification type is PREPEND_WITH_SPACE
                        structureHandler.setAttribute(newAttributeName, newAttributeValue + ' ' + currentValue);
                    }
                }
            }
        }
    }
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) AssignationSequence(org.thymeleaf.standard.expression.AssignationSequence) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) Assignation(org.thymeleaf.standard.expression.Assignation) StandardExpressionExecutionContext(org.thymeleaf.standard.expression.StandardExpressionExecutionContext)

Example 2 with StandardExpressionExecutionContext

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

the class AbstractStandardExpressionAttributeTagProcessor method doProcess.

@Override
protected final void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue, final IElementTagStructureHandler structureHandler) {
    final Object expressionResult;
    if (attributeValue != null) {
        final IStandardExpression expression = EngineEventUtils.computeAttributeExpression(context, tag, attributeName, attributeValue);
        if (expression != null && expression instanceof FragmentExpression) {
            // This is merely a FragmentExpression (not complex, not combined with anything), so we can apply a shortcut
            // so that we don't require a "null" result for this expression if the template does not exist. That will
            // save a call to resource.exists() which might be costly.
            final FragmentExpression.ExecutedFragmentExpression executedFragmentExpression = FragmentExpression.createExecutedFragmentExpression(context, (FragmentExpression) expression);
            expressionResult = FragmentExpression.resolveExecutedFragmentExpression(context, executedFragmentExpression, true);
        } else {
            /*
                 * Some attributes will require the execution of the expressions contained in them in RESTRICTED
                 * mode, so that e.g. access to request parameters is forbidden.
                 */
            final StandardExpressionExecutionContext expCtx = (this.restrictedExpressionExecution ? StandardExpressionExecutionContext.RESTRICTED : StandardExpressionExecutionContext.NORMAL);
            expressionResult = expression.execute(context, expCtx);
        }
    } else {
        expressionResult = null;
    }
    // If the result of this expression is NO-OP, there is nothing to execute
    if (expressionResult == NoOpToken.VALUE) {
        if (this.removeIfNoop) {
            structureHandler.removeAttribute(attributeName);
        }
        return;
    }
    doProcess(context, tag, attributeName, attributeValue, expressionResult, structureHandler);
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) FragmentExpression(org.thymeleaf.standard.expression.FragmentExpression) StandardExpressionExecutionContext(org.thymeleaf.standard.expression.StandardExpressionExecutionContext)

Aggregations

IStandardExpression (org.thymeleaf.standard.expression.IStandardExpression)2 StandardExpressionExecutionContext (org.thymeleaf.standard.expression.StandardExpressionExecutionContext)2 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)1 Assignation (org.thymeleaf.standard.expression.Assignation)1 AssignationSequence (org.thymeleaf.standard.expression.AssignationSequence)1 FragmentExpression (org.thymeleaf.standard.expression.FragmentExpression)1