Skip to content

Commit 3699d71

Browse files
committed
Polishing for gh-44
1 parent 97b7bc0 commit 3699d71

File tree

5 files changed

+15
-95
lines changed

5 files changed

+15
-95
lines changed

src/main/java/org/mybatis/scripting/freemarker/FreeMarkerLanguageDriver.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import java.io.IOException;
2323
import java.io.StringReader;
24+
import java.nio.charset.StandardCharsets;
2425

2526
import freemarker.template.TemplateException;
2627
import org.apache.ibatis.executor.parameter.ParameterHandler;
@@ -71,15 +72,7 @@ public FreeMarkerLanguageDriver(FreeMarkerLanguageDriverConfig driverConfig) {
7172
*/
7273
protected freemarker.template.Configuration createFreeMarkerConfiguration() {
7374
freemarker.template.Configuration cfg = new freemarker.template.Configuration(
74-
driverConfig.getIncompatibleImprovementsVersion());
75-
driverConfig.getFreemarkerSettings().forEach((name, value) -> {
76-
try {
77-
cfg.setSetting(name, value);
78-
} catch (TemplateException e) {
79-
throw new IllegalStateException(
80-
String.format("Fail to configure FreeMarker template setting. name[%s] value[%s]", name, value), e);
81-
}
82-
});
75+
freemarker.template.Configuration.VERSION_2_3_22);
8376

8477
TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass().getClassLoader(),
8578
driverConfig.getBasePackage());
@@ -89,7 +82,16 @@ protected freemarker.template.Configuration createFreeMarkerConfiguration() {
8982
cfg.setNumberFormat("computer");
9083

9184
// Because it defaults to default system encoding, we should set it always explicitly
92-
cfg.setDefaultEncoding(driverConfig.getDefaultEncoding().name());
85+
cfg.setDefaultEncoding(StandardCharsets.UTF_8.name());
86+
87+
driverConfig.getFreemarkerSettings().forEach((name, value) -> {
88+
try {
89+
cfg.setSetting(name, value);
90+
} catch (TemplateException e) {
91+
throw new IllegalStateException(
92+
String.format("Fail to configure FreeMarker template setting. name[%s] value[%s]", name, value), e);
93+
}
94+
});
9395

9496
return cfg;
9597
}
@@ -147,7 +149,7 @@ public SqlSource createSqlSource(Configuration configuration, String script, Cla
147149
}
148150

149151
protected SqlSource createSqlSource(Template template, Configuration configuration) {
150-
return new FreeMarkerSqlSource(template, configuration, driverConfig.getIncompatibleImprovementsVersion());
152+
return new FreeMarkerSqlSource(template, configuration, freemarkerCfg.getIncompatibleImprovements());
151153
}
152154

