langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/formats/html/SourceToHTMLConverter.java
author jjg
Thu, 26 May 2016 17:35:26 -0700
changeset 38614 364c1a6618c0
parent 37938 42baa89d2156
child 38617 d93a7f64e231
permissions -rw-r--r--
8157608: deprecate old entry points for javadoc tool Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
38614
364c1a6618c0 8157608: deprecate old entry points for javadoc tool
jjg
parents: 37938
diff changeset
     2
 * Copyright (c) 2001, 2016, 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
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    26
package com.sun.tools.doclets.formats.html;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
    29
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
    30
import javax.tools.FileObject;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
    31
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
    32
import com.sun.javadoc.*;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
    33
import com.sun.tools.doclets.formats.html.markup.*;
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
    34
import com.sun.tools.doclets.internal.toolkit.*;
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    35
import com.sun.tools.doclets.internal.toolkit.util.*;
38614
364c1a6618c0 8157608: deprecate old entry points for javadoc tool
jjg
parents: 37938
diff changeset
    36
import com.sun.tools.javadoc.main.SourcePositionImpl;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * Converts Java Source Code to HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 9606
diff changeset
    41
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 9606
diff changeset
    42
 *  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
    43
 *  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
    44
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * @author Jamie Ho
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    47
 * @author Bhavesh Patel (Modified)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * @since 1.4
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
public class SourceToHTMLConverter {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     * The number of trailing blank lines at the end of the page.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     * This is inserted so that anchors at the bottom of small pages
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     * can be reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    57
    private static final int NUM_BLANK_LINES = 60;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    59
    /**
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    60
     * 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
    61
     */
17560
9f6771abbd1a 8011650: reduce use of RawHtml nodes in doclet
jjg
parents: 14542
diff changeset
    62
    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
    63
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    64
    private final ConfigurationImpl configuration;
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22159
diff changeset
    65
    private final Utils utils;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    66
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    67
    private final RootDoc rootDoc;
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    68
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    69
    private DocPath outputdir;
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    70
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    71
    /**
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    72
     * 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
    73
     * generated.
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    74
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    75
    private DocPath relativePath = DocPath.empty;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    77
    private SourceToHTMLConverter(ConfigurationImpl configuration, RootDoc rd,
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    78
            DocPath outputdir) {
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    79
        this.configuration  = configuration;
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22159
diff changeset
    80
        this.utils = configuration.utils;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    81
        this.rootDoc = rd;
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    82
        this.outputdir = outputdir;
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    83
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     * Convert the Classes in the given RootDoc to an HTML.
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    87
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * @param configuration the configuration.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     * @param rd the RootDoc to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     * @param outputdir the name of the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    92
    public static void convertRoot(ConfigurationImpl configuration, RootDoc rd,
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
    93
            DocPath outputdir) {
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    94
        new SourceToHTMLConverter(configuration, rd, outputdir).generate();
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    95
    }
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    96
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    97
    void generate() {
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    98
        if (rootDoc == null || outputdir == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        }
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   101
        for (PackageDoc pd : rootDoc.specifiedPackages()) {
9606
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   102
            // 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
   103
            // do not convert the package files to HTML.
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22159
diff changeset
   104
            if (!(configuration.nodeprecated && utils.isDeprecated(pd)))
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   105
                convertPackage(pd, outputdir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        }
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   107
        for (ClassDoc cd : rootDoc.specifiedClasses()) {
9606
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   108
            // 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
   109
            // 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
   110
            // package files to HTML.
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   111
            if (!(configuration.nodeprecated &&
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22159
diff changeset
   112
                  (utils.isDeprecated(cd) || utils.isDeprecated(cd.containingPackage()))))
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   113
                convertClass(cd, outputdir);
10
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
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * Convert the Classes in the given Package to an HTML.
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   119
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     * @param pd the Package to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
     * @param outputdir the name of the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   123
    public void convertPackage(PackageDoc pd, DocPath outputdir) {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   124
        if (pd == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        }
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   127
        for (ClassDoc cd : pd.allClasses()) {
9606
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   128
            // 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
   129
            // 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
   130
            // 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
   131
            // the calling method above.
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22159
diff changeset
   132
            if (!(configuration.nodeprecated && utils.isDeprecated(cd)))
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   133
                convertClass(cd, outputdir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        }
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
     * 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
   139
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * @param cd the class to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * @param outputdir the name of the directory to output to.
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   143
    public void convertClass(ClassDoc cd, DocPath outputdir) {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   144
        if (cd == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        try {
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   148
            SourcePosition sp = cd.position();
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   149
            if (sp == null)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   150
                return;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   151
            Reader r;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   152
            // temp hack until we can update SourcePosition API.
38614
364c1a6618c0 8157608: deprecate old entry points for javadoc tool
jjg
parents: 37938
diff changeset
   153
            if (sp instanceof SourcePositionImpl) {
364c1a6618c0 8157608: deprecate old entry points for javadoc tool
jjg
parents: 37938
diff changeset
   154
                FileObject fo = ((SourcePositionImpl) sp).fileObject();
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   155
                if (fo == null)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   156
                    return;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   157
                r = fo.openReader(true);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   158
            } else {
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   159
                File file = sp.file();
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   160
                if (file == null)
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   161
                    return;
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   162
                r = new FileReader(file);
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
   163
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            int lineno = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
            String line;
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   166
            relativePath = DocPaths.SOURCE_OUTPUT
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   167
                    .resolve(DocPath.forPackage(cd))
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   168
                    .invert();
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   169
            Content body = getHeader();
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   170
            Content pre = new HtmlTree(HtmlTag.PRE);
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   171
            try (LineNumberReader reader = new LineNumberReader(r)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
                while ((line = reader.readLine()) != null) {
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   173
                    addLineNo(pre, lineno);
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   174
                    addLine(pre, line, lineno);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                    lineno++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            }
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   178
            addBlankLines(pre);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   179
            Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
29957
7740f9657f56 8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents: 25874
diff changeset
   180
            body.addContent((configuration.allowTag(HtmlTag.MAIN)) ? HtmlTree.MAIN(div) : div);
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   181
            writeToFile(body, outputdir.resolve(DocPath.forClass(cd)));
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   182
        } catch (IOException e) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            e.printStackTrace();
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
     * 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
   189
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   190
     * @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
   191
     * @param path the path for the file.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   193
    private void writeToFile(Content body, DocPath path) throws IOException {
29957
7740f9657f56 8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents: 25874
diff changeset
   194
        Content htmlDocType = configuration.isOutputHtml5()
7740f9657f56 8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents: 25874
diff changeset
   195
                ? DocType.HTML5
7740f9657f56 8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents: 25874
diff changeset
   196
                : DocType.TRANSITIONAL;
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   197
        Content head = new HtmlTree(HtmlTag.HEAD);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   198
        head.addContent(HtmlTree.TITLE(new StringContent(
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   199
                configuration.getText("doclet.Window_Source_title"))));
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   200
        head.addContent(getStyleSheetProperties());
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   201
        Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   202
                head, body);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   203
        Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   204
        configuration.message.notice("doclet.Generating_0", path.getPath());
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   205
        DocFile df = DocFile.createFileForOutput(configuration, path);
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   206
        try (Writer w = df.openWriter()) {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   207
            htmlDocument.write(w, true);
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   208
        }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   209
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   213
     * Returns a link to the stylesheet file.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     *
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   215
     * @return an HtmlTree for the lINK tag which provides the stylesheet location
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   217
    public HtmlTree getStyleSheetProperties() {
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   218
        String filename = configuration.stylesheetfile;
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   219
        DocPath stylesheet;
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   220
        if (filename.length() > 0) {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   221
            DocFile file = DocFile.createFileForInput(configuration, filename);
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   222
            stylesheet = DocPath.create(file.getName());
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   223
        } else {
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   224
            stylesheet = DocPaths.STYLESHEET;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        }
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   226
        DocPath p = relativePath.resolve(stylesheet);
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   227
        HtmlTree link = HtmlTree.LINK("stylesheet", "text/css", p.getPath(), "Style");
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   228
        return link;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
     * Get the header.
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   233
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   234
     * @return the header content for the HTML file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   236
    private static Content getHeader() {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   237
        return new HtmlTree(HtmlTag.BODY);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   241
     * 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
   242
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   243
     * @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
   244
     * @param lineno The line number
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   246
    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
   247
        HtmlTree span = new HtmlTree(HtmlTag.SPAN);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   248
        span.addStyle(HtmlStyle.sourceLineNo);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   249
        if (lineno < 10) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   250
            span.addContent("00" + Integer.toString(lineno));
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   251
        } else if (lineno < 100) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   252
            span.addContent("0" + Integer.toString(lineno));
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   253
        } else {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   254
            span.addContent(Integer.toString(lineno));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        }
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   256
        pre.addContent(span);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   260
     * 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
   261
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   262
     * @param pre the content tree to which the line will be added.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
     * @param line the string to format.
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
     * @param currentLineNo the current number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   266
    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
   267
        if (line != null) {
31297
86fe1414f12c 8081854: Javadoc should generate named anchors for HTML4 output
bpatel
parents: 29957
diff changeset
   268
            Content anchor = HtmlTree.A(configuration.htmlVersion,
86fe1414f12c 8081854: Javadoc should generate named anchors for HTML4 output
bpatel
parents: 29957
diff changeset
   269
                    "line." + Integer.toString(currentLineNo),
29957
7740f9657f56 8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents: 25874
diff changeset
   270
                    new StringContent(utils.replaceTabs(configuration, line)));
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   271
            pre.addContent(anchor);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   272
            pre.addContent(NEW_LINE);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   277
     * 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
   278
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   279
     * @param pre the content tree to which the blank lines will be added.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   281
    private static void addBlankLines(Content pre) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   282
        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
   283
            pre.addContent(NEW_LINE);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   284
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
     * Given a <code>Doc</code>, 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
   289
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
     * @param d the <code>Doc</code> to check.
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
     * @return the name of the anchor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    public static String getAnchorName(Doc d) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
        return "line." + d.position().line();
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
}