langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/AbstractDoclet.java
author ohair
Wed, 06 Apr 2011 20:33:44 -0700
changeset 9087 e9e44877cd18
parent 5520 86e4b9a9da40
child 14258 8d2148961366
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
     2
 * Copyright (c) 2003, 2008, 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: 1652
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: 1652
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: 1652
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1652
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;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.doclets.internal.toolkit.builders.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.doclets.internal.toolkit.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 * An abstract implementation of a Doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * This code is not part of an API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * It is implementation that is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * Do not use it as an API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * @author Jamie Ho
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
public abstract class AbstractDoclet {
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
     * The global configuration information for this run.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
     */
1475
19c0851667ca 6748541: javadoc should be reusable
jjg
parents: 10
diff changeset
    48
    public Configuration configuration;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
     * The only doclet that may use this toolkit is {@value}
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    private static final String TOOLKIT_DOCLET_NAME = new
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        com.sun.tools.doclets.formats.html.HtmlDoclet().getClass().getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     * Verify that the only doclet that is using this toolkit is
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
     * {@value #TOOLKIT_DOCLET_NAME}.
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private boolean isValidDoclet(AbstractDoclet doclet) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
        if (! doclet.getClass().getName().equals(TOOLKIT_DOCLET_NAME)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
            configuration.message.error("doclet.Toolkit_Usage_Violation",
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
                TOOLKIT_DOCLET_NAME);
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        return true;
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
     * The method that starts the execution of the doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * @param doclet the doclet to start the execution for.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * @param root   the {@link RootDoc} that points to the source to document.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     * @return true if the doclet executed without error.  False otherwise.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    public boolean start(AbstractDoclet doclet, RootDoc root) {
1475
19c0851667ca 6748541: javadoc should be reusable
jjg
parents: 10
diff changeset
    77
        configuration = configuration();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        configuration.root = root;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        if (! isValidDoclet(doclet)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            doclet.startGeneration(root);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        } catch (Exception exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            exc.printStackTrace();
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     * Indicate that this doclet supports the 1.5 language features.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     * @return JAVA_1_5, indicating that the new features are supported.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    public static LanguageVersion languageVersion() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        return LanguageVersion.JAVA_1_5;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * Create the configuration instance and returns it.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     * @return the configuration of the doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    public abstract Configuration configuration();
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     * Start the generation of files. Call generate methods in the individual
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * writers, which will in turn genrate the documentation files. Call the
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * TreeWriter generation first to ensure the Class Hierarchy is built
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     * first and then can be used in the later generation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     * @see com.sun.javadoc.RootDoc
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    private void startGeneration(RootDoc root) throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        if (root.classes().length == 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            configuration.message.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
                error("doclet.No_Public_Classes_To_Document");
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        configuration.setOptions();
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        configuration.getDocletSpecificMsg().notice("doclet.build_version",
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            configuration.getDocletSpecificBuildDate());
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        ClassTree classtree = new ClassTree(configuration, configuration.nodeprecated);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        generateClassFiles(root, classtree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        if (configuration.sourcepath != null && configuration.sourcepath.length() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            StringTokenizer pathTokens = new StringTokenizer(configuration.sourcepath,
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
                String.valueOf(File.pathSeparatorChar));
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            boolean first = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            while(pathTokens.hasMoreTokens()){
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                Util.copyDocFiles(configuration,
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
                    pathTokens.nextToken() + File.separator,
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
                    DocletConstants.DOC_FILES_DIR_NAME, first);
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
                first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        PackageListWriter.generate(configuration);
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        generatePackageFiles(classtree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        generateOtherFiles(root, classtree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        configuration.tagletManager.printReport();
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     * Generate additional documentation that is added to the API documentation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     * @param root      the RootDoc of source to document.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     * @param classtree the data structure representing the class tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    protected void generateOtherFiles(RootDoc root, ClassTree classtree) throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        BuilderFactory builderFactory = configuration.getBuilderFactory();
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        AbstractBuilder constantsSummaryBuilder = builderFactory.getConstantsSummaryBuider();
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        constantsSummaryBuilder.build();
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        AbstractBuilder serializedFormBuilder = builderFactory.getSerializedFormBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        serializedFormBuilder.build();
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     * Generate the package documentation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     * @param classtree the data structure representing the class tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    protected abstract void generatePackageFiles(ClassTree classtree) throws Exception;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     * Generate the class documentation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     * @param classtree the data structure representing the class tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    protected abstract void generateClassFiles(ClassDoc[] arr, ClassTree classtree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     * Iterate through all classes and construct documentation for them.
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     * @param root      the RootDoc of source to document.
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     * @param classtree the data structure representing the class tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    protected void generateClassFiles(RootDoc root, ClassTree classtree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        generateClassFiles(classtree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        PackageDoc[] packages = root.specifiedPackages();
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        for (int i = 0; i < packages.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            generateClassFiles(packages[i].allClasses(), classtree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
     * Generate the class files for single classes specified on the command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     * @param classtree the data structure representing the class tree.
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    private void generateClassFiles(ClassTree classtree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        String[] packageNames = configuration.classDocCatalog.packageNames();
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        for (int packageNameIndex = 0; packageNameIndex < packageNames.length;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                packageNameIndex++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
            generateClassFiles(configuration.classDocCatalog.allClasses(
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
                packageNames[packageNameIndex]), classtree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
}