use of org.thymeleaf.standard.expression.IStandardExpressionParser in project thymeleaf by thymeleaf.
the class StandardDefaultAttributesTagProcessor method processDefaultAttribute.
private static void processDefaultAttribute(final TemplateMode templateMode, final ITemplateContext context, final IProcessableElementTag tag, final IAttribute attribute, final IElementTagStructureHandler structureHandler) {
try {
final AttributeName attributeName = attribute.getAttributeDefinition().getAttributeName();
final String attributeValue = EscapedAttributeUtils.unescapeAttribute(context.getTemplateMode(), attribute.getValue());
/*
* Compute the new attribute name (i.e. the same, without the prefix)
*/
final String originalCompleteAttributeName = attribute.getAttributeCompleteName();
final String canonicalAttributeName = attributeName.getAttributeName();
final String newAttributeName;
if (TextUtil.endsWith(true, originalCompleteAttributeName, canonicalAttributeName)) {
// We avoid creating a new String instance
newAttributeName = canonicalAttributeName;
} else {
newAttributeName = originalCompleteAttributeName.substring(originalCompleteAttributeName.length() - canonicalAttributeName.length());
}
/*
* Obtain the parser
*/
final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(context.getConfiguration());
/*
* Execute the expression, handling nulls in a way consistent with the rest of the Standard Dialect
*/
final Object expressionResult;
if (attributeValue != null) {
final IStandardExpression expression = expressionParser.parseExpression(context, attributeValue);
if (expression != null && expression instanceof FragmentExpression) {
// This is merely a FragmentExpression (not complex, not combined with anything), so we can apply a shortcut
// so that we don't require a "null" result for this expression if the template does not exist. That will
// save a call to resource.exists() which might be costly.
final FragmentExpression.ExecutedFragmentExpression executedFragmentExpression = FragmentExpression.createExecutedFragmentExpression(context, (FragmentExpression) expression);
expressionResult = FragmentExpression.resolveExecutedFragmentExpression(context, executedFragmentExpression, true);
} else {
// Default attributes will ALWAYS be executed in RESTRICTED mode, for safety reasons (they might
// create attributes involved in code execution)
expressionResult = expression.execute(context, StandardExpressionExecutionContext.RESTRICTED);
}
} else {
expressionResult = null;
}
/*
* If the result of this expression is NO-OP, there is nothing to execute
*/
if (expressionResult == NoOpToken.VALUE) {
structureHandler.removeAttribute(attributeName);
return;
}
/*
* Compute the new attribute value
*/
final String newAttributeValue = EscapedAttributeUtils.escapeAttribute(templateMode, expressionResult == null ? null : expressionResult.toString());
/*
* Set the new value, removing the attribute completely if the expression evaluated to null
*/
if (newAttributeValue == null || newAttributeValue.length() == 0) {
// We are removing the equivalent attribute name, without the prefix...
structureHandler.removeAttribute(newAttributeName);
structureHandler.removeAttribute(attributeName);
} else {
// We are setting the equivalent attribute name, without the prefix...
structureHandler.replaceAttribute(attributeName, newAttributeName, (newAttributeValue == null ? "" : newAttributeValue));
}
} catch (final TemplateProcessingException e) {
// specific because we know exactly what attribute was being executed and caused the error
if (!e.hasTemplateName()) {
e.setTemplateName(tag.getTemplateName());
}
if (!e.hasLineAndCol()) {
e.setLineAndCol(attribute.getLine(), attribute.getCol());
}
throw e;
} catch (final Exception e) {
throw new TemplateProcessingException("Error during execution of processor '" + StandardDefaultAttributesTagProcessor.class.getName() + "'", tag.getTemplateName(), attribute.getLine(), attribute.getCol(), e);
}
}
use of org.thymeleaf.standard.expression.IStandardExpressionParser in project thymeleaf by thymeleaf.
the class StandardIfTagProcessor method isVisible.
@Override
protected boolean isVisible(final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue) {
final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(context.getConfiguration());
final IStandardExpression expression = expressionParser.parseExpression(context, attributeValue);
final Object value = expression.execute(context);
return EvaluationUtils.evaluateAsBoolean(value);
}
use of org.thymeleaf.standard.expression.IStandardExpressionParser in project thymeleaf by thymeleaf.
the class StandardUnlessTagProcessor method isVisible.
@Override
protected boolean isVisible(final ITemplateContext context, final IProcessableElementTag tag, final AttributeName attributeName, final String attributeValue) {
final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(context.getConfiguration());
final IStandardExpression expression = expressionParser.parseExpression(context, attributeValue);
final Object value = expression.execute(context);
return !EvaluationUtils.evaluateAsBoolean(value);
}
use of org.thymeleaf.standard.expression.IStandardExpressionParser 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.standard.expression.IStandardExpressionParser in project sling by apache.
the class SlingIncludeAttributeTagProcessor method doProcess.
@Override
protected void doProcess(final ITemplateContext templateContext, final IProcessableElementTag processableElementTag, final AttributeName attributeName, final String attributeValue, final IElementTagStructureHandler elementTagStructureHandler) {
try {
final SlingHttpServletRequest slingHttpServletRequest = (SlingHttpServletRequest) templateContext.getVariable(SlingBindings.REQUEST);
final SlingHttpServletResponse slingHttpServletResponse = (SlingHttpServletResponse) templateContext.getVariable(SlingBindings.RESPONSE);
final IEngineConfiguration configuration = templateContext.getConfiguration();
final IStandardExpressionParser expressionParser = StandardExpressions.getExpressionParser(configuration);
final IStandardExpression expression = expressionParser.parseExpression(templateContext, attributeValue);
final Object include = expression.execute(templateContext);
String path = null;
if (include instanceof String) {
path = (String) include;
}
Resource resource = null;
if (include instanceof Resource) {
resource = (Resource) include;
}
// request dispatcher options
final RequestDispatcherOptions requestDispatcherOptions = prepareRequestDispatcherOptions(expressionParser, templateContext, processableElementTag, elementTagStructureHandler);
// dispatch
final String content = dispatch(resource, path, slingHttpServletRequest, slingHttpServletResponse, requestDispatcherOptions);
// add output
final Boolean unwrap = (Boolean) parseAttribute(expressionParser, templateContext, processableElementTag, elementTagStructureHandler, UNWRAP_ATTRIBUTE_NAME);
if (unwrap != null && unwrap) {
elementTagStructureHandler.replaceWith(content, false);
} else {
elementTagStructureHandler.setBody(content, false);
}
} catch (Exception e) {
throw new RuntimeException("unable to process include attribute", e);
}
}
Aggregations