34
34
import freemarker .template .Version ;
35
35
import org .apache .commons .text .WordUtils ;
36
36
import org .apache .ibatis .io .Resources ;
37
+ import org .apache .ibatis .logging .Log ;
38
+ import org .apache .ibatis .logging .LogFactory ;
37
39
import org .apache .ibatis .reflection .DefaultReflectorFactory ;
38
40
import org .apache .ibatis .reflection .MetaObject ;
39
41
import org .apache .ibatis .reflection .factory .DefaultObjectFactory ;
46
48
* @since 1.2.0
47
49
*/
48
50
public class FreeMarkerLanguageDriverConfig {
49
-
50
51
private static final String PROPERTY_KEY_CONFIG_FILE = "mybatis-freemarker.config.file" ;
51
52
private static final String PROPERTY_KEY_CONFIG_ENCODING = "mybatis-freemarker.config.encoding" ;
52
53
private static final String DEFAULT_PROPERTIES_FILE = "mybatis-freemarker.properties" ;
53
- private static Map <Class <?>, Function <String , Object >> TYPE_CONVERTERS ;
54
+ private static final Map <Class <?>, Function <String , Object >> TYPE_CONVERTERS ;
54
55
55
56
static {
56
57
Map <Class <?>, Function <String , Object >> converters = new HashMap <>();
57
58
converters .put (String .class , String ::trim );
59
+ converters .put (boolean .class , v -> Boolean .valueOf (v .trim ()));
58
60
converters .put (Object .class , v -> v );
59
61
TYPE_CONVERTERS = Collections .unmodifiableMap (converters );
60
62
}
61
63
64
+ private static final Log log = LogFactory .getLog (FreeMarkerLanguageDriverConfig .class );
65
+
62
66
/**
63
67
* The configuration properties.
64
68
*/
65
69
private final Map <String , String > freemarkerSettings = new HashMap <>();
66
70
67
71
/**
68
- * The base directory for reading template resources .
72
+ * Template file configuration .
69
73
*/
70
- private String basePackage = "" ;
74
+ private final TemplateFileConfig templateFile = new TemplateFileConfig () ;
71
75
72
76
/**
73
77
* Get FreeMarker settings.
@@ -85,19 +89,228 @@ public Map<String, String> getFreemarkerSettings() {
85
89
* </p>
86
90
*
87
91
* @return a base directory for reading template resources
92
+ * @deprecated Recommend to use the {@link TemplateFileConfig#getBaseDir()}} because this method defined for keeping
93
+ * backward compatibility (There is possibility that this method removed at a future version)
88
94
*/
95
+ @ Deprecated
89
96
public String getBasePackage () {
90
- return basePackage ;
97
+ return templateFile . getBaseDir () ;
91
98
}
92
99
93
100
/**
94
101
* Set a base directory for reading template resources.
95
102
*
96
103
* @param basePackage
97
104
* a base directory for reading template resources
105
+ * @deprecated Recommend to use the {@link TemplateFileConfig#setBaseDir(String)} because this method defined for
106
+ * keeping backward compatibility (There is possibility that this method removed at a future version)
98
107
*/
108
+ @ Deprecated
99
109
public void setBasePackage (String basePackage ) {
100
- this .basePackage = basePackage ;
110
+ log .warn ("The 'basePackage' has been deprecated since 1.2.0. Please use the 'templateFile.baseDir'." );
111
+ templateFile .setBaseDir (basePackage );
112
+ }
113
+
114
+ /**
115
+ * Get a template file configuration.
116
+ *
117
+ * @return a template file configuration
118
+ */
119
+ public TemplateFileConfig getTemplateFile () {
120
+ return templateFile ;
121
+ }
122
+
123
+ /**
124
+ * Template file configuration.
125
+ */
126
+ public static class TemplateFileConfig {
127
+
128
+ /**
129
+ * The base directory for reading template resources.
130
+ */
131
+ private String baseDir = "" ;
132
+
133
+ /**
134
+ * The template file path provider configuration.
135
+ */
136
+ private final PathProviderConfig pathProvider = new PathProviderConfig ();
137
+
138
+ /**
139
+ * Get the base directory for reading template resource file.
140
+ * <p>
141
+ * Default is {@code ""}(none).
142
+ * </p>
143
+ *
144
+ * @return the base directory for reading template resource file
145
+ */
146
+ public String getBaseDir () {
147
+ return baseDir ;
148
+ }
149
+
150
+ /**
151
+ * Set the base directory for reading template resource file.
152
+ *
153
+ * @param baseDir
154
+ * the base directory for reading template resource file
155
+ */
156
+ public void setBaseDir (String baseDir ) {
157
+ this .baseDir = baseDir ;
158
+ }
159
+
160
+ /**
161
+ * Get the template file path provider configuration.
162
+ *
163
+ * @return the template file path provider configuration
164
+ */
165
+ public PathProviderConfig getPathProvider () {
166
+ return pathProvider ;
167
+ }
168
+
169
+ /**
170
+ * The template file path provider configuration.
171
+ */
172
+ public static class PathProviderConfig {
173
+
174
+ /**
175
+ * The prefix for adding to template file path.
176
+ */
177
+ private String prefix = "" ;
178
+
179
+ /**
180
+ * Whether includes package path part.
181
+ */
182
+ private boolean includesPackagePath = true ;
183
+
184
+ /**
185
+ * Whether separate directory per mapper.
186
+ */
187
+ private boolean separateDirectoryPerMapper = true ;
188
+
189
+ /**
190
+ * Whether includes mapper name into file name when separate directory per mapper.
191
+ */
192
+ private boolean includesMapperNameWhenSeparateDirectory = true ;
193
+
194
+ /**
195
+ * Whether cache a resolved template file path.
196
+ */
197
+ private boolean cacheEnabled = true ;
198
+
199
+ /**
200
+ * Get a prefix for adding to template file path.
201
+ * <p>
202
+ * Default is {@code ""}.
203
+ * </p>
204
+ *
205
+ * @return a prefix for adding to template file path
206
+ */
207
+ public String getPrefix () {
208
+ return prefix ;
209
+ }
210
+
211
+ /**
212
+ * Set the prefix for adding to template file path.
213
+ *
214
+ * @param prefix
215
+ * The prefix for adding to template file path
216
+ */
217
+ public void setPrefix (String prefix ) {
218
+ this .prefix = prefix ;
219
+ }
220
+
221
+ /**
222
+ * Get whether includes package path part.
223
+ * <p>
224
+ * Default is {@code true}.
225
+ * </p>
226
+ *
227
+ * @return If includes package path, return {@code true}
228
+ */
229
+ public boolean isIncludesPackagePath () {
230
+ return includesPackagePath ;
231
+ }
232
+
233
+ /**
234
+ * Set whether includes package path part.
235
+ *
236
+ * @param includesPackagePath
237
+ * If want to includes, set {@code true}
238
+ */
239
+ public void setIncludesPackagePath (boolean includesPackagePath ) {
240
+ this .includesPackagePath = includesPackagePath ;
241
+ }
242
+
243
+ /**
244
+ * Get whether separate directory per mapper.
245
+ *
246
+ * @return If separate directory per mapper, return {@code true}
247
+ */
248
+ public boolean isSeparateDirectoryPerMapper () {
249
+ return separateDirectoryPerMapper ;
250
+ }
251
+
252
+ /**
253
+ * Set whether separate directory per mapper.
254
+ * <p>
255
+ * Default is {@code true}.
256
+ * </p>
257
+ *
258
+ * @param separateDirectoryPerMapper
259
+ * If want to separate directory, set {@code true}
260
+ */
261
+ public void setSeparateDirectoryPerMapper (boolean separateDirectoryPerMapper ) {
262
+ this .separateDirectoryPerMapper = separateDirectoryPerMapper ;
263
+ }
264
+
265
+ /**
266
+ * Get whether includes mapper name into file name when separate directory per mapper.
267
+ * <p>
268
+ * Default is {@code true}.
269
+ * </p>
270
+ *
271
+ * @return If includes mapper name, return {@code true}
272
+ */
273
+ public boolean isIncludesMapperNameWhenSeparateDirectory () {
274
+ return includesMapperNameWhenSeparateDirectory ;
275
+ }
276
+
277
+ /**
278
+ * Set whether includes mapper name into file name when separate directory per mapper.
279
+ * <p>
280
+ * Default is {@code true}.
281
+ * </p>
282
+ *
283
+ * @param includesMapperNameWhenSeparateDirectory
284
+ * If want to includes, set {@code true}
285
+ */
286
+ public void setIncludesMapperNameWhenSeparateDirectory (boolean includesMapperNameWhenSeparateDirectory ) {
287
+ this .includesMapperNameWhenSeparateDirectory = includesMapperNameWhenSeparateDirectory ;
288
+ }
289
+
290
+ /**
291
+ * Get whether cache a resolved template file path.
292
+ * <p>
293
+ * Default is {@code true}.
294
+ * </p>
295
+ *
296
+ * @return If cache a resolved template file path, return {@code true}
297
+ */
298
+ public boolean isCacheEnabled () {
299
+ return cacheEnabled ;
300
+ }
301
+
302
+ /**
303
+ * Set whether cache a resolved template file path.
304
+ *
305
+ * @param cacheEnabled
306
+ * If want to cache, set {@code true}
307
+ */
308
+ public void setCacheEnabled (boolean cacheEnabled ) {
309
+ this .cacheEnabled = cacheEnabled ;
310
+ }
311
+
312
+ }
313
+
101
314
}
102
315
103
316
/**
0 commit comments