View Javadoc
1   /*
2    *    Copyright 2018-2022 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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   * The Dialect for integrating with MyBatis. <br>
38   * This dialect provides following features. This dialect prefix is {@code "mb"} by default.
39   * <ul>
40   * <li>{@code #likes} expression : {@link Likes}</li>
41   * <li>{@code mb:p} attribute tag: {@link MyBatisParamTagProcessor}</li>
42   * <li>{@code mb:bind} attribute tag : {@link MyBatisBindTagProcessor}</li>
43   * </ul>
44   *
45   * @author Kazuki Shimizu
46   *
47   * @version 1.0.0
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     * Default constructor.
59     */
60    public MyBatisDialect() {
61      this(DEFAULT_PREFIX);
62    }
63  
64    /**
65     * Constructor that can be specified the dialect prefix.
66     *
67     * @param prefix
68     *          A dialect prefix
69     */
70    public MyBatisDialect(String prefix) {
71      super("MyBatis Dialect", prefix, StandardDialect.PROCESSOR_PRECEDENCE);
72    }
73  
74    /**
75     * Set an expression utility object that provide helper method for like feature. <br>
76     *
77     * @param likes
78     *          An expression utility object that provide helper method for like feature
79     */
80    public void setLikes(Likes likes) {
81      this.likes = likes;
82    }
83  
84    /**
85     * Set a bind variable render.
86     *
87     * @param bindVariableRender
88     *          a bind variable render
89     *
90     * @since 1.0.2
91     */
92    public void setBindVariableRender(BindVariableRender bindVariableRender) {
93      this.bindVariableRender = bindVariableRender;
94    }
95  
96    /**
97     * {@inheritDoc}
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    * {@inheritDoc}
114    */
115   @Override
116   public IExpressionObjectFactory getExpressionObjectFactory() {
117     return new MyBatisExpressionObjectFactory();
118   }
119 
120   private class MyBatisExpressionObjectFactory implements IExpressionObjectFactory {
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public Set<String> getAllExpressionObjectNames() {
127       return Collections.singleton("likes");
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public Object buildObject(IExpressionContext context, String expressionObjectName) {
135       return likes;
136     }
137 
138     /**
139      * {@inheritDoc}
140      */
141     @Override
142     public boolean isCacheable(String expressionObjectName) {
143       return true;
144     }
145 
146   }
147 
148 }