KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Example2


1 /*
2  * Copyright 2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not 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.
15  */

16
17 import java.io.StringWriter JavaDoc;
18 import java.util.Properties JavaDoc;
19 import org.apache.velocity.app.Velocity;
20 import org.apache.velocity.VelocityContext;
21
22 import org.apache.velocity.exception.ParseErrorException;
23 import org.apache.velocity.exception.MethodInvocationException;
24
25 /**
26  * This class is a simple demonstration of how the Velocity Template Engine
27  * can be used in a standalone application using the Velocity utility class.
28  *
29  * It demonstrates two of the 'helper' methods found in the org.apache.velocity.util.Velocity
30  * class, mergeTemplate() and evaluate().
31  *
32  *
33  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
34  * @version $Id: Example2.java,v 1.3.14.1 2004/03/04 00:18:29 geirm Exp $
35  */

36
37 public class Example2
38 {
39     public static void main( String JavaDoc args[] )
40     {
41         /* first, we init the runtime engine. Defaults are fine. */
42         
43         try
44         {
45             Velocity.init();
46         }
47         catch(Exception JavaDoc e)
48         {
49             System.out.println("Problem initializing Velocity : " + e );
50             return;
51         }
52
53         /* lets make a Context and put data into it */
54
55         VelocityContext context = new VelocityContext();
56
57         context.put("name", "Velocity");
58         context.put("project", "Jakarta");
59         
60         /* lets render a template */
61
62         StringWriter JavaDoc w = new StringWriter JavaDoc();
63
64         try
65         {
66             Velocity.mergeTemplate("example2.vm", context, w );
67         }
68         catch (Exception JavaDoc e )
69         {
70             System.out.println("Problem merging template : " + e );
71         }
72
73         System.out.println(" template : " + w );
74
75         /*
76          * lets dynamically 'create' our template
77          * and use the evaluate() method to render it
78          */

79
80         String JavaDoc s = "We are using $project $name to render this.";
81         w = new StringWriter JavaDoc();
82
83         try
84         {
85             Velocity.evaluate( context, w, "mystring", s );
86         }
87         catch( ParseErrorException pee )
88         {
89             /*
90              * thrown if something is wrong with the
91              * syntax of our template string
92              */

93             System.out.println("ParseErrorException : " + pee );
94         }
95         catch( MethodInvocationException mee )
96         {
97             /*
98              * thrown if a method of a reference
99              * called by the template
100              * throws an exception. That won't happen here
101              * as we aren't calling any methods in this
102              * example, but we have to catch them anyway
103              */

104             System.out.println("MethodInvocationException : " + mee );
105         }
106         catch( Exception JavaDoc e )
107         {
108             System.out.println("Exception : " + e );
109         }
110
111         System.out.println(" string : " + w );
112     }
113 }
114
Popular Tags