KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > tags > VelocityTag


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

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 JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.jsp.JspException JavaDoc;
32 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
33
34 /**
35  * @author David M Johnson, Gregory Gerard
36  * @version 1.0
37  */

38 public abstract class VelocityTag extends HybridTag // TagSupport
39
{
40     private static Log mLogger =
41             LogFactory.getFactory().getInstance(VelocityTag.class);
42 /*
43     private String title;
44     private String subtitle;
45     private List images;
46     private Map ingredients;
47 */

48
49     /** Return path to Velocity template to render this tag */
50     public abstract String JavaDoc getTemplateClasspath();
51
52     /** Prepare context for execution */
53     public abstract void prepareContext( VelocityContext ctx );
54
55
56     public VelocityTag()
57     {
58     }
59
60     /**
61      * Release any resources we might have acquired.
62      */

63     public void release()
64     {
65         super.release();
66         /*
67         title = null;
68         subtitle = null;
69         images = null;
70         ingredients = null;
71         */

72     }
73
74     /**
75      * Evaluate any tags inside us. This will also allow us to have child tags
76      * send us messages.
77      * @return
78      * @throws JspException
79      */

80     public int doStartTag(java.io.PrintWriter JavaDoc pw)
81         throws JspException JavaDoc
82     {
83         return TagSupport.EVAL_BODY_INCLUDE;
84     }
85
86     /**
87      * Check all all the public properties that must be set by now, either by
88      * tag arguments or tag children.
89      * @return
90      * @throws JspException
91      */

92     public int doEndTag(java.io.PrintWriter JavaDoc pw) throws JspException JavaDoc
93     {
94         String JavaDoc myResource= getVelocityClasspathResource(getTemplateClasspath());
95
96         try
97         {
98             VelocityContext myVelocityContext = getVelocityContext();
99
100             // ask concrete class to prepare context
101
prepareContext( myVelocityContext );
102
103             StringWriter JavaDoc myStringWriter = new StringWriter JavaDoc();
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 JavaDoc e)
115         {
116             mLogger.error("Unexpected exception",e);
117
118             throw new JspException JavaDoc(
119                 "Exception; TEMPLATE_CLASSPATH=" + getTemplateClasspath()
120                 + "; exception=" + e.getMessage());
121         }
122     }
123
124     public String JavaDoc doStandaloneTest(Map JavaDoc inMap)
125         throws Exception JavaDoc
126     {
127         String JavaDoc resource = getVelocityClasspathResource(
128                                         getTemplateClasspath());
129         VelocityContext context = getVelocityContext();
130         StringWriter JavaDoc w = new StringWriter JavaDoc();
131         Iterator JavaDoc iter = inMap.keySet().iterator();
132
133         while (iter.hasNext())
134         {
135             Object JavaDoc 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 JavaDoc
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 JavaDoc(0));
156         Velocity.init();
157
158         return new VelocityContext();
159     }
160
161     /**
162      * Velocity wants classpath resources to not have a leading slash. This is
163      * contrary to how one would normally load a resource off the classpath
164      * (e.g. SampleTag.class.getResourceAsStream("/extag/resources/test.html")
165      * @param inClasspathString
166      * @return
167      */

168     protected static String JavaDoc getVelocityClasspathResource(
169         String JavaDoc 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