KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > XMLTest


1 /*
2  * Copyright 2000,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.BufferedWriter JavaDoc;
18 import java.io.OutputStreamWriter JavaDoc;
19 import java.io.Writer JavaDoc;
20
21 import org.apache.velocity.VelocityContext;
22 import org.apache.velocity.Template;
23 import org.apache.velocity.app.Velocity;
24
25 import org.jdom.Document;
26 import org.jdom.input.SAXBuilder;
27
28
29 /**
30  * Example to show basic XML handling in a template.
31  *
32  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
33  * @version $Id: XMLTest.java,v 1.2.12.1 2004/03/04 00:18:30 geirm Exp $
34  */

35 public class XMLTest
36 {
37     public XMLTest( String JavaDoc templateFile)
38     {
39         Writer JavaDoc writer = null;
40
41         try
42         {
43             /*
44              * and now call init
45              */

46
47             Velocity.init();
48
49                        
50             /*
51              * build a Document from our xml
52              */

53
54             SAXBuilder builder;
55             Document root = null;
56
57             try
58             {
59                 builder = new SAXBuilder( "org.apache.xerces.parsers.SAXParser" );
60                 root = builder.build("test.xml");
61             }
62             catch( Exception JavaDoc ee)
63             {
64                 System.out.println("Exception building Document : " + ee);
65                 return;
66             }
67
68             /*
69              * now, make a Context object and populate it.
70              */

71
72             VelocityContext context = new VelocityContext();
73             context.put("root", root);
74
75             /*
76              * make a writer, and merge the template 'against' the context
77              */

78  
79             Template template = Velocity.getTemplate(templateFile);
80
81             writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(System.out));
82             template.merge( context , writer);
83         }
84         catch( Exception JavaDoc e )
85         {
86            System.out.println("Exception : " + e);
87         }
88         finally
89         {
90             if ( writer != null)
91             {
92                 try
93                 {
94                     writer.flush();
95                     writer.close();
96                 }
97                 catch( Exception JavaDoc ee )
98                 {
99                     System.out.println("Exception : " + ee );
100                 }
101             }
102         }
103     }
104
105     public static void main(String JavaDoc[] args)
106     {
107         XMLTest t;
108
109         if( args.length < 1 )
110         {
111             System.out.println("Usage : java XMLTest <templatename>");
112             return;
113         }
114
115         t = new XMLTest(args[0]);
116     }
117 }
118
119
Popular Tags