1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.scripting.thymeleaf.processor;
17
18 import java.util.List;
19 import java.util.Objects;
20
21 import org.mybatis.scripting.thymeleaf.MyBatisBindingContext;
22 import org.thymeleaf.context.ITemplateContext;
23 import org.thymeleaf.engine.AttributeName;
24 import org.thymeleaf.exceptions.TemplateProcessingException;
25 import org.thymeleaf.model.IProcessableElementTag;
26 import org.thymeleaf.processor.element.AbstractAttributeTagProcessor;
27 import org.thymeleaf.processor.element.IElementTagStructureHandler;
28 import org.thymeleaf.standard.expression.Assignation;
29 import org.thymeleaf.standard.expression.AssignationSequence;
30 import org.thymeleaf.standard.expression.AssignationUtils;
31 import org.thymeleaf.standard.expression.IStandardExpression;
32 import org.thymeleaf.templatemode.TemplateMode;
33 import org.thymeleaf.util.StringUtils;
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class MyBatisBindTagProcessor extends AbstractAttributeTagProcessor {
48
49 private static final int PRECEDENCE = 600;
50 private static final String ATTR_NAME = "bind";
51
52
53
54
55
56
57
58
59
60 public MyBatisBindTagProcessor(final TemplateMode templateMode, final String prefix) {
61 super(templateMode, prefix, null, false, ATTR_NAME, true, PRECEDENCE, true);
62 }
63
64
65
66
67 @Override
68 protected void doProcess(ITemplateContext context, IProcessableElementTag tag, AttributeName attributeName,
69 String attributeValue, IElementTagStructureHandler structureHandler) {
70 AssignationSequence assignations = AssignationUtils.parseAssignationSequence(context, attributeValue, false);
71
72 List<Assignation> assignationValues = assignations.getAssignations();
73 assignationValues.forEach(assignation -> {
74 IStandardExpression nameExp = assignation.getLeft();
75 Object name = nameExp.execute(context);
76 IStandardExpression valueExp = assignation.getRight();
77 Object value = valueExp.execute(context);
78
79 if (Objects.isNull(name) || StringUtils.isEmpty(name.toString())) {
80 throw new TemplateProcessingException(
81 "Variable name expression evaluated as null or empty: \"" + nameExp + "\"");
82 }
83
84 MyBatisBindingContext bindingContext = MyBatisBindingContext.load(context);
85 bindingContext.setCustomBindVariable(name.toString(), value);
86 });
87 }
88
89 }