KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > tools > ajdoc > PackageDocImpl


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  */

22 package org.aspectj.tools.ajdoc;
23
24 import org.aspectj.ajdoc.AspectDoc;
25 import org.aspectj.ajdoc.PackageDoc;
26 import org.aspectj.compiler.base.ast.Type;
27 import org.aspectj.compiler.base.ast.TypeDec;
28 import org.aspectj.compiler.crosscuts.AspectJCompiler;
29
30 import com.sun.javadoc.ClassDoc;
31
32 import java.io.File JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Set JavaDoc;
41
42 /**
43  * Represents a package in the aspectj-world.
44  * A package is a passive container of classes given to it.
45  * It does not enforce visibility rules. However, you can
46  * purge packages without classes from the static list of packages.
47  * @author Jeff Palm
48  */

49 public class PackageDocImpl
50     extends DocImpl
51     implements org.aspectj.ajdoc.PackageDoc {
52
53     public final static String JavaDoc UNNAMED_PACKAGE = ""; //unnamed-package";
54

55     private static AjdocCompiler ajdoc = Ajdoc.instance();
56     private static AspectJCompiler ajc = ajdoc;
57     public AspectJCompiler ajc() { return ajc; }
58     private Set JavaDoc allClasses = new HashSet JavaDoc();
59     private Comment comment;
60     private String JavaDoc name;
61
62     /**
63      * Only want to create these within the static access.
64      *
65      * @param name name of the new package.
66      */

67     private PackageDocImpl(String JavaDoc name) {
68         this.name = name;
69         findDocumentation();
70     }
71
72     /**
73      * Adds a class to this package.
74      *
75      * @param classDoc The new class.
76      */

77     public void addClass(ClassDoc classDoc) {
78         allClasses.add(classDoc);
79     }
80     
81     /**
82      * Attempts to find a class with name <code>className</code>
83      * in this package.
84      *
85      * @param className name of the class to find.
86      */

87     public ClassDoc findClass(String JavaDoc className) {
88         Type type = ajc.getTypeManager().
89             findType(name(), className);
90         if (type != null) {
91             return ClassDocImpl.getInstance(type.getTypeDec());
92         }
93         for (Iterator JavaDoc i = allClasses.iterator(); i.hasNext();) {
94             ClassDoc cd = (ClassDoc)i.next();
95             if (className.equals(cd.name())) {
96                 return cd; //todo wes was null?
97
}
98         }
99         return null;
100     }
101
102     /**
103      * Returns all the classes in this package.
104      *
105      * @return an array of ClassDoc representing all
106      * the classes in this package.
107      */

108     public ClassDoc[] allClasses() {
109         return (ClassDoc[])allClasses.toArray(new ClassDoc[allClasses.size()]);
110     }
111
112     /**
113      * Returns all the aspects in this package.
114      *
115      * @return an array of AspectDoc representing all
116      * the aspects in this package.
117      */

118     public AspectDoc[] aspects() {
119         List JavaDoc list = new ArrayList JavaDoc();
120         for (Iterator JavaDoc i = allClasses.iterator(); i.hasNext();) {
121             org.aspectj.ajdoc.ClassDoc doc = (org.aspectj.ajdoc.ClassDoc)i.next();
122             if (doc.isAspect()) list.add(doc);
123         }
124         return (AspectDoc[])list.toArray
125             (new AspectDoc[list.size()]);
126     }
127
128     /**
129      * Returns all the ordinary classes in this package.
130      *
131      * @return an array of ClassDoc representing all
132      * the ordinary classes in this package.
133      */

134     public ClassDoc[] ordinaryClasses() {
135         List JavaDoc list = new ArrayList JavaDoc();
136         for (Iterator JavaDoc i = allClasses.iterator(); i.hasNext();) {
137             ClassDoc doc = (ClassDoc)i.next();
138             if (doc.isOrdinaryClass()) list.add(doc);
139         }
140         return (ClassDoc[])list.toArray
141             (new ClassDoc[list.size()]);
142     }
143     
144     /**
145      * Returns all the exceptions in this package.
146      *
147      * @return an array of ClassDoc representing all
148      * the exceptions in this package.
149      */

150     public ClassDoc[] exceptions() {
151         List JavaDoc list = new ArrayList JavaDoc();
152         for (Iterator JavaDoc i = allClasses.iterator(); i.hasNext();) {
153             ClassDoc doc = (ClassDoc)i.next();
154             if (doc.isException()) list.add(doc);
155         }
156         return (ClassDoc[])list.toArray
157             (new ClassDoc[list.size()]);
158     }
159
160     /**
161      * Returns all the errors in this package.
162      *
163      * @return an array of ClassDoc representing all
164      * the errors in this package.
165      */

166     public ClassDoc[] errors() {
167         List JavaDoc list = new ArrayList JavaDoc();
168         for (Iterator JavaDoc i = allClasses.iterator(); i.hasNext();) {
169             ClassDoc doc = (ClassDoc)i.next();
170             if (doc.isError()) list.add(doc);
171         }
172         return (ClassDoc[])list.toArray
173             (new ClassDoc[list.size()]);
174     }
175
176     /**
177      * Returns all the interfaces in this package.
178      *
179      * @return an array of ClassDoc representing all
180      * the interfaces in this package.
181      */

182     public ClassDoc[] interfaces() {
183         List JavaDoc list = new ArrayList JavaDoc();
184         for (Iterator JavaDoc i = allClasses.iterator(); i.hasNext();) {
185             ClassDoc doc = (ClassDoc)i.next();
186             if (doc.isInterface()) list.add(doc);
187         }
188         return (ClassDoc[])list.toArray
189             (new ClassDoc[list.size()]);
190     }
191
192     /**
193      * Returns the name if included -- null otherwise.
194      *
195      * @return the name if included -- null otherwise.
196      */

197     public String JavaDoc name() {
198         return isIncluded() ? name : null;
199     }
200
201     /**
202      * Compare based on <code>name()</code>.
203      *
204      * @param other other Object.
205      * @return <code>true</code> if the other Object is a
206      * PackageDocImpl and has the same name.
207      */

208     public boolean equals(Object JavaDoc other) {
209         return other instanceof PackageDocImpl && other != null
210             ? name().equals(((PackageDocImpl)other).name())
211             : super.equals(other);
212     }
213
214     /**
215      * Returns the name.
216      *
217      * @return the name.
218      */

219     public String JavaDoc toString() {
220         return name();
221     }
222
223     private void findDocumentation() {
224         if (ajdoc == null) return;
225         String JavaDoc filename = (name.equals("")
226                            ? name
227                            : name.replace('.',File.separatorChar)
228                            + File.separatorChar) + "package.html";
229         File JavaDoc html = ajdoc.findFile(filename, false);
230         if (html == null) return;
231         String JavaDoc rawCommentText = Util.documentation(html, ajdoc.err());
232
233         //TODO: should be done in aspect from Aspects.java
234
//FormalComment comment = new FormalComment(rawCommentText);
235
setComment(new Comment(this, rawCommentText));
236     }
237
238
239     /* ------------------------------------------------------------
240      * Factory stuff
241      * ------------------------------------------------------------
242      */

243
244     private static Map JavaDoc namesToPackages = new HashMap JavaDoc();
245
246     /**
247      * Returns the collection of known packages.
248      *
249      * @return a Collection representing the known packages.
250      */

251     public static Collection JavaDoc packages() {
252         return namesToPackages.values();
253     }
254
255     /**
256      * Inits the world and AspectJCompiler with these
257      * packages.
258      *
259      * @param world current World.
260      * @param ajc current compiler.
261      */

262     public static void init(AspectJCompiler ajc) {
263         PackageDocImpl.ajc = ajc;
264         if (ajc instanceof AjdocCompiler) {
265             PackageDocImpl.ajdoc = (AjdocCompiler)ajc;
266         }
267     }
268
269     /**
270      * Returns a package for a TypeDec.
271      *
272      * @param typeDec TypeDec whose package is desired.
273      * @return a PackageDocImpl for a given TypeDec.
274      */

275     public static PackageDocImpl getPackageDoc(TypeDec typeDec) {
276         return getPackageDoc(typeDec.getPackageName());
277     }
278     
279     /**
280      * Returns a package for a name.
281      *
282      * @param packageName package name for which to look.
283      * @return a PackageDocImpl for a given package name.
284      */

285     public static PackageDocImpl getPackageDoc(String JavaDoc packageName) {
286         return addPackageDoc(packageName);
287     }
288
289     /**
290      * Adds a package with name <code>name</code> if it
291      * already doesn't exist, and returns the new package
292      * (if own with the same name didn't exist) or the
293      * existing package.
294      *
295      * @param name name of the new package.
296      * @return current package mapping to
297      * <code>name</code>.
298      */

299     public static PackageDocImpl addPackageDoc(String JavaDoc name) {
300         if (name == null) name = UNNAMED_PACKAGE;
301         PackageDocImpl packageDoc = (PackageDocImpl)namesToPackages.get(name);
302         if (packageDoc == null) {
303             packageDoc = new PackageDocImpl(name);
304             addPackageDoc(packageDoc);
305         }
306         return packageDoc;
307     }
308
309     /**
310      * Adds the PackageDoc if one doesn't already exist
311      * with the same name, and returns the existing or created
312      * package with the same name as the one passed in .
313      *
314      * @param packageDoc PackageDoc we want to add.
315      * @return package in the world with the same
316      * name as <code>packageDoc</code>.
317      */

318     public static PackageDocImpl addPackageDoc(PackageDoc packageDoc) {
319         if (packageDoc == null) return null;
320         return (PackageDocImpl)namesToPackages.put(packageDoc.name(),
321                                                    packageDoc);
322     }
323
324     /**
325      * Sets the include flag to false for empty packages
326      * todo: remove instead?
327      */

328     public static void excludeEmptyPackages() {
329         for (Iterator JavaDoc i = packages().iterator(); i.hasNext();) {
330             PackageDocImpl pkg = (PackageDocImpl)i.next();
331             if (pkg.allClasses().length < 1) {
332                 pkg.setIncluded(false);
333             }
334         }
335     }
336 }
337
Popular Tags