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);
}
}
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;
}
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;
}
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;
}
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);
}
}
Aggregations