30 | * This class rename from {@code Driver}. 31 | *
32 | * 33 | * @since 2.1.0 34 | * 35 | * @author Kazuki Shimizu 36 | */ 37 | public class VelocityLanguageDriver implements LanguageDriver { 38 | 39 | /** 40 | * Default constructor. 41 | */ 42 | public VelocityLanguageDriver() { 43 | this(VelocityLanguageDriverConfig.newInstance()); 44 | } 45 | 46 | /** 47 | * Constructor. 48 | * 49 | * @param driverConfig 50 | * a language driver configuration 51 | */ 52 | public VelocityLanguageDriver(VelocityLanguageDriverConfig driverConfig) { 53 | VelocityFacade.initialize(driverConfig); 54 | } 55 | 56 | /** 57 | * {@inheritDoc} 58 | */ 59 | @Override 60 | public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject, 61 | BoundSql boundSql) { 62 | return new DefaultParameterHandler(mappedStatement, parameterObject, boundSql); 63 | } 64 | 65 | /** 66 | * {@inheritDoc} 67 | */ 68 | @Override 69 | public SqlSource createSqlSource(Configuration configuration, XNode script, Class> parameterTypeClass) { 70 | return new SQLScriptSource(configuration, script.getNode().getTextContent(), 71 | parameterTypeClass == null ? Object.class : parameterTypeClass); 72 | } 73 | 74 | /** 75 | * {@inheritDoc} 76 | */ 77 | @Override 78 | public SqlSource createSqlSource(Configuration configuration, String script, Class> parameterTypeClass) { 79 | return new SQLScriptSource(configuration, script, parameterTypeClass == null ? Object.class : parameterTypeClass); 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/mybatis/scripting/velocity/VelocityLanguageDriverConfig.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2012-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.velocity; 17 | 18 | import java.io.BufferedReader; 19 | import java.io.IOException; 20 | import java.io.InputStream; 21 | import java.io.InputStreamReader; 22 | import java.nio.charset.Charset; 23 | import java.nio.charset.StandardCharsets; 24 | import java.util.Collections; 25 | import java.util.HashMap; 26 | import java.util.HashSet; 27 | import java.util.Map; 28 | import java.util.Objects; 29 | import java.util.Optional; 30 | import java.util.Properties; 31 | import java.util.Set; 32 | import java.util.StringJoiner; 33 | import java.util.function.Consumer; 34 | import java.util.function.Function; 35 | import java.util.stream.Stream; 36 | 37 | import org.apache.commons.text.WordUtils; 38 | import org.apache.ibatis.io.Resources; 39 | import org.apache.ibatis.logging.Log; 40 | import org.apache.ibatis.logging.LogFactory; 41 | import org.apache.ibatis.reflection.DefaultReflectorFactory; 42 | import org.apache.ibatis.reflection.MetaObject; 43 | import org.apache.ibatis.reflection.factory.DefaultObjectFactory; 44 | import org.apache.ibatis.reflection.property.PropertyTokenizer; 45 | import org.apache.ibatis.reflection.wrapper.DefaultObjectWrapperFactory; 46 | import org.apache.ibatis.scripting.ScriptingException; 47 | import org.apache.velocity.runtime.RuntimeConstants; 48 | import org.apache.velocity.runtime.RuntimeInstance; 49 | import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader; 50 | 51 | /** 52 | * Configuration class for {@link Driver}. 53 | * 54 | * @author Kazuki Shimizu 55 | * 56 | * @since 2.1.0 57 | */ 58 | public class VelocityLanguageDriverConfig { 59 | 60 | private static final String PROPERTY_KEY_CONFIG_FILE = "mybatis-velocity.config.file"; 61 | private static final String PROPERTY_KEY_CONFIG_ENCODING = "mybatis-velocity.config.encoding"; 62 | private static final String DEFAULT_PROPERTIES_FILE = "mybatis-velocity.properties"; 63 | private static final String PROPERTY_KEY_ADDITIONAL_CONTEXT_ATTRIBUTE = "additional.context.attributes"; 64 | private static final String[] BUILT_IN_DIRECTIVES = { TrimDirective.class.getName(), WhereDirective.class.getName(), 65 | SetDirective.class.getName(), InDirective.class.getName(), RepeatDirective.class.getName() }; 66 | 67 | private static final MapProperty Key | 171 | *Description | 172 | *Default | 173 | *
---|---|---|
Directive configuration | 176 | *||
userdirective | 179 | *The user defined directives (Recommend to use the 'velocity-settings.runtime.custom_directives' property 180 | * because this property defined for keeping backward compatibility) | 181 | *None(empty) | 182 | *
Additional context attribute configuration | 185 | *||
additional.context.attributes | 188 | *The user defined additional context attribute values(Recommend to use the 189 | * 'additional-context-attributes.{name}' because this property defined for keeping backward compatibility) | 190 | *None(empty) | 191 | *
additional-context-attributes.{name} | 194 | *The user defined additional context attributes value(FQCN) | 195 | *- | 196 | *
Velocity settings configuration | 199 | *||
velocity-settings.{name} | 202 | *The settings of Velocity's {@link RuntimeInstance#setProperty(String, Object)} | 203 | *- | 204 | *
{name} | 207 | *The settings of Velocity's {@link RuntimeInstance#setProperty(String, Object)} (Recommend to use the 208 | * 'velocity-settings.{name}' because this property defined for keeping backward compatibility) | 209 | *- | 210 | *