• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.apache.velocity.test.view;
2 
3 /*
4  * Licensed to the Apache Software Foundation (ASF) under one
5  * or more contributor license agreements.  See the NOTICE file
6  * distributed with this work for additional information
7  * regarding copyright ownership.  The ASF licenses this file
8  * to you under the Apache License, Version 2.0 (the
9  * "License"); you may not use this file except in compliance
10  * with the License.  You may obtain a copy of the License at
11  *
12  *   http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17  * KIND, either express or implied.  See the License for the
18  * specific language governing permissions and limitations
19  * under the License.
20  */
21 
22 import org.apache.velocity.Template;
23 import org.apache.velocity.runtime.RuntimeSingleton;
24 import org.apache.velocity.runtime.parser.node.SimpleNode;
25 import org.apache.velocity.runtime.visitor.NodeViewMode;
26 
27 import java.io.BufferedReader;
28 import java.io.FileInputStream;
29 import java.io.InputStreamReader;
30 import java.io.PrintWriter;
31 
32 /**
33  * Simple class for dumping the AST for a template.
34  * Good for debugging and writing new directives.
35  */
36 public class TemplateNodeView
37 {
38     /**
39      * Root of the AST node structure that results from
40      * parsing a template.
41      */
42     private SimpleNode document;
43 
44     /**
45      * Visitor used to traverse the AST node structure
46      * and produce a visual representation of the
47      * node structure. Very good for debugging and
48      * writing new directives.
49      */
50     private NodeViewMode visitor;
51 
52     /**
53      * Default constructor: sets up the Velocity
54      * Runtime, creates the visitor for traversing
55      * the node structure and then produces the
56      * visual representation by the visitation.
57      */
TemplateNodeView(String templateFile)58     public TemplateNodeView(String templateFile)
59     {
60         try
61         {
62             RuntimeSingleton.init("velocity.properties");
63 
64             InputStreamReader isr = new InputStreamReader(
65                                        new FileInputStream(templateFile),
66                                        RuntimeSingleton.getString(RuntimeSingleton.INPUT_ENCODING));
67 
68             BufferedReader br = new BufferedReader( isr );
69 
70             Template tmpl = new Template();
71             tmpl.setName(templateFile);
72             document = RuntimeSingleton.parse( br, tmpl);
73 
74             visitor = new NodeViewMode();
75             visitor.setContext(null);
76             visitor.setWriter(new PrintWriter(System.out));
77             document.jjtAccept(visitor, null);
78         }
79         catch (Exception e)
80         {
81             System.out.println(e);
82             e.printStackTrace();
83         }
84     }
85 }
86