1 package org.roller.presentation.tags; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 import org.apache.velocity.VelocityContext; 6 import org.apache.velocity.app.Velocity; 7 import org.apache.velocity.app.VelocityEngine; 8 9 import java.io.StringWriter ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Map ; 13 14 import javax.servlet.jsp.JspException ; 15 import javax.servlet.jsp.tagext.TagSupport ; 16 17 21 public abstract class VelocityTag extends HybridTag { 23 private static Log mLogger = 24 LogFactory.getFactory().getInstance(VelocityTag.class); 25 31 32 33 public abstract String getTemplateClasspath(); 34 35 36 public abstract void prepareContext( VelocityContext ctx ); 37 38 39 public VelocityTag() 40 { 41 } 42 43 46 public void release() 47 { 48 super.release(); 49 55 } 56 57 63 public int doStartTag(java.io.PrintWriter pw) 64 throws JspException 65 { 66 return TagSupport.EVAL_BODY_INCLUDE; 67 } 68 69 75 public int doEndTag(java.io.PrintWriter pw) throws JspException 76 { 77 String myResource= getVelocityClasspathResource(getTemplateClasspath()); 78 79 try 80 { 81 VelocityContext myVelocityContext = getVelocityContext(); 82 83 prepareContext( myVelocityContext ); 85 86 StringWriter myStringWriter = new StringWriter (); 87 88 Velocity.mergeTemplate(myResource, 89 org.apache.velocity.runtime.RuntimeSingleton.getString( 90 Velocity.INPUT_ENCODING, Velocity.ENCODING_DEFAULT), 91 myVelocityContext, myStringWriter); 92 93 pw.println(myStringWriter); 94 95 return EVAL_PAGE; 96 } 97 catch (Exception e) 98 { 99 mLogger.error("Unexpected exception",e); 100 101 throw new JspException ( 102 "Exception; TEMPLATE_CLASSPATH=" + getTemplateClasspath() 103 + "; exception=" + e.getMessage()); 104 } 105 } 106 107 public String doStandaloneTest(Map inMap) 108 throws Exception 109 { 110 String resource = getVelocityClasspathResource( 111 getTemplateClasspath()); 112 VelocityContext context = getVelocityContext(); 113 StringWriter w = new StringWriter (); 114 Iterator iter = inMap.keySet().iterator(); 115 116 while (iter.hasNext()) 117 { 118 Object o = iter.next(); 119 120 context.put(o.toString(), inMap.get(o)); 121 } 122 123 Velocity.mergeTemplate(resource, 124 org.apache.velocity.runtime.RuntimeSingleton.getString( 125 Velocity.INPUT_ENCODING, Velocity.ENCODING_DEFAULT), 126 context, w); 127 128 return w.toString(); 129 } 130 131 protected static VelocityContext getVelocityContext() 132 throws java.lang.Exception 133 { 134 Velocity.addProperty(Velocity.RESOURCE_LOADER, "classpath"); 135 Velocity.addProperty("classpath." + Velocity.RESOURCE_LOADER + ".class", 136 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 137 Velocity.setProperty( 138 VelocityEngine.COUNTER_INITIAL_VALUE, new Integer (0)); 139 Velocity.init(); 140 141 return new VelocityContext(); 142 } 143 144 151 protected static String getVelocityClasspathResource( 152 String inClasspathString) 153 { 154 if (inClasspathString.length() < 1) 155 { 156 return inClasspathString; 157 } 158 else 159 { 160 if (inClasspathString.startsWith("/")) 161 { 162 return inClasspathString.substring(1); 163 } 164 else 165 { 166 return inClasspathString; 167 } 168 } 169 } 170 } 171 | Popular Tags |