|
1 | 1 | /**
|
2 |
| - * Copyright 2015 the original author or authors. |
| 2 | + * Copyright 2015-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
40 | 40 | * @author elwood
|
41 | 41 | */
|
42 | 42 | public class FreeMarkerLanguageDriver implements LanguageDriver {
|
43 |
| - /** |
44 |
| - * Base package for all FreeMarker templates. |
45 |
| - */ |
46 |
| - public static final String basePackage; |
47 |
| - |
48 |
| - public static final String DEFAULT_BASE_PACKAGE = ""; |
49 |
| - |
50 |
| - static { |
51 |
| - Properties properties = new Properties(); |
52 |
| - try { |
53 |
| - try (InputStream stream = FreeMarkerLanguageDriver.class.getClassLoader() |
54 |
| - .getResourceAsStream("mybatis-freemarker.properties")) { |
55 |
| - if (stream != null) { |
56 |
| - properties.load(stream); |
57 |
| - basePackage = properties.getProperty("basePackage", DEFAULT_BASE_PACKAGE); |
58 |
| - } else { |
59 |
| - basePackage = DEFAULT_BASE_PACKAGE; |
60 |
| - } |
61 |
| - } |
62 |
| - } catch (IOException e) { |
63 |
| - throw new IllegalStateException(e); |
| 43 | + /** |
| 44 | + * Base package for all FreeMarker templates. |
| 45 | + */ |
| 46 | + public static final String basePackage; |
| 47 | + |
| 48 | + public static final String DEFAULT_BASE_PACKAGE = ""; |
| 49 | + |
| 50 | + static { |
| 51 | + Properties properties = new Properties(); |
| 52 | + try { |
| 53 | + try (InputStream stream = FreeMarkerLanguageDriver.class.getClassLoader() |
| 54 | + .getResourceAsStream("mybatis-freemarker.properties")) { |
| 55 | + if (stream != null) { |
| 56 | + properties.load(stream); |
| 57 | + basePackage = properties.getProperty("basePackage", DEFAULT_BASE_PACKAGE); |
| 58 | + } else { |
| 59 | + basePackage = DEFAULT_BASE_PACKAGE; |
64 | 60 | }
|
| 61 | + } |
| 62 | + } catch (IOException e) { |
| 63 | + throw new IllegalStateException(e); |
65 | 64 | }
|
66 |
| - |
67 |
| - protected freemarker.template.Configuration freemarkerCfg; |
68 |
| - |
69 |
| - public FreeMarkerLanguageDriver() { |
70 |
| - freemarkerCfg = createFreeMarkerConfiguration(); |
71 |
| - } |
72 |
| - |
73 |
| - /** |
74 |
| - * Creates a {@link ParameterHandler} that passes the actual parameters to the the JDBC statement. |
75 |
| - * |
76 |
| - * @see DefaultParameterHandler |
77 |
| - * @param mappedStatement The mapped statement that is being executed |
78 |
| - * @param parameterObject The input parameter object (can be null) |
79 |
| - * @param boundSql The resulting SQL once the dynamic language has been executed. |
80 |
| - */ |
81 |
| - @Override |
82 |
| - public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) { |
83 |
| - // As default XMLLanguageDriver |
84 |
| - return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql); |
| 65 | + } |
| 66 | + |
| 67 | + protected freemarker.template.Configuration freemarkerCfg; |
| 68 | + |
| 69 | + public FreeMarkerLanguageDriver() { |
| 70 | + freemarkerCfg = createFreeMarkerConfiguration(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Creates a {@link ParameterHandler} that passes the actual parameters to the the JDBC statement. |
| 75 | + * |
| 76 | + * @see DefaultParameterHandler |
| 77 | + * @param mappedStatement The mapped statement that is being executed |
| 78 | + * @param parameterObject The input parameter object (can be null) |
| 79 | + * @param boundSql The resulting SQL once the dynamic language has been executed. |
| 80 | + */ |
| 81 | + @Override |
| 82 | + public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, |
| 83 | + BoundSql boundSql) { |
| 84 | + // As default XMLLanguageDriver |
| 85 | + return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Creates an {@link SqlSource} that will hold the statement read from a mapper xml file. |
| 90 | + * It is called during startup, when the mapped statement is read from a class or an xml file. |
| 91 | + * |
| 92 | + * @param configuration The MyBatis configuration |
| 93 | + * @param script XNode parsed from a XML file |
| 94 | + * @param parameterType input parameter type got from a mapper method or specified in the parameterType xml attribute. Can be null. |
| 95 | + */ |
| 96 | + @Override |
| 97 | + public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) { |
| 98 | + return createSqlSource(configuration, script.getNode().getTextContent()); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Creates an {@link SqlSource} that will hold the statement read from an annotation. |
| 103 | + * It is called during startup, when the mapped statement is read from a class or an xml file. |
| 104 | + * |
| 105 | + * @param configuration The MyBatis configuration |
| 106 | + * @param script The content of the annotation |
| 107 | + * @param parameterType input parameter type got from a mapper method or specified in the parameterType xml attribute. Can be null. |
| 108 | + */ |
| 109 | + @Override |
| 110 | + public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) { |
| 111 | + return createSqlSource(configuration, script); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Creates the {@link freemarker.template.Configuration} instance and sets it up. |
| 116 | + * If you want to change it (set another props, for example), you can override it in |
| 117 | + * inherited class and use your own class in @Lang directive. |
| 118 | + */ |
| 119 | + protected freemarker.template.Configuration createFreeMarkerConfiguration() { |
| 120 | + freemarker.template.Configuration cfg = new freemarker.template.Configuration( |
| 121 | + freemarker.template.Configuration.VERSION_2_3_22); |
| 122 | + |
| 123 | + TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass().getClassLoader(), basePackage); |
| 124 | + cfg.setTemplateLoader(templateLoader); |
| 125 | + |
| 126 | + // To avoid formatting numbers using spaces and commas in SQL |
| 127 | + cfg.setNumberFormat("computer"); |
| 128 | + |
| 129 | + // Because it defaults to default system encoding, we should set it always explicitly |
| 130 | + cfg.setDefaultEncoding("utf-8"); |
| 131 | + |
| 132 | + return cfg; |
| 133 | + } |
| 134 | + |
| 135 | + protected SqlSource createSqlSource(Template template, Configuration configuration) { |
| 136 | + return new FreeMarkerSqlSource(template, configuration); |
| 137 | + } |
| 138 | + |
| 139 | + private SqlSource createSqlSource(Configuration configuration, String scriptText) { |
| 140 | + Template template; |
| 141 | + if (scriptText.trim().contains(" ")) { |
| 142 | + // Consider that script is inline script |
| 143 | + try { |
| 144 | + template = new Template(null, new StringReader(scriptText), freemarkerCfg); |
| 145 | + } catch (IOException e) { |
| 146 | + throw new RuntimeException(e); |
| 147 | + } |
| 148 | + } else { |
| 149 | + // Consider that script is template name, trying to find the template in classpath |
| 150 | + try { |
| 151 | + template = freemarkerCfg.getTemplate(scriptText.trim()); |
| 152 | + } catch (IOException e) { |
| 153 | + throw new RuntimeException(e); |
| 154 | + } |
85 | 155 | }
|
86 | 156 |
|
87 |
| - /** |
88 |
| - * Creates an {@link SqlSource} that will hold the statement read from a mapper xml file. |
89 |
| - * It is called during startup, when the mapped statement is read from a class or an xml file. |
90 |
| - * |
91 |
| - * @param configuration The MyBatis configuration |
92 |
| - * @param script XNode parsed from a XML file |
93 |
| - * @param parameterType input parameter type got from a mapper method or specified in the parameterType xml attribute. Can be null. |
94 |
| - */ |
95 |
| - @Override |
96 |
| - public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) { |
97 |
| - return createSqlSource(configuration, script.getNode().getTextContent()); |
98 |
| - } |
99 |
| - |
100 |
| - /** |
101 |
| - * Creates an {@link SqlSource} that will hold the statement read from an annotation. |
102 |
| - * It is called during startup, when the mapped statement is read from a class or an xml file. |
103 |
| - * |
104 |
| - * @param configuration The MyBatis configuration |
105 |
| - * @param script The content of the annotation |
106 |
| - * @param parameterType input parameter type got from a mapper method or specified in the parameterType xml attribute. Can be null. |
107 |
| - */ |
108 |
| - @Override |
109 |
| - public SqlSource createSqlSource(Configuration configuration, String script, Class<?> parameterType) { |
110 |
| - return createSqlSource(configuration, script); |
111 |
| - } |
112 |
| - |
113 |
| - /** |
114 |
| - * Creates the {@link freemarker.template.Configuration} instance and sets it up. |
115 |
| - * If you want to change it (set another props, for example), you can override it in |
116 |
| - * inherited class and use your own class in @Lang directive. |
117 |
| - */ |
118 |
| - protected freemarker.template.Configuration createFreeMarkerConfiguration() { |
119 |
| - freemarker.template.Configuration cfg = new freemarker.template.Configuration(freemarker.template.Configuration.VERSION_2_3_22); |
120 |
| - |
121 |
| - TemplateLoader templateLoader = new ClassTemplateLoader(this.getClass().getClassLoader(), basePackage); |
122 |
| - cfg.setTemplateLoader(templateLoader); |
123 |
| - |
124 |
| - // To avoid formatting numbers using spaces and commas in SQL |
125 |
| - cfg.setNumberFormat("computer"); |
126 |
| - |
127 |
| - // Because it defaults to default system encoding, we should set it always explicitly |
128 |
| - cfg.setDefaultEncoding("utf-8"); |
129 |
| - |
130 |
| - return cfg; |
131 |
| - } |
132 |
| - |
133 |
| - protected SqlSource createSqlSource(Template template, Configuration configuration) { |
134 |
| - return new FreeMarkerSqlSource(template, configuration); |
135 |
| - } |
136 |
| - |
137 |
| - private SqlSource createSqlSource(Configuration configuration, String scriptText) { |
138 |
| - Template template; |
139 |
| - if (scriptText.trim().contains(" ")) { |
140 |
| - // Consider that script is inline script |
141 |
| - try { |
142 |
| - template = new Template(null, new StringReader(scriptText), freemarkerCfg); |
143 |
| - } catch (IOException e) { |
144 |
| - throw new RuntimeException(e); |
145 |
| - } |
146 |
| - } else { |
147 |
| - // Consider that script is template name, trying to find the template in classpath |
148 |
| - try { |
149 |
| - template = freemarkerCfg.getTemplate(scriptText.trim()); |
150 |
| - } catch (IOException e) { |
151 |
| - throw new RuntimeException(e); |
152 |
| - } |
153 |
| - } |
154 |
| - |
155 |
| - return createSqlSource(template, configuration); |
156 |
| - } |
| 157 | + return createSqlSource(template, configuration); |
| 158 | + } |
157 | 159 | }
|
0 commit comments