KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > roller > presentation > tags > VelocityTag


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 JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13
14 import javax.servlet.jsp.JspException JavaDoc;
15 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
16
17 /**
18  * @author David M Johnson, Gregory Gerard
19  * @version 1.0
20  */

21 public abstract class VelocityTag extends HybridTag // TagSupport
22
{
23     private static Log mLogger =
24         LogFactory.getFactory().getInstance(VelocityTag.class);
25 /*
26     private String title;
27     private String subtitle;
28     private List images;
29     private Map ingredients;
30 */

31
32     /** Return path to Velocity template to render this tag */
33     public abstract String JavaDoc getTemplateClasspath();
34
35     /** Prepare context for execution */
36     public abstract void prepareContext( VelocityContext ctx );
37
38
39     public VelocityTag()
40     {
41     }
42
43     /**
44      * Release any resources we might have acquired.
45      */

46     public void release()
47     {
48         super.release();
49         /*
50         title = null;
51         subtitle = null;
52         images = null;
53         ingredients = null;
54         */

55     }
56
57     /**
58      * Evaluate any tags inside us. This will also allow us to have child tags
59      * send us messages.
60      * @return
61      * @throws JspException
62      */

63     public int doStartTag(java.io.PrintWriter JavaDoc pw)
64         throws JspException JavaDoc
65     {
66         return TagSupport.EVAL_BODY_INCLUDE;
67     }
68
69     /**
70      * Check all all the public properties that must be set by now, either by
71      * tag arguments or tag children.
72      * @return
73      * @throws JspException
74      */

75     public int doEndTag(java.io.PrintWriter JavaDoc pw) throws JspException JavaDoc
76     {
77         String JavaDoc myResource= getVelocityClasspathResource(getTemplateClasspath());
78
79         try
80         {
81             VelocityContext myVelocityContext = getVelocityContext();
82
83             // ask concrete class to prepare context
84
prepareContext( myVelocityContext );
85
86             StringWriter JavaDoc myStringWriter = new StringWriter JavaDoc();
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 JavaDoc e)
98         {
99             mLogger.error("Unexpected exception",e);
100
101             throw new JspException JavaDoc(
102                 "Exception; TEMPLATE_CLASSPATH=" + getTemplateClasspath()
103                 + "; exception=" + e.getMessage());
104         }
105     }
106
107     public String JavaDoc doStandaloneTest(Map JavaDoc inMap)
108         throws Exception JavaDoc
109     {
110         String JavaDoc resource = getVelocityClasspathResource(
111                                         getTemplateClasspath());
112         VelocityContext context = getVelocityContext();
113         StringWriter JavaDoc w = new StringWriter JavaDoc();
114         Iterator JavaDoc iter = inMap.keySet().iterator();
115
116         while (iter.hasNext())
117         {
118             Object JavaDoc 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 JavaDoc
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 JavaDoc(0));
139         Velocity.init();
140
141         return new VelocityContext();
142     }
143
144     /**
145      * Velocity wants classpath resources to not have a leading slash. This is
146      * contrary to how one would normally load a resource off the classpath
147      * (e.g. SampleTag.class.getResourceAsStream("/extag/resources/test.html")
148      * @param inClasspathString
149      * @return
150      */

151     protected static String JavaDoc getVelocityClasspathResource(
152         String JavaDoc 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