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.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   * The processor class for handling the {@code mybatis:bind} tag. <br>
37   * This processor register an any value to the MyBatis’s bind variables (similar to that of the {@code <bind>} provided
38   * by MyBatis core module) This class's implementation was inspired with the {@code StandardWithTagProcessor} provide by
39   * Thymeleaf.
40   *
41   * @author Kazuki Shimizu
42   *
43   * @version 1.0.0
44   *
45   * @see org.thymeleaf.standard.processor.StandardWithTagProcessor
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     * Constructor that can be specified the template mode and dialect prefix.
54     *
55     * @param templateMode
56     *          A target template mode
57     * @param prefix
58     *          A target dialect prefix
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     * {@inheritDoc}
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  }