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 java.util.ArrayList;
023 import java.util.List;
024 
025 import com.javadoq.JavaNestedType;
026 import com.javadoq.JavaType;
027 import com.javadoq.SourceFile;
028 import com.javadoq.jjtree.ASTCompilationUnit;
029 import com.javadoq.jjtree.ASTGrammar;
030 import com.javadoq.jjtree.ASTImportName;
031 import com.javadoq.jjtree.ASTName;
032 import com.javadoq.jjtree.ASTNewName;
033 import com.javadoq.jjtree.ASTNewType;
034 import com.javadoq.jjtree.ASTPackageName;
035 import com.javadoq.jjtree.ASTSuperList;
036 import com.javadoq.jjtree.JJTreeParserVisitor;
037 import com.javadoq.jjtree.SimpleNode;
038 
039 /**
040  * <p>A JJTree visitor to collect new Java types defined in a source file.</p>
041  * 
042  * @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>
043  */
044 public class NewTypeVisitor implements JJTreeParserVisitor
045 {
046     /**
047      * <p>The source file where this visitor is collecting new Java types.</p>
048      * @since 1.0
049      */
050     public final SourceFile file;
051 
052     /**
053      * <p>Constructs a {@link NewTypeVisitor}.</p>
054      * @param file The source file where this visitor is collecting new Java types.
055      * @since 1.0
056      */
057     public NewTypeVisitor(SourceFile file) {
058         this.file = file;
059     }
060 
061     private List<JavaType> stack = new ArrayList<JavaType>();
062 
063     /**
064      * <p>Visits a JJTree node with the specific type.</p>
065      * @param node The JJTree node to visit.
066      * @param data Visitor data.
067      * @return A visitor data.
068      * @since 1.0
069      */
070     public Object visit(ASTNewType node, Object data) {
071         JavaType t;
072         if (stack.isEmpty()) {
073             t = new JavaType(file, node.name);
074         } else {
075             JavaType outer = stack.get(stack.size() - 1);
076             t = new JavaNestedType(outer, node.name);
077             outer.inners.put(t.name, (JavaNestedType)t);
078             
079         }
080         stack.add(t);
081         data = node.childrenAccept(this, data);
082         stack.remove(stack.size() - 1);
083         return data;
084     }
085 
086     /**
087      * <p>Visits a JJTree node with the specific type.</p>
088      * @param node The JJTree node to visit.
089      * @param data Visitor data.
090      * @return A visitor data.
091      * @since 1.0
092      */
093     public Object visit(SimpleNode node, Object data) {
094         if (node instanceof ASTCompilationUnit) {
095             return visit((ASTCompilationUnit)node, data);
096         } else if (node instanceof ASTNewType) {
097             return visit((ASTNewType)node, data);
098         }
099         return data;
100     }
101 
102     /**
103      * <p>Visits a JJTree node with the specific type.</p>
104      * @param node The JJTree node to visit.
105      * @param data Visitor data.
106      * @return A visitor data.
107      * @since 1.0
108      */
109     public Object visit(ASTCompilationUnit node, Object data) {
110         if (node.packageName.equals(file.pckg.name.text)) {
111             return node.childrenAccept(this, data);
112         }
113         return data;
114     }
115 
116     /**
117      * <p>Visits a JJTree node with the specific type.</p>
118      * <p>This method simply throws an UnsupportedOperationException.</p>
119      * @param node The JJTree node to visit.
120      * @param data Visitor data.
121      * @return A visitor data.
122      * @since 1.0
123      */
124     public Object visit(ASTName node, Object data) {
125         throw new UnsupportedOperationException();
126     }
127 
128     /**
129      * <p>Visits a JJTree node with the specific type.</p>
130      * <p>This method simply throws an UnsupportedOperationException.</p>
131      * @param node The JJTree node to visit.
132      * @param data Visitor data.
133      * @return A visitor data.
134      * @since 1.0
135      */
136     public Object visit(ASTImportName node, Object data) {
137         throw new UnsupportedOperationException();
138     }
139 
140     /**
141      * <p>Visits a JJTree node with the specific type.</p>
142      * <p>This method simply throws an UnsupportedOperationException.</p>
143      * @param node The JJTree node to visit.
144      * @param data Visitor data.
145      * @return A visitor data.
146      * @since 1.0
147      */
148     public Object visit(ASTNewName node, Object data) {
149         throw new UnsupportedOperationException();
150     }
151 
152     /**
153      * <p>Visits a JJTree node with the specific type.</p>
154      * <p>This method simply throws an UnsupportedOperationException.</p>
155      * @param node The JJTree node to visit.
156      * @param data Visitor data.
157      * @return A visitor data.
158      * @since 1.0
159      */
160     public Object visit(ASTPackageName node, Object data) {
161         throw new UnsupportedOperationException();
162     }
163 
164     /**
165      * <p>Visits a JJTree node with the specific type.</p>
166      * <p>This method simply throws an UnsupportedOperationException.</p>
167      * @param node The JJTree node to visit.
168      * @param data Visitor data.
169      * @return A visitor data.
170      * @since 1.0
171      */
172     public Object visit(ASTSuperList node, Object data) {
173         throw new UnsupportedOperationException();
174     }
175 
176     /**
177      * <p>Visits a JJTree node with the specific type.</p>
178      * <p>This method simply throws an UnsupportedOperationException.</p>
179      * @param node The JJTree node to visit.
180      * @param data Visitor data.
181      * @return A visitor data.
182      * @since 1.0
183      */
184     public Object visit(ASTGrammar node, Object data) {
185         throw new UnsupportedOperationException();
186     }
187 }