|
|
Groovy example source code file (NodeAsHTMLPrinter.java)
This example Groovy source code file (NodeAsHTMLPrinter.java) is included in the DevDaily.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.
The Groovy NodeAsHTMLPrinter.java source code
/*
* Copyright 2003-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.antlr.treewalker;
import java.io.PrintStream;
import java.util.Stack;
import org.codehaus.groovy.antlr.GroovySourceAST;
import org.codehaus.groovy.antlr.parser.GroovyTokenTypes;
/**
* A visitor that prints a html tags of each node to the supplied PrintStream
*
* @author <a href="mailto:groovy@ross-rayner.com">Jeremy Rayner
* @version $Revision: 20442 $
*/
public class NodeAsHTMLPrinter extends VisitorAdapter {
private final String[] tokenNames;
private final PrintStream out;
private final Stack stack;
/**
* A visitor that prints a html tags, for each node, to the supplied PrintStream.
* @param out supplied PrintStream to output nodes to
* @param tokenNames an array of token names to use
*/
public NodeAsHTMLPrinter(PrintStream out,String[] tokenNames) {
this.tokenNames = tokenNames;
this.out = out;
this.stack = new Stack();
}
public void setUp() {
out.println("<html>");
}
public void visitDefault(GroovySourceAST t,int visit) {
if (visit == OPENING_VISIT) {
out.print("<code title=" + quote(tokenNames[t.getType()]) + ">");
} else if (visit == CLOSING_VISIT) {
out.print("</font>");
}
}
private String quote(String tokenName) {
if (tokenName.length() > 0 && tokenName.charAt(0) != '\'')
return "'" + tokenName + "'";
else
return "\"" + tokenName + "\"";
}
public void tearDown() {
out.println("</pre> |