| 1 19 20 package za.org.coefficient.util.ejb; 21 22 import za.org.coefficient.core.Constants; 23 24 import org.apache.commons.lang.StringUtils; 25 26 import org.apache.velocity.Template; 27 import org.apache.velocity.VelocityContext; 28 import org.apache.velocity.app.Velocity; 29 import org.apache.velocity.context.Context; 30 31 import java.io.StringWriter ; 32 33 import java.text.SimpleDateFormat ; 34 35 import java.util.Map ; 36 import java.util.Properties ; 37 38 44 public class VelocityScreenUtil { 45 47 public static final String TEMPLATES = "/templates/"; 48 49 static { 50 initVelocity(); 51 } 52 53 55 73 public static StringBuffer getProcessedScreen(String templateName, 74 Map context) { 75 String fullTemplateName = 76 getCallingClassPackage() + TEMPLATES + templateName; 77 StringBuffer retVal = null; 78 try { 79 Template currTemplate = Velocity.getTemplate(fullTemplateName); 80 StringWriter sw = new StringWriter (); 81 context.put("dateFormat", 82 new SimpleDateFormat (Constants.SYSTEM_DATE_FORMAT)); 83 Context ctx = new VelocityContext(context); 84 currTemplate.merge(ctx, sw); 85 retVal = sw.getBuffer(); 86 } catch (Exception e) { 87 e.printStackTrace(); 88 } 89 90 return retVal; 91 } 92 93 public static StringBuffer getProcessedScreenFullyQualified( 94 String fullyQualifiedTemplateName, Map context) { 95 StringBuffer retVal = null; 96 try { 97 Template currTemplate = 98 Velocity.getTemplate(fullyQualifiedTemplateName); 99 StringWriter sw = new StringWriter (); 100 context.put("dateFormat", 101 new SimpleDateFormat (Constants.SYSTEM_DATE_FORMAT)); 102 Context ctx = new VelocityContext(context); 103 currTemplate.merge(ctx, sw); 104 retVal = sw.getBuffer(); 105 } catch (Exception e) { 106 e.printStackTrace(); 107 } 108 109 return retVal; 110 } 111 112 124 public static StringBuffer getProcessedString(String inString, Map context) { 125 StringBuffer retVal = null; 126 try { 127 StringWriter sw = new StringWriter (); 128 context.put("dateFormat", 129 new SimpleDateFormat (Constants.SYSTEM_DATE_FORMAT)); 130 Context ctx = new VelocityContext(context); 131 Velocity.evaluate(ctx, sw, "temp", inString); 132 retVal = sw.getBuffer(); 133 } catch (Exception e) { 134 e.printStackTrace(); 135 } 136 137 return retVal; 138 } 139 140 public static String getCallingClassPackage() { 141 StackTraceElement [] ste = new Exception ().getStackTrace(); 142 if ((ste == null) || (ste.length < 3)) { 143 return null; 144 } else { 145 String temp = ste[2].getClassName(); 146 147 return StringUtils.replace(temp.substring(0, temp.lastIndexOf(".")), 148 ".", "/"); 149 } 150 } 151 152 private static void initVelocity() { 153 try { 154 Properties props = new Properties (); 155 props.setProperty("resource.loader", "templateLoader"); 156 props.setProperty("templateLoader.loader.description", 157 "Project Engine loader"); 158 props.setProperty("templateLoader.resource.loader.class", 159 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 160 props.setProperty("velocimacro.permissions.allow.inline.local.scope", 161 "true"); 162 Velocity.init(props); 163 } catch (Exception ex) { 164 ex.printStackTrace(); 165 166 } 168 } 169 } 170 | Popular Tags |