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 java.io.BufferedReader;
23  import java.io.FileInputStream;
24  import java.io.InputStreamReader;
25  import java.io.PrintWriter;
26  
27  import org.apache.velocity.runtime.RuntimeSingleton;
28  import org.apache.velocity.runtime.parser.node.SimpleNode;
29  import org.apache.velocity.runtime.visitor.NodeViewMode;
30  
31  /**
32   * Simple class for dumping the AST for a template.
33   * Good for debugging and writing new directives.
34   */
35  public class TemplateNodeView
36  {
37      /**
38       * Root of the AST node structure that results from
39       * parsing a template.
40       */
41      private SimpleNode document;
42  
43      /**
44       * Visitor used to traverse the AST node structure
45       * and produce a visual representation of the
46       * node structure. Very good for debugging and
47       * writing new directives.
48       */
49      private NodeViewMode visitor;
50  
51      /**
52       * Default constructor: sets up the Velocity
53       * Runtime, creates the visitor for traversing
54       * the node structure and then produces the
55       * visual representation by the visitation.
56       */
57      public TemplateNodeView(String template)
58      {
59          try
60          {
61              RuntimeSingleton.init("velocity.properties");
62  
63              InputStreamReader isr = new InputStreamReader(
64                                         new FileInputStream(template),
65                                         RuntimeSingleton.getString(RuntimeSingleton.INPUT_ENCODING));
66  
67              BufferedReader br = new BufferedReader( isr );
68  
69              document = RuntimeSingleton.parse( br, template);
70  
71              visitor = new NodeViewMode();
72              visitor.setContext(null);
73              visitor.setWriter(new PrintWriter(System.out));
74              document.jjtAccept(visitor, null);
75          }
76          catch (Exception e)
77          {
78              System.out.println(e);
79              e.printStackTrace();
80          }
81      }
82  }