KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > percederberg > grammatica > TreePrinter


1 /*
2  * TreePrinter.java
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * as published by the Free Software Foundation; either version 2.1
7  * of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
17  * MA 02111-1307, USA.
18  *
19  * Copyright (c) 2003-2005 Per Cederberg. All rights reserved.
20  */

21
22 package net.percederberg.grammatica;
23
24 import java.io.OutputStream JavaDoc;
25 import java.io.PrintWriter JavaDoc;
26 import java.io.Writer JavaDoc;
27
28 import net.percederberg.grammatica.parser.Analyzer;
29 import net.percederberg.grammatica.parser.Node;
30
31 /**
32  * A parse tree printer. This class prints the parse tree while it is
33  * being parsed.
34  *
35  * @author Per Cederberg, <per at percederberg dot net>
36  * @version 1.4
37  * @since 1.4
38  */

39 public class TreePrinter extends Analyzer {
40
41     /**
42      * The current indentation level.
43      */

44     private int indentation = 0;
45
46     /**
47      * The output stream to use.
48      */

49     private PrintWriter JavaDoc output;
50
51     /**
52      * Creates a new parse tree printer.
53      *
54      * @param output the output stream to use
55      */

56     public TreePrinter(OutputStream JavaDoc output) {
57         this(new PrintWriter JavaDoc(output));
58     }
59
60     /**
61      * Creates a new parse tree printer.
62      *
63      * @param output the output stream to use
64      */

65     public TreePrinter(Writer JavaDoc output) {
66         if (output instanceof PrintWriter JavaDoc) {
67             this.output = (PrintWriter JavaDoc) output;
68         } else {
69             this.output = new PrintWriter JavaDoc(output);
70         }
71     }
72
73     /**
74      * Called when entering a parse tree node. By default this method
75      * does nothing. A subclass can override this method to handle
76      * each node separately.
77      *
78      * @param node the node being entered
79      */

80     protected void enter(Node node) {
81         for (int i = 0; i < indentation; i++) {
82             output.print(" ");
83         }
84         output.println(node.toString());
85         output.flush();
86         indentation++;
87     }
88
89     /**
90      * Called when exiting a parse tree node. By default this method
91      * returns the node. A subclass can override this method to handle
92      * each node separately. If no parse tree should be created, this
93      * method should return null.
94      *
95      * @param node the node being exited
96      *
97      * @return the node to add to the parse tree, or
98      * null to remove the node
99      */

100     protected Node exit(Node node) {
101         indentation--;
102         return null;
103     }
104 }
105
Popular Tags