1 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 ; 33 import java.util.ArrayList ; 34 import java.util.Collection ; 35 import java.util.HashMap ; 36 import java.util.HashSet ; 37 import java.util.Iterator ; 38 import java.util.List ; 39 import java.util.Map ; 40 import java.util.Set ; 41 42 49 public class PackageDocImpl 50 extends DocImpl 51 implements org.aspectj.ajdoc.PackageDoc { 52 53 public final static String UNNAMED_PACKAGE = ""; 55 private static AjdocCompiler ajdoc = Ajdoc.instance(); 56 private static AspectJCompiler ajc = ajdoc; 57 public AspectJCompiler ajc() { return ajc; } 58 private Set allClasses = new HashSet (); 59 private Comment comment; 60 private String name; 61 62 67 private PackageDocImpl(String name) { 68 this.name = name; 69 findDocumentation(); 70 } 71 72 77 public void addClass(ClassDoc classDoc) { 78 allClasses.add(classDoc); 79 } 80 81 87 public ClassDoc findClass(String className) { 88 Type type = ajc.getTypeManager(). 89 findType(name(), className); 90 if (type != null) { 91 return ClassDocImpl.getInstance(type.getTypeDec()); 92 } 93 for (Iterator i = allClasses.iterator(); i.hasNext();) { 94 ClassDoc cd = (ClassDoc)i.next(); 95 if (className.equals(cd.name())) { 96 return cd; } 98 } 99 return null; 100 } 101 102 108 public ClassDoc[] allClasses() { 109 return (ClassDoc[])allClasses.toArray(new ClassDoc[allClasses.size()]); 110 } 111 112 118 public AspectDoc[] aspects() { 119 List list = new ArrayList (); 120 for (Iterator 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 134 public ClassDoc[] ordinaryClasses() { 135 List list = new ArrayList (); 136 for (Iterator 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 150 public ClassDoc[] exceptions() { 151 List list = new ArrayList (); 152 for (Iterator 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 166 public ClassDoc[] errors() { 167 List list = new ArrayList (); 168 for (Iterator 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 182 public ClassDoc[] interfaces() { 183 List list = new ArrayList (); 184 for (Iterator 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 197 public String name() { 198 return isIncluded() ? name : null; 199 } 200 201 208 public boolean equals(Object other) { 209 return other instanceof PackageDocImpl && other != null 210 ? name().equals(((PackageDocImpl)other).name()) 211 : super.equals(other); 212 } 213 214 219 public String toString() { 220 return name(); 221 } 222 223 private void findDocumentation() { 224 if (ajdoc == null) return; 225 String filename = (name.equals("") 226 ? name 227 : name.replace('.',File.separatorChar) 228 + File.separatorChar) + "package.html"; 229 File html = ajdoc.findFile(filename, false); 230 if (html == null) return; 231 String rawCommentText = Util.documentation(html, ajdoc.err()); 232 233 setComment(new Comment(this, rawCommentText)); 236 } 237 238 239 243 244 private static Map namesToPackages = new HashMap (); 245 246 251 public static Collection packages() { 252 return namesToPackages.values(); 253 } 254 255 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 275 public static PackageDocImpl getPackageDoc(TypeDec typeDec) { 276 return getPackageDoc(typeDec.getPackageName()); 277 } 278 279 285 public static PackageDocImpl getPackageDoc(String packageName) { 286 return addPackageDoc(packageName); 287 } 288 289 299 public static PackageDocImpl addPackageDoc(String 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 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 328 public static void excludeEmptyPackages() { 329 for (Iterator 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 |