Search in sources :

Example 1 with IStandardExpression

use of org.thymeleaf.standard.expression.IStandardExpression in project sling by apache.

the class SlingIncludeAttributeTagProcessor method parseAttribute.

protected Object parseAttribute(final IStandardExpressionParser expressionParser, final ITemplateContext templateContext, final IProcessableElementTag processableElementTag, final IElementTagStructureHandler elementTagStructureHandler, final String name) {
    final String value = processableElementTag.getAttributeValue(getDialectPrefix(), name);
    Object result = null;
    if (value != null) {
        final IStandardExpression expression = expressionParser.parseExpression(templateContext, value);
        result = expression.execute(templateContext);
    }
    elementTagStructureHandler.removeAttribute(getDialectPrefix(), name);
    return result;
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression)

Example 2 with IStandardExpression

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

the class StandardDefaultAttributesTagProcessor method processDefaultAttribute.

private static void processDefaultAttribute(final TemplateMode templateMode, final ITemplateContext context, final IProcessableElementTag tag, final IAttribute attribute, final IElementTagStructureHandler structureHandler) {
    try {
        final AttributeName attributeName = attribute.getAttributeDefinition().getAttributeName();
        final String attributeValue = EscapedAttributeUtils.unescapeAttribute(context.getTemplateMode(), attribute.getValue());
        /*
             * Compute the new attribute name (i.e. the same, without the prefix)
             */
        final String originalCompleteAttributeName = attribute.getAttributeCompleteName();
        final String canonicalAttributeName = attributeName.getAttributeName();
        final String newAttributeName;
        if (TextUtil.endsWith(true, originalCompleteAttributeName, canonicalAttributeName)) {
            // We avoid creating a new String instance
            newAttributeName = canonicalAttributeName;
        } else {
            newAttributeName = originalCompleteAttributeName.substring(originalCompleteAttributeName.length() - canonicalAttributeName.length());
        }
        /*
             * Obtain the parser
             */
        final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(context.getConfiguration());
        /*
             * Execute the expression, handling nulls in a way consistent with the rest of the Standard Dialect
             */
        final Object expressionResult;
        if (attributeValue != null) {
            final IStandardExpression expression = expressionParser.parseExpression(context, 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 {
                // Default attributes will ALWAYS be executed in RESTRICTED mode, for safety reasons (they might
                // create attributes involved in code execution)
                expressionResult = expression.execute(context, StandardExpressionExecutionContext.RESTRICTED);
            }
        } else {
            expressionResult = null;
        }
        /*
             * If the result of this expression is NO-OP, there is nothing to execute
             */
        if (expressionResult == NoOpToken.VALUE) {
            structureHandler.removeAttribute(attributeName);
            return;
        }
        /*
             * Compute the new attribute value
             */
        final String newAttributeValue = EscapedAttributeUtils.escapeAttribute(templateMode, expressionResult == null ? null : expressionResult.toString());
        /*
             * Set the new value, removing the attribute completely if the expression evaluated to null
             */
        if (newAttributeValue == null || newAttributeValue.length() == 0) {
            // We are removing the equivalent attribute name, without the prefix...
            structureHandler.removeAttribute(newAttributeName);
            structureHandler.removeAttribute(attributeName);
        } else {
            // We are setting the equivalent attribute name, without the prefix...
            structureHandler.replaceAttribute(attributeName, newAttributeName, (newAttributeValue == null ? "" : newAttributeValue));
        }
    } catch (final TemplateProcessingException e) {
        // specific because we know exactly what attribute was being executed and caused the error
        if (!e.hasTemplateName()) {
            e.setTemplateName(tag.getTemplateName());
        }
        if (!e.hasLineAndCol()) {
            e.setLineAndCol(attribute.getLine(), attribute.getCol());
        }
        throw e;
    } catch (final Exception e) {
        throw new TemplateProcessingException("Error during execution of processor '" + StandardDefaultAttributesTagProcessor.class.getName() + "'", tag.getTemplateName(), attribute.getLine(), attribute.getCol(), e);
    }
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) FragmentExpression(org.thymeleaf.standard.expression.FragmentExpression) IStandardExpressionParser(org.thymeleaf.standard.expression.IStandardExpressionParser) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) MatchingAttributeName(org.thymeleaf.processor.element.MatchingAttributeName) AttributeName(org.thymeleaf.engine.AttributeName) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 3 with IStandardExpression

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

