Search in sources :

Example 36 with TemplateProcessingException

use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.

the class AbstractElementModelProcessor method process.

public final void process(final ITemplateContext context, final IModel model, final IElementModelStructureHandler structureHandler) {
    ITemplateEvent firstEvent = null;
    try {
        firstEvent = model.get(0);
        doProcess(context, model, structureHandler);
    } catch (final TemplateProcessingException e) {
        if (firstEvent != null) {
            String modelTemplateName = firstEvent.getTemplateName();
            int modelLine = firstEvent.getLine();
            int modelCol = firstEvent.getCol();
            if (modelTemplateName != null) {
                if (!e.hasTemplateName()) {
                    e.setTemplateName(modelTemplateName);
                }
            }
            if (modelLine != -1 && modelCol != -1) {
                if (!e.hasLineAndCol()) {
                    e.setLineAndCol(modelLine, modelCol);
                }
            }
        }
        throw e;
    } catch (final Exception e) {
        // We will try to add all information possible to the exception report (template name, line, col)
        String modelTemplateName = null;
        int modelLine = -1;
        int modelCol = -1;
        if (firstEvent != null) {
            modelTemplateName = firstEvent.getTemplateName();
            modelLine = firstEvent.getLine();
            modelCol = firstEvent.getCol();
        }
        throw new TemplateProcessingException("Error during execution of processor '" + this.getClass().getName() + "'", modelTemplateName, modelLine, modelCol, e);
    }
}
Also used : ITemplateEvent(org.thymeleaf.model.ITemplateEvent) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 37 with TemplateProcessingException

use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.

the class AbstractAttributeModelProcessor method doProcess.

@Override
protected final void doProcess(final ITemplateContext context, final IModel model, final IElementModelStructureHandler structureHandler) {
    AttributeName attributeName = null;
    IProcessableElementTag firstEvent = null;
    try {
        attributeName = getMatchingAttributeName().getMatchingAttributeName();
        firstEvent = (IProcessableElementTag) model.get(0);
        final String attributeValue = EscapedAttributeUtils.unescapeAttribute(context.getTemplateMode(), firstEvent.getAttributeValue(attributeName));
        doProcess(context, model, attributeName, attributeValue, structureHandler);
        if (this.removeAttribute) {
            if (model.size() > 0 && model.get(0) instanceof IProcessableElementTag) {
                firstEvent = (IProcessableElementTag) model.get(0);
                final IModelFactory modelFactory = context.getModelFactory();
                final IProcessableElementTag newFirstEvent = modelFactory.removeAttribute(firstEvent, attributeName);
                if (newFirstEvent != firstEvent) {
                    model.replace(0, newFirstEvent);
                }
            }
        }
    } catch (final TemplateProcessingException e) {
        if (firstEvent != null) {
            String attributeTemplateName = firstEvent.getTemplateName();
            final IAttribute attribute = firstEvent.getAttribute(attributeName);
            int attributeLine = (attribute != null ? attribute.getLine() : -1);
            int attributeCol = (attribute != null ? attribute.getCol() : -1);
            if (attributeTemplateName != null) {
                if (!e.hasTemplateName()) {
                    e.setTemplateName(attributeTemplateName);
                }
            }
            if (attributeLine != -1 && attributeCol != -1) {
                if (!e.hasLineAndCol()) {
                    e.setLineAndCol(attributeLine, attributeCol);
                }
            }
        }
        throw e;
    } catch (final Exception e) {
        // We will try to add all information possible to the exception report (template name, line, col)
        String attributeTemplateName = null;
        int attributeLine = -1;
        int attributeCol = -1;
        if (firstEvent != null) {
            attributeTemplateName = firstEvent.getTemplateName();
            final IAttribute attribute = firstEvent.getAttribute(attributeName);
            attributeLine = (attribute != null ? attribute.getLine() : -1);
            attributeCol = (attribute != null ? attribute.getCol() : -1);
        }
        throw new TemplateProcessingException("Error during execution of processor '" + this.getClass().getName() + "'", attributeTemplateName, attributeLine, attributeCol, e);
    }
}
Also used : IProcessableElementTag(org.thymeleaf.model.IProcessableElementTag) IAttribute(org.thymeleaf.model.IAttribute) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) AttributeName(org.thymeleaf.engine.AttributeName) IModelFactory(org.thymeleaf.model.IModelFactory) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 38 with TemplateProcessingException

use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.

the class LessOrEqualToExpression method executeLessOrEqualTo.

