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

Wed, 13 Apr 2011 11:35:43 -0700

author
jjh
date
Wed, 13 Apr 2011 11:35:43 -0700
changeset 972
694ff82ca68e
parent 910
ebf7c13df6c0
child 995
62bc3775d5bb
permissions
-rw-r--r--

7032975: API files in javax.annotation.processing need to be updated for references to JLS
7032972: API files in javax.tools need to updated for references to JVM Spec with editions/hyperlinks
7032978: API files in javax.tools need to be updated for references to JLS with editions/hyperlinks
Summary: Removed URLs and 'edition' references
Reviewed-by: jjg, darcy

duke@1 1 /*
jjg@910 2 * Copyright (c) 1999, 2011, 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 /**
jjh@972 178 * According to
jjh@972 179 * <cite>The Java&trade; Language Specification</cite>,
jjh@972 180 * all the outer classes and static inner classes are core classes.
duke@1 181 */
duke@1 182 public static boolean isCoreClass(ClassDoc cd) {
duke@1 183 return cd.containingClass() == null || cd.isStatic();
duke@1 184 }
duke@1 185
duke@1 186 public static boolean matches(ProgramElementDoc doc1,
duke@1 187 ProgramElementDoc doc2) {
duke@1 188 if (doc1 instanceof ExecutableMemberDoc &&
duke@1 189 doc2 instanceof ExecutableMemberDoc) {
duke@1 190 ExecutableMemberDoc ed1 = (ExecutableMemberDoc)doc1;
duke@1 191 ExecutableMemberDoc ed2 = (ExecutableMemberDoc)doc2;
duke@1 192 return executableMembersEqual(ed1, ed2);
duke@1 193 } else {
duke@1 194 return doc1.name().equals(doc2.name());
duke@1 195 }
duke@1 196 }
duke@1 197
duke@1 198 /**
duke@1 199 * Copy source file to destination file.
duke@1 200 *
duke@1 201 * @throws SecurityException
duke@1 202 * @throws IOException
duke@1 203 */
duke@1 204 public static void copyFile(File destfile, File srcfile)
duke@1 205 throws IOException {
duke@1 206 byte[] bytearr = new byte[512];
duke@1 207 int len = 0;
duke@1 208 FileInputStream input = new FileInputStream(srcfile);
duke@1 209 File destDir = destfile.getParentFile();
duke@1 210 destDir.mkdirs();
duke@1 211 FileOutputStream output = new FileOutputStream(destfile);
duke@1 212 try {
duke@1 213 while ((len = input.read(bytearr)) != -1) {
duke@1 214 output.write(bytearr, 0, len);
bpatel@793 215 }
duke@1 216 } catch (FileNotFoundException exc) {
duke@1 217 } catch (SecurityException exc) {
bpatel@793 218 } finally {
duke@1 219 input.close();
duke@1 220 output.close();
bpatel@793 221 }
duke@1 222 }
duke@1 223
duke@1 224 /**
duke@1 225 * Copy the given directory contents from the source package directory
duke@1 226 * to the generated documentation directory. For example for a package
duke@1 227 * java.lang this method find out the source location of the package using
duke@1 228 * {@link SourcePath} and if given directory is found in the source
duke@1 229 * directory structure, copy the entire directory, to the generated
duke@1 230 * documentation hierarchy.
duke@1 231 *
duke@1 232 * @param configuration The configuration of the current doclet.
duke@1 233 * @param path The relative path to the directory to be copied.
duke@1 234 * @param dir The original directory name to copy from.
duke@1 235 * @param overwrite Overwrite files if true.
duke@1 236 */
duke@1 237 public static void copyDocFiles(Configuration configuration,
duke@1 238 String path, String dir, boolean overwrite) {
duke@1 239 if (checkCopyDocFilesErrors(configuration, path, dir)) {
duke@1 240 return;
duke@1 241 }
duke@1 242 String destname = configuration.docFileDestDirName;
duke@1 243 File srcdir = new File(path + dir);
duke@1 244 if (destname.length() > 0 && !destname.endsWith(
bpatel@766 245 DirectoryManager.URL_FILE_SEPARATOR)) {
bpatel@766 246 destname += DirectoryManager.URL_FILE_SEPARATOR;
duke@1 247 }
duke@1 248 String dest = destname + dir;
duke@1 249 try {
duke@1 250 File destdir = new File(dest);
duke@1 251 DirectoryManager.createDirectory(configuration, dest);
duke@1 252 String[] files = srcdir.list();
duke@1 253 for (int i = 0; i < files.length; i++) {
duke@1 254 File srcfile = new File(srcdir, files[i]);
duke@1 255 File destfile = new File(destdir, files[i]);
duke@1 256 if (srcfile.isFile()) {
duke@1 257 if(destfile.exists() && ! overwrite) {
duke@1 258 configuration.message.warning((SourcePosition) null,
duke@1 259 "doclet.Copy_Overwrite_warning",
duke@1 260 srcfile.toString(), destdir.toString());
duke@1 261 } else {
duke@1 262 configuration.message.notice(
duke@1 263 "doclet.Copying_File_0_To_Dir_1",
duke@1 264 srcfile.toString(), destdir.toString());
duke@1 265 Util.copyFile(destfile, srcfile);
duke@1 266 }
duke@1 267 } else if(srcfile.isDirectory()) {
duke@1 268 if(configuration.copydocfilesubdirs
duke@1 269 && ! configuration.shouldExcludeDocFileDir(
duke@1 270 srcfile.getName())){
duke@1 271 copyDocFiles(configuration, path, dir +
bpatel@766 272 DirectoryManager.URL_FILE_SEPARATOR + srcfile.getName(),
duke@1 273 overwrite);
duke@1 274 }
duke@1 275 }
duke@1 276 }
duke@1 277 } catch (SecurityException exc) {
duke@1 278 throw new DocletAbortException();
duke@1 279 } catch (IOException exc) {
duke@1 280 throw new DocletAbortException();
duke@1 281 }
duke@1 282 }
duke@1 283
duke@1 284 /**
duke@1 285 * Given the parameters for copying doc-files, check for errors.
duke@1 286 *
duke@1 287 * @param configuration The configuration of the current doclet.
duke@1 288 * @param path The relative path to the directory to be copied.
duke@1 289 * @param dirName The original directory name to copy from.
duke@1 290 */
duke@1 291 private static boolean checkCopyDocFilesErrors (Configuration configuration,
duke@1 292 String path, String dirName) {
duke@1 293 if ((configuration.sourcepath == null || configuration.sourcepath.length() == 0) &&
duke@1 294 (configuration.destDirName == null || configuration.destDirName.length() == 0)) {
duke@1 295 //The destination path and source path are definitely equal.
duke@1 296 return true;
duke@1 297 }
duke@1 298 File sourcePath, destPath = new File(configuration.destDirName);
duke@1 299 StringTokenizer pathTokens = new StringTokenizer(
duke@1 300 configuration.sourcepath == null ? "" : configuration.sourcepath,
duke@1 301 File.pathSeparator);
duke@1 302 //Check if the destination path is equal to the source path. If yes,
duke@1 303 //do not copy doc-file directories.
duke@1 304 while(pathTokens.hasMoreTokens()){
duke@1 305 sourcePath = new File(pathTokens.nextToken());
duke@1 306 if(destPath.equals(sourcePath)){
duke@1 307 return true;
duke@1 308 }
duke@1 309 }
duke@1 310 //Make sure the doc-file being copied exists.
duke@1 311 File srcdir = new File(path + dirName);
duke@1 312 if (! srcdir.exists()) {
duke@1 313 return true;
duke@1 314 }
duke@1 315 return false;
duke@1 316 }
duke@1 317
duke@1 318 /**
duke@1 319 * Copy a file in the resources directory to the destination
duke@1 320 * directory (if it is not there already). If
duke@1 321 * <code>overwrite</code> is true and the destination file
duke@1 322 * already exists, overwrite it.
duke@1 323 *
duke@1 324 * @param configuration Holds the destination directory and error message
duke@1 325 * @param resourcefile The name of the resource file to copy
duke@1 326 * @param overwrite A flag to indicate whether the file in the
duke@1 327 * destination directory will be overwritten if
duke@1 328 * it already exists.
duke@1 329 */
duke@1 330 public static void copyResourceFile(Configuration configuration,
bpatel@766 331 String resourcefile, boolean overwrite) {
bpatel@766 332 String destresourcesdir = configuration.destDirName + RESOURCESDIR;
bpatel@766 333 copyFile(configuration, resourcefile, RESOURCESDIR, destresourcesdir,
bpatel@793 334 overwrite, false);
bpatel@766 335 }
bpatel@766 336
bpatel@766 337 /**
bpatel@766 338 * Copy a file from a source directory to a destination directory
bpatel@766 339 * (if it is not there already). If <code>overwrite</code> is true and
bpatel@766 340 * the destination file already exists, overwrite it.
bpatel@766 341 *
bpatel@766 342 * @param configuration Holds the error message
bpatel@766 343 * @param file The name of the file to copy
bpatel@766 344 * @param source The source directory
bpatel@766 345 * @param destination The destination directory where the file needs to be copied
bpatel@766 346 * @param overwrite A flag to indicate whether the file in the
bpatel@766 347 * destination directory will be overwritten if
bpatel@766 348 * it already exists.
bpatel@793 349 * @param replaceNewLine true if the newline needs to be replaced with platform-
bpatel@793 350 * specific newline.
bpatel@766 351 */
bpatel@766 352 public static void copyFile(Configuration configuration, String file, String source,
bpatel@793 353 String destination, boolean overwrite, boolean replaceNewLine) {
bpatel@766 354 DirectoryManager.createDirectory(configuration, destination);
bpatel@766 355 File destfile = new File(destination, file);
duke@1 356 if(destfile.exists() && (! overwrite)) return;
duke@1 357 try {
duke@1 358 InputStream in = Configuration.class.getResourceAsStream(
bpatel@793 359 source + DirectoryManager.URL_FILE_SEPARATOR + file);
duke@1 360 if(in==null) return;
duke@1 361 OutputStream out = new FileOutputStream(destfile);
bpatel@793 362 try {
bpatel@793 363 if (!replaceNewLine) {
bpatel@793 364 byte[] buf = new byte[2048];
bpatel@793 365 int n;
bpatel@793 366 while((n = in.read(buf))>0) out.write(buf,0,n);
bpatel@793 367 } else {
bpatel@793 368 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
bpatel@793 369 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));
bpatel@793 370 try {
bpatel@793 371 String line;
bpatel@793 372 while ((line = reader.readLine()) != null) {
bpatel@793 373 writer.write(line);
bpatel@793 374 writer.write(DocletConstants.NL);
bpatel@793 375 }
bpatel@793 376 } finally {
bpatel@793 377 reader.close();
bpatel@793 378 writer.close();
bpatel@793 379 }
bpatel@793 380 }
bpatel@793 381 } finally {
bpatel@793 382 in.close();
bpatel@793 383 out.close();
bpatel@793 384 }
bpatel@793 385 } catch (IOException ie) {
jjg@910 386 ie.printStackTrace(System.err);
bpatel@793 387 throw new DocletAbortException();
bpatel@793 388 }
duke@1 389 }
duke@1 390
duke@1 391 /**
duke@1 392 * Given a PackageDoc, return the source path for that package.
duke@1 393 * @param configuration The Configuration for the current Doclet.
duke@1 394 * @param pkgDoc The package to seach the path for.
duke@1 395 * @return A string representing the path to the given package.
duke@1 396 */
duke@1 397 public static String getPackageSourcePath(Configuration configuration,
duke@1 398 PackageDoc pkgDoc){
duke@1 399 try{
duke@1 400 String pkgPath = DirectoryManager.getDirectoryPath(pkgDoc);
duke@1 401 String completePath = new SourcePath(configuration.sourcepath).
bpatel@766 402 getDirectory(pkgPath) + DirectoryManager.URL_FILE_SEPARATOR;
jjg@910 403 //Make sure that both paths are using the same separators.
duke@1 404 completePath = Util.replaceText(completePath, File.separator,
bpatel@766 405 DirectoryManager.URL_FILE_SEPARATOR);
duke@1 406 pkgPath = Util.replaceText(pkgPath, File.separator,
bpatel@766 407 DirectoryManager.URL_FILE_SEPARATOR);
jjg@910 408 return completePath.substring(0, completePath.lastIndexOf(pkgPath));
duke@1 409 } catch (Exception e){
duke@1 410 return "";
duke@1 411 }
duke@1 412 }
duke@1 413
duke@1 414 /**
duke@1 415 * We want the list of types in alphabetical order. However, types are not
duke@1 416 * comparable. We need a comparator for now.
duke@1 417 */
jjg@74 418 private static class TypeComparator implements Comparator<Type> {
jjg@74 419 public int compare(Type type1, Type type2) {
jjg@74 420 return type1.qualifiedTypeName().toLowerCase().compareTo(
jjg@74 421 type2.qualifiedTypeName().toLowerCase());
duke@1 422 }
duke@1 423 }
duke@1 424
duke@1 425 /**
duke@1 426 * For the class return all implemented interfaces including the
duke@1 427 * superinterfaces of the implementing interfaces, also iterate over for
duke@1 428 * all the superclasses. For interface return all the extended interfaces
duke@1 429 * as well as superinterfaces for those extended interfaces.
duke@1 430 *
duke@1 431 * @param type type whose implemented or
duke@1 432 * super interfaces are sought.
duke@1 433 * @param configuration the current configuration of the doclet.
duke@1 434 * @param sort if true, return list of interfaces sorted alphabetically.
duke@1 435 * @return List of all the required interfaces.
duke@1 436 */
jjg@74 437 public static List<Type> getAllInterfaces(Type type,
duke@1 438 Configuration configuration, boolean sort) {
jjg@74 439 Map<ClassDoc,Type> results = sort ? new TreeMap<ClassDoc,Type>() : new LinkedHashMap<ClassDoc,Type>();
duke@1 440 Type[] interfaceTypes = null;
duke@1 441 Type superType = null;
duke@1 442 if (type instanceof ParameterizedType) {
duke@1 443 interfaceTypes = ((ParameterizedType) type).interfaceTypes();
duke@1 444 superType = ((ParameterizedType) type).superclassType();
duke@1 445 } else if (type instanceof ClassDoc) {
duke@1 446 interfaceTypes = ((ClassDoc) type).interfaceTypes();
duke@1 447 superType = ((ClassDoc) type).superclassType();
duke@1 448 } else {
duke@1 449 interfaceTypes = type.asClassDoc().interfaceTypes();
duke@1 450 superType = type.asClassDoc().superclassType();
duke@1 451 }
duke@1 452
duke@1 453 for (int i = 0; i < interfaceTypes.length; i++) {
duke@1 454 Type interfaceType = interfaceTypes[i];
duke@1 455 ClassDoc interfaceClassDoc = interfaceType.asClassDoc();
duke@1 456 if (! (interfaceClassDoc.isPublic() ||
duke@1 457 (configuration == null ||
duke@1 458 isLinkable(interfaceClassDoc, configuration)))) {
duke@1 459 continue;
duke@1 460 }
duke@1 461 results.put(interfaceClassDoc, interfaceType);
mcimadamore@184 462 List<Type> superInterfaces = getAllInterfaces(interfaceType, configuration, sort);
mcimadamore@184 463 for (Iterator<Type> iter = superInterfaces.iterator(); iter.hasNext(); ) {
mcimadamore@184 464 Type t = iter.next();
duke@1 465 results.put(t.asClassDoc(), t);
duke@1 466 }
duke@1 467 }
duke@1 468 if (superType == null)
jjg@74 469 return new ArrayList<Type>(results.values());
duke@1 470 //Try walking the tree.
duke@1 471 addAllInterfaceTypes(results,
duke@1 472 superType,
duke@1 473 superType instanceof ClassDoc ?
duke@1 474 ((ClassDoc) superType).interfaceTypes() :
duke@1 475 ((ParameterizedType) superType).interfaceTypes(),
duke@1 476 false, configuration);
jjg@74 477 List<Type> resultsList = new ArrayList<Type>(results.values());
duke@1 478 if (sort) {
duke@1 479 Collections.sort(resultsList, new TypeComparator());
duke@1 480 }
duke@1 481 return resultsList;
duke@1 482 }
duke@1 483
mcimadamore@184 484 public static List<Type> getAllInterfaces(Type type, Configuration configuration) {
duke@1 485 return getAllInterfaces(type, configuration, true);
duke@1 486 }
duke@1 487
jjg@74 488 private static void findAllInterfaceTypes(Map<ClassDoc,Type> results, ClassDoc c, boolean raw,
duke@1 489 Configuration configuration) {
duke@1 490 Type superType = c.superclassType();
duke@1 491 if (superType == null)
duke@1 492 return;
duke@1 493 addAllInterfaceTypes(results, superType,
duke@1 494 superType instanceof ClassDoc ?
duke@1 495 ((ClassDoc) superType).interfaceTypes() :
duke@1 496 ((ParameterizedType) superType).interfaceTypes(),
duke@1 497 raw, configuration);
duke@1 498 }
duke@1 499
jjg@74 500 private static void findAllInterfaceTypes(Map<ClassDoc,Type> results, ParameterizedType p,
duke@1 501 Configuration configuration) {
duke@1 502 Type superType = p.superclassType();
duke@1 503 if (superType == null)
duke@1 504 return;
duke@1 505 addAllInterfaceTypes(results, superType,
duke@1 506 superType instanceof ClassDoc ?
duke@1 507 ((ClassDoc) superType).interfaceTypes() :
duke@1 508 ((ParameterizedType) superType).interfaceTypes(),
duke@1 509 false, configuration);
duke@1 510 }
duke@1 511
jjg@74 512 private static void addAllInterfaceTypes(Map<ClassDoc,Type> results, Type type,
duke@1 513 Type[] interfaceTypes, boolean raw,
duke@1 514 Configuration configuration) {
duke@1 515 for (int i = 0; i < interfaceTypes.length; i++) {
duke@1 516 Type interfaceType = interfaceTypes[i];
duke@1 517 ClassDoc interfaceClassDoc = interfaceType.asClassDoc();
duke@1 518 if (! (interfaceClassDoc.isPublic() ||
duke@1 519 (configuration != null &&
duke@1 520 isLinkable(interfaceClassDoc, configuration)))) {
duke@1 521 continue;
duke@1 522 }
duke@1 523 if (raw)
duke@1 524 interfaceType = interfaceType.asClassDoc();
duke@1 525 results.put(interfaceClassDoc, interfaceType);
mcimadamore@184 526 List<Type> superInterfaces = getAllInterfaces(interfaceType, configuration);
mcimadamore@184 527 for (Iterator<Type> iter = superInterfaces.iterator(); iter.hasNext(); ) {
mcimadamore@184 528 Type superInterface = iter.next();
duke@1 529 results.put(superInterface.asClassDoc(), superInterface);
duke@1 530 }
duke@1 531 }
duke@1 532 if (type instanceof ParameterizedType)
duke@1 533 findAllInterfaceTypes(results, (ParameterizedType) type, configuration);
duke@1 534 else if (((ClassDoc) type).typeParameters().length == 0)
duke@1 535 findAllInterfaceTypes(results, (ClassDoc) type, raw, configuration);
duke@1 536 else
duke@1 537 findAllInterfaceTypes(results, (ClassDoc) type, true, configuration);
duke@1 538 }
duke@1 539
duke@1 540 /**
duke@1 541 * Enclose in quotes, used for paths and filenames that contains spaces
duke@1 542 */
duke@1 543 public static String quote(String filepath) {
duke@1 544 return ("\"" + filepath + "\"");
duke@1 545 }
duke@1 546
duke@1 547 /**
duke@1 548 * Given a package, return it's name.
duke@1 549 * @param packageDoc the package to check.
duke@1 550 * @return the name of the given package.
duke@1 551 */
duke@1 552 public static String getPackageName(PackageDoc packageDoc) {
duke@1 553 return packageDoc == null || packageDoc.name().length() == 0 ?
duke@1 554 DocletConstants.DEFAULT_PACKAGE_NAME : packageDoc.name();
duke@1 555 }
duke@1 556
duke@1 557 /**
duke@1 558 * Given a package, return it's file name without the extension.
duke@1 559 * @param packageDoc the package to check.
duke@1 560 * @return the file name of the given package.
duke@1 561 */
duke@1 562 public static String getPackageFileHeadName(PackageDoc packageDoc) {
duke@1 563 return packageDoc == null || packageDoc.name().length() == 0 ?
duke@1 564 DocletConstants.DEFAULT_PACKAGE_FILE_NAME : packageDoc.name();
duke@1 565 }
duke@1 566
duke@1 567 /**
duke@1 568 * Given a string, replace all occurraces of 'newStr' with 'oldStr'.
duke@1 569 * @param originalStr the string to modify.
duke@1 570 * @param oldStr the string to replace.
duke@1 571 * @param newStr the string to insert in place of the old string.
duke@1 572 */
duke@1 573 public static String replaceText(String originalStr, String oldStr,
duke@1 574 String newStr) {
duke@1 575 if (oldStr == null || newStr == null || oldStr.equals(newStr)) {
duke@1 576 return originalStr;
duke@1 577 }
jjg@910 578 return originalStr.replace(oldStr, newStr);
duke@1 579 }
duke@1 580
duke@1 581 /**
duke@1 582 * Given a string, escape all special html characters and
duke@1 583 * return the result.
duke@1 584 *
duke@1 585 * @param s The string to check.
duke@1 586 * @return the original string with all of the HTML characters
duke@1 587 * escaped.
duke@1 588 *
duke@1 589 * @see #HTML_ESCAPE_CHARS
duke@1 590 */
duke@1 591 public static String escapeHtmlChars(String s) {
duke@1 592 String result = s;
duke@1 593 for (int i = 0; i < HTML_ESCAPE_CHARS.length; i++) {
duke@1 594 result = Util.replaceText(result,
duke@1 595 HTML_ESCAPE_CHARS[i][0], HTML_ESCAPE_CHARS[i][1]);
duke@1 596 }
duke@1 597 return result;
duke@1 598 }
duke@1 599
duke@1 600 /**
bpatel@766 601 * Given a string, strips all html characters and
bpatel@766 602 * return the result.
bpatel@766 603 *
bpatel@766 604 * @param rawString The string to check.
bpatel@766 605 * @return the original string with all of the HTML characters
bpatel@766 606 * stripped.
bpatel@766 607 *
bpatel@766 608 */
bpatel@766 609 public static String stripHtml(String rawString) {
bpatel@766 610 // remove HTML tags
bpatel@766 611 rawString = rawString.replaceAll("\\<.*?>", " ");
bpatel@766 612 // consolidate multiple spaces between a word to a single space
bpatel@766 613 rawString = rawString.replaceAll("\\b\\s{2,}\\b", " ");
bpatel@766 614 // remove extra whitespaces
bpatel@766 615 return rawString.trim();
bpatel@766 616 }
bpatel@766 617
bpatel@766 618 /**
duke@1 619 * Create the directory path for the file to be generated, construct
duke@1 620 * FileOutputStream and OutputStreamWriter depending upon docencoding.
duke@1 621 *
duke@1 622 * @param path The directory path to be created for this file.
duke@1 623 * @param filename File Name to which the PrintWriter will do the Output.
duke@1 624 * @param docencoding Encoding to be used for this file.
duke@1 625 * @exception IOException Exception raised by the FileWriter is passed on
duke@1 626 * to next level.
jjg@197 627 * @exception UnsupportedEncodingException Exception raised by the
duke@1 628 * OutputStreamWriter is passed on to next level.
duke@1 629 * @return Writer Writer for the file getting generated.
duke@1 630 * @see java.io.FileOutputStream
duke@1 631 * @see java.io.OutputStreamWriter
duke@1 632 */
duke@1 633 public static Writer genWriter(Configuration configuration,
duke@1 634 String path, String filename,
duke@1 635 String docencoding)
duke@1 636 throws IOException, UnsupportedEncodingException {
duke@1 637 FileOutputStream fos;
duke@1 638 if (path != null) {
duke@1 639 DirectoryManager.createDirectory(configuration, path);
duke@1 640 fos = new FileOutputStream(((path.length() > 0)?
duke@1 641 path + File.separator: "") + filename);
duke@1 642 } else {
duke@1 643 fos = new FileOutputStream(filename);
duke@1 644 }
duke@1 645 if (docencoding == null) {
jjg@197 646 return new OutputStreamWriter(fos);
duke@1 647 } else {
duke@1 648 return new OutputStreamWriter(fos, docencoding);
duke@1 649 }
duke@1 650 }
duke@1 651
duke@1 652 /**
duke@1 653 * Given an annotation, return true if it should be documented and false
duke@1 654 * otherwise.
duke@1 655 *
duke@1 656 * @param annotationDoc the annotation to check.
duke@1 657 *
duke@1 658 * @return true return true if it should be documented and false otherwise.
duke@1 659 */
duke@1 660 public static boolean isDocumentedAnnotation(AnnotationTypeDoc annotationDoc) {
duke@1 661 AnnotationDesc[] annotationDescList = annotationDoc.annotations();
duke@1 662 for (int i = 0; i < annotationDescList.length; i++) {
duke@1 663 if (annotationDescList[i].annotationType().qualifiedName().equals(
duke@1 664 java.lang.annotation.Documented.class.getName())){
duke@1 665 return true;
duke@1 666 }
duke@1 667 }
duke@1 668 return false;
duke@1 669 }
duke@1 670
duke@1 671 /**
duke@1 672 * Given a string, return an array of tokens. The separator can be escaped
duke@1 673 * with the '\' character. The '\' character may also be escaped by the
duke@1 674 * '\' character.
duke@1 675 *
duke@1 676 * @param s the string to tokenize.
duke@1 677 * @param separator the separator char.
duke@1 678 * @param maxTokens the maxmimum number of tokens returned. If the
duke@1 679 * max is reached, the remaining part of s is appended
duke@1 680 * to the end of the last token.
duke@1 681 *
duke@1 682 * @return an array of tokens.
duke@1 683 */
duke@1 684 public static String[] tokenize(String s, char separator, int maxTokens) {
jjg@74 685 List<String> tokens = new ArrayList<String>();
duke@1 686 StringBuilder token = new StringBuilder ();
duke@1 687 boolean prevIsEscapeChar = false;
duke@1 688 for (int i = 0; i < s.length(); i += Character.charCount(i)) {
duke@1 689 int currentChar = s.codePointAt(i);
duke@1 690 if (prevIsEscapeChar) {
duke@1 691 // Case 1: escaped character
duke@1 692 token.appendCodePoint(currentChar);
duke@1 693 prevIsEscapeChar = false;
duke@1 694 } else if (currentChar == separator && tokens.size() < maxTokens-1) {
duke@1 695 // Case 2: separator
duke@1 696 tokens.add(token.toString());
duke@1 697 token = new StringBuilder();
duke@1 698 } else if (currentChar == '\\') {
duke@1 699 // Case 3: escape character
duke@1 700 prevIsEscapeChar = true;
duke@1 701 } else {
duke@1 702 // Case 4: regular character
duke@1 703 token.appendCodePoint(currentChar);
duke@1 704 }
duke@1 705 }
duke@1 706 if (token.length() > 0) {
duke@1 707 tokens.add(token.toString());
duke@1 708 }
jjg@74 709 return tokens.toArray(new String[] {});
duke@1 710 }
duke@1 711
duke@1 712 /**
duke@1 713 * Return true if this class is linkable and false if we can't link to the
duke@1 714 * desired class.
duke@1 715 * <br>
duke@1 716 * <b>NOTE:</b> You can only link to external classes if they are public or
duke@1 717 * protected.
duke@1 718 *
duke@1 719 * @param classDoc the class to check.
duke@1 720 * @param configuration the current configuration of the doclet.
duke@1 721 *
duke@1 722 * @return true if this class is linkable and false if we can't link to the
duke@1 723 * desired class.
duke@1 724 */
duke@1 725 public static boolean isLinkable(ClassDoc classDoc,
duke@1 726 Configuration configuration) {
duke@1 727 return
duke@1 728 ((classDoc.isIncluded() && configuration.isGeneratedDoc(classDoc))) ||
duke@1 729 (configuration.extern.isExternal(classDoc) &&
duke@1 730 (classDoc.isPublic() || classDoc.isProtected()));
duke@1 731 }
duke@1 732
duke@1 733 /**
duke@1 734 * Given a class, return the closest visible super class.
duke@1 735 *
duke@1 736 * @param classDoc the class we are searching the parent for.
duke@1 737 * @param configuration the current configuration of the doclet.
duke@1 738 * @return the closest visible super class. Return null if it cannot
duke@1 739 * be found (i.e. classDoc is java.lang.Object).
duke@1 740 */
duke@1 741 public static Type getFirstVisibleSuperClass(ClassDoc classDoc,
duke@1 742 Configuration configuration) {
duke@1 743 if (classDoc == null) {
duke@1 744 return null;
duke@1 745 }
duke@1 746 Type sup = classDoc.superclassType();
duke@1 747 ClassDoc supClassDoc = classDoc.superclass();
duke@1 748 while (sup != null &&
duke@1 749 (! (supClassDoc.isPublic() ||
duke@1 750 isLinkable(supClassDoc, configuration))) ) {
duke@1 751 if (supClassDoc.superclass().qualifiedName().equals(supClassDoc.qualifiedName()))
duke@1 752 break;
duke@1 753 sup = supClassDoc.superclassType();
duke@1 754 supClassDoc = supClassDoc.superclass();
duke@1 755 }
duke@1 756 if (classDoc.equals(supClassDoc)) {
duke@1 757 return null;
duke@1 758 }
duke@1 759 return sup;
duke@1 760 }
duke@1 761
duke@1 762 /**
duke@1 763 * Given a class, return the closest visible super class.
duke@1 764 *
duke@1 765 * @param classDoc the class we are searching the parent for.
duke@1 766 * @param configuration the current configuration of the doclet.
duke@1 767 * @return the closest visible super class. Return null if it cannot
duke@1 768 * be found (i.e. classDoc is java.lang.Object).
duke@1 769 */
duke@1 770 public static ClassDoc getFirstVisibleSuperClassCD(ClassDoc classDoc,
duke@1 771 Configuration configuration) {
duke@1 772 if (classDoc == null) {
duke@1 773 return null;
duke@1 774 }
duke@1 775 ClassDoc supClassDoc = classDoc.superclass();
duke@1 776 while (supClassDoc != null &&
duke@1 777 (! (supClassDoc.isPublic() ||
duke@1 778 isLinkable(supClassDoc, configuration))) ) {
duke@1 779 supClassDoc = supClassDoc.superclass();
duke@1 780 }
duke@1 781 if (classDoc.equals(supClassDoc)) {
duke@1 782 return null;
duke@1 783 }
duke@1 784 return supClassDoc;
duke@1 785 }
duke@1 786
duke@1 787 /**
duke@1 788 * Given a ClassDoc, return the name of its type (Class, Interface, etc.).
duke@1 789 *
duke@1 790 * @param cd the ClassDoc to check.
duke@1 791 * @param lowerCaseOnly true if you want the name returned in lower case.
duke@1 792 * If false, the first letter of the name is capatilized.
duke@1 793 * @return
duke@1 794 */
duke@1 795 public static String getTypeName(Configuration config,
duke@1 796 ClassDoc cd, boolean lowerCaseOnly) {
duke@1 797 String typeName = "";
duke@1 798 if (cd.isOrdinaryClass()) {
duke@1 799 typeName = "doclet.Class";
duke@1 800 } else if (cd.isInterface()) {
duke@1 801 typeName = "doclet.Interface";
duke@1 802 } else if (cd.isException()) {
duke@1 803 typeName = "doclet.Exception";
duke@1 804 } else if (cd.isError()) {
duke@1 805 typeName = "doclet.Error";
duke@1 806 } else if (cd.isAnnotationType()) {
duke@1 807 typeName = "doclet.AnnotationType";
duke@1 808 } else if (cd.isEnum()) {
duke@1 809 typeName = "doclet.Enum";
duke@1 810 }
duke@1 811 return config.getText(
duke@1 812 lowerCaseOnly ? typeName.toLowerCase() : typeName);
duke@1 813 }
duke@1 814
duke@1 815 /**
duke@1 816 * Given a string, replace all tabs with the appropriate
duke@1 817 * number of spaces.
duke@1 818 * @param tabLength the length of each tab.
duke@1 819 * @param s the String to scan.
duke@1 820 */
jjg@910 821 public static void replaceTabs(int tabLength, StringBuilder s) {
jjg@910 822 if (whitespace == null || whitespace.length() < tabLength)
jjg@910 823 whitespace = String.format("%" + tabLength + "s", " ");
jjg@910 824 int index = 0;
jjg@910 825 while ((index = s.indexOf("\t", index)) != -1) {
jjg@910 826 int spaceCount = tabLength - index % tabLength;
jjg@910 827 s.replace(index, index+1, whitespace.substring(0, spaceCount));
jjg@910 828 index += spaceCount;
duke@1 829 }
duke@1 830 }
jjg@910 831 private static String whitespace;
duke@1 832
duke@1 833 /**
duke@1 834 * The documentation for values() and valueOf() in Enums are set by the
duke@1 835 * doclet.
duke@1 836 */
duke@1 837 public static void setEnumDocumentation(Configuration configuration,
duke@1 838 ClassDoc classDoc) {
duke@1 839 MethodDoc[] methods = classDoc.methods();
duke@1 840 for (int j = 0; j < methods.length; j++) {
duke@1 841 MethodDoc currentMethod = methods[j];
duke@1 842 if (currentMethod.name().equals("values") &&
duke@1 843 currentMethod.parameters().length == 0) {
duke@1 844 currentMethod.setRawCommentText(
duke@1 845 configuration.getText("doclet.enum_values_doc", classDoc.name()));
duke@1 846 } else if (currentMethod.name().equals("valueOf") &&
duke@1 847 currentMethod.parameters().length == 1) {
duke@1 848 Type paramType = currentMethod.parameters()[0].type();
duke@1 849 if (paramType != null &&
duke@1 850 paramType.qualifiedTypeName().equals(String.class.getName())) {
duke@1 851 currentMethod.setRawCommentText(
duke@1 852 configuration.getText("doclet.enum_valueof_doc"));
duke@1 853 }
duke@1 854 }
duke@1 855 }
duke@1 856 }
duke@1 857
duke@1 858 /**
duke@1 859 * Return true if the given Doc is deprecated.
duke@1 860 *
duke@1 861 * @param doc the Doc to check.
duke@1 862 * @return true if the given Doc is deprecated.
duke@1 863 */
duke@1 864 public static boolean isDeprecated(ProgramElementDoc doc) {
duke@1 865 if (doc.tags("deprecated").length > 0) {
duke@1 866 return true;
duke@1 867 }
duke@1 868 AnnotationDesc[] annotationDescList = doc.annotations();
duke@1 869 for (int i = 0; i < annotationDescList.length; i++) {
duke@1 870 if (annotationDescList[i].annotationType().qualifiedName().equals(
duke@1 871 java.lang.Deprecated.class.getName())){
duke@1 872 return true;
duke@1 873 }
duke@1 874 }
duke@1 875 return false;
duke@1 876 }
duke@1 877 }

mercurial