the class StandardIfTagProcessor method isVisible.

@Override
protected boolean isVisible(final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue) {
    final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(context.getConfiguration());
    final IStandardExpression expression = expressionParser.parseExpression(context, attributeValue);
    final Object value = expression.execute(context);
    return EvaluationUtils.evaluateAsBoolean(value);
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) IStandardExpressionParser(org.thymeleaf.standard.expression.IStandardExpressionParser)

Example 4 with IStandardExpression

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

the class AbstractStandardInliner method processExpression.

private String processExpression(final ITemplateContext context, final IStandardExpressionParser expressionParser, final String expression, final boolean escape, final String templateName, final int line, final int col) {
    try {
        final String unescapedExpression = EscapedAttributeUtils.unescapeAttribute(context.getTemplateMode(), expression);
        final Object expressionResult;
        if (unescapedExpression != null) {
            final IStandardExpression expressionObj = expressionParser.parseExpression(context, unescapedExpression);
            expressionResult = expressionObj.execute(context);
        } else {
            expressionResult = null;
        }
        if (escape) {
            return produceEscapedOutput(expressionResult);
        } else {
            return (expressionResult == null ? "" : expressionResult.toString());
        }
    } catch (final TemplateProcessingException e) {
        // We will add location info
        if (!e.hasTemplateName()) {
            e.setTemplateName(templateName);
        }
        if (!e.hasLineAndCol()) {
            e.setLineAndCol(line, col);
        }
        throw e;
    } catch (final Exception e) {
        throw new TemplateProcessingException("Error during execution of inlined expression '" + expression + "'", templateName, line, col, e);
    }
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 5 with IStandardExpression

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

the class StandardUnlessTagProcessor method isVisible.

@Override
protected boolean isVisible(final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue) {
    final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(context.getConfiguration());
    final IStandardExpression expression = expressionParser.parseExpression(context, attributeValue);
    final Object value = expression.execute(context);
    return !EvaluationUtils.evaluateAsBoolean(value);
}
Also used : IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) IStandardExpressionParser(org.thymeleaf.standard.expression.IStandardExpressionParser)

Aggregations

IStandardExpression (org.thymeleaf.standard.expression.IStandardExpression)21 IStandardExpressionParser (org.thymeleaf.standard.expression.IStandardExpressionParser)11 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)7 Assignation (org.thymeleaf.standard.expression.Assignation)6 AssignationSequence (org.thymeleaf.standard.expression.AssignationSequence)6 FragmentExpression (org.thymeleaf.standard.expression.FragmentExpression)5 Configuration (org.thymeleaf.Configuration)4 HashMap (java.util.HashMap)2 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)2 Fragment (org.thymeleaf.standard.expression.Fragment)2 StandardExpressionExecutionContext (org.thymeleaf.standard.expression.StandardExpressionExecutionContext)2 IOException (java.io.IOException)1 Map (java.util.Map)1 ServletException (javax.servlet.ServletException)1 SlingHttpServletRequest (org.apache.sling.api.SlingHttpServletRequest)1 SlingHttpServletResponse (org.apache.sling.api.SlingHttpServletResponse)1 RequestDispatcherOptions (org.apache.sling.api.request.RequestDispatcherOptions)1 Resource (org.apache.sling.api.resource.Resource)1 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)1 IEngineContext (org.thymeleaf.context.IEngineContext)1