Search in sources :

Example 1 with IStandardExpressionParser

use of org.thymeleaf.standard.expression.IStandardExpressionParser 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 2 with IStandardExpressionParser

use of org.thymeleaf.standard.expression.IStandardExpressionParser 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 3 with IStandardExpressionParser

use of org.thymeleaf.standard.expression.IStandardExpressionParser 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)

Example 4 with IStandardExpressionParser

use of org.thymeleaf.standard.expression.IStandardExpressionParser 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)

Example 5 with IStandardExpressionParser

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

the class SlingIncludeAttributeTagProcessor method doProcess.

@Override
protected void doProcess(final ITemplateContext templateContext, final IProcessableElementTag processableElementTag, final AttributeName attributeName, final String attributeValue, final IElementTagStructureHandler elementTagStructureHandler) {
    try {
        final SlingHttpServletRequest slingHttpServletRequest = (SlingHttpServletRequest) templateContext.getVariable(SlingBindings.REQUEST);
        final SlingHttpServletResponse slingHttpServletResponse = (SlingHttpServletResponse) templateContext.getVariable(SlingBindings.RESPONSE);
        final IEngineConfiguration configuration = templateContext.getConfiguration();
        final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
        final IStandardExpression expression = expressionParser.parseExpression(templateContext, attributeValue);
        final Object include = expression.execute(templateContext);
        String path = null;
        if (include instanceof String) {
            path = (String) include;
        }
        Resource resource = null;
        if (include instanceof Resource) {
            resource = (Resource) include;
        }
        // request dispatcher options
        final RequestDispatcherOptions requestDispatcherOptions = prepareRequestDispatcherOptions(expressionParser, templateContext, processableElementTag, elementTagStructureHandler);
        // dispatch
        final String content = dispatch(resource, path, slingHttpServletRequest, slingHttpServletResponse, requestDispatcherOptions);
        // add output
        final Boolean unwrap = (Boolean) parseAttribute(expressionParser, templateContext, processableElementTag, elementTagStructureHandler, UNWRAP_ATTRIBUTE_NAME);
        if (unwrap != null && unwrap) {
            elementTagStructureHandler.replaceWith(content, false);
        } else {
            elementTagStructureHandler.setBody(content, false);
        }
    } catch (Exception e) {
        throw new RuntimeException("unable to process include attribute", e);
    }
}
Also used : SlingHttpServletResponse(org.apache.sling.api.SlingHttpServletResponse) IStandardExpression(org.thymeleaf.standard.expression.IStandardExpression) IEngineConfiguration(org.thymeleaf.IEngineConfiguration) RequestDispatcherOptions(org.apache.sling.api.request.RequestDispatcherOptions) IStandardExpressionParser(org.thymeleaf.standard.expression.IStandardExpressionParser) Resource(org.apache.sling.api.resource.Resource) SyntheticResource(org.apache.sling.api.resource.SyntheticResource) SlingHttpServletRequest(org.apache.sling.api.SlingHttpServletRequest) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Aggregations

IStandardExpressionParser (org.thymeleaf.standard.expression.IStandardExpressionParser)12 IStandardExpression (org.thymeleaf.standard.expression.IStandardExpression)11 TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)3 FragmentExpression (org.thymeleaf.standard.expression.FragmentExpression)3 Configuration (org.thymeleaf.Configuration)2 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)2 Assignation (org.thymeleaf.standard.expression.Assignation)2 AssignationSequence (org.thymeleaf.standard.expression.AssignationSequence)2 Fragment (org.thymeleaf.standard.expression.Fragment)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 AttributeName (org.thymeleaf.engine.AttributeName)1 TemplateModel (org.thymeleaf.engine.TemplateModel)1 IPostProcessor (org.thymeleaf.postprocessor.IPostProcessor)1