src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/SourceToHTMLConverter.java
author jjg
Wed, 20 Feb 2019 16:15:02 -0800
changeset 53863 d001808c57e8
parent 53562 0d9dee001667
child 54002 fb9541185457
permissions -rw-r--r--
8218998: Add metadata to generated API documentation files Reviewed-by: hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
53863
d001808c57e8 8218998: Add metadata to generated API documentation files
jjg
parents: 53562
diff changeset
     2
 * Copyright (c) 2001, 2019, 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
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    26
package jdk.javadoc.internal.doclets.formats.html;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
47849
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
    28
import jdk.javadoc.internal.doclets.formats.html.markup.Head;
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.io.*;
47748
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
    31
import java.util.List;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
    32
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    33
import javax.lang.model.element.Element;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    34
import javax.lang.model.element.PackageElement;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    35
import javax.lang.model.element.TypeElement;
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
    36
import javax.tools.FileObject;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
    37
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    38
import jdk.javadoc.doclet.DocletEnvironment;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    39
import jdk.javadoc.internal.doclets.formats.html.markup.DocType;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    40
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlDocument;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    41
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    42
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    43
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    44
import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    45
import jdk.javadoc.internal.doclets.toolkit.Content;
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 39364
diff changeset
    46
import jdk.javadoc.internal.doclets.toolkit.Messages;
47847
7640bc1eb94f 8190818: Introduce a new Script builder class
jjg
parents: 47846
diff changeset
    47
import jdk.javadoc.internal.doclets.toolkit.Resources;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    48
import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
    49
import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    50
import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    51
import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    52
import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
    53
import jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    54
import jdk.javadoc.internal.doclets.toolkit.util.Utils;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 * Converts Java Source Code to HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 9606
diff changeset
    59
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 9606
diff changeset
    60
 *  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
    61
 *  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
    62
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
 * @author Jamie Ho
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    65
 * @author Bhavesh Patel (Modified)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
