• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.apache.velocity.example;
2 /*
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  */
20 
21 import org.apache.velocity.Template;
22 import org.apache.velocity.VelocityContext;
23 import org.apache.velocity.app.Velocity;
24 
25 import org.jdom.Document;
26 import org.jdom.input.SAXBuilder;
27 
28 import java.io.BufferedWriter;
29 import java.io.OutputStreamWriter;
30 import java.io.Writer;
31 
32 
33 /**
34  * Example to show basic XML handling in a template.
35  *
36  * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
37  * @version $Id$
38  */
39 public class XMLTest
40 {
XMLTest( String templateFile)41     public XMLTest( String templateFile)
42     {
43         Writer writer = null;
44 
45         try
46         {
47             /*
48              *  and now call init
49              */
50 
51             Velocity.init();
52 
53 
54             /*
55              * build a Document from our xml
56              */
57 
58             SAXBuilder builder;
59             Document root = null;
60 
61             try
62             {
63                 builder = new SAXBuilder();
64                 root = builder.build("test.xml");
65             }
66             catch( Exception ee)
67             {
68                 System.out.println("Exception building Document : " + ee);
69                 return;
70             }
71 
72             /*
73              * now, make a Context object and populate it.
74              */
75 
76             VelocityContext context = new VelocityContext();
77             context.put("root", root);
78 
79             /*
80              *  make a writer, and merge the template 'against' the context
81              */
82 
83             Template template = Velocity.getTemplate(templateFile);
84 
85             writer = new BufferedWriter(new OutputStreamWriter(System.out));
86             template.merge( context , writer);
87         }
88         catch( Exception e )
89         {
90            System.out.println("Exception : " + e);
91         }
92         finally
93         {
94             if ( writer != null)
95             {
96                 try
97                 {
98                     writer.flush();
99                     writer.close();
100                 }
101                 catch( Exception ee )
102                 {
103                     System.out.println("Exception : " + ee );
104                 }
105             }
106         }
107     }
108 
main(String[] args)109     public static void main(String[] args)
110     {
111         XMLTest t;
112 
113         if( args.length < 1 )
114         {
115             System.out.println("Usage : java XMLTest <templatename>");
116             return;
117         }
118 
119         t = new XMLTest(args[0]);
120     }
121 }
122