153155
private SqlSource createSqlSource(Configuration configuration, String scriptText) {

src/main/java/org/mybatis/scripting/freemarker/FreeMarkerLanguageDriverConfig.java

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public class FreeMarkerLanguageDriverConfig {
5555
static {
5656
Map<Class<?>, Function<String, Object>> converters = new HashMap<>();
5757
converters.put(String.class, String::trim);
58-
converters.put(Version.class, v -> new Version(v.trim()));
59-
converters.put(Charset.class, v -> Charset.forName(v.trim()));
6058
converters.put(Object.class, v -> v);
6159
TYPE_CONVERTERS = Collections.unmodifiableMap(converters);
6260
}
@@ -71,16 +69,6 @@ public class FreeMarkerLanguageDriverConfig {
7169
*/
7270
private String basePackage = "";
7371

74-
/**
75-
* The default encoding for reading template resources.
76-
*/
77-
private Charset defaultEncoding = StandardCharsets.UTF_8;
78-
79-
/**
80-
* The incompatible improvements version of freemarker.
81-
*/
82-
private Version incompatibleImprovementsVersion = Configuration.VERSION_2_3_22;
83-
8472
/**
8573
* Get FreeMarker settings.
8674
*
@@ -112,50 +100,6 @@ public void setBasePackage(String basePackage) {
112100
this.basePackage = basePackage;
113101
}
114102

115-
/**
116-
* Get a default encoding for reading template resources.
117-
* <p>
118-
* Default is {@code UTF-8}.
119-
* </p>
120-
*
121-
* @return a default encoding for reading template resources
122-
*/
123-
public Charset getDefaultEncoding() {
124-
return defaultEncoding;
125-
}
126-
127-
/**
128-
* Set a default encoding for reading template resources.
129-
*
130-
* @param defaultEncoding
131-
* a default encoding for reading template resources
132-
*/
133-
public void setDefaultEncoding(Charset defaultEncoding) {
134-
this.defaultEncoding = defaultEncoding;
135-
}
136-
137-
/**
138-
* Get an incompatible improvements version of freemarker.
139-
* <p>
140-
* Default is 2.3.22.
141-
* </p>
142-
*
143-
* @return an incompatible improvements version of freemarker
144-
*/
145-
public Version getIncompatibleImprovementsVersion() {
146-
return incompatibleImprovementsVersion;
147-
}
148-
149-
/**
150-
* Set an incompatible improvements version of freemarker.
151-
*
152-
* @param incompatibleImprovementsVersion
153-
* an incompatible improvements version of freemarker
154-
*/
155-
public void setIncompatibleImprovementsVersion(Version incompatibleImprovementsVersion) {
156-
this.incompatibleImprovementsVersion = incompatibleImprovementsVersion;
157-
}
158-
159103
/**
160104
* Create an instance from default properties file. <br>
161105
* If you want to customize a default {@code TemplateEngine}, you can configure some property using
@@ -178,18 +122,8 @@ public void setIncompatibleImprovementsVersion(Version incompatibleImprovementsV
178122
* <td>None(just under classpath)</td>
179123
* </tr>
180124
* <tr>
181-
* <td>default-encoding</td>
182-
* <td>The default encoding for reading template resources</td>
183-
* <td>UTF-8</td>
184-
* </tr>
185-
* <tr>
186-
* <td>incompatible-improvements-version</td>
187-
* <td>The incompatible improvements version of freemarker</td>
188-
* <td>2.3.22</td>
189-
* </tr>
190-
* <tr>
191125
* <td>freemarker-settings.*</td>
192-
* <td>The settings of freemarker {@link Configuration#setSetting(String, String)}).</td>
126+
* <td>The settings of freemarker {@link freemarker.core.Configurable#setSetting(String, String)}).</td>
193127
* <td>-</td>
194128
* </tr>
195129
* </table>

src/test/java/org/mybatis/scripting/freemarker/CustomizedDataContextTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ protected Object preProcessDataContext(Object dataContext, boolean isMap) {
9595
public static class CustomFreeMarkerLanguageDriver extends FreeMarkerLanguageDriver {
9696
@Override
9797
protected SqlSource createSqlSource(Template template, Configuration configuration) {
98-
return new CustomSqlSource(template, configuration, driverConfig.getIncompatibleImprovementsVersion());
98+
return new CustomSqlSource(template, configuration, freemarkerCfg.getIncompatibleImprovements());
9999
}
100100
}
101101

src/test/java/org/mybatis/scripting/freemarker/FreeMarkerLanguageDriverConfigTest.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,26 +55,20 @@ void newInstanceWithEmptyPropertiesFile() {
5555
System.setProperty("mybatis-freemarker.config.file", "mybatis-freemarker-empty.properties");
5656
FreeMarkerLanguageDriverConfig config = FreeMarkerLanguageDriverConfig.newInstance();
5757
Assertions.assertEquals("", config.getBasePackage());
58-
Assertions.assertEquals(StandardCharsets.UTF_8, config.getDefaultEncoding());
59-
Assertions.assertEquals(Configuration.VERSION_2_3_22, config.getIncompatibleImprovementsVersion());
6058
}
6159

6260
@Test
6361
void newInstanceWithPropertiesFileNotFound() {
6462
System.setProperty("mybatis-freemarker.config.file", "mybatis-freemarker-notfound.properties");
6563
FreeMarkerLanguageDriverConfig config = FreeMarkerLanguageDriverConfig.newInstance();
6664
Assertions.assertEquals("", config.getBasePackage());
67-
Assertions.assertEquals(StandardCharsets.UTF_8, config.getDefaultEncoding());
68-
Assertions.assertEquals(Configuration.VERSION_2_3_22, config.getIncompatibleImprovementsVersion());
6965
}
7066

7167
@Test
7268
void newInstanceWithCustomPropertiesFile() {
7369
System.setProperty("mybatis-freemarker.config.file", "mybatis-freemarker-custom.properties");
7470
FreeMarkerLanguageDriverConfig config = FreeMarkerLanguageDriverConfig.newInstance();
7571
Assertions.assertEquals("sqls", config.getBasePackage());
76-
Assertions.assertEquals(Charset.forName("Windows-31J"), config.getDefaultEncoding());
77-
Assertions.assertEquals(Configuration.VERSION_2_3_28, config.getIncompatibleImprovementsVersion());
7872
Assertions.assertEquals(2, config.getFreemarkerSettings().size());
7973
Assertions.assertEquals("dollar", config.getFreemarkerSettings().get("interpolation_syntax"));
8074
Assertions.assertEquals("yes", config.getFreemarkerSettings().get("whitespace_stripping"));
@@ -83,15 +77,11 @@ void newInstanceWithCustomPropertiesFile() {
8377
@Test
8478
void newInstanceWithCustomProperties() {
8579
Properties properties = new Properties();
86-
properties.setProperty("defaultEncoding", " " + StandardCharsets.ISO_8859_1.name() + " ");
87-
properties.setProperty("incompatibleImprovementsVersion", " 2.3.27 ");
8880
properties.setProperty("freemarkerSettings.interpolation_syntax", "dollar");
8981
properties.setProperty("freemarkerSettings.whitespace_stripping", "yes");
9082

9183
FreeMarkerLanguageDriverConfig config = FreeMarkerLanguageDriverConfig.newInstance(properties);
9284
Assertions.assertEquals("sql", config.getBasePackage());
93-
Assertions.assertEquals(StandardCharsets.ISO_8859_1, config.getDefaultEncoding());
94-
Assertions.assertEquals(Configuration.VERSION_2_3_27, config.getIncompatibleImprovementsVersion());
9585
Assertions.assertEquals("dollar", config.getFreemarkerSettings().get("interpolation_syntax"));
9686
Assertions.assertEquals("yes", config.getFreemarkerSettings().get("whitespace_stripping"));
9787
}
@@ -100,12 +90,8 @@ void newInstanceWithCustomProperties() {
10090
void newInstanceWithConsumer() {
10191
FreeMarkerLanguageDriverConfig config = FreeMarkerLanguageDriverConfig.newInstance(c -> {
10292
c.setBasePackage("sqls");
103-
c.setDefaultEncoding(StandardCharsets.UTF_16);
104-
c.setIncompatibleImprovementsVersion(Configuration.VERSION_2_3_26);
10593
});
10694
Assertions.assertEquals("sql", config.getBasePackage());
107-
Assertions.assertEquals(StandardCharsets.UTF_16, config.getDefaultEncoding());
108-
Assertions.assertEquals(Configuration.VERSION_2_3_26, config.getIncompatibleImprovementsVersion());
10995
}
11096

11197
}

src/test/resources/mybatis-freemarker-custom.properties

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,5 @@
1515
#
1616

1717
base-package=sqls
18-
default-encoding=Windows-31J
19-
incompatible-improvements-version=2.3.28
2018
freemarker-settings.interpolation_syntax=dollar
2119
freemarker-settings.whitespace_stripping=yes

0 commit comments

Comments
 (0)