use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.
the class GreaterThanExpression method executeGreaterThan.
@SuppressWarnings("unchecked")
static Object executeGreaterThan(final IExpressionContext context, final GreaterThanExpression expression, final StandardExpressionExecutionContext expContext) {
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Evaluating GREATER 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 GREATER 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 GREATER THAN from Expression \"" + expression.getStringRepresentation() + "\". Left is \"" + leftValue + "\", right is \"" + rightValue + "\"");
}
}
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Evaluating GREATER 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 LinkExpression method executeLinkExpression.
static Object executeLinkExpression(final IExpressionContext context, final LinkExpression expression) {
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Evaluating link: \"{}\"", TemplateEngine.threadIndex(), expression.getStringRepresentation());
}
if (!(context instanceof ITemplateContext)) {
throw new TemplateProcessingException("Cannot evaluate expression \"" + expression + "\". Link expressions " + "can only be evaluated in a template-processing environment (as a part of an in-template expression) " + "where processing context is an implementation of " + ITemplateContext.class.getClass() + ", which it isn't (" + context.getClass().getName() + ")");
}
final ITemplateContext templateContext = (ITemplateContext) context;
final IStandardExpression baseExpression = expression.getBase();
// The URL base in a link expression will always be executed in RESTRICTED mode, so we will forbid that
// base URLs come directly from user input (request parameters). Note this restriction does not need to apply
// to URL parameters.
Object base = baseExpression.execute(templateContext, StandardExpressionExecutionContext.RESTRICTED);
base = LiteralValue.unwrap(base);
if (base != null && !(base instanceof String)) {
base = base.toString();
}
if (base == null || StringUtils.isEmptyOrWhitespace((String) base)) {
base = "";
}
/*
* Resolve the parameters from the expression into a LinkParameters object.
* Note the parameters variable might be null if there are no parameters
*
* Also note that link parameters, which should be correctly URL-encoded before being added to
* the query string of the URL, will always be executed using UNRESTRICTED mode, so that request
* params can be directly passed along to other generated URLs.
*/
final Map<String, Object> parameters = resolveParameters(templateContext, expression, StandardExpressionExecutionContext.NORMAL);
return templateContext.buildLink((String) base, parameters);
}
use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.
the class MultiplicationExpression method executeMultiplication.
static Object executeMultiplication(final IExpressionContext context, final MultiplicationExpression expression, final StandardExpressionExecutionContext expContext) {
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Evaluating multiplication expression: \"{}\"", TemplateEngine.threadIndex(), expression.getStringRepresentation());
}
Object leftValue = expression.getLeft().execute(context, expContext);
Object rightValue = expression.getRight().execute(context, expContext);
if (leftValue == null) {
leftValue = "null";
}
if (rightValue == null) {
rightValue = "null";
}
final BigDecimal leftNumberValue = EvaluationUtils.evaluateAsNumber(leftValue);
final BigDecimal rightNumberValue = EvaluationUtils.evaluateAsNumber(rightValue);
if (leftNumberValue != null && rightNumberValue != null) {
// Addition will act as a mathematical 'plus'
return leftNumberValue.multiply(rightNumberValue);
}
throw new TemplateProcessingException("Cannot execute multiplication: operands are \"" + LiteralValue.unwrap(leftValue) + "\" and \"" + LiteralValue.unwrap(rightValue) + "\"");
}
use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.
the class StandardWithTagProcessor method doProcess.
@Override
protected void doProcess(final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue, final IElementTagStructureHandler structureHandler) {
final AssignationSequence assignations = AssignationUtils.parseAssignationSequence(context, attributeValue, false);
if (assignations == null) {
throw new TemplateProcessingException("Could not parse value as attribute assignations: \"" + attributeValue + "\"");
}
// Normally we would just allow the structure handler to be in charge of declaring the local variables
// by using structureHandler.setLocalVariable(...) but in this case we want each variable defined at an
// expression to be available for the next expressions, and that forces us to cast our ITemplateContext into
// a more specific interface --which shouldn't be used directly except in this specific, special case-- and
// put the local variables directly into it.
IEngineContext engineContext = null;
if (context instanceof IEngineContext) {
// NOTE this interface is internal and should not be used in users' code
engineContext = (IEngineContext) context;
}
final List<Assignation> assignationValues = assignations.getAssignations();
final int assignationValuesLen = assignationValues.size();
for (int i = 0; i < assignationValuesLen; i++) {
final Assignation assignation = assignationValues.get(i);
final IStandardExpression leftExpr = assignation.getLeft();
final Object leftValue = leftExpr.execute(context);
final IStandardExpression rightExpr = assignation.getRight();
final Object rightValue = rightExpr.execute(context);
final String newVariableName = (leftValue == null ? null : leftValue.toString());
if (StringUtils.isEmptyOrWhitespace(newVariableName)) {
throw new TemplateProcessingException("Variable name expression evaluated as null or empty: \"" + leftExpr + "\"");
}
if (engineContext != null) {
// The advantage of this vs. using the structure handler is that we will be able to
// use this newly created value in other expressions in the same 'th:with'
engineContext.setVariable(newVariableName, rightValue);
} else {
// The problem is, these won't be available until we execute the next processor
structureHandler.setLocalVariable(newVariableName, rightValue);
}
}
}
use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.
the class AssignationUtils method parseAssignationSequence.
public static AssignationSequence parseAssignationSequence(final IExpressionContext context, final String input, final boolean allowParametersWithoutValue) {
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 AssignationSequence cachedAssignationSequence = ExpressionCache.getAssignationSequenceFromCache(configuration, preprocessedInput);
if (cachedAssignationSequence != null) {
return cachedAssignationSequence;
}
}
final AssignationSequence assignationSequence = internalParseAssignationSequence(preprocessedInput.trim(), allowParametersWithoutValue);
if (assignationSequence == null) {
throw new TemplateProcessingException("Could not parse as assignation sequence: \"" + input + "\"");
}
if (configuration != null) {
ExpressionCache.putAssignationSequenceIntoCache(configuration, preprocessedInput, assignationSequence);
}
return assignationSequence;
}
Aggregations