1 16 17 package org.springframework.ui.velocity; 18 19 import java.io.StringWriter ; 20 import java.io.Writer ; 21 import java.util.Map ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.apache.velocity.VelocityContext; 26 import org.apache.velocity.app.VelocityEngine; 27 import org.apache.velocity.exception.VelocityException; 28 29 36 public abstract class VelocityEngineUtils { 37 38 private static final Log logger = LogFactory.getLog(VelocityEngineUtils.class); 39 40 41 52 public static void mergeTemplate( 53 VelocityEngine velocityEngine, String templateLocation, Map model, Writer writer) 54 throws VelocityException { 55 56 try { 57 VelocityContext velocityContext = new VelocityContext(model); 58 velocityEngine.mergeTemplate(templateLocation, velocityContext, writer); 59 } 60 catch (VelocityException ex) { 61 throw ex; 62 } 63 catch (RuntimeException ex) { 64 throw ex; 65 } 66 catch (Exception ex) { 67 logger.error("Why does VelocityEngine throw a generic checked exception, after all?", ex); 68 throw new VelocityException(ex.toString()); 69 } 70 } 71 72 84 public static void mergeTemplate( 85 VelocityEngine velocityEngine, String templateLocation, String encoding, Map model, Writer writer) 86 throws VelocityException { 87 88 try { 89 VelocityContext velocityContext = new VelocityContext(model); 90 velocityEngine.mergeTemplate(templateLocation, encoding, velocityContext, writer); 91 } 92 catch (VelocityException ex) { 93 throw ex; 94 } 95 catch (RuntimeException ex) { 96 throw ex; 97 } 98 catch (Exception ex) { 99 logger.error("Why does VelocityEngine throw a generic checked exception, after all?", ex); 100 throw new VelocityException(ex.toString()); 101 } 102 } 103 104 117 public static String mergeTemplateIntoString( 118 VelocityEngine velocityEngine, String templateLocation, Map model) 119 throws VelocityException { 120 121 StringWriter result = new StringWriter (); 122 mergeTemplate(velocityEngine, templateLocation, model, result); 123 return result.toString(); 124 } 125 126 140 public static String mergeTemplateIntoString( 141 VelocityEngine velocityEngine, String templateLocation, String encoding, Map model) 142 throws VelocityException { 143 144 StringWriter result = new StringWriter (); 145 mergeTemplate(velocityEngine, templateLocation, encoding, model, result); 146 return result.toString(); 147 } 148 149 } 150 | Popular Tags |