KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > view > TemplateNodeView


1 package org.apache.velocity.test.view;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.BufferedReader JavaDoc;
23 import java.io.InputStreamReader JavaDoc;
24
25 import org.apache.velocity.runtime.RuntimeSingleton;
26
27 import org.apache.velocity.runtime.visitor.NodeViewMode;
28 import org.apache.velocity.runtime.parser.node.SimpleNode;
29
30 /**
31  * Simple class for dumping the AST for a template.
32  * Good for debugging and writing new directives.
33  */

34 public class TemplateNodeView
35 {
36     /**
37      * Root of the AST node structure that results from
38      * parsing a template.
39      */

40     private SimpleNode document;
41     
42     /**
43      * Visitor used to traverse the AST node structure
44      * and produce a visual representation of the
45      * node structure. Very good for debugging and
46      * writing new directives.
47      */

48     private NodeViewMode visitor;
49
50     /**
51      * Default constructor: sets up the Velocity
52      * Runtime, creates the visitor for traversing
53      * the node structure and then produces the
54      * visual representation by the visitation.
55      */

56     public TemplateNodeView(String JavaDoc template)
57     {
58         try
59         {
60             RuntimeSingleton.init("velocity.properties");
61
62             InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(
63                                        new FileInputStream JavaDoc(template),
64                                        RuntimeSingleton.getString(RuntimeSingleton.INPUT_ENCODING));
65
66             BufferedReader JavaDoc br = new BufferedReader JavaDoc( isr );
67                                          
68             document = RuntimeSingleton.parse( br, template);
69
70             visitor = new NodeViewMode();
71             visitor.setContext(null);
72             visitor.setWriter(new PrintWriter JavaDoc(System.out));
73             document.jjtAccept(visitor, null);
74         }
75         catch (Exception JavaDoc e)
76         {
77             System.out.println(e);
78             e.printStackTrace();
79         }
80     }
81
82     /** For testing */
83     public static void main(String JavaDoc args[])
84     {
85         TemplateNodeView v = new TemplateNodeView(args[0]);
86     }
87 }
88
Popular Tags