• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mybatis / velocity-scripting / #375

24 Sep 2023 01:30AM CUT coverage: 82.545%. Remained the same
#375

Pull #187

github

web-flow
Merge 833f74f79 into df890ab13
Pull Request #187: Update dependency org.mybatis:mybatis-parent to v39

506 of 613 relevant lines covered (82.54%)

0.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.21
/src/main/java/org/mybatis/scripting/velocity/VelocityFacade.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.StringReader;
19
import java.io.StringWriter;
20
import java.util.HashMap;
21
import java.util.Map;
22
import java.util.Properties;
23
import java.util.stream.Collectors;
24

25
import org.apache.ibatis.builder.BuilderException;
26
import org.apache.ibatis.io.Resources;
27
import org.apache.ibatis.scripting.ScriptingException;
28
import org.apache.velocity.Template;
29
import org.apache.velocity.VelocityContext;
30
import org.apache.velocity.runtime.RuntimeConstants;
31
import org.apache.velocity.runtime.RuntimeInstance;
32
import org.apache.velocity.runtime.parser.node.SimpleNode;
33

34
public class VelocityFacade {
35

36
  private static final RuntimeInstance engine = new RuntimeInstance();
1✔
37
  private static final Map<String, Object> additionalCtxAttributes = new HashMap<>();
1✔
38

39
  private VelocityFacade() {
40
    // Prevent instantiation
41
  }
42

43
  /**
44
   * Initialize a template engine.
45
   *
46
   * @param driverConfig
47
   *          a language driver configuration
48
   *
49
   * @since 2.1.0
50
   */
51
  public static void initialize(VelocityLanguageDriverConfig driverConfig) {
52
    Properties properties = new Properties();
1✔
53
    driverConfig.getVelocitySettings().forEach(properties::setProperty);
1✔
54
    properties.setProperty(RuntimeConstants.CUSTOM_DIRECTIVES, driverConfig.generateCustomDirectivesString());
1✔
55
    engine.init(properties);
1✔
56
    additionalCtxAttributes.putAll(driverConfig.getAdditionalContextAttributes().entrySet().stream()
1✔
57
        .collect(Collectors.toMap(Map.Entry::getKey, v -> {
1✔
58
          try {
59
            return Resources.classForName(v.getValue()).getConstructor().newInstance();
1✔
60
          } catch (Exception e) {
×
61
            throw new ScriptingException("Cannot load additional context attribute class.", e);
×
62
          }
63
        })));
64
  }
1✔
65

66
  /**
67
   * Destroy a template engine.
68
   *
69
   * @since 2.1.0
70
   */
71
  public static void destroy() {
72
    engine.reset();
1✔
73
    additionalCtxAttributes.clear();
1✔
74
  }
1✔
75

76
  public static Object compile(String script, String name) {
77
    try {
78
      StringReader reader = new StringReader(script);
1✔
79
      Template template = new Template();
1✔
80
      SimpleNode node = engine.parse(reader, template);
1✔
81
      template.setRuntimeServices(engine);
1✔
82
      template.setData(node);
1✔
83
      template.setName(name);
1✔
84
      template.initDocument();
1✔
85
      return template;
1✔
86
    } catch (Exception ex) {
×
87
      throw new BuilderException("Error parsing velocity script '" + name + "'", ex);
×
88
    }
89
  }
90

91
  public static String apply(Object template, Map<String, Object> context) {
92
    final StringWriter out = new StringWriter();
1✔
93
    context.putAll(additionalCtxAttributes);
1✔
94
    ((Template) template).merge(new VelocityContext(context), out);
1✔
95
    return out.toString();
1✔
96
  }
97

98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc