Search in sources :

Example 31 with TemplateProcessingException

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

the class Numbers method formatDecimal.

public String formatDecimal(final Number target, final Integer minIntegerDigits, final String thousandsPointType, final Integer decimalDigits, final String decimalPointType) {
    if (target == null) {
        return null;
    }
    final NumberPointType decimalNumberPointType = NumberPointType.match(decimalPointType);
    if (decimalNumberPointType == null) {
        throw new TemplateProcessingException("Unrecognized point format \"" + decimalPointType + "\"");
    }
    final NumberPointType thousandsNumberPointType = NumberPointType.match(thousandsPointType);
    if (thousandsNumberPointType == null) {
        throw new TemplateProcessingException("Unrecognized point format \"" + thousandsPointType + "\"");
    }
    try {
        return NumberUtils.format(target, minIntegerDigits, thousandsNumberPointType, decimalDigits, decimalNumberPointType, this.locale);
    } catch (final Exception e) {
        throw new TemplateProcessingException("Error formatting decimal with minimum integer digits = " + minIntegerDigits + ", thousands point type = " + thousandsPointType + ", decimal digits = " + decimalDigits + " and decimal point type = " + decimalPointType, e);
    }
}
Also used : NumberPointType(org.thymeleaf.util.NumberPointType) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 32 with TemplateProcessingException

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

the class BinaryOperationExpression method doComposeBinaryOperationExpression.

private static ExpressionParsingState doComposeBinaryOperationExpression(final ExpressionParsingState state, final int nodeIndex, final String operator, final Class<? extends BinaryOperationExpression> operationClass, final Method leftAllowedMethod, final Method rightAllowedMethod, final String input, final int operatorPos) {
    final String leftStr = input.substring(0, operatorPos).trim();
    final String rightStr = input.substring(operatorPos + operator.length()).trim();
    if (leftStr.length() == 0 || rightStr.length() == 0) {
        return null;
    }
    final Expression leftExpr = ExpressionParsingUtil.parseAndCompose(state, leftStr);
    try {
        if (leftExpr == null || !((Boolean) leftAllowedMethod.invoke(null, leftExpr)).booleanValue()) {
            return null;
        }
    } catch (final IllegalAccessException e) {
        // Should never happen, would be a programming error
        throw new TemplateProcessingException("Error invoking operand validation in binary operation", e);
    } catch (final InvocationTargetException e) {
        // Should never happen, would be a programming error
        throw new TemplateProcessingException("Error invoking operand validation in binary operation", e);
    }
    final Expression rightExpr = ExpressionParsingUtil.parseAndCompose(state, rightStr);
    try {
        if (rightExpr == null || !((Boolean) rightAllowedMethod.invoke(null, rightExpr)).booleanValue()) {
            return null;
        }
    } catch (final IllegalAccessException e) {
        // Should never happen, would be a programming error
        throw new TemplateProcessingException("Error invoking operand validation in binary operation", e);
    } catch (final InvocationTargetException e) {
        // Should never happen, would be a programming error
        throw new TemplateProcessingException("Error invoking operand validation in binary operation", e);
    }
    try {
        final BinaryOperationExpression operationExpression = operationClass.getDeclaredConstructor(IStandardExpression.class, IStandardExpression.class).newInstance(leftExpr, rightExpr);
        state.setNode(nodeIndex, operationExpression);
    } catch (final TemplateProcessingException e) {
        throw e;
    } catch (final Exception e) {
        throw new TemplateProcessingException("Error during creation of Binary Operation expression for operator: \"" + operator + "\"", e);
    }
    return state;
}
Also used : TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 33 with TemplateProcessingException

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

the class EachUtils method parseEach.

public static Each parseEach(final IExpressionContext context, final String input) {
    Validate.notNull(context, "Context cannot be null");
    Validate.notNull(input, "Input cannot be null");
    final String preprocessedInput = StandardExpressionPreprocessor.preprocess(context, input);
    final IEngineConfiguration configuration = context.getConfiguration();
    if (configuration != null) {
        final Each cachedEach = ExpressionCache.getEachFromCache(configuration, preprocessedInput);
        if (cachedEach != null) {
            return cachedEach;
        }
    }
    final Each each = internalParseEach(preprocessedInput.trim());
    if (each == null) {
        throw new TemplateProcessingException("Could not parse as each: \"" + input + "\"");
    }
    if (configuration != null) {
        ExpressionCache.putEachIntoCache(configuration, preprocessedInput, each);
    }
    return each;
}
Also used : IEngineConfiguration(org.thymeleaf.IEngineConfiguration) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

Example 34 with TemplateProcessingException

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

the class LessThanExpression method executeLessThan.

@SuppressWarnings("unchecked")
static Object executeLessThan(final IExpressionContext context, final LessThanExpression expression, final StandardExpressionExecutionContext expContext) {
    if (logger.isTraceEnabled()) {
        logger.trace("[THYMELEAF][{}] Evaluating LESS THAN 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 THAN 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 THAN from Expression \"" + expression.getStringRepresentation() + "\". Left is \"" + leftValue + "\", right is \"" + rightValue + "\"");
        }
    }
    if (logger.isTraceEnabled()) {
        logger.trace("[THYMELEAF][{}] Evaluating LESS THAN 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 35 with TemplateProcessingException

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

the class AbstractAttributeTagProcessor method doProcess.

@Override
protected final void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final IElementTagStructureHandler structureHandler) {
    AttributeName attributeName = null;
    try {
        attributeName = getMatchingAttributeName().getMatchingAttributeName();
        final String attributeValue = EscapedAttributeUtils.unescapeAttribute(context.getTemplateMode(), tag.getAttributeValue(attributeName));
        doProcess(context, tag, attributeName, attributeValue, structureHandler);
        if (this.removeAttribute) {
            structureHandler.removeAttribute(attributeName);
        }
    } catch (final TemplateProcessingException e) {
        // specific because we know exactly what attribute was being executed and caused the error
        if (tag.hasLocation()) {
            if (!e.hasTemplateName()) {
                e.setTemplateName(tag.getTemplateName());
            }
            if (!e.hasLineAndCol()) {
                if (attributeName == null) {
                    // We don't have info about the specific attribute provoking the error
                    e.setLineAndCol(tag.getLine(), tag.getCol());
                } else {
                    final IAttribute attribute = tag.getAttribute(attributeName);
                    if (attribute != null) {
                        e.setLineAndCol(attribute.getLine(), attribute.getCol());
                    }
                }
            }
        }
        throw e;
    } catch (final Exception e) {
        int line = tag.getLine();
        int col = tag.getCol();
        if (attributeName != null) {
            // We don't have info about the specific attribute provoking the error
            final IAttribute attribute = tag.getAttribute(attributeName);
            if (attribute != null) {
                line = attribute.getLine();
                col = attribute.getCol();
            }
        }
        throw new TemplateProcessingException("Error during execution of processor '" + this.getClass().getName() + "'", tag.getTemplateName(), line, col, e);
    }
}
Also used : IAttribute(org.thymeleaf.model.IAttribute) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException) AttributeName(org.thymeleaf.engine.AttributeName) TemplateProcessingException(org.thymeleaf.exceptions.TemplateProcessingException)

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