001 
002 /*
003  *  JavaDoq 1.0 - DOCUment JAVA In Source
004  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.javadoq.com>
005  *  
006  *  This program is free software: you can redistribute it and/or modify
007  *  it under the terms of the GNU Affero General Public License as published by
008  *  the Free Software Foundation, either version 3 of the License, or
009  *  (at your option) any later version.
010  *  
011  *  This program is distributed in the hope that it will be useful,
012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014  *  GNU Affero General Public License for more details.
015  *  
016  *  You should have received a copy of the GNU Affero General Public License
017  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
018  */
019 
020 package com.javadoq.jjtree.ast;
021 
022 import com.javadoq.jjtree.Token;
023 
024 /**
025  * <p>An abstract base class for JJTree visitors to transform JJTree node at token level.</p>
026  * 
027  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.javadoq.com" target="_blank">http://www.javadoq.com</a>
028  */
029 public abstract class TokenTransformVisitor extends TokenVisitor
030 {
031     /**
032      * <p>Visits a token before transformation.</p>
033      * @param t The JJTree token to visit.
034      * @since 1.0
035      */
036     protected abstract void start(Token t);
037     /**
038      * <p>Visits a token after transformation.</p>
039      * @param t The JJTree token to visit.
040      * @since 1.0
041      */
042     protected abstract void close(Token t);
043     /**
044      * <p>Transforms a token.</p>
045      * @param t The JJTree token to output.
046      * @since 1.0
047      */
048     protected abstract void write(Token t);
049     /**
050      * <p>Transforms a string.</p>
051      * @param s The string to output.
052      * @since 1.0
053      */
054     protected abstract void write(String s);
055 
056     private Token visited = null;
057 
058     /**
059      * <p>Visits a token.</p>
060      * @param t The JJTree token to visit.
061      * @since 1.0
062      */
063     @Override
064     protected void visit(Token t) {
065         if (t == visited) {
066             return;
067         }
068         visitSpecial(t.specialToken);
069         visitToken(t);
070     }
071 
072     /**
073      * <p>Transforms a special token.</p>
074      * @param t The JJTree special token to transform.
075      * @since 1.0
076      */
077     protected void visitSpecial(Token t) {
078         if (t != null) {
079             while (t.specialToken != null) {
080                 t = t.specialToken;
081             }
082             while (t != null) {
083                 start(t);
084                 write(t);
085                 close(t);
086                 t = t.next;
087             }
088         }
089     }
090 
091     /**
092      * <p>Transforms a token.</p>
093      * @param t The JJTree token to transform.
094      * @since 1.0
095      */
096     protected void visitToken(Token t) {
097         if (t == visited) {
098             return;
099         }
100         visited = t;
101         start(t);
102         write(t);
103         close(t);
104     }
105 }