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