use of org.thymeleaf.standard.expression.Assignation 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.standard.expression.Assignation 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.standard.expression.Assignation in project thymeleaf-tests by thymeleaf.
the class AddLocalVariableToResult method processAttribute.
@Override
protected ProcessorResult processAttribute(final Arguments arguments, final Element element, final String attributeName) {
final String attributeValue = element.getAttributeValue(attributeName);
final Configuration configuration = arguments.getConfiguration();
final AssignationSequence assignationSequence = AssignationUtils.parseAssignationSequence(configuration, arguments, attributeValue, false);
final Map<String, Object> localVariables = new HashMap<String, Object>();
for (final Assignation assignation : assignationSequence.getAssignations()) {
final IStandardExpression varNameExpr = assignation.getLeft();
final IStandardExpression varValueExpr = assignation.getRight();
final Object varName = varNameExpr.execute(configuration, arguments);
final Object varValue = varValueExpr.execute(configuration, arguments);
localVariables.put((varName == null ? null : varName.toString()), varValue);
}
element.removeAttribute(attributeName);
return ProcessorResult.setLocalVariables(localVariables);
}
use of org.thymeleaf.standard.expression.Assignation in project thymeleaf-tests by thymeleaf.
the class AddLocalVariableToResult method processAttribute.
@Override
protected ProcessorResult processAttribute(final Arguments arguments, final Element element, final String attributeName) {
final String attributeValue = element.getAttributeValue(attributeName);
final AssignationSequence assignationSequence = StandardExpressionProcessor.parseAssignationSequence(arguments, attributeValue, false);
final Map<String, Object> localVariables = new HashMap<String, Object>();
for (final Assignation assignation : assignationSequence.getAssignations()) {
final String varName = assignation.getLeft().getValue();
final Expression varValueExpr = assignation.getRight();
final Object varValue = StandardExpressionProcessor.executeExpression(arguments, varValueExpr);
localVariables.put(varName, varValue);
}
element.removeAttribute(attributeName);
return ProcessorResult.setLocalVariables(localVariables);
}
use of org.thymeleaf.standard.expression.Assignation in project thymeleaf-tests by thymeleaf.
the class AddLocalVariableToNode method processAttribute.
@Override
protected ProcessorResult processAttribute(final Arguments arguments, final Element element, final String attributeName) {
final String attributeValue = element.getAttributeValue(attributeName);
final AssignationSequence assignationSequence = StandardExpressionProcessor.parseAssignationSequence(arguments, attributeValue, false);
for (final Assignation assignation : assignationSequence.getAssignations()) {
final String varName = assignation.getLeft().getValue();
final Expression varValueExpr = assignation.getRight();
final Object varValue = StandardExpressionProcessor.executeExpression(arguments, varValueExpr);
element.setNodeLocalVariable(varName, varValue);
}
element.removeAttribute(attributeName);
return ProcessorResult.OK;
}
Aggregations