KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > campware > cream > modules > scheduledjobs > ShedVelocityTool


1 package org.campware.cream.modules.scheduledjobs;
2
3 import java.io.StringWriter JavaDoc;
4 import org.apache.commons.logging.*;
5 import org.apache.velocity.context.Context;
6 import org.apache.velocity.exception.*;
7 import org.apache.velocity.app.Velocity;
8
9 /**
10  * This tool allows access to velocity via the current context.
11  * The evaluate method allows the string results of a reference to be
12  * parsed at will using the evaluate() method.
13  *
14  * @author Bill Boland
15  */

16
17 public class ShedVelocityTool
18 {
19     static Log log = LogFactory.getLog( ShedVelocityTool.class );
20
21     /**
22      * Context used for evaluate method.
23      */

24     protected Context context;
25
26     /**
27      * Default constructor.
28      */

29     public ShedVelocityTool()
30     {
31     }
32
33     /**
34      * Constructs the tool and sets the context.
35      * @param c the Context object
36      */

37     public ShedVelocityTool( Context c )
38     {
39         context = c;
40     }
41
42     /**
43      * Sets the context that will be used by the tool.
44      * @param context the Context object
45      */

46     public void setContext( Context context )
47     {
48         this.context = context;
49     }
50
51     /**
52      * Get the context currently assigned to the tool
53      * @param c the Context object
54      */

55     public Context getContext()
56     {
57         return context;
58     }
59
60     /**
61      * A method to evaluate a template string using the assigned context.
62      * It is just a think wrapper around the Velocity.evaluate method.
63      * The context is usually the context in which the tool resides but it could
64      * be initialized with a different context.
65      * @param s the string to be evaluated (a string template)
66      * @return the resulting sting from the evaluation.
67      */

68     public String JavaDoc evaluate( String JavaDoc s )
69     {
70         if ( s != null )
71         {
72             log.debug( "evaluate: " + s );
73             StringWriter JavaDoc stringWriter = new StringWriter JavaDoc( s.length() );
74             try
75             {
76                 Velocity.evaluate( context, stringWriter, "ShedVelocityTool.evaluate", s );
77                 stringWriter.close();
78                 return stringWriter.toString();
79             }
80             catch( ResourceNotFoundException rnfe )
81             {
82                 log.error( "ResourceNotFoundException: " + rnfe, rnfe );
83             }
84             catch( ParseErrorException pee )
85             {
86                 log.error( "ParseErrorException: " + pee, pee );
87             }
88             catch( Exception JavaDoc e )
89             {
90                 log.error( "Exception: " + e, e );
91             }
92         }
93
94         return s;
95     }
96
97     protected void finalize()
98         throws Throwable JavaDoc
99     {
100         log.debug( "Finalizing Velocity Tool." );
101     }
102
103 }
Popular Tags