public class SourceToHTMLConverter {
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 number of trailing blank lines at the end of the page.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * This is inserted so that anchors at the bottom of small pages
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * can be reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    74
    private static final int NUM_BLANK_LINES = 60;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    76
    /**
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    77
     * New line to be added to the documentation.
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    78
     */
17560
9f6771abbd1a 8011650: reduce use of RawHtml nodes in doclet
jjg
parents: 14542
diff changeset
    79
    private static final String NEW_LINE = DocletConstants.NL;
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    80
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 42277
diff changeset
    81
    private final HtmlConfiguration configuration;
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 39364
diff changeset
    82
    private final Messages messages;
47847
7640bc1eb94f 8190818: Introduce a new Script builder class
jjg
parents: 47846
diff changeset
    83
    private final Resources resources;
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22159
diff changeset
    84
    private final Utils utils;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    85
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
    86
    private final DocletEnvironment docEnv;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    87
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
    88
    private final DocPath outputdir;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    89
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    90
    /**
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    91
     * Relative path from the documentation root to the file that is being
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    92
     * generated.
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    93
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    94
    private DocPath relativePath = DocPath.empty;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 42277
diff changeset
    96
    private SourceToHTMLConverter(HtmlConfiguration configuration, DocletEnvironment rd,
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 42277
diff changeset
    97
                                  DocPath outputdir) {
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    98
        this.configuration  = configuration;
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 39364
diff changeset
    99
        this.messages = configuration.getMessages();
47847
7640bc1eb94f 8190818: Introduce a new Script builder class
jjg
parents: 47846
diff changeset
   100
        this.resources = configuration.resources;
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22159
diff changeset
   101
        this.utils = configuration.utils;
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
   102
        this.docEnv = rd;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   103
        this.outputdir = outputdir;
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   104
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /**
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   107
     * Translate the TypeElements in the given DocletEnvironment to HTML representation.
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   108
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     * @param configuration the configuration.
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
   110
     * @param docEnv the DocletEnvironment to convert.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     * @param outputdir the name of the directory to output to.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   112
     * @throws DocFileIOException if there is a problem generating an output file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   113
     * @throws SimpleDocletException if there is a problem reading a source file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
     */
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 42277
diff changeset
   115
    public static void convertRoot(HtmlConfiguration configuration, DocletEnvironment docEnv,
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 42277
diff changeset
   116
                                   DocPath outputdir) throws DocFileIOException, SimpleDocletException {
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
   117
        new SourceToHTMLConverter(configuration, docEnv, outputdir).generate();
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   118
    }
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   119
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   120
    void generate() throws DocFileIOException, SimpleDocletException {
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
   121
        if (docEnv == null || outputdir == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        }
42277
2668b0bc7ad7 8164316: Refine the Doclet APIs
ksrini
parents: 40587
diff changeset
   124
        for (PackageElement pkg : configuration.getSpecifiedPackageElements()) {
9606
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   125
            // If -nodeprecated option is set and the package is marked as deprecated,
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   126
            // do not convert the package files to HTML.
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   127
            if (!(configuration.nodeprecated && utils.isDeprecated(pkg)))
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   128
                convertPackage(pkg, outputdir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        }
42277
2668b0bc7ad7 8164316: Refine the Doclet APIs
ksrini
parents: 40587
diff changeset
   130
        for (TypeElement te : configuration.getSpecifiedTypeElements()) {
9606
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   131
            // If -nodeprecated option is set and the class is marked as deprecated
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   132
            // or the containing package is deprecated, do not convert the
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   133
            // package files to HTML.
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   134
            if (!(configuration.nodeprecated &&
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   135
                  (utils.isDeprecated(te) || utils.isDeprecated(utils.containingPackage(te)))))
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   136
                convertClass(te, outputdir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    /**
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   141
     * Convert the Classes in the given Package to an HTML file.
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   142
     *
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   143
     * @param pkg the Package to convert.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
     * @param outputdir the name of the directory to output to.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   145
     * @throws DocFileIOException if there is a problem generating an output file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   146
     * @throws SimpleDocletException if there is a problem reading a source file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   148
    public void convertPackage(PackageElement pkg, DocPath outputdir)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   149
            throws DocFileIOException, SimpleDocletException {
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   150
        if (pkg == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        }
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   153
        for (Element te : utils.getAllClasses(pkg)) {
9606
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   154
            // If -nodeprecated option is set and the class is marked as deprecated,
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   155
            // do not convert the package files to HTML. We do not check for
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   156
            // containing package deprecation since it is already check in
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   157
            // the calling method above.
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   158
            if (!(configuration.nodeprecated && utils.isDeprecated(te)))
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   159
                convertClass((TypeElement)te, outputdir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     * Convert the given Class to an HTML.
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   165
     *
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   166
     * @param te the class to convert.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   167
     * @param outputdir the name of the directory to output to
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   168
     * @throws DocFileIOException if there is a problem generating the output file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   169
     * @throws SimpleDocletException if there is a problem reading the source file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   171
    public void convertClass(TypeElement te, DocPath outputdir)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   172
            throws DocFileIOException, SimpleDocletException {
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   173
        if (te == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        }
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   176
        FileObject fo = utils.getFileObject(te);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   177
        if (fo == null)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   178
            return;
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   179
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        try {
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   181
            Reader r = fo.openReader(true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            int lineno = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            String line;
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   184
            relativePath = DocPaths.SOURCE_OUTPUT
48759
ffa68af7da87 8195795: Organize output files by module/package, not just package
jjg
parents: 47849
diff changeset
   185
                    .resolve(configuration.docPaths.forPackage(te))
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   186
                    .invert();
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   187
            Content body = getHeader();
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   188
            Content pre = new HtmlTree(HtmlTag.PRE);
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   189
            try (LineNumberReader reader = new LineNumberReader(r)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
                while ((line = reader.readLine()) != null) {
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   191
                    addLineNo(pre, lineno);
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   192
                    addLine(pre, line, lineno);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                    lineno++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            }
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   196
            addBlankLines(pre);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   197
            Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
53562
0d9dee001667 8215577: Remove javadoc support for HTML 4
pmuthuswamy
parents: 48759
diff changeset
   198
            body.addContent(HtmlTree.MAIN(div));
53863
d001808c57e8 8218998: Add metadata to generated API documentation files
jjg
parents: 53562
diff changeset
   199
            writeToFile(body, outputdir.resolve(configuration.docPaths.forClass(te)), te);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   200
        } catch (IOException e) {
47847
7640bc1eb94f 8190818: Introduce a new Script builder class
jjg
parents: 47846
diff changeset
   201
            String message = resources.getText("doclet.exception.read.file", fo.getName());
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   202
            throw new SimpleDocletException(message, e);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     * Write the output to the file.
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   208
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   209
     * @param body the documentation content to be written to the file.
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   210
     * @param path the path for the file.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     */
53863
d001808c57e8 8218998: Add metadata to generated API documentation files
jjg
parents: 53562
diff changeset
   212
    private void writeToFile(Content body, DocPath path, TypeElement te) throws DocFileIOException {
53562
0d9dee001667 8215577: Remove javadoc support for HTML 4
pmuthuswamy
parents: 48759
diff changeset
   213
        Head head = new Head(path, configuration.docletVersion)
47849
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
   214
//                .setTimestamp(!configuration.notimestamp) // temporary: compatibility!
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
   215
                .setTitle(resources.getText("doclet.Window_Source_title"))
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
   216
//                .setCharset(configuration.charset) // temporary: compatibility!
53863
d001808c57e8 8218998: Add metadata to generated API documentation files
jjg
parents: 53562
diff changeset
   217
                .setDescription(HtmlDocletWriter.getDescription("source", te))
d001808c57e8 8218998: Add metadata to generated API documentation files
jjg
parents: 53562
diff changeset
   218
                .setGenerator(HtmlDocletWriter.getGenerator(getClass()))
47849
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
   219
                .addDefaultScript(false)
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
   220
                .setStylesheets(configuration.getMainStylesheet(), configuration.getAdditionalStylesheets());
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   221
        Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
47849
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
   222
                head.toContent(), body);
53562
0d9dee001667 8215577: Remove javadoc support for HTML 4
pmuthuswamy
parents: 48759
diff changeset
   223
        HtmlDocument htmlDocument = new HtmlDocument(htmlTree);
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 39364
diff changeset
   224
        messages.notice("doclet.Generating_0", path.getPath());
47848
6b1311fbbaba 8190819: Merge HtmlWriter into HtmlDocument
jjg
parents: 47847
diff changeset
   225
        htmlDocument.write(DocFile.createFileForOutput(configuration, path));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   229
     * Returns a link to the stylesheet file.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
     *
47748
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   231
     * @param head an HtmlTree to which the stylesheet links will be added
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
     */
47748
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   233
    public void addStyleSheetProperties(Content head) {
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   234
        String filename = configuration.stylesheetfile;
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   235
        DocPath stylesheet;
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   236
        if (filename.length() > 0) {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   237
            DocFile file = DocFile.createFileForInput(configuration, filename);
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   238
            stylesheet = DocPath.create(file.getName());
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   239
        } else {
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   240
            stylesheet = DocPaths.STYLESHEET;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        }
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   242
        DocPath p = relativePath.resolve(stylesheet);
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   243
        HtmlTree link = HtmlTree.LINK("stylesheet", "text/css", p.getPath(), "Style");
47748
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   244
        head.addContent(link);
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   245
        addStylesheets(head);
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   246
    }
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   247
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   248
    protected void addStylesheets(Content tree) {
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   249
        List<String> stylesheets = configuration.additionalStylesheets;
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   250
        if (!stylesheets.isEmpty()) {
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   251
            stylesheets.forEach((ssheet) -> {
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   252
                DocFile file = DocFile.createFileForInput(configuration, ssheet);
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   253
                DocPath ssheetPath = DocPath.create(file.getName());
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   254
                HtmlTree slink = HtmlTree.LINK("stylesheet", "text/css", relativePath.resolve(ssheetPath).getPath(),
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   255
                        "Style");
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   256
                tree.addContent(slink);
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   257
            });
dfc709c80775 8185371: Support for multiple stylesheets in javadoc
bpatel
parents: 47216
diff changeset
   258
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
     * Get the header.
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   263
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   264
     * @return the header content for the HTML file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   266
    private static Content getHeader() {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   267
        return new HtmlTree(HtmlTag.BODY);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   271
     * Add the line numbers for the source code.
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   272
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   273
     * @param pre the content tree to which the line number will be added
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   274
     * @param lineno The line number
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   276
    private static void addLineNo(Content pre, int lineno) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   277
        HtmlTree span = new HtmlTree(HtmlTag.SPAN);
47846
4e78aba768f0 8190295: Introduce a new Table builder class
jjg
parents: 47748
diff changeset
   278
        span.setStyle(HtmlStyle.sourceLineNo);
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   279
        if (lineno < 10) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   280
            span.addContent("00" + Integer.toString(lineno));
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   281
        } else if (lineno < 100) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   282
            span.addContent("0" + Integer.toString(lineno));
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   283
        } else {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   284
            span.addContent(Integer.toString(lineno));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        }
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   286
        pre.addContent(span);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   290
     * Add a line from source to the HTML file that is generated.
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   291
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   292
     * @param pre the content tree to which the line will be added.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
     * @param line the string to format.
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
     * @param currentLineNo the current number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   296
    private void addLine(Content pre, String line, int currentLineNo) {
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   297
        if (line != null) {
53562
0d9dee001667 8215577: Remove javadoc support for HTML 4
pmuthuswamy
parents: 48759
diff changeset
   298
            Content anchor = HtmlTree.A_ID(
31297
86fe1414f12c 8081854: Javadoc should generate named anchors for HTML4 output
bpatel
parents: 29957
diff changeset
   299
                    "line." + Integer.toString(currentLineNo),
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   300
                    new StringContent(utils.replaceTabs(line)));
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   301
            pre.addContent(anchor);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   302
            pre.addContent(NEW_LINE);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   307
     * Add trailing blank lines at the end of the page.
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   308
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   309
     * @param pre the content tree to which the blank lines will be added.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   311
    private static void addBlankLines(Content pre) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   312
        for (int i = 0; i < NUM_BLANK_LINES; i++) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   313
            pre.addContent(NEW_LINE);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   314
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    /**
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   318
     * Given an element, return an anchor name for it.
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   319
     *
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   320
     * @param utils the utility class, used to get the line number of the element
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   321
     * @param e the element to check.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
     * @return the name of the anchor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   324
    public static String getAnchorName(Utils utils, Element e) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   325
        return "line." + utils.getLineNumber(e);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
}