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.util.HashMap;
023 import java.util.Map;
024 
025 /**
026  * <p>Represents a Java package.</p>
027  * 
028  * @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>
029  */
030 public final class JavaPackage
031 {
032     /**
033      * <p>The type map with the simple names as the keys.</p>
034      * @since 1.0
035      */
036     public final Map<String, JavaType> types = new HashMap<String, JavaType>();
037     /**
038      * <p>The source file map with the simple file names as the keys.</p>
039      * @since 1.0
040      */
041     public final Map<String, SourceFile> files = new HashMap<String, SourceFile>();
042 
043     /**
044      * <p>The JavaDoq environment of the package.</p>
045      * @since 1.0
046      */
047     public final JavaDoq jdoq;
048     /**
049      * <p>The fully qualified name of the package.</p>
050      * @since 1.0
051      */
052     public final JavaName name;
053     /**
054      * <p>Whether the package is default.</p>
055      * @since 1.0
056      */
057     public final boolean isDefault;
058     /**
059      * <p>Whether the package has a description file.</p>
060      * @since 1.0
061      */
062     public boolean hasDescription;
063 
064     /**
065      * <p>Constructs a {@link JavaPackage}.</p>
066      * @param jdoq The JavaDoq environment for the package being constructed.
067      * @param name The fully qualified name for the package.
068      * @since 1.0
069      */
070     public JavaPackage(JavaDoq jdoq, JavaName name) {
071         this.jdoq = jdoq;
072         this.name = name;
073         isDefault = name.length == 0;
074     }
075 
076     /**
077      * <p>Gets the title for the {@link JavaPackage}.</p>
078      * @return The title for target documents.
079      * @since 1.0
080      */
081     public final String title() {
082         return isDefault ? "[default]" : name.text;
083     }
084 
085     private String link;
086 
087     /**
088      * <p>Gets the hyper link to the target package file.</p>
089      * @return The hyper link to the target package file.
090      * @since 1.0
091      */
092     public final String getLink() {
093         if (link == null) {
094             link = "package." + (isDefault ? "default" : name) + ".html";
095         }
096         return link;
097     }
098 
099     private String root;
100 
101     /**
102      * <p>Gets the hyper link to the root of the source from the location of the
103      * current package.</p>
104      * @return The relative hyper link to the source root.
105      * @since 1.0
106      */
107     public final String getRoot() {
108         if (root == null) {
109             if (isDefault) {
110                 root = "";
111             } else {
112                 StringBuilder sb = new StringBuilder("../");
113                 int i = 0;
114                 while (true) {
115                     i = name.text.indexOf('.', i);
116                     if (i == -1) {
117                         break;
118                     }
119                     sb.append("../");
120                     i++;
121                 }
122                 root = sb.toString();
123             }
124         }
125         return root;
126     }
127 
128     /**
129      * <p>Gets the hyper link to the specified source file from the location of the
130      * current package.</p>
131      * @param f A source file.
132      * @return The relative hyper link to the source file.
133      * @since 1.0
134      */
135     public final String getLink(SourceFile f) {
136         if (f == null) {
137             return null;
138         }
139         return f.pckg.equals(this) ? f.getLinkName() :
140             getRoot() + f.getLink();
141     }
142 
143     /**
144      * <p>Gets the hyper link to the specified package from the location of the
145      * current package.</p>
146      * @param p A package.
147      * @return The relative hyper link to the specified package.
148      * @since 1.0
149      */
150     public final String getLink(JavaPackage p) {
151         if (p == null) {
152             return null;
153         }
154         return getRoot() + p.getLink();
155     }
156 
157     /**
158      * <p>Finds a Java type in the scope of this package.</p>
159      * @param name The name for the type being found.
160      * @return The matched Java type or <tt>null</tt> if it is not found.
161      * @since 1.0
162      */
163     public final JavaType findType(JavaName name) {
164         if (name.isSimple) {
165             return types.get(name.text);
166         }
167         JavaType t = types.get(name.getFirst());
168         return t == null ? t : t.findNestedType(name.chopFirst());
169     }
170 
171     /**
172      * <p>Returns a string representation of this package.</p>
173      * @return The string representation of this package, that is, the fully
174      * qualified name of the package.
175      * @since 1.0
176      */
177     @Override
178     public String toString() {
179         return name.toString();
180     }
181 }