01: package org.apache.velocity.test.view;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "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: * 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 template) {
57: try {
58: RuntimeSingleton.init("velocity.properties");
59:
60: InputStreamReader isr = new InputStreamReader(
61: new FileInputStream(template), RuntimeSingleton
62: .getString(RuntimeSingleton.INPUT_ENCODING));
63:
64: BufferedReader br = new BufferedReader(isr);
65:
66: document = RuntimeSingleton.parse(br, template);
67:
68: visitor = new NodeViewMode();
69: visitor.setContext(null);
70: visitor.setWriter(new PrintWriter(System.out));
71: document.jjtAccept(visitor, null);
72: } catch (Exception e) {
73: System.out.println(e);
74: e.printStackTrace();
75: }
76: }
77: }
|