1 package org.javabb.component; 2 3 import java.io.StringWriter ; 4 import java.util.Iterator ; 5 import java.util.Map ; 6 7 import org.apache.commons.logging.Log; 8 import org.apache.commons.logging.LogFactory; 9 import org.apache.velocity.Template; 10 import org.apache.velocity.VelocityContext; 11 import org.apache.velocity.app.VelocityEngine; 12 13 import com.opensymphony.webwork.views.velocity.VelocityManager; 14 15 30 31 35 public class VelocityTemplate { 36 protected static final Log log = LogFactory.getLog(VelocityTemplate.class); 37 38 44 public static String makeTemplate(Map velValues, String template) { 45 String htmlDoVelocity = ""; 46 try { 47 VelocityManager vle = VelocityManager.getInstance(); 48 VelocityEngine ve = vle.getVelocityEngine(); 49 Template t = ve.getTemplate(template); 50 VelocityContext context = new VelocityContext(); 51 Iterator itChaves = velValues.keySet().iterator(); 52 while (itChaves.hasNext()) { 53 String chave = (String ) itChaves.next(); 54 context.put(chave, velValues.get(chave)); 55 } 56 StringWriter writer = new StringWriter (); 57 t.merge(context, writer); 58 59 htmlDoVelocity = writer.toString(); 60 61 } catch (Exception e) { 62 log.error("Error at VelocityTemplate:" + e); 63 e.printStackTrace(); 64 } 65 66 return htmlDoVelocity; 67 } 68 69 } | Popular Tags |