001    /**
002     *
003     * Copyright 2005 Jeremy Rayner
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     * http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     *
017     **/
018    package org.codehaus.groovy.antlr.treewalker;
019    
020    import java.io.PrintStream;
021    import org.codehaus.groovy.antlr.GroovySourceAST;
022    
023    /**
024     * A simple antlr AST visitor that outputs the tokenName of each node in a pseudo xml style.
025     *
026     * @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
027     * @version $Revision: 1.1 $
028     */
029    
030    public class NodePrinter extends VisitorAdapter {
031        private String[] tokenNames;
032        private PrintStream out;
033    
034        /**
035         * A visitor that prints a pseudo xml output to the supplied PrintStream
036         * @param out supplied PrintStream to output nodes to
037         * @param tokenNames an array of token names to use
038         */
039        public NodePrinter(PrintStream out,String[] tokenNames) {
040            this.tokenNames = tokenNames;
041            this.out = out;
042        }
043    
044        public void visitDefault(GroovySourceAST t,int visit) {
045            if (visit == OPENING_VISIT) {
046                out.print("<"+ tokenNames[t.getType()] + ">");
047            } else {
048                out.print("</" + tokenNames[t.getType()] + ">");
049            }
050        }
051    
052    }