Package org.radsimplified.genapp

Source Code of org.radsimplified.genapp.VelocityUtil

package org.radsimplified.genapp;

import java.io.StringWriter;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

public class VelocityUtil {

  private static VelocityEngine engine = getVelocityEngine();

  public static String mergeTemplateIntoString(String templatePath, VelocityContext appData) {
      try
      {

        // Fetch Template to a StringWriter
        Template template = engine.getTemplate(templatePath);
        StringWriter writer = new StringWriter();
        template.merge(appData, writer);

        return writer.toString();
      }
      catch (Exception e)
      {
        System.out.println("Oops! We have an exception");
        e.printStackTrace();
        return null;
      }
  }

    private static VelocityEngine getVelocityEngine() {

      Properties p = new Properties();

      p.setProperty("resource.loader", "file");
      p.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
      p.setProperty("file.resource.loader.path", GenApp.properties.getProperty("templatePath"));
      VelocityEngine engine = new VelocityEngine();

      try {
      engine.init(p);
    } catch (Exception e) {
      System.out.println("Oops! We have an exception");
        e.printStackTrace();
    }

      return engine;
    }
   
}
TOP

Related Classes of org.radsimplified.genapp.VelocityUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.