Java Code Examples for org.apache.velocity.Template#setRuntimeServices()

The following examples show how to use org.apache.velocity.Template#setRuntimeServices() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CustomVelocityResponseTransformer.java    From AuTe-Framework with Apache License 2.0 6 votes vote down vote up
private String getRenderedBodyFromFile(final ResponseDefinition response) throws ParseException {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(response.getBody());
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    Template template = new Template();
    template.setEncoding("UTF-8");
    template.setRuntimeServices(runtimeServices);
    template.setData(node);
    template.initDocument();

    StringWriter writer = new StringWriter();
    template.merge(context, writer);
    return String.valueOf(writer.getBuffer());
}
 
Example 2
Source File: OTPService.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
public static String getSmsBody(String templateFile, Map<String, String> smsTemplate) {
  try {
    String sms = getOTPSMSTemplate(templateFile);
    RuntimeServices rs = RuntimeSingleton.getRuntimeServices();
    SimpleNode sn = rs.parse(sms, "Sms Information");
    Template t = new Template();
    t.setRuntimeServices(rs);
    t.setData(sn);
    t.initDocument();
    VelocityContext context = new VelocityContext();
    context.put(JsonKey.OTP, smsTemplate.get(JsonKey.OTP));
    context.put(
        JsonKey.OTP_EXPIRATION_IN_MINUTES, smsTemplate.get(JsonKey.OTP_EXPIRATION_IN_MINUTES));
    context.put(
        JsonKey.INSTALLATION_NAME,
        ProjectUtil.getConfigValue(JsonKey.SUNBIRD_INSTALLATION_DISPLAY_NAME));

    StringWriter writer = new StringWriter();
    t.merge(context, writer);
    return writer.toString();
  } catch (Exception ex) {
    ProjectLogger.log(
        "OTPService:getSmsBody: Exception occurred with error message = " + ex.getMessage(), ex);
  }
  return "";
}
 
Example 3
Source File: VelocityTemplateEngine.java    From pippo with Apache License 2.0 6 votes vote down vote up
@Override
public void renderString(String templateContent, Map<String, Object> model, Writer writer) {
    // create the velocity context
    VelocityContext context = createVelocityContext(model);

    // merge the template
    try {
        RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
        StringReader reader = new StringReader(templateContent);
        SimpleNode node = runtimeServices.parse(reader, "StringTemplate");
        Template template = new Template();
        template.setRuntimeServices(runtimeServices);
        template.setData(node);
        template.initDocument();
        template.merge(context, writer);
    } catch (Exception e) {
        throw new PippoRuntimeException(e);
    }
}
 
Example 4
Source File: VelocityScriptEngine.java    From velocity-engine with Apache License 2.0 6 votes vote down vote up
/**
 * Compile a template
 * @param script template source
 * @return compiled template
 * @throws ScriptException
 */
public CompiledScript compile(Reader script) throws ScriptException
{
    initVelocityEngine(null);
    ResourceLoader resourceLoader = new SingleResourceReader(script);
    Template template = new Template();
    template.setRuntimeServices(velocityEngine);
    template.setResourceLoader(resourceLoader);
    try
    {
        template.process();
    }
    catch(Exception e)
    {
        // CB TODO - exception may have line/col informations, that ScriptException can exploit
        throw new ScriptException(e);
    }
    return new VelocityCompiledScript(this, template);
}
 
Example 5
Source File: VelocityTransformer.java    From AuTe-Framework with Apache License 2.0 5 votes vote down vote up
private String render(final String stringTemplate) throws ParseException {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    runtimeServices.setProperty("userdirective", GroovyDirective.class.getName());
    StringReader reader = new StringReader(stringTemplate);
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    Template template = new Template();
    template.setEncoding("UTF-8");
    template.setRuntimeServices(runtimeServices);
    template.setData(node);
    template.initDocument();

    StringWriter writer = new StringWriter();
    template.merge(velocityContext, writer);
    return String.valueOf(writer.getBuffer());
}
 
Example 6
Source File: VelocityUtils.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
public static Template create(String source, String templateName) throws ParseException {
  RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
  StringReader reader = new StringReader(source);
  SimpleNode node = runtimeServices.parse(reader, templateName);
  Template template = new Template();
  template.setRuntimeServices(runtimeServices);
  template.setData(node);
  template.initDocument();
  return template;
}
 
Example 7
Source File: VelocitySqlSource.java    From mybaties with Apache License 2.0 5 votes vote down vote up
public VelocitySqlSource(Configuration configuration, String scriptText) {
  this.configuration = configuration;
  try {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(scriptText);
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    script = new Template();
    script.setRuntimeServices(runtimeServices);
    script.setData(node);
    script.initDocument();
  } catch (Exception ex) {
    throw new BuilderException("Error parsing velocity script", ex);
  }
}
 
Example 8
Source File: VelocitySqlSource.java    From mybatis with Apache License 2.0 5 votes vote down vote up
public VelocitySqlSource(Configuration configuration, String scriptText) {
  this.configuration = configuration;
  try {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(scriptText);
    SimpleNode node = runtimeServices.parse(reader, "Template name");
    script = new Template();
    script.setRuntimeServices(runtimeServices);
    script.setData(node);
    script.initDocument();
  } catch (Exception ex) {
    throw new BuilderException("Error parsing velocity script", ex);
  }
}
 
Example 9
Source File: TestVelocityView.java    From gocd with Apache License 2.0 5 votes vote down vote up
private Template setupTemplate(ResourceLoader loader, RuntimeInstance runtimeServices, String templateName, InputStream templateContents) throws IOException {
    Template template = new Template();
    template.setRuntimeServices(runtimeServices);
    template.setResourceLoader(loader);
    template.setName(templateName);

    byte[] bytes = IOUtils.toByteArray(templateContents);
    templateContents.close();

    when(loader.getResourceStream(templateName)).thenReturn(new ByteArrayInputStream(bytes));
    doReturn(template).when(runtimeServices).getTemplate(templateName);
    doReturn(template).when(runtimeServices).getTemplate(eq(templateName), any(String.class));

    return template;
}