@SuppressWarnings("unchecked")
static Object executeLessOrEqualTo(final IExpressionContext context, final LessOrEqualToExpression expression, final StandardExpressionExecutionContext expContext) {
    if (logger.isTraceEnabled()) {
        logger.trace("[THYMELEAF][{}] Evaluating LESS OR EQUAL TO expression: \"{}\"", TemplateEngine.threadIndex(), expression.getStringRepresentation());
    }
    Object leftValue = expression.getLeft().execute(context, expContext);
    Object rightValue = expression.getRight().execute(context, expContext);
    if (leftValue == null || rightValue == null) {
        throw new TemplateProcessingException("Cannot execute LESS OR EQUAL TO comparison: operands are \"" + LiteralValue.unwrap(leftValue) + "\" and \"" + LiteralValue.unwrap(rightValue) + "\"");
    }
    leftValue = LiteralValue.unwrap(leftValue);
    rightValue = LiteralValue.unwrap(rightValue);
    Boolean result = null;
    final BigDecimal leftNumberValue = EvaluationUtils.evaluateAsNumber(leftValue);
    final BigDecimal rightNumberValue = EvaluationUtils.evaluateAsNumber(rightValue);
    if (leftNumberValue != null && rightNumberValue != null) {
        result = Boolean.valueOf(leftNumberValue.compareTo(rightNumberValue) != 1);
    } else {
        if (leftValue != null && rightValue != null && leftValue.getClass().equals(rightValue.getClass()) && Comparable.class.isAssignableFrom(leftValue.getClass())) {
            result = Boolean.valueOf(((Comparable<Object>) leftValue).compareTo(rightValue) <= 0);
        } else {
            throw new TemplateProcessingException("Cannot execute LESS OR EQUAL TO from Expression \"" + expression.getStringRepresentation() + "\". Left is \"" + leftValue + "\", right is \"" + rightValue + "\"");
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("[THYMELEAF][{}] Evaluating LESS OR EQUAL TO expression: \"{}\". Left is \"{}\", right is \"{}\". Result is \"{}\"", new Object[] { TemplateEngine.threadIndex(), expression.getStringRepresentation(), leftValue, rightValue, result });
    }
    return result;
}
Also used : TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) BigDecimal(java.math.BigDecimal)

Example 39 with TemplateProcessingException

use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.

the class GreaterOrEqualToExpression method executeGreaterOrEqualTo.

@SuppressWarnings("unchecked")
static Object executeGreaterOrEqualTo(final IExpressionContext context, final GreaterOrEqualToExpression expression, final StandardExpressionExecutionContext expContext) {
    Object leftValue = expression.getLeft().execute(context, expContext);
    Object rightValue = expression.getRight().execute(context, expContext);
    leftValue = LiteralValue.unwrap(leftValue);
    rightValue = LiteralValue.unwrap(rightValue);
    Boolean result = null;
    final BigDecimal leftNumberValue = EvaluationUtils.evaluateAsNumber(leftValue);
    final BigDecimal rightNumberValue = EvaluationUtils.evaluateAsNumber(rightValue);
    if (leftNumberValue != null && rightNumberValue != null) {
        result = Boolean.valueOf(leftNumberValue.compareTo(rightNumberValue) != -1);
    } else {
        if (leftValue != null && rightValue != null && leftValue.getClass().equals(rightValue.getClass()) && Comparable.class.isAssignableFrom(leftValue.getClass())) {
            result = Boolean.valueOf(((Comparable<Object>) leftValue).compareTo(rightValue) >= 0);
        } else {
            throw new TemplateProcessingException("Cannot execute GREATER OR EQUAL TO from Expression \"" + expression.getStringRepresentation() + "\". Left is \"" + leftValue + "\", right is \"" + rightValue + "\"");
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("[THYMELEAF][{}] Evaluating GREATER OR EQUAL TO expression: \"{}\". Left is \"{}\", right is \"{}\". Result is \"{}\"", new Object[] { TemplateEngine.threadIndex(), expression.getStringRepresentation(), leftValue, rightValue, result });
    }
    return result;
}
Also used : TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) BigDecimal(java.math.BigDecimal)

Example 40 with TemplateProcessingException

use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.

the class OGNLVariableExpressionEvaluator method evaluate.

