1 18 package org.apache.roller.ui.core.tags; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 import org.apache.velocity.VelocityContext; 23 import org.apache.velocity.app.Velocity; 24 import org.apache.velocity.app.VelocityEngine; 25 26 import java.io.StringWriter ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import javax.servlet.jsp.JspException ; 32 import javax.servlet.jsp.tagext.TagSupport ; 33 34 38 public abstract class VelocityTag extends HybridTag { 40 private static Log mLogger = 41 LogFactory.getFactory().getInstance(VelocityTag.class); 42 48 49 50 public abstract String getTemplateClasspath(); 51 52 53 public abstract void prepareContext( VelocityContext ctx ); 54 55 56 public VelocityTag() 57 { 58 } 59 60 63 public void release() 64 { 65 super.release(); 66 72 } 73 74 80 public int doStartTag(java.io.PrintWriter pw) 81 throws JspException 82 { 83 return TagSupport.EVAL_BODY_INCLUDE; 84 } 85 86 92 public int doEndTag(java.io.PrintWriter pw) throws JspException 93 { 94 String myResource= getVelocityClasspathResource(getTemplateClasspath()); 95 96 try 97 { 98 VelocityContext myVelocityContext = getVelocityContext(); 99 100 prepareContext( myVelocityContext ); 102 103 StringWriter myStringWriter = new StringWriter (); 104 105 Velocity.mergeTemplate(myResource, 106 org.apache.velocity.runtime.RuntimeSingleton.getString( 107 Velocity.INPUT_ENCODING, Velocity.ENCODING_DEFAULT), 108 myVelocityContext, myStringWriter); 109 110 pw.println(myStringWriter); 111 112 return EVAL_PAGE; 113 } 114 catch (Exception e) 115 { 116 mLogger.error("Unexpected exception",e); 117 118 throw new JspException ( 119 "Exception; TEMPLATE_CLASSPATH=" + getTemplateClasspath() 120 + "; exception=" + e.getMessage()); 121 } 122 } 123 124 public String doStandaloneTest(Map inMap) 125 throws Exception 126 { 127 String resource = getVelocityClasspathResource( 128 getTemplateClasspath()); 129 VelocityContext context = getVelocityContext(); 130 StringWriter w = new StringWriter (); 131 Iterator iter = inMap.keySet().iterator(); 132 133 while (iter.hasNext()) 134 { 135 Object o = iter.next(); 136 137 context.put(o.toString(), inMap.get(o)); 138 } 139 140 Velocity.mergeTemplate(resource, 141 org.apache.velocity.runtime.RuntimeSingleton.getString( 142 Velocity.INPUT_ENCODING, Velocity.ENCODING_DEFAULT), 143 context, w); 144 145 return w.toString(); 146 } 147 148 protected static VelocityContext getVelocityContext() 149 throws java.lang.Exception 150 { 151 Velocity.addProperty(Velocity.RESOURCE_LOADER, "classpath"); 152 Velocity.addProperty("classpath." + Velocity.RESOURCE_LOADER + ".class", 153 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); 154 Velocity.setProperty( 155 VelocityEngine.COUNTER_INITIAL_VALUE, new Integer (0)); 156 Velocity.init(); 157 158 return new VelocityContext(); 159 } 160 161 168 protected static String getVelocityClasspathResource( 169 String inClasspathString) 170 { 171 if (inClasspathString.length() < 1) 172 { 173 return inClasspathString; 174 } 175 else 176 { 177 if (inClasspathString.startsWith("/")) 178 { 179 return inClasspathString.substring(1); 180 } 181 else 182 { 183 return inClasspathString; 184 } 185 } 186 } 187 } 188 | Popular Tags |