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.Optional;
19  import java.util.function.Consumer;
20  
21  import org.thymeleaf.TemplateEngine;
22  import org.thymeleaf.templateresolver.ITemplateResolver;
23  
24  /**
25   * The interface for customizing a default {@code TemplateEngine} instanced by the MyBatis Thymeleaf. <br>
26   * If you want to customize a default {@code TemplateEngine}, you implements class of this interface and you need to
27   * specify the 'customizer' property of mybatis-thymeleaf.properties. <br>
28   * <br>
29   * e.g.) Implementation class:
30   *
31   * <pre>
32   * package com.example;
33   *
34   * // ...
35   * public class MyTemplateEngineCustomizer implements TemplateEngineCustomizer {
36   *   public void customize(TemplateEngine defaultTemplateEngine) {
37   *     // ...
38   *   }
39   * }
40   * </pre>
41   *
42   * <br>
43   * e.g.) Configuration file (mybatis-thymeleaf.properties):
44   *
45   * <pre>
46   * customizer = com.example.MyTemplateEngineCustomizer
47   * </pre>
48   *
49   * @author Kazuki Shimizu
50   *
51   * @version 1.0.0
52   */
53  @FunctionalInterface
54  public interface TemplateEngineCustomizer extends Consumer<TemplateEngine> {
55  
56    /**
57     * {@inheritDoc}
58     *
59     * @see #customize(TemplateEngine)
60     */
61    @Override
62    default void accept(TemplateEngine templateEngine) {
63      customize(templateEngine);
64    }
65  
66    /**
67     * Customize a default {@code TemplateEngine} instanced by the MyBatis Thymeleaf.
68     *
69     * @param defaultTemplateEngine
70     *          a default {@code TemplateEngine} instanced by the MyBatis Thymeleaf
71     */
72    void customize(TemplateEngine defaultTemplateEngine);
73  
74    /**
75     * Utility method to extract a {@code ITemplateResolver} that implements specified type.
76     *
77     * @param templateEngine
78     *          A target {@code TemplateEngine}
79     * @param type
80     *          A target type for extracting instance
81     * @param <T>
82     *          A type that implements the {@code ITemplateResolver}
83     *
84     * @return A {@code ITemplateResolver} instance that implements specified type
85     */
86    static <T extends ITemplateResolver> Optional<T> extractTemplateResolver(TemplateEngine templateEngine,
87        Class<T> type) {
88      return templateEngine.getTemplateResolvers().stream().filter(type::isInstance).map(type::cast).findFirst();
89    }
90  
91    /**
92     * Enum for providing builtin customizer instance.
93     *
94     * @since 1.0.0
95     */
96    enum BuiltIn implements TemplateEngineCustomizer {
97  
98      /**
99       * Default instance.
100      * <p>
101      * This customizer instance do nothing.
102      * </p>
103      */
104     DO_NOTHING {
105       @Override
106       public void customize(TemplateEngine defaultTemplateEngine) {
107         // NOP
108       }
109     }
110 
111   }
112 
113 }