src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Util.java

Wed, 01 Dec 2010 11:02:38 -0800

author
bpatel
date
Wed, 01 Dec 2010 11:02:38 -0800
changeset 766
90af8d87741f
parent 554
9d9f26857129
child 793
ffbf2b2a8611
permissions
-rw-r--r--

6851834: Javadoc doclet needs a structured approach to generate the output HTML.
Reviewed-by: jjg

duke@1 1 /*
ohair@554 2 * Copyright (c) 1999, 2009, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.doclets.internal.toolkit.util;
duke@1 27
jjg@197 28 import java.io.*;
jjg@197 29 import java.util.*;
jjg@197 30
duke@1 31 import com.sun.javadoc.*;
duke@1 32 import com.sun.tools.doclets.internal.toolkit.*;
duke@1 33
duke@1 34 /**
duke@1 35 * Utilities Class for Doclets.
duke@1 36 *
duke@1 37 * This code is not part of an API.
duke@1 38 * It is implementation that is subject to change.
duke@1 39 * Do not use it as an API
duke@1 40 *
duke@1 41 * @author Atul M Dambalkar
duke@1 42 * @author Jamie Ho
duke@1 43 */
duke@1 44 public class Util {
duke@1 45
duke@1 46 /**
duke@1 47 * A mapping between characters and their
duke@1 48 * corresponding HTML escape character.
duke@1 49 */
duke@1 50 public static final String[][] HTML_ESCAPE_CHARS =
duke@1 51 {{"&", "&amp;"}, {"<", "&lt;"}, {">", "&gt;"}};
duke@1 52
duke@1 53 /**
bpatel@766 54 * Name of the resource directory.
bpatel@766 55 */
bpatel@766 56 public static final String RESOURCESDIR = "resources";
bpatel@766 57
bpatel@766 58 /**
duke@1 59 * Return array of class members whose documentation is to be generated.
duke@1 60 * If the member is deprecated do not include such a member in the
duke@1 61 * returned array.
duke@1 62 *
duke@1 63 * @param members Array of members to choose from.
duke@1 64 * @return ProgramElementDoc[] Array of eligible members for whom
duke@1 65 * documentation is getting generated.
duke@1 66 */
duke@1 67 public static ProgramElementDoc[] excludeDeprecatedMembers(
duke@1 68 ProgramElementDoc[] members) {
duke@1 69 return
duke@1 70 toProgramElementDocArray(excludeDeprecatedMembersAsList(members));
duke@1 71 }
duke@1 72
duke@1 73 /**
duke@1 74 * Return array of class members whose documentation is to be generated.
duke@1 75 * If the member is deprecated do not include such a member in the
duke@1 76 * returned array.
duke@1 77 *
duke@1 78 * @param members Array of members to choose from.
duke@1 79 * @return List List of eligible members for whom
duke@1 80 * documentation is getting generated.
duke@1 81 */
jjg@74 82 public static List<ProgramElementDoc> excludeDeprecatedMembersAsList(
duke@1 83 ProgramElementDoc[] members) {
jjg@74 84 List<ProgramElementDoc> list = new ArrayList<ProgramElementDoc>();
duke@1 85 for (int i = 0; i < members.length; i++) {
duke@1 86 if (members[i].tags("deprecated").length == 0) {
duke@1 87 list.add(members[i]);
duke@1 88 }
duke@1 89 }
duke@1 90 Collections.sort(list);
duke@1 91 return list;
duke@1 92 }
duke@1 93
duke@1 94 /**
duke@1 95 * Return the list of ProgramElementDoc objects as Array.
duke@1 96 */
mcimadamore@184 97 public static ProgramElementDoc[] toProgramElementDocArray(List<ProgramElementDoc> list) {
duke@1 98 ProgramElementDoc[] pgmarr = new ProgramElementDoc[list.size()];
duke@1 99 for (int i = 0; i < list.size(); i++) {
mcimadamore@184 100 pgmarr[i] = list.get(i);
duke@1 101 }
duke@1 102 return pgmarr;
duke@1 103 }
duke@1 104
duke@1 105 /**
duke@1 106 * Return true if a non-public member found in the given array.
duke@1 107 *
duke@1 108 * @param members Array of members to look into.
duke@1 109 * @return boolean True if non-public member found, false otherwise.
duke@1 110 */
duke@1 111 public static boolean nonPublicMemberFound(ProgramElementDoc[] members) {
duke@1 112 for (int i = 0; i < members.length; i++) {
duke@1 113 if (!members[i].isPublic()) {
duke@1 114 return true;
duke@1 115 }
duke@1 116 }
duke@1 117 return false;
duke@1 118 }
duke@1 119
duke@1 120 /**
duke@1 121 * Search for the given method in the given class.
duke@1 122 *
duke@1 123 * @param cd Class to search into.
duke@1 124 * @param method Method to be searched.
duke@1 125 * @return MethodDoc Method found, null otherwise.
duke@1 126 */
duke@1 127 public static MethodDoc findMethod(ClassDoc cd, MethodDoc method) {
duke@1 128 MethodDoc[] methods = cd.methods();
duke@1 129 for (int i = 0; i < methods.length; i++) {
duke@1 130 if (executableMembersEqual(method, methods[i])) {
duke@1 131 return methods[i];
duke@1 132
duke@1 133 }
duke@1 134 }
duke@1 135 return null;
duke@1 136 }
duke@1 137
duke@1 138 /**
duke@1 139 * @param member1 the first method to compare.
duke@1 140 * @param member2 the second method to compare.
duke@1 141 * @return true if member1 overrides/hides or is overriden/hidden by member2.
duke@1 142 */
duke@1 143 public static boolean executableMembersEqual(ExecutableMemberDoc member1,
duke@1 144 ExecutableMemberDoc member2) {
duke@1 145 if (! (member1 instanceof MethodDoc && member2 instanceof MethodDoc))
duke@1 146 return false;
duke@1 147
duke@1 148 MethodDoc method1 = (MethodDoc) member1;
duke@1 149 MethodDoc method2 = (MethodDoc) member2;
duke@1 150 if (method1.isStatic() && method2.isStatic()) {
duke@1 151 Parameter[] targetParams = method1.parameters();
duke@1 152 Parameter[] currentParams;
duke@1 153 if (method1.name().equals(method2.name()) &&
duke@1 154 (currentParams = method2.parameters()).length ==
duke@1 155 targetParams.length) {
duke@1 156 int j;
duke@1 157 for (j = 0; j < targetParams.length; j++) {
duke@1 158 if (! (targetParams[j].typeName().equals(
duke@1 159 currentParams[j].typeName()) ||
duke@1 160 currentParams[j].type() instanceof TypeVariable ||
duke@1 161 targetParams[j].type() instanceof TypeVariable)) {
duke@1 162 break;
duke@1 163 }
duke@1 164 }
duke@1 165 if (j == targetParams.length) {
duke@1 166 return true;
duke@1 167 }
duke@1 168 }
duke@1 169 return false;
duke@1 170 } else {
duke@1 171 return method1.overrides(method2) ||
duke@1 172 method2.overrides(method1) ||
duke@1 173 member1 == member2;
duke@1 174 }
duke@1 175 }
duke@1 176
duke@1 177 /**
duke@1 178 * According to the Java Language Specifications, all the outer classes
duke@1 179 * and static inner classes are core classes.
duke@1 180 */
duke@1 181 public static boolean isCoreClass(ClassDoc cd) {
duke@1 182 return cd.containingClass() == null || cd.isStatic();
duke@1 183 }
duke@1 184
duke@1 185 public static boolean matches(ProgramElementDoc doc1,
duke@1 186 ProgramElementDoc doc2) {
duke@1 187 if (doc1 instanceof ExecutableMemberDoc &&
duke@1 188 doc2 instanceof ExecutableMemberDoc) {
duke@1 189 ExecutableMemberDoc ed1 = (ExecutableMemberDoc)doc1;
duke@1 190 ExecutableMemberDoc ed2 = (ExecutableMemberDoc)doc2;
duke@1 191 return executableMembersEqual(ed1, ed2);
duke@1 192 } else {
duke@1 193 return doc1.name().equals(doc2.name());
duke@1 194 }
duke@1 195 }
duke@1 196
duke@1 197 /**
duke@1 198 * Copy source file to destination file.
duke@1 199 *
duke@1 200 * @throws SecurityException
duke@1 201 * @throws IOException
duke@1 202 */
duke@1 203 public static void copyFile(File destfile, File srcfile)
duke@1 204 throws IOException {
duke@1 205 byte[] bytearr = new byte[512];
duke@1 206 int len = 0;
duke@1 207 FileInputStream input = new FileInputStream(srcfile);
duke@1 208 File destDir = destfile.getParentFile();
duke@1 209 destDir.mkdirs();
duke@1 210 FileOutputStream output = new FileOutputStream(destfile);
duke@1 211 try {
duke@1 212 while ((len = input.read(bytearr)) != -1) {
duke@1 213 output.write(bytearr, 0, len);
duke@1 214 }
duke@1 215 } catch (FileNotFoundException exc) {
duke@1 216 } catch (SecurityException exc) {
duke@1 217 } finally {
duke@1 218 input.close();
duke@1 219 output.close();
duke@1 220 }
duke@1 221 }
duke@1 222
duke@1 223 /**
duke@1 224 * Copy the given directory contents from the source package directory
duke@1 225 * to the generated documentation directory. For example for a package
duke@1 226 * java.lang this method find out the source location of the package using
duke@1 227 * {@link SourcePath} and if given directory is found in the source
duke@1 228 * directory structure, copy the entire directory, to the generated
duke@1 229 * documentation hierarchy.
duke@1 230 *
duke@1 231 * @param configuration The configuration of the current doclet.
duke@1 232 * @param path The relative path to the directory to be copied.
duke@1 233 * @param dir The original directory name to copy from.
duke@1 234 * @param overwrite Overwrite files if true.
duke@1 235 */
duke@1 236 public static void copyDocFiles(Configuration configuration,
duke@1 237 String path, String dir, boolean overwrite) {
duke@1 238 if (checkCopyDocFilesErrors(configuration, path, dir)) {
duke@1 239 return;
duke@1 240 }
duke@1 241 String destname = configuration.docFileDestDirName;
duke@1 242 File srcdir = new File(path + dir);
duke@1 243 if (destname.length() > 0 && !destname.endsWith(
bpatel@766 244 DirectoryManager.URL_FILE_SEPARATOR)) {
bpatel@766 245 destname += DirectoryManager.URL_FILE_SEPARATOR;
duke@1 246 }
duke@1 247 String dest = destname + dir;
duke@1 248 try {
duke@1 249 File destdir = new File(dest);
duke@1 250 DirectoryManager.createDirectory(configuration, dest);
duke@1 251 String[] files = srcdir.list();
duke@1 252 for (int i = 0; i < files.length; i++) {
duke@1 253 File srcfile = new File(srcdir, files[i]);
duke@1 254 File destfile = new File(destdir, files[i]);
duke@1 255 if (srcfile.isFile()) {
duke@1 256 if(destfile.exists() && ! overwrite) {
duke@1 257 configuration.message.warning((SourcePosition) null,
duke@1 258 "doclet.Copy_Overwrite_warning",
duke@1 259 srcfile.toString(), destdir.toString());
duke@1 260 } else {
duke@1 261 configuration.message.notice(
duke@1 262 "doclet.Copying_File_0_To_Dir_1",
duke@1 263 srcfile.toString(), destdir.toString());
duke@1 264 Util.copyFile(destfile, srcfile);
duke@1 265 }
duke@1 266 } else if(srcfile.isDirectory()) {
duke@1 267 if(configuration.copydocfilesubdirs
duke@1 268 && ! configuration.shouldExcludeDocFileDir(
duke@1 269 srcfile.getName())){
duke@1 270 copyDocFiles(configuration, path, dir +
bpatel@766 271 DirectoryManager.URL_FILE_SEPARATOR + srcfile.getName(),
duke@1 272 overwrite);
duke@1 273 }
duke@1 274 }
duke@1 275 }
duke@1 276 } catch (SecurityException exc) {
duke@1 277 throw new DocletAbortException();
duke@1 278 } catch (IOException exc) {
duke@1 279 throw new DocletAbortException();
duke@1 280 }
duke@1 281 }
duke@1 282
duke@1 283 /**
duke@1 284 * Given the parameters for copying doc-files, check for errors.
duke@1 285 *
duke@1 286 * @param configuration The configuration of the current doclet.
duke@1 287 * @param path The relative path to the directory to be copied.
duke@1 288 * @param dirName The original directory name to copy from.
duke@1 289 */
duke@1 290 private static boolean checkCopyDocFilesErrors (Configuration configuration,
duke@1 291 String path, String dirName) {
duke@1 292 if ((configuration.sourcepath == null || configuration.sourcepath.length() == 0) &&
duke@1 293 (configuration.destDirName == null || configuration.destDirName.length() == 0)) {
duke@1 294 //The destination path and source path are definitely equal.
duke@1 295 return true;
duke@1 296 }
duke@1 297 File sourcePath, destPath = new File(configuration.destDirName);
duke@1 298 StringTokenizer pathTokens = new StringTokenizer(
duke@1 299 configuration.sourcepath == null ? "" : configuration.sourcepath,
duke@1 300 File.pathSeparator);
duke@1 301 //Check if the destination path is equal to the source path. If yes,
duke@1 302 //do not copy doc-file directories.
duke@1 303 while(pathTokens.hasMoreTokens()){
duke@1 304 sourcePath = new File(pathTokens.nextToken());
duke@1 305 if(destPath.equals(sourcePath)){
duke@1 306 return true;
duke@1 307 }
duke@1 308 }
duke@1 309 //Make sure the doc-file being copied exists.
duke@1 310 File srcdir = new File(path + dirName);
duke@1 311 if (! srcdir.exists()) {
duke@1 312 return true;
duke@1 313 }
duke@1 314 return false;
duke@1 315 }
duke@1 316
duke@1 317 /**
duke@1 318 * Copy a file in the resources directory to the destination
duke@1 319 * directory (if it is not there already). If
duke@1 320 * <code>overwrite</code> is true and the destination file
duke@1 321 * already exists, overwrite it.
duke@1 322 *
duke@1 323 * @param configuration Holds the destination directory and error message
duke@1 324 * @param resourcefile The name of the resource file to copy
duke@1 325 * @param overwrite A flag to indicate whether the file in the
duke@1 326 * destination directory will be overwritten if
duke@1 327 * it already exists.
duke@1 328 */
duke@1 329 public static void copyResourceFile(Configuration configuration,
bpatel@766 330 String resourcefile, boolean overwrite) {
bpatel@766 331 String destresourcesdir = configuration.destDirName + RESOURCESDIR;
bpatel@766 332 copyFile(configuration, resourcefile, RESOURCESDIR, destresourcesdir,
bpatel@766 333 overwrite);
bpatel@766 334 }
bpatel@766 335
bpatel@766 336 /**
bpatel@766 337 * Copy a file from a source directory to a destination directory
bpatel@766 338 * (if it is not there already). If <code>overwrite</code> is true and
bpatel@766 339 * the destination file already exists, overwrite it.
bpatel@766 340 *
bpatel@766 341 * @param configuration Holds the error message
bpatel@766 342 * @param file The name of the file to copy
bpatel@766 343 * @param source The source directory
bpatel@766 344 * @param destination The destination directory where the file needs to be copied
bpatel@766 345 * @param overwrite A flag to indicate whether the file in the
bpatel@766 346 * destination directory will be overwritten if
bpatel@766 347 * it already exists.
bpatel@766 348 */
bpatel@766 349 public static void copyFile(Configuration configuration, String file, String source,
bpatel@766 350 String destination, boolean overwrite) {
bpatel@766 351 DirectoryManager.createDirectory(configuration, destination);
bpatel@766 352 File destfile = new File(destination, file);
duke@1 353 if(destfile.exists() && (! overwrite)) return;
duke@1 354 try {
duke@1 355 InputStream in = Configuration.class.getResourceAsStream(
bpatel@766 356 source + DirectoryManager.URL_FILE_SEPARATOR + file);
duke@1 357 if(in==null) return;
duke@1 358 OutputStream out = new FileOutputStream(destfile);
duke@1 359 byte[] buf = new byte[2048];
duke@1 360 int n;
duke@1 361 while((n = in.read(buf))>0) out.write(buf,0,n);
duke@1 362 in.close();
duke@1 363 out.close();
duke@1 364 } catch(Throwable t) {}
duke@1 365 }
duke@1 366
duke@1 367 /**
duke@1 368 * Given a PackageDoc, return the source path for that package.
duke@1 369 * @param configuration The Configuration for the current Doclet.
duke@1 370 * @param pkgDoc The package to seach the path for.
duke@1 371 * @return A string representing the path to the given package.
duke@1 372 */
duke@1 373 public static String getPackageSourcePath(Configuration configuration,
duke@1 374 PackageDoc pkgDoc){
duke@1 375 try{
duke@1 376 String pkgPath = DirectoryManager.getDirectoryPath(pkgDoc);
duke@1 377 String completePath = new SourcePath(configuration.sourcepath).
bpatel@766 378 getDirectory(pkgPath) + DirectoryManager.URL_FILE_SEPARATOR;
duke@1 379 //Make sure that both paths are using the same seperators.
duke@1 380 completePath = Util.replaceText(completePath, File.separator,
bpatel@766 381 DirectoryManager.URL_FILE_SEPARATOR);
duke@1 382 pkgPath = Util.replaceText(pkgPath, File.separator,
bpatel@766 383 DirectoryManager.URL_FILE_SEPARATOR);
duke@1 384 return completePath.substring(0, completePath.indexOf(pkgPath));
duke@1 385 } catch (Exception e){
duke@1 386 return "";
duke@1 387 }
duke@1 388 }
duke@1 389
duke@1 390 /**
duke@1 391 * We want the list of types in alphabetical order. However, types are not
duke@1 392 * comparable. We need a comparator for now.
duke@1 393 */
jjg@74 394 private static class TypeComparator implements Comparator<Type> {
jjg@74 395 public int compare(Type type1, Type type2) {
jjg@74 396 return type1.qualifiedTypeName().toLowerCase().compareTo(
jjg@74 397 type2.qualifiedTypeName().toLowerCase());
duke@1 398 }
duke@1 399 }
duke@1 400
duke@1 401 /**
duke@1 402 * For the class return all implemented interfaces including the
duke@1 403 * superinterfaces of the implementing interfaces, also iterate over for
duke@1 404 * all the superclasses. For interface return all the extended interfaces
duke@1 405 * as well as superinterfaces for those extended interfaces.
duke@1 406 *
duke@1 407 * @param type type whose implemented or
duke@1 408 * super interfaces are sought.
duke@1 409 * @param configuration the current configuration of the doclet.
duke@1 410 * @param sort if true, return list of interfaces sorted alphabetically.
duke@1 411 * @return List of all the required interfaces.
duke@1 412 */
jjg@74 413 public static List<Type> getAllInterfaces(Type type,
duke@1 414 Configuration configuration, boolean sort) {
jjg@74 415 Map<ClassDoc,Type> results = sort ? new TreeMap<ClassDoc,Type>() : new LinkedHashMap<ClassDoc,Type>();
duke@1 416 Type[] interfaceTypes = null;
duke@1 417 Type superType = null;
duke@1 418 if (type instanceof ParameterizedType) {
duke@1 419 interfaceTypes = ((ParameterizedType) type).interfaceTypes();
duke@1 420 superType = ((ParameterizedType) type).superclassType();
duke@1 421 } else if (type instanceof ClassDoc) {
duke@1 422 interfaceTypes = ((ClassDoc) type).interfaceTypes();
duke@1 423 superType = ((ClassDoc) type).superclassType();
duke@1 424 } else {
duke@1 425 interfaceTypes = type.asClassDoc().interfaceTypes();
duke@1 426 superType = type.asClassDoc().superclassType();
duke@1 427 }
duke@1 428
duke@1 429 for (int i = 0; i < interfaceTypes.length; i++) {
duke@1 430 Type interfaceType = interfaceTypes[i];
duke@1 431 ClassDoc interfaceClassDoc = interfaceType.asClassDoc();
duke@1 432 if (! (interfaceClassDoc.isPublic() ||
duke@1 433 (configuration == null ||
duke@1 434 isLinkable(interfaceClassDoc, configuration)))) {
duke@1 435 continue;
duke@1 436 }
duke@1 437 results.put(interfaceClassDoc, interfaceType);
mcimadamore@184 438 List<Type> superInterfaces = getAllInterfaces(interfaceType, configuration, sort);
mcimadamore@184 439 for (Iterator<Type> iter = superInterfaces.iterator(); iter.hasNext(); ) {
mcimadamore@184 440 Type t = iter.next();
duke@1 441 results.put(t.asClassDoc(), t);
duke@1 442 }
duke@1 443 }
duke@1 444 if (superType == null)
jjg@74 445 return new ArrayList<Type>(results.values());
duke@1 446 //Try walking the tree.
duke@1 447 addAllInterfaceTypes(results,
duke@1 448 superType,
duke@1 449 superType instanceof ClassDoc ?
duke@1 450 ((ClassDoc) superType).interfaceTypes() :
duke@1 451 ((ParameterizedType) superType).interfaceTypes(),
duke@1 452 false, configuration);
jjg@74 453 List<Type> resultsList = new ArrayList<Type>(results.values());
duke@1 454 if (sort) {
duke@1 455 Collections.sort(resultsList, new TypeComparator());
duke@1 456 }
duke@1 457 return resultsList;
duke@1 458 }
duke@1 459
mcimadamore@184 460 public static List<Type> getAllInterfaces(Type type, Configuration configuration) {
duke@1 461 return getAllInterfaces(type, configuration, true);
duke@1 462 }
duke@1 463
jjg@74 464 private static void findAllInterfaceTypes(Map<ClassDoc,Type> results, ClassDoc c, boolean raw,
duke@1 465 Configuration configuration) {
duke@1 466 Type superType = c.superclassType();
duke@1 467 if (superType == null)
duke@1 468 return;
duke@1 469 addAllInterfaceTypes(results, superType,
duke@1 470 superType instanceof ClassDoc ?
duke@1 471 ((ClassDoc) superType).interfaceTypes() :
duke@1 472 ((ParameterizedType) superType).interfaceTypes(),
duke@1 473 raw, configuration);
duke@1 474 }
duke@1 475
jjg@74 476 private static void findAllInterfaceTypes(Map<ClassDoc,Type> results, ParameterizedType p,
duke@1 477 Configuration configuration) {
duke@1 478 Type superType = p.superclassType();
duke@1 479 if (superType == null)
duke@1 480 return;
duke@1 481 addAllInterfaceTypes(results, superType,
duke@1 482 superType instanceof ClassDoc ?
duke@1 483 ((ClassDoc) superType).interfaceTypes() :
duke@1 484 ((ParameterizedType) superType).interfaceTypes(),
duke@1 485 false, configuration);
duke@1 486 }
duke@1 487
jjg@74 488 private static void addAllInterfaceTypes(Map<ClassDoc,Type> results, Type type,
duke@1 489 Type[] interfaceTypes, boolean raw,
duke@1 490 Configuration configuration) {
duke@1 491 for (int i = 0; i < interfaceTypes.length; i++) {
duke@1 492 Type interfaceType = interfaceTypes[i];
duke@1 493 ClassDoc interfaceClassDoc = interfaceType.asClassDoc();
duke@1 494 if (! (interfaceClassDoc.isPublic() ||
duke@1 495 (configuration != null &&
duke@1 496 isLinkable(interfaceClassDoc, configuration)))) {
duke@1 497 continue;
duke@1 498 }
duke@1 499 if (raw)
duke@1 500 interfaceType = interfaceType.asClassDoc();
duke@1 501 results.put(interfaceClassDoc, interfaceType);
mcimadamore@184 502 List<Type> superInterfaces = getAllInterfaces(interfaceType, configuration);
mcimadamore@184 503 for (Iterator<Type> iter = superInterfaces.iterator(); iter.hasNext(); ) {
mcimadamore@184 504 Type superInterface = iter.next();
duke@1 505 results.put(superInterface.asClassDoc(), superInterface);
duke@1 506 }
duke@1 507 }
duke@1 508 if (type instanceof ParameterizedType)
duke@1 509 findAllInterfaceTypes(results, (ParameterizedType) type, configuration);
duke@1 510 else if (((ClassDoc) type).typeParameters().length == 0)
duke@1 511 findAllInterfaceTypes(results, (ClassDoc) type, raw, configuration);
duke@1 512 else
duke@1 513 findAllInterfaceTypes(results, (ClassDoc) type, true, configuration);
duke@1 514 }
duke@1 515
duke@1 516
mcimadamore@184 517 public static <T extends ProgramElementDoc> List<T> asList(T[] members) {
mcimadamore@184 518 List<T> list = new ArrayList<T>();
duke@1 519 for (int i = 0; i < members.length; i++) {
duke@1 520 list.add(members[i]);
duke@1 521 }
duke@1 522 return list;
duke@1 523 }
duke@1 524
duke@1 525 /**
duke@1 526 * Enclose in quotes, used for paths and filenames that contains spaces
duke@1 527 */
duke@1 528 public static String quote(String filepath) {
duke@1 529 return ("\"" + filepath + "\"");
duke@1 530 }
duke@1 531
duke@1 532 /**
duke@1 533 * Given a package, return it's name.
duke@1 534 * @param packageDoc the package to check.
duke@1 535 * @return the name of the given package.
duke@1 536 */
duke@1 537 public static String getPackageName(PackageDoc packageDoc) {
duke@1 538 return packageDoc == null || packageDoc.name().length() == 0 ?
duke@1 539 DocletConstants.DEFAULT_PACKAGE_NAME : packageDoc.name();
duke@1 540 }
duke@1 541
duke@1 542 /**
duke@1 543 * Given a package, return it's file name without the extension.
duke@1 544 * @param packageDoc the package to check.
duke@1 545 * @return the file name of the given package.
duke@1 546 */
duke@1 547 public static String getPackageFileHeadName(PackageDoc packageDoc) {
duke@1 548 return packageDoc == null || packageDoc.name().length() == 0 ?
duke@1 549 DocletConstants.DEFAULT_PACKAGE_FILE_NAME : packageDoc.name();
duke@1 550 }
duke@1 551
duke@1 552 /**
duke@1 553 * Given a string, replace all occurraces of 'newStr' with 'oldStr'.
duke@1 554 * @param originalStr the string to modify.
duke@1 555 * @param oldStr the string to replace.
duke@1 556 * @param newStr the string to insert in place of the old string.
duke@1 557 */
duke@1 558 public static String replaceText(String originalStr, String oldStr,
duke@1 559 String newStr) {
duke@1 560 if (oldStr == null || newStr == null || oldStr.equals(newStr)) {
duke@1 561 return originalStr;
duke@1 562 }
duke@1 563 StringBuffer result = new StringBuffer(originalStr);
duke@1 564 int startIndex = 0;
duke@1 565 while ((startIndex = result.indexOf(oldStr, startIndex)) != -1) {
duke@1 566 result = result.replace(startIndex, startIndex + oldStr.length(),
duke@1 567 newStr);
duke@1 568 startIndex += newStr.length();
duke@1 569 }
duke@1 570 return result.toString();
duke@1 571 }
duke@1 572
duke@1 573 /**
duke@1 574 * Given a string, escape all special html characters and
duke@1 575 * return the result.
duke@1 576 *
duke@1 577 * @param s The string to check.
duke@1 578 * @return the original string with all of the HTML characters
duke@1 579 * escaped.
duke@1 580 *
duke@1 581 * @see #HTML_ESCAPE_CHARS
duke@1 582 */
duke@1 583 public static String escapeHtmlChars(String s) {
duke@1 584 String result = s;
duke@1 585 for (int i = 0; i < HTML_ESCAPE_CHARS.length; i++) {
duke@1 586 result = Util.replaceText(result,
duke@1 587 HTML_ESCAPE_CHARS[i][0], HTML_ESCAPE_CHARS[i][1]);
duke@1 588 }
duke@1 589 return result;
duke@1 590 }
duke@1 591
duke@1 592 /**
bpatel@766 593 * Given a string, strips all html characters and
bpatel@766 594 * return the result.
bpatel@766 595 *
bpatel@766 596 * @param rawString The string to check.
bpatel@766 597 * @return the original string with all of the HTML characters
bpatel@766 598 * stripped.
bpatel@766 599 *
bpatel@766 600 */
bpatel@766 601 public static String stripHtml(String rawString) {
bpatel@766 602 // remove HTML tags
bpatel@766 603 rawString = rawString.replaceAll("\\<.*?>", " ");
bpatel@766 604 // consolidate multiple spaces between a word to a single space
bpatel@766 605 rawString = rawString.replaceAll("\\b\\s{2,}\\b", " ");
bpatel@766 606 // remove extra whitespaces
bpatel@766 607 return rawString.trim();
bpatel@766 608 }
bpatel@766 609
bpatel@766 610 /**
duke@1 611 * Create the directory path for the file to be generated, construct
duke@1 612 * FileOutputStream and OutputStreamWriter depending upon docencoding.
duke@1 613 *
duke@1 614 * @param path The directory path to be created for this file.
duke@1 615 * @param filename File Name to which the PrintWriter will do the Output.
duke@1 616 * @param docencoding Encoding to be used for this file.
duke@1 617 * @exception IOException Exception raised by the FileWriter is passed on
duke@1 618 * to next level.
jjg@197 619 * @exception UnsupportedEncodingException Exception raised by the
duke@1 620 * OutputStreamWriter is passed on to next level.
duke@1 621 * @return Writer Writer for the file getting generated.
duke@1 622 * @see java.io.FileOutputStream
duke@1 623 * @see java.io.OutputStreamWriter
duke@1 624 */
duke@1 625 public static Writer genWriter(Configuration configuration,
duke@1 626 String path, String filename,
duke@1 627 String docencoding)
duke@1 628 throws IOException, UnsupportedEncodingException {
duke@1 629 FileOutputStream fos;
duke@1 630 if (path != null) {
duke@1 631 DirectoryManager.createDirectory(configuration, path);
duke@1 632 fos = new FileOutputStream(((path.length() > 0)?
duke@1 633 path + File.separator: "") + filename);
duke@1 634 } else {
duke@1 635 fos = new FileOutputStream(filename);
duke@1 636 }
duke@1 637 if (docencoding == null) {
jjg@197 638 return new OutputStreamWriter(fos);
duke@1 639 } else {
duke@1 640 return new OutputStreamWriter(fos, docencoding);
duke@1 641 }
duke@1 642 }
duke@1 643
duke@1 644 /**
duke@1 645 * Given an annotation, return true if it should be documented and false
duke@1 646 * otherwise.
duke@1 647 *
duke@1 648 * @param annotationDoc the annotation to check.
duke@1 649 *
duke@1 650 * @return true return true if it should be documented and false otherwise.
duke@1 651 */
duke@1 652 public static boolean isDocumentedAnnotation(AnnotationTypeDoc annotationDoc) {
duke@1 653 AnnotationDesc[] annotationDescList = annotationDoc.annotations();
duke@1 654 for (int i = 0; i < annotationDescList.length; i++) {
duke@1 655 if (annotationDescList[i].annotationType().qualifiedName().equals(
duke@1 656 java.lang.annotation.Documented.class.getName())){
duke@1 657 return true;
duke@1 658 }
duke@1 659 }
duke@1 660 return false;
duke@1 661 }
duke@1 662
duke@1 663 /**
duke@1 664 * Given a string, return an array of tokens. The separator can be escaped
duke@1 665 * with the '\' character. The '\' character may also be escaped by the
duke@1 666 * '\' character.
duke@1 667 *
duke@1 668 * @param s the string to tokenize.
duke@1 669 * @param separator the separator char.
duke@1 670 * @param maxTokens the maxmimum number of tokens returned. If the
duke@1 671 * max is reached, the remaining part of s is appended
duke@1 672 * to the end of the last token.
duke@1 673 *
duke@1 674 * @return an array of tokens.
duke@1 675 */
duke@1 676 public static String[] tokenize(String s, char separator, int maxTokens) {
jjg@74 677 List<String> tokens = new ArrayList<String>();
duke@1 678 StringBuilder token = new StringBuilder ();
duke@1 679 boolean prevIsEscapeChar = false;
duke@1 680 for (int i = 0; i < s.length(); i += Character.charCount(i)) {
duke@1 681 int currentChar = s.codePointAt(i);
duke@1 682 if (prevIsEscapeChar) {
duke@1 683 // Case 1: escaped character
duke@1 684 token.appendCodePoint(currentChar);
duke@1 685 prevIsEscapeChar = false;
duke@1 686 } else if (currentChar == separator && tokens.size() < maxTokens-1) {
duke@1 687 // Case 2: separator
duke@1 688 tokens.add(token.toString());
duke@1 689 token = new StringBuilder();
duke@1 690 } else if (currentChar == '\\') {
duke@1 691 // Case 3: escape character
duke@1 692 prevIsEscapeChar = true;
duke@1 693 } else {
duke@1 694 // Case 4: regular character
duke@1 695 token.appendCodePoint(currentChar);
duke@1 696 }
duke@1 697 }
duke@1 698 if (token.length() > 0) {
duke@1 699 tokens.add(token.toString());
duke@1 700 }
jjg@74 701 return tokens.toArray(new String[] {});
duke@1 702 }
duke@1 703
duke@1 704 /**
duke@1 705 * Return true if this class is linkable and false if we can't link to the
duke@1 706 * desired class.
duke@1 707 * <br>
duke@1 708 * <b>NOTE:</b> You can only link to external classes if they are public or
duke@1 709 * protected.
duke@1 710 *
duke@1 711 * @param classDoc the class to check.
duke@1 712 * @param configuration the current configuration of the doclet.
duke@1 713 *
duke@1 714 * @return true if this class is linkable and false if we can't link to the
duke@1 715 * desired class.
duke@1 716 */
duke@1 717 public static boolean isLinkable(ClassDoc classDoc,
duke@1 718 Configuration configuration) {
duke@1 719 return
duke@1 720 ((classDoc.isIncluded() && configuration.isGeneratedDoc(classDoc))) ||
duke@1 721 (configuration.extern.isExternal(classDoc) &&
duke@1 722 (classDoc.isPublic() || classDoc.isProtected()));
duke@1 723 }
duke@1 724
duke@1 725 /**
duke@1 726 * Given a class, return the closest visible super class.
duke@1 727 *
duke@1 728 * @param classDoc the class we are searching the parent for.
duke@1 729 * @param configuration the current configuration of the doclet.
duke@1 730 * @return the closest visible super class. Return null if it cannot
duke@1 731 * be found (i.e. classDoc is java.lang.Object).
duke@1 732 */
duke@1 733 public static Type getFirstVisibleSuperClass(ClassDoc classDoc,
duke@1 734 Configuration configuration) {
duke@1 735 if (classDoc == null) {
duke@1 736 return null;
duke@1 737 }
duke@1 738 Type sup = classDoc.superclassType();
duke@1 739 ClassDoc supClassDoc = classDoc.superclass();
duke@1 740 while (sup != null &&
duke@1 741 (! (supClassDoc.isPublic() ||
duke@1 742 isLinkable(supClassDoc, configuration))) ) {
duke@1 743 if (supClassDoc.superclass().qualifiedName().equals(supClassDoc.qualifiedName()))
duke@1 744 break;
duke@1 745 sup = supClassDoc.superclassType();
duke@1 746 supClassDoc = supClassDoc.superclass();
duke@1 747 }
duke@1 748 if (classDoc.equals(supClassDoc)) {
duke@1 749 return null;
duke@1 750 }
duke@1 751 return sup;
duke@1 752 }
duke@1 753
duke@1 754 /**
duke@1 755 * Given a class, return the closest visible super class.
duke@1 756 *
duke@1 757 * @param classDoc the class we are searching the parent for.
duke@1 758 * @param configuration the current configuration of the doclet.
duke@1 759 * @return the closest visible super class. Return null if it cannot
duke@1 760 * be found (i.e. classDoc is java.lang.Object).
duke@1 761 */
duke@1 762 public static ClassDoc getFirstVisibleSuperClassCD(ClassDoc classDoc,
duke@1 763 Configuration configuration) {
duke@1 764 if (classDoc == null) {
duke@1 765 return null;
duke@1 766 }
duke@1 767 ClassDoc supClassDoc = classDoc.superclass();
duke@1 768 while (supClassDoc != null &&
duke@1 769 (! (supClassDoc.isPublic() ||
duke@1 770 isLinkable(supClassDoc, configuration))) ) {
duke@1 771 supClassDoc = supClassDoc.superclass();
duke@1 772 }
duke@1 773 if (classDoc.equals(supClassDoc)) {
duke@1 774 return null;
duke@1 775 }
duke@1 776 return supClassDoc;
duke@1 777 }
duke@1 778
duke@1 779 /**
duke@1 780 * Given a ClassDoc, return the name of its type (Class, Interface, etc.).
duke@1 781 *
duke@1 782 * @param cd the ClassDoc to check.
duke@1 783 * @param lowerCaseOnly true if you want the name returned in lower case.
duke@1 784 * If false, the first letter of the name is capatilized.
duke@1 785 * @return
duke@1 786 */
duke@1 787 public static String getTypeName(Configuration config,
duke@1 788 ClassDoc cd, boolean lowerCaseOnly) {
duke@1 789 String typeName = "";
duke@1 790 if (cd.isOrdinaryClass()) {
duke@1 791 typeName = "doclet.Class";
duke@1 792 } else if (cd.isInterface()) {
duke@1 793 typeName = "doclet.Interface";
duke@1 794 } else if (cd.isException()) {
duke@1 795 typeName = "doclet.Exception";
duke@1 796 } else if (cd.isError()) {
duke@1 797 typeName = "doclet.Error";
duke@1 798 } else if (cd.isAnnotationType()) {
duke@1 799 typeName = "doclet.AnnotationType";
duke@1 800 } else if (cd.isEnum()) {
duke@1 801 typeName = "doclet.Enum";
duke@1 802 }
duke@1 803 return config.getText(
duke@1 804 lowerCaseOnly ? typeName.toLowerCase() : typeName);
duke@1 805 }
duke@1 806
duke@1 807 /**
duke@1 808 * Given a string, replace all tabs with the appropriate
duke@1 809 * number of spaces.
duke@1 810 * @param tabLength the length of each tab.
duke@1 811 * @param s the String to scan.
duke@1 812 */
duke@1 813 public static void replaceTabs(int tabLength, StringBuffer s) {
duke@1 814 int index, col;
duke@1 815 StringBuffer whitespace;
duke@1 816 while ((index = s.indexOf("\t")) != -1) {
duke@1 817 whitespace = new StringBuffer();
duke@1 818 col = index;
duke@1 819 do {
duke@1 820 whitespace.append(" ");
duke@1 821 col++;
duke@1 822 } while ((col%tabLength) != 0);
duke@1 823 s.replace(index, index+1, whitespace.toString());
duke@1 824 }
duke@1 825 }
duke@1 826
duke@1 827 /**
duke@1 828 * The documentation for values() and valueOf() in Enums are set by the
duke@1 829 * doclet.
duke@1 830 */
duke@1 831 public static void setEnumDocumentation(Configuration configuration,
duke@1 832 ClassDoc classDoc) {
duke@1 833 MethodDoc[] methods = classDoc.methods();
duke@1 834 for (int j = 0; j < methods.length; j++) {
duke@1 835 MethodDoc currentMethod = methods[j];
duke@1 836 if (currentMethod.name().equals("values") &&
duke@1 837 currentMethod.parameters().length == 0) {
duke@1 838 currentMethod.setRawCommentText(
duke@1 839 configuration.getText("doclet.enum_values_doc", classDoc.name()));
duke@1 840 } else if (currentMethod.name().equals("valueOf") &&
duke@1 841 currentMethod.parameters().length == 1) {
duke@1 842 Type paramType = currentMethod.parameters()[0].type();
duke@1 843 if (paramType != null &&
duke@1 844 paramType.qualifiedTypeName().equals(String.class.getName())) {
duke@1 845 currentMethod.setRawCommentText(
duke@1 846 configuration.getText("doclet.enum_valueof_doc"));
duke@1 847 }
duke@1 848 }
duke@1 849 }
duke@1 850 }
duke@1 851
duke@1 852 /**
duke@1 853 * Return true if the given Doc is deprecated.
duke@1 854 *
duke@1 855 * @param doc the Doc to check.
duke@1 856 * @return true if the given Doc is deprecated.
duke@1 857 */
duke@1 858 public static boolean isDeprecated(ProgramElementDoc doc) {
duke@1 859 if (doc.tags("deprecated").length > 0) {
duke@1 860 return true;
duke@1 861 }
duke@1 862 AnnotationDesc[] annotationDescList = doc.annotations();
duke@1 863 for (int i = 0; i < annotationDescList.length; i++) {
duke@1 864 if (annotationDescList[i].annotationType().qualifiedName().equals(
duke@1 865 java.lang.Deprecated.class.getName())){
duke@1 866 return true;
duke@1 867 }
duke@1 868 }
duke@1 869 return false;
duke@1 870 }
duke@1 871 }

mercurial