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
022 import org.codehaus.groovy.antlr.GroovySourceAST;
023
024 /**
025 * A simple antlr AST visitor that outputs the tokenName of each node in a pseudo xml style.
026 *
027 * @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner</a>
028 * @version $Revision: 4032 $
029 */
030
031 public class NodePrinter extends VisitorAdapter {
032 private String[] tokenNames;
033 private PrintStream out;
034
035 /**
036 * A visitor that prints a pseudo xml output to the supplied PrintStream
037 * @param out supplied PrintStream to output nodes to
038 * @param tokenNames an array of token names to use
039 */
040 public NodePrinter(PrintStream out,String[] tokenNames) {
041 this.tokenNames = tokenNames;
042 this.out = out;
043 }
044
045 public void visitDefault(GroovySourceAST t,int visit) {
046 if (visit == OPENING_VISIT) {
047 out.print("<"+ tokenNames[t.getType()] + ">");
048 } else {
049 out.print("</" + tokenNames[t.getType()] + ">");
050 }
051 }
052 }