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.Properties;
19  import java.util.function.Consumer;
20  
21  /**
22   * Configuration class for {@link ThymeleafLanguageDriver}.
23   *
24   * @author Kazuki Shimizu
25   *
26   * @since 1.0.0
27   */
28  public class ThymeleafLanguageDriverConfig extends SqlGeneratorConfig {
29  
30    /**
31     * Template file configuration.
32     */
33    private final TemplateFileConfig templateFile = new TemplateFileConfig();
34  
35    @Override
36    public TemplateFileConfig getTemplateFile() {
37      return templateFile;
38    }
39  
40    /**
41     * Template file configuration for language driver of the MyBatis.
42     *
43     * @since 1.0.2
44     */
45    public static class TemplateFileConfig extends SqlGeneratorConfig.TemplateFileConfig {
46  
47      /**
48       * The template file path provider configuration.
49       */
50      private final PathProviderConfig pathProvider = new PathProviderConfig();
51  
52      /**
53       * Get the template file path provider configuration.
54       *
55       * @return the template file path provider configuration
56       *
57       * @since 1.0.1
58       */
59      public PathProviderConfig getPathProvider() {
60        return pathProvider;
61      }
62  
63      /**
64       * The template file path provider configuration.
65       *
66       * @since 1.0.1
67       */
68      public static class PathProviderConfig {
69  
70        /**
71         * The prefix for adding to template file path.
72         */
73        private String prefix = "";
74  
75        /**
76         * Whether includes package path part.
77         */
78        private boolean includesPackagePath = true;
79  
80        /**
81         * Whether separate directory per mapper.
82         */
83        private boolean separateDirectoryPerMapper = true;
84  
85        /**
86         * Whether includes mapper name into file name when separate directory per mapper.
87         */
88        private boolean includesMapperNameWhenSeparateDirectory = true;
89  
90        /**
91         * Whether cache a resolved template file path.
92         */
93        private boolean cacheEnabled = true;
94  
95        /**
96         * Get a prefix for adding to template file path.
97         * <p>
98         * Default is {@code ""}.
99         * </p>
100        *
101        * @return a prefix for adding to template file path
102        */
103       public String getPrefix() {
104         return prefix;
105       }
106 
107       /**
108        * Set the prefix for adding to template file path.
109        *
110        * @param prefix
111        *          The prefix for adding to template file path
112        */
113       public void setPrefix(String prefix) {
114         this.prefix = prefix;
115       }
116 
117       /**
118        * Get whether includes package path part.
119        * <p>
120        * Default is {@code true}.
121        * </p>
122        *
123        * @return If includes package path, return {@code true}
124        */
125       public boolean isIncludesPackagePath() {
126         return includesPackagePath;
127       }
128 
129       /**
130        * Set whether includes package path part.
131        *
132        * @param includesPackagePath
133        *          If want to includes, set {@code true}
134        */
135       public void setIncludesPackagePath(boolean includesPackagePath) {
136         this.includesPackagePath = includesPackagePath;
137       }
138 
139       /**
140        * Get whether separate directory per mapper.
141        *
142        * @return If separate directory per mapper, return {@code true}
143        */
144       public boolean isSeparateDirectoryPerMapper() {
145         return separateDirectoryPerMapper;
146       }
147 
148       /**
149        * Set whether separate directory per mapper.
150        * <p>
151        * Default is {@code true}.
152        * </p>
153        *
154        * @param separateDirectoryPerMapper
155        *          If want to separate directory, set {@code true}
156        */
157       public void setSeparateDirectoryPerMapper(boolean separateDirectoryPerMapper) {
158         this.separateDirectoryPerMapper = separateDirectoryPerMapper;
159       }
160 
161       /**
162        * Get whether includes mapper name into file name when separate directory per mapper.
163        * <p>
164        * Default is {@code true}.
165        * </p>
166        *
167        * @return If includes mapper name, return {@code true}
168        */
169       public boolean isIncludesMapperNameWhenSeparateDirectory() {
170         return includesMapperNameWhenSeparateDirectory;
171       }
172 
173       /**
174        * Set whether includes mapper name into file name when separate directory per mapper.
175        * <p>
176        * Default is {@code true}.
177        * </p>
178        *
179        * @param includesMapperNameWhenSeparateDirectory
180        *          If want to includes, set {@code true}
181        */
182       public void setIncludesMapperNameWhenSeparateDirectory(boolean includesMapperNameWhenSeparateDirectory) {
183         this.includesMapperNameWhenSeparateDirectory = includesMapperNameWhenSeparateDirectory;
184       }
185 
186       /**
187        * Get whether cache a resolved template file path.
188        * <p>
189        * Default is {@code true}.
190        * </p>
191        *
192        * @return If cache a resolved template file path, return {@code true}
193        */
194       public boolean isCacheEnabled() {
195         return cacheEnabled;
196       }
197 
198       /**
199        * Set whether cache a resolved template file path.
200        *
201        * @param cacheEnabled
202        *          If want to cache, set {@code true}
203        */
204       public void setCacheEnabled(boolean cacheEnabled) {
205         this.cacheEnabled = cacheEnabled;
206       }
207 
208     }
209 
210   }
211 
212   /**
213    * Create an instance from default properties file. <br>
214    * If you want to customize a default {@code TemplateEngine}, you can configure some property using
215    * mybatis-thymeleaf.properties that encoded by UTF-8. Also, you can change the properties file that will read using
216    * system property (-Dmybatis-thymeleaf.config.file=... -Dmybatis-thymeleaf.config.encoding=...). <br>
217    * About supported common properties see {@link SqlGeneratorConfig#newInstance()}. Supported specific properties are
218    * as follows:
219    * <table border="1">
220    * <caption>Supported properties</caption>
221    * <tr>
222    * <th>Property Key</th>
223    * <th>Description</th>
224    * <th>Default</th>
225    * </tr>
226    * <tr>
227    * <th colspan="3">Template file path provider configuration(TemplateFilePathProvider)</th>
228    * </tr>
229    * <tr>
230    * <td>template-file.path-provider.prefix</td>
231    * <td>The prefix for adding to template file path</td>
232    * <td>{@code ""}</td>
233    * </tr>
234    * <tr>
235    * <td>template-file.path-provider.includes-package-path</td>
236    * <td>Whether includes package path part</td>
237    * <td>{@code true}</td>
238    * </tr>
239    * <tr>
240    * <td>template-file.path-provider.separate-directory-per-mapper</td>
241    * <td>Whether separate directory per mapper</td>
242    * <td>{@code true}</td>
243    * </tr>
244    * <tr>
245    * <td>template-file.path-provider.includes-mapper-name-when-separate-directory</td>
246    * <td>Whether includes mapper name into file name when separate directory per mapper</td>
247    * <td>{@code true}</td>
248    * </tr>
249    * <tr>
250    * <td>template-file.path-provider.cache-enabled</td>
251    * <td>Whether cache a resolved template file path</td>
252    * <td>{@code true}</td>
253    * </tr>
254    * </table>
255    *
256    * @return a configuration instance
257    *
258    * @see SqlGeneratorConfig#newInstance()
259    */
260   public static ThymeleafLanguageDriverConfig newInstance() {
261     ThymeleafLanguageDriverConfig config = new ThymeleafLanguageDriverConfig();
262     applyDefaultProperties(config);
263     return config;
264   }
265 
266   /**
267    * Create an instance from specified properties file. <br>
268    * you can configure some property using specified properties file that encoded by UTF-8. Also, you can change file
269    * encoding that will read using system property (-Dmybatis-thymeleaf.config.encoding=...).
270    *
271    * @param resourcePath
272    *          A property file resource path
273    *
274    * @return a configuration instance
275    *
276    * @see #newInstance()
277    * @see SqlGeneratorConfig#newInstance()
278    */
279   public static ThymeleafLanguageDriverConfig newInstance(String resourcePath) {
280     ThymeleafLanguageDriverConfig config = new ThymeleafLanguageDriverConfig();
281     applyResourcePath(config, resourcePath);
282     return config;
283   }
284 
285   /**
286    * Create an instance from specified properties.
287    *
288    * @param customProperties
289    *          custom configuration properties
290    *
291    * @return a configuration instance
292    *
293    * @see #newInstance()
294    * @see SqlGeneratorConfig#newInstance()
295    */
296   public static ThymeleafLanguageDriverConfig newInstance(Properties customProperties) {
297     ThymeleafLanguageDriverConfig config = new ThymeleafLanguageDriverConfig();
298     applyProperties(config, customProperties);
299     return config;
300   }
301 
302   /**
303    * Create an instance using specified customizer and override using a default properties file.
304    *
305    * @param customizer
306    *          baseline customizer
307    *
308    * @return a configuration instance
309    *
310    * @see #newInstance()
311    * @see SqlGeneratorConfig#newInstance()
312    */
313   public static ThymeleafLanguageDriverConfig newInstance(Consumer<ThymeleafLanguageDriverConfig> customizer) {
314     ThymeleafLanguageDriverConfig config = new ThymeleafLanguageDriverConfig();
315     customizer.accept(config);
316     applyDefaultProperties(config);
317     return config;
318   }
319 
320 }