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;
021 
022 import java.io.File;
023 import java.util.ArrayList;
024 import java.util.List;
025 
026 /**
027  * <p>Represents a source file in a Java package.</p>
028  * 
029  * @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>
030  */
031 public class SourceFile
032 {
033     /**
034      * <p>The absolute path name of the source file.</p>
035      * @since 1.0
036      */
037     public final String source;
038     /**
039      * <p>The package where the source file is located.</p>
040      * @since 1.0
041      */
042     public final JavaPackage pckg;
043     /**
044      * <p>The simple name of the source file.</p>
045      * @since 1.0
046      */
047     public final String name;
048     /**
049      * <p>Whether the file is a Java source file.</p>
050      * @since 1.0
051      */
052     public final boolean isJava;
053     /**
054      * <p>Whether the file is a JavaCC source file.</p>
055      * @since 1.0
056      */
057     public final boolean isJavaCC;
058     /**
059      * <p>Whether the file is a JJTree source file.</p>
060      * @since 1.0
061      */
062     public final boolean isJJTree;
063 
064     /**
065      * <p>Constructs a {@link SourceFile}.</p>
066      * @param source The absolute path name for the source file.
067      * @param pckg The package where the source file is located.
068      * @param name The simple name for the source file.
069      * @since 1.0
070      */
071     public SourceFile(String source, JavaPackage pckg, String name) {
072         this.source = source;
073         this.pckg = pckg;
074         this.name = name;
075         isJava = name.endsWith(".java");
076         isJavaCC = name.endsWith(".jj");
077         isJJTree = name.endsWith(".jjt");
078     }
079 
080     private JavaName fqdn;
081 
082     /**
083      * <p>Gets the fully qualified name of the file.</p>
084      * @return The fully qualified name of the file.
085      * @since 1.0
086      */
087     public final JavaName getFullName() {
088         if (fqdn == null) {
089             fqdn = pckg.name.addLast(name);
090         }
091         return fqdn;
092     }
093 
094     /**
095      * <p>Gets the target name of the file for hyper linking.</p>
096      * @return The target name of the file.
097      * @since 1.0
098      */
099     public String getLinkName() {
100         return isJava || isJavaCC || isJJTree ? name + ".html" : name;
101     }
102 
103     protected String link;
104 
105     /**
106      * <p>Gets the hyper link to the file from the root.</p>
107      * @return The hyper link to the file from the root.
108      * @since 1.0
109      */
110     public final String getLink() {
111         if (link == null) {
112             link = getLinkName();
113             if (!pckg.isDefault) {
114                 link = pckg.name.path() + '/' + link;
115             }
116         }
117         return link;
118     }
119 
120     private String target;
121 
122     /**
123      * <p>Gets the target path name from the root to the file.</p>
124      * @return The target path name from the root to the file.
125      * @since 1.0
126      */
127     public final String getTarget() {
128         if (target == null) {
129             target = pckg.jdoq.dst + File.separator + getLink().replace('/', File.separatorChar);
130         }
131         return target;
132     }
133 
134     /**
135      * <p>Imported types in the file.</p>
136      * @since 1.0
137      */
138     public final List<JavaType> imports = new ArrayList<JavaType>();
139 
140     /**
141      * <p>Finds a Java type in the scope of this file.</p>
142      * @param name The name for the type being found.
143      * @return The matched Java type or <tt>null</tt> if it is not found.
144      * @since 1.0
145      */
146     public final JavaType findType(JavaName name) {
147         for (JavaType t : imports) {
148             if (t.qname.endsWith(name)) {
149                 return t;
150             }
151         }
152         JavaType t = pckg.findType(name);
153         if (t == null) {
154             t = pckg.jdoq.findType(name);
155         }
156         return t;
157     }
158 }