private static Object evaluate(final IExpressionContext context, final IStandardVariableExpression expression, final StandardExpressionExecutionContext expContext, final boolean applyOGNLShortcuts) {
    try {
        if (logger.isTraceEnabled()) {
            logger.trace("[THYMELEAF][{}] OGNL expression: evaluating expression \"{}\" on target", TemplateEngine.threadIndex(), expression.getExpression());
        }
        final IEngineConfiguration configuration = context.getConfiguration();
        final String exp = expression.getExpression();
        final boolean useSelectionAsRoot = expression.getUseSelectionAsRoot();
        if (exp == null) {
            throw new TemplateProcessingException("Expression content is null, which is not allowed");
        }
        final ComputedOGNLExpression parsedExpression = obtainComputedOGNLExpression(configuration, expression, exp, applyOGNLShortcuts);
        final Map<String, Object> contextVariablesMap;
        if (parsedExpression.mightNeedExpressionObjects) {
            // The IExpressionObjects implementation returned by processing contexts that include the Standard
            // Dialects will be lazy in the creation of expression objects (i.e. they won't be created until really
            // needed). And in order for this behaviour to be accepted by OGNL, we will be wrapping this object
            // inside an implementation of Map<String,Object>, which will afterwards be fed to the constructor
            // of an OgnlContext object.
            // Note this will never happen with shortcut expressions, as the '#' character with which all
            // expression object names start is not allowed by the OGNLShortcutExpression parser.
            final IExpressionObjects expressionObjects = context.getExpressionObjects();
            contextVariablesMap = new OGNLExpressionObjectsWrapper(expressionObjects, expContext.getRestrictVariableAccess());
            // can later lookup during evaluation.
            if (expContext.getRestrictVariableAccess()) {
                contextVariablesMap.put(OGNLContextPropertyAccessor.RESTRICT_REQUEST_PARAMETERS, OGNLContextPropertyAccessor.RESTRICT_REQUEST_PARAMETERS);
            } else {
                contextVariablesMap.remove(OGNLContextPropertyAccessor.RESTRICT_REQUEST_PARAMETERS);
            }
        } else {
            if (expContext.getRestrictVariableAccess()) {
                contextVariablesMap = CONTEXT_VARIABLES_MAP_NOEXPOBJECTS_RESTRICTIONS;
            } else {
                contextVariablesMap = Collections.EMPTY_MAP;
            }
        }
        // The root object on which we will evaluate expressions will depend on whether a selection target is
        // active or not...
        final ITemplateContext templateContext = (context instanceof ITemplateContext ? (ITemplateContext) context : null);
        final Object evaluationRoot = (useSelectionAsRoot && templateContext != null && templateContext.hasSelectionTarget() ? templateContext.getSelectionTarget() : templateContext);
        // Execute the expression!
        final Object result;
        try {
            result = executeExpression(configuration, parsedExpression.expression, contextVariablesMap, evaluationRoot);
        } catch (final OGNLShortcutExpression.OGNLShortcutExpressionNotApplicableException notApplicable) {
            // We tried to apply shortcuts, but it is not possible for this expression even if it parsed OK,
            // so we need to empty the cache and try again disabling shortcuts. Once processed for the first time,
            // an OGNL (non-shortcut) parsed expression will already be cached and this exception will not be
            // thrown again
            invalidateComputedOGNLExpression(configuration, expression, exp);
            return evaluate(context, expression, expContext, false);
        }
        if (!expContext.getPerformTypeConversion()) {
            return result;
        }
        final IStandardConversionService conversionService = StandardExpressions.getConversionService(configuration);
        return conversionService.convert(context, result, String.class);
    } catch (final Exception e) {
        throw new TemplateProcessingException("Exception evaluating OGNL expression: \"" + expression.getExpression() + "\"", e);
    }
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) IExpressionObjects(org.thymeleaf.expression.IExpressionObjects) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) OgnlException(ognl.OgnlException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) ITemplateContext(org.thymeleaf.context.ITemplateContext)

Aggregations

TemplateProcessingException (org.thymeleaf.exceptions.TemplateProcessingException)45 BigDecimal (java.math.BigDecimal)12 IEngineConfiguration (org.thymeleaf.IEngineConfiguration)8 IStandardExpression (org.thymeleaf.standard.expression.IStandardExpression)6 IOException (java.io.IOException)5 ITemplateContext (org.thymeleaf.context.ITemplateContext)4 Writer (java.io.Writer)3 Test (org.junit.Test)3 Context (org.thymeleaf.context.Context)3 AttributeName (org.thymeleaf.engine.AttributeName)3 TemplateEngineException (org.thymeleaf.exceptions.TemplateEngineException)3 TemplateInputException (org.thymeleaf.exceptions.TemplateInputException)3 TemplateOutputException (org.thymeleaf.exceptions.TemplateOutputException)3 FastStringWriter (org.thymeleaf.util.FastStringWriter)3 DataBuffer (org.springframework.core.io.buffer.DataBuffer)2 IEngineContext (org.thymeleaf.context.IEngineContext)2 TemplateManager (org.thymeleaf.engine.TemplateManager)2 IAttribute (org.thymeleaf.model.IAttribute)2 IOpenElementTag (org.thymeleaf.model.IOpenElementTag)2 IProcessableElementTag (org.thymeleaf.model.IProcessableElementTag)2