1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mybatis.scripting.thymeleaf;
17
18 import java.util.Arrays;
19 import java.util.Collections;
20 import java.util.HashSet;
21 import java.util.Optional;
22 import java.util.Set;
23
24 import org.mybatis.scripting.thymeleaf.expression.Likes;
25 import org.mybatis.scripting.thymeleaf.processor.BindVariableRender;
26 import org.mybatis.scripting.thymeleaf.processor.MyBatisBindTagProcessor;
27 import org.mybatis.scripting.thymeleaf.processor.MyBatisParamTagProcessor;
28 import org.thymeleaf.context.IExpressionContext;
29 import org.thymeleaf.dialect.AbstractProcessorDialect;
30 import org.thymeleaf.dialect.IExpressionObjectDialect;
31 import org.thymeleaf.expression.IExpressionObjectFactory;
32 import org.thymeleaf.processor.IProcessor;
33 import org.thymeleaf.standard.StandardDialect;
34 import org.thymeleaf.templatemode.TemplateMode;
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 public class MyBatisDialect extends AbstractProcessorDialect implements IExpressionObjectDialect {
50
51 private static final String DEFAULT_PREFIX = "mb";
52
53 private Likes likes = Likes.newBuilder().build();
54
55 private BindVariableRender bindVariableRender;
56
57
58
59
60 public MyBatisDialect() {
61 this(DEFAULT_PREFIX);
62 }
63
64
65
66
67
68
69
70 public MyBatisDialect(String prefix) {
71 super("MyBatis Dialect", prefix, StandardDialect.PROCESSOR_PRECEDENCE);
72 }
73
74
75
76
77
78
79
80 public void setLikes(Likes likes) {
81 this.likes = likes;
82 }
83
84
85
86
87
88
89
90
91
92 public void setBindVariableRender(BindVariableRender bindVariableRender) {
93 this.bindVariableRender = bindVariableRender;
94 }
95
96
97
98
99 @Override
100 public Set<IProcessor> getProcessors(String dialectPrefix) {
101 return new HashSet<>(Arrays.asList(new MyBatisBindTagProcessor(TemplateMode.TEXT, dialectPrefix),
102 new MyBatisBindTagProcessor(TemplateMode.CSS, dialectPrefix),
103 configure(new MyBatisParamTagProcessor(TemplateMode.TEXT, dialectPrefix)),
104 configure(new MyBatisParamTagProcessor(TemplateMode.CSS, dialectPrefix))));
105 }
106
107 private MyBatisParamTagProcessor configure(MyBatisParamTagProcessor processor) {
108 Optional.ofNullable(bindVariableRender).ifPresent(processor::setBindVariableRender);
109 return processor;
110 }
111
112
113
114
115 @Override
116 public IExpressionObjectFactory getExpressionObjectFactory() {
117 return new MyBatisExpressionObjectFactory();
118 }
119
120 private class MyBatisExpressionObjectFactory implements IExpressionObjectFactory {
121
122
123
124
125 @Override
126 public Set<String> getAllExpressionObjectNames() {
127 return Collections.singleton("likes");
128 }
129
130
131
132
133 @Override
134 public Object buildObject(IExpressionContext context, String expressionObjectName) {
135 return likes;
136 }
137
138
139
140
141 @Override
142 public boolean isCacheable(String expressionObjectName) {
143 return true;
144 }
145
146 }
147
148 }