use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.
the class TemplateEngine method processThrottled.
public final IThrottledTemplateProcessor processThrottled(final TemplateSpec templateSpec, final IContext context) {
if (!this.initialized) {
initialize();
}
final IThrottledTemplateProcessor throttledTemplateProcessor;
try {
Validate.notNull(templateSpec, "Template Specification cannot be null");
Validate.notNull(context, "Context cannot be null");
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] STARTING PREPARATION OF THROTTLED TEMPLATE \"{}\" WITH LOCALE {}", new Object[] { TemplateEngine.threadIndex(), templateSpec, context.getLocale() });
}
final long startNanos = System.nanoTime();
final TemplateManager templateManager = this.configuration.getTemplateManager();
throttledTemplateProcessor = templateManager.parseAndProcessThrottled(templateSpec, context);
final long endNanos = System.nanoTime();
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] FINISHED PREPARATION OF THROTTLED TEMPLATE \"{}\" WITH LOCALE {}", new Object[] { TemplateEngine.threadIndex(), templateSpec, context.getLocale() });
}
if (timerLogger.isTraceEnabled()) {
final BigDecimal elapsed = BigDecimal.valueOf(endNanos - startNanos);
final BigDecimal elapsedMs = elapsed.divide(BigDecimal.valueOf(NANOS_IN_SECOND), RoundingMode.HALF_UP);
timerLogger.trace("[THYMELEAF][{}][{}][{}][{}][{}] TEMPLATE \"{}\" WITH LOCALE {} PREPARED FOR THROTTLED PROCESSING IN {} nanoseconds (approx. {}ms)", new Object[] { TemplateEngine.threadIndex(), LoggingUtils.loggifyTemplateName(templateSpec.getTemplate()), context.getLocale(), elapsed, elapsedMs, templateSpec, context.getLocale(), elapsed, elapsedMs });
}
} catch (final TemplateOutputException e) {
// We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
logger.error(String.format("[THYMELEAF][%s] Exception preparing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
throw e;
} catch (final TemplateEngineException e) {
// We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
logger.error(String.format("[THYMELEAF][%s] Exception preparing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
throw e;
} catch (final RuntimeException e) {
// We log the exception just in case higher levels do not end up logging it (e.g. they could simply display traces in the browser
logger.error(String.format("[THYMELEAF][%s] Exception preparing throttled template \"%s\": %s", new Object[] { TemplateEngine.threadIndex(), templateSpec, e.getMessage() }), e);
throw new TemplateProcessingException("Exception preparing throttled template", templateSpec.toString(), e);
}
return throttledTemplateProcessor;
}
use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.
the class MessageExpression method executeMessageExpression.
static Object executeMessageExpression(final IExpressionContext context, final MessageExpression expression, final StandardExpressionExecutionContext expContext) {
if (logger.isTraceEnabled()) {
logger.trace("[THYMELEAF][{}] Evaluating message: \"{}\"", TemplateEngine.threadIndex(), expression.getStringRepresentation());
}
if (!(context instanceof ITemplateContext)) {
throw new TemplateProcessingException("Cannot evaluate expression \"" + expression + "\". Message externalization 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();
Object messageKey = baseExpression.execute(templateContext, expContext);
messageKey = LiteralValue.unwrap(messageKey);
if (messageKey != null && !(messageKey instanceof String)) {
messageKey = messageKey.toString();
}
if (StringUtils.isEmptyOrWhitespace((String) messageKey)) {
throw new TemplateProcessingException("Message key for message resolution must be a non-null and non-empty String");
}
final Object[] messageParameters;
if (expression.hasParameters()) {
final ExpressionSequence parameterExpressionSequence = expression.getParameters();
final List<IStandardExpression> parameterExpressionValues = parameterExpressionSequence.getExpressions();
final int parameterExpressionValuesLen = parameterExpressionValues.size();
messageParameters = new Object[parameterExpressionValuesLen];
for (int i = 0; i < parameterExpressionValuesLen; i++) {
final IStandardExpression parameterExpression = parameterExpressionValues.get(i);
final Object result = parameterExpression.execute(templateContext, expContext);
messageParameters[i] = LiteralValue.unwrap(result);
}
} else {
messageParameters = NO_PARAMETERS;
}
// Note message expressions will always return an absent representation if message does not exist
return templateContext.getMessage(null, (String) messageKey, messageParameters, true);
}
use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.
the class AbstractStandardMultipleAttributeModifierTagProcessor method doProcess.
@Override
protected final 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 + "\"");
}
// Compute the required execution context depending on whether execution should be restricted or not
final StandardExpressionExecutionContext expCtx = (this.restrictedExpressionExecution ? StandardExpressionExecutionContext.RESTRICTED : StandardExpressionExecutionContext.NORMAL);
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, expCtx);
final IStandardExpression rightExpr = assignation.getRight();
final Object rightValue = rightExpr.execute(context, expCtx);
if (rightValue == NoOpToken.VALUE) {
// No changes to be done for this attribute
continue;
}
final String newAttributeName = (leftValue == null ? null : leftValue.toString());
if (StringUtils.isEmptyOrWhitespace(newAttributeName)) {
throw new TemplateProcessingException("Attribute name expression evaluated as null or empty: \"" + leftExpr + "\"");
}
if (getTemplateMode() == TemplateMode.HTML && this.modificationType == ModificationType.SUBSTITUTION && ArrayUtils.contains(StandardConditionalFixedValueTagProcessor.ATTR_NAMES, newAttributeName)) {
if (EvaluationUtils.evaluateAsBoolean(rightValue)) {
structureHandler.setAttribute(newAttributeName, newAttributeName);
} else {
structureHandler.removeAttribute(newAttributeName);
}
} else {
// Attribute is a "normal" attribute, not a fixed-value conditional one - or we are not just replacing
final String newAttributeValue = EscapedAttributeUtils.escapeAttribute(getTemplateMode(), rightValue == null ? null : rightValue.toString());
if (newAttributeValue == null || newAttributeValue.length() == 0) {
if (this.modificationType == ModificationType.SUBSTITUTION) {
// Substituting by a no-value will be equivalent to simply removing
structureHandler.removeAttribute(newAttributeName);
}
// Prepend and append simply ignored in this case
} else {
if (this.modificationType == ModificationType.SUBSTITUTION || !tag.hasAttribute(newAttributeName) || tag.getAttributeValue(newAttributeName).length() == 0) {
// Normal value replace
structureHandler.setAttribute(newAttributeName, newAttributeValue);
} else {
String currentValue = tag.getAttributeValue(newAttributeName);
if (this.modificationType == ModificationType.APPEND) {
structureHandler.setAttribute(newAttributeName, currentValue + newAttributeValue);
} else if (this.modificationType == ModificationType.APPEND_WITH_SPACE) {
structureHandler.setAttribute(newAttributeName, currentValue + ' ' + newAttributeValue);
} else if (this.modificationType == ModificationType.PREPEND) {
structureHandler.setAttribute(newAttributeName, newAttributeValue + currentValue);
} else {
// modification type is PREPEND_WITH_SPACE
structureHandler.setAttribute(newAttributeName, newAttributeValue + ' ' + currentValue);
}
}
}
}
}
}
use of org.thymeleaf.exceptions.TemplateProcessingException 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;
}
use of org.thymeleaf.exceptions.TemplateProcessingException in project thymeleaf by thymeleaf.
the class ExpressionSequenceUtils method parseExpressionSequence.
public static ExpressionSequence parseExpressionSequence(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 ExpressionSequence cachedExpressionSequence = ExpressionCache.getExpressionSequenceFromCache(configuration, preprocessedInput);
if (cachedExpressionSequence != null) {
return cachedExpressionSequence;
}
}
final ExpressionSequence expressionSequence = internalParseExpressionSequence(preprocessedInput.trim());
if (expressionSequence == null) {
throw new TemplateProcessingException("Could not parse as expression sequence: \"" + input + "\"");
}
if (configuration != null) {
ExpressionCache.putExpressionSequenceIntoCache(configuration, preprocessedInput, expressionSequence);
}
return expressionSequence;
}
Aggregations