/******************************************************************************
 * Copyright (c) 2007 Stefan Franke, Robert Hanussek, Benjamin Keil,          *
 *                    Steffen Kieß, Johannes Langauf,                         *
 *                    Christoph Marian Müller, Igor Podolskiy,                *
 *                    Tilmann Scheller, Michael Starzmann, Markus Wittlinger  *
 * All rights reserved. This program and the accompanying materials           *
 * are made available under the terms of the Eclipse Public License v1.0      *
 * which accompanies this distribution, and is available at                   *
 * http://www.eclipse.org/legal/epl-v10.html                                  *
 ******************************************************************************/

// Generated by JTB 1.3.2
package org.codecover.instrumentation.xampil.visitor;

import org.codecover.instrumentation.xampil.syntaxtree.Node;
import org.codecover.instrumentation.xampil.syntaxtree.NodeToken;

/**
 * Dumps the syntax tree to a StringWriter using the location information in
 * each NodeToken.
 * 
 * @see #convertToString(Node)
 */
public class StringTreeDumper extends DepthFirstVisitor {
    private StringBuilder writer = new StringBuilder();

    /**
     * Constructor.
     */
    public StringTreeDumper() {
        reset();
    }

    /**
     * The internal {@link StringBuilder} is replaced by an empty instance.
     */
    public void reset() {
        this.writer = new StringBuilder();
    }

    /**
     * @return The String, that has been captured through all the visiting since
     *         the last {@link #reset()} and {@link String#trim()}.
     */
    public String getContent() {
        return this.writer.toString().trim();
    }

    /**
     * @return The String, that has been captured through all the visiting since
     *         the last {@link #reset()} without {@link String#trim()};
     */
    public String getContentUntrimmed() {
        return this.writer.toString();
    }

    /**
     * Dumps the current NodeToken to the {@link #writer}.
     */
    @Override
    public void visit(NodeToken n) {
        if (n.numSpecials() > 0) {
            for (NodeToken nt : n.specialTokens) {
                this.writer.append(nt.tokenImage);
            }
        }

        this.writer.append(n.tokenImage);
    }

    /**
     * This method converts a {@link Node} to a String by capturing all
     * {@link NodeToken}s.<br>
     * <br>
     * This method calls:
     * 
     * <pre>
     * TreeStringDumper treeStringDumper = new TreeStringDumper();
     * n.accept(treeStringDumper);
     * return treeStringDumper.getContent();
     * </pre>
     * 
     * @param n
     *            The Node to convert.
     * 
     * @return The captured String.
     */
    public static String convertToString(Node n) {
        StringTreeDumper treeStringDumper = new StringTreeDumper();
        n.accept(treeStringDumper);
        return treeStringDumper.getContent();
    }
}

