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