langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/SourceToHTMLConverter.java
author jjg
Mon, 22 Aug 2016 16:32:40 -0700
changeset 40587 1c355ea550ed
parent 40508 74ef30d16fb9
child 42277 2668b0bc7ad7
permissions -rw-r--r--
8164130: Simplify doclet IOException handling Reviewed-by: bpatel, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
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
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
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
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    30
import javax.lang.model.element.Element;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    31
import javax.lang.model.element.PackageElement;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    32
import javax.lang.model.element.TypeElement;
1869
0e193a8f3520 6794582: javadoc should read files using a FileManager
jjg
parents: 1863
diff changeset
    33
import javax.tools.FileObject;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
    34
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    35
import jdk.javadoc.doclet.DocletEnvironment;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    36
import jdk.javadoc.internal.doclets.formats.html.markup.DocType;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    37
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlDocument;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    38
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    39
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    40
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    41
import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    42
import jdk.javadoc.internal.doclets.toolkit.Content;
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 39364
diff changeset
    43
import jdk.javadoc.internal.doclets.toolkit.Messages;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    44
import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
    45
import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    46
import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    47
import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    48
import jdk.javadoc.internal.doclets.toolkit.util.DocletConstants;
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
    49
import jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    50
import jdk.javadoc.internal.doclets.toolkit.util.Utils;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * Converts Java Source Code to HTML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 9606
diff changeset
    55
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 9606
diff changeset
    56
 *  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
    57
 *  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
    58
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 * @author Jamie Ho
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    61
 * @author Bhavesh Patel (Modified)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
public class SourceToHTMLConverter {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     * The number of trailing blank lines at the end of the page.
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     * This is inserted so that anchors at the bottom of small pages
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     * can be reached.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    70
    private static final int NUM_BLANK_LINES = 60;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    72
    /**
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    73
     * 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
    74
     */
17560
9f6771abbd1a 8011650: reduce use of RawHtml nodes in doclet
jjg
parents: 14542
diff changeset
    75
    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
    76
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    77
    private final ConfigurationImpl configuration;
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 39364
diff changeset
    78
    private final Messages messages;
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22159
diff changeset
    79
    private final Utils utils;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    80
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
    81
    private final DocletEnvironment docEnv;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    82
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
    83
    private final DocPath outputdir;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    84
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    85
    /**
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    86
     * 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
    87
     * generated.
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
    88
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    89
    private DocPath relativePath = DocPath.empty;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
    91
    private SourceToHTMLConverter(ConfigurationImpl configuration, DocletEnvironment rd,
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    92
            DocPath outputdir) {
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    93
        this.configuration  = configuration;
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 39364
diff changeset
    94
        this.messages = configuration.getMessages();
25454
376a52c9540c 8039028: [javadoc] refactor the usage of Util.java
ksrini
parents: 22159
diff changeset
    95
        this.utils = configuration.utils;
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
    96
        this.docEnv = rd;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    97
        this.outputdir = outputdir;
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
    98
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    /**
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   101
     * 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
   102
     *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
     * @param configuration the configuration.
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
   104
     * @param docEnv the DocletEnvironment to convert.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     * @param outputdir the name of the directory to output to.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   106
     * @throws DocFileIOException if there is a problem generating an output file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   107
     * @throws SimpleDocletException if there is a problem reading a source file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     */
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
   109
    public static void convertRoot(ConfigurationImpl configuration, DocletEnvironment docEnv,
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   110
            DocPath outputdir) throws DocFileIOException, SimpleDocletException {
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
   111
        new SourceToHTMLConverter(configuration, docEnv, outputdir).generate();
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   112
    }
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   113
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   114
    void generate() throws DocFileIOException, SimpleDocletException {
39364
bd6d4a7936b4 8160301: javadoc RootDoclmpl and DocEnv needs to be renamed
ksrini
parents: 35426
diff changeset
   115
        if (docEnv == null || outputdir == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        }
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40303
diff changeset
   118
        for (PackageElement pkg : configuration.getSpecifiedPackages()) {
9606
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   119
            // 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
   120
            // do not convert the package files to HTML.
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   121
            if (!(configuration.nodeprecated && utils.isDeprecated(pkg)))
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   122
                convertPackage(pkg, outputdir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        }
40508
74ef30d16fb9 8159305: Enhance the javadoc tool to support module related options
ksrini
parents: 40303
diff changeset
   124
        for (TypeElement te : configuration.getSpecifiedClasses()) {
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 class is marked as deprecated
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   126
            // 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
   127
            // package files to HTML.
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   128
            if (!(configuration.nodeprecated &&
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   129
                  (utils.isDeprecated(te) || utils.isDeprecated(utils.containingPackage(te)))))
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   130
                convertClass(te, outputdir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /**
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   135
     * 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
   136
     *
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   137
     * @param pkg the Package to convert.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     * @param outputdir the name of the directory to output to.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   139
     * @throws DocFileIOException if there is a problem generating an output file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   140
     * @throws SimpleDocletException if there is a problem reading a source file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   142
    public void convertPackage(PackageElement pkg, DocPath outputdir)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   143
            throws DocFileIOException, SimpleDocletException {
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   144
        if (pkg == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        }
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   147
        for (Element te : utils.getAllClasses(pkg)) {
9606
e5a619cb5dd3 6492694: @deprecated tag doesn't work in package-info files.
bpatel
parents: 8631
diff changeset
   148
            // 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
   149
            // 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
   150
            // 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
   151
            // the calling method above.
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   152
            if (!(configuration.nodeprecated && utils.isDeprecated(te)))
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   153
                convertClass((TypeElement)te, outputdir);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     * 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
   159
     *
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   160
     * @param te the class to convert.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   161
     * @param outputdir the name of the directory to output to
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   162
     * @throws DocFileIOException if there is a problem generating the output file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   163
     * @throws SimpleDocletException if there is a problem reading the source file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   165
    public void convertClass(TypeElement te, DocPath outputdir)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   166
            throws DocFileIOException, SimpleDocletException {
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   167
        if (te == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        }
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   170
        FileObject fo = utils.getFileObject(te);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   171
        if (fo == null)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   172
            return;
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   173
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        try {
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   175
            Reader r = fo.openReader(true);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            int lineno = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            String line;
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   178
            relativePath = DocPaths.SOURCE_OUTPUT
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   179
                    .resolve(DocPath.forPackage(utils, te))
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   180
                    .invert();
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   181
            Content body = getHeader();
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   182
            Content pre = new HtmlTree(HtmlTag.PRE);
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   183
            try (LineNumberReader reader = new LineNumberReader(r)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
                while ((line = reader.readLine()) != null) {
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   185
                    addLineNo(pre, lineno);
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   186
                    addLine(pre, line, lineno);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                    lineno++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
            }
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   190
            addBlankLines(pre);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   191
            Content div = HtmlTree.DIV(HtmlStyle.sourceContainer, pre);
29957
7740f9657f56 8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents: 25874
diff changeset
   192
            body.addContent((configuration.allowTag(HtmlTag.MAIN)) ? HtmlTree.MAIN(div) : div);
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   193
            writeToFile(body, outputdir.resolve(DocPath.forClass(utils, te)));
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   194
        } catch (IOException e) {
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   195
            String message = configuration.resources.getText("doclet.exception.read.file", fo.getName());
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   196
            throw new SimpleDocletException(message, e);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
     * 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
   202
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   203
     * @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
   204
     * @param path the path for the file.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   206
    private void writeToFile(Content body, DocPath path) throws DocFileIOException {
29957
7740f9657f56 8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents: 25874
diff changeset
   207
        Content htmlDocType = configuration.isOutputHtml5()
7740f9657f56 8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents: 25874
diff changeset
   208
                ? DocType.HTML5
7740f9657f56 8072945: Javadoc should generate valid and compliant HTML5 output
bpatel
parents: 25874
diff changeset
   209
                : DocType.TRANSITIONAL;
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   210
        Content head = new HtmlTree(HtmlTag.HEAD);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   211
        head.addContent(HtmlTree.TITLE(new StringContent(
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   212
                configuration.getText("doclet.Window_Source_title"))));
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   213
        head.addContent(getStyleSheetProperties());
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   214
        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
   215
                head, body);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   216
        Content htmlDocument = new HtmlDocument(htmlDocType, htmlTree);
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 39364
diff changeset
   217
        messages.notice("doclet.Generating_0", path.getPath());
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   218
        DocFile df = DocFile.createFileForOutput(configuration, path);
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17571
diff changeset
   219
        try (Writer w = df.openWriter()) {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   220
            htmlDocument.write(w, true);
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   221
        } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   222
            throw new DocFileIOException(df, DocFileIOException.Mode.WRITE, e);
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   223
        }
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   224
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   228
     * Returns a link to the stylesheet file.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     *
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   230
     * @return an HtmlTree for the lINK tag which provides the stylesheet location
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   232
    public HtmlTree getStyleSheetProperties() {
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   233
        String filename = configuration.stylesheetfile;
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   234
        DocPath stylesheet;
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   235
        if (filename.length() > 0) {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   236
            DocFile file = DocFile.createFileForInput(configuration, filename);
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14357
diff changeset
   237
            stylesheet = DocPath.create(file.getName());
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   238
        } else {
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   239
            stylesheet = DocPaths.STYLESHEET;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        }
14357
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   241
        DocPath p = relativePath.resolve(stylesheet);
faf9cde2817b 8000741: refactor javadoc to use abstraction to handle relative paths
jjg
parents: 14260
diff changeset
   242
        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
   243
        return link;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     * Get the header.
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   248
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   249
     * @return the header content for the HTML file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   251
    private static Content getHeader() {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   252
        return new HtmlTree(HtmlTag.BODY);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
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
     * 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
   257
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   258
     * @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
   259
     * @param lineno The line number
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   261
    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
   262
        HtmlTree span = new HtmlTree(HtmlTag.SPAN);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   263
        span.addStyle(HtmlStyle.sourceLineNo);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   264
        if (lineno < 10) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   265
            span.addContent("00" + Integer.toString(lineno));
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   266
        } else if (lineno < 100) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   267
            span.addContent("0" + Integer.toString(lineno));
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   268
        } else {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   269
            span.addContent(Integer.toString(lineno));
10
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
        pre.addContent(span);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   275
     * 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
   276
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   277
     * @param pre the content tree to which the line will be added.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
     * @param line the string to format.
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     * @param currentLineNo the current number.
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     */
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14368
diff changeset
   281
    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
   282
        if (line != null) {
31297
86fe1414f12c 8081854: Javadoc should generate named anchors for HTML4 output
bpatel
parents: 29957
diff changeset
   283
            Content anchor = HtmlTree.A(configuration.htmlVersion,
86fe1414f12c 8081854: Javadoc should generate named anchors for HTML4 output
bpatel
parents: 29957
diff changeset
   284
                    "line." + Integer.toString(currentLineNo),
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   285
                    new StringContent(utils.replaceTabs(line)));
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   286
            pre.addContent(anchor);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   287
            pre.addContent(NEW_LINE);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
    /**
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   292
     * 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
   293
     *
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   294
     * @param pre the content tree to which the blank lines will be added.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
     */
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   296
    private static void addBlankLines(Content pre) {
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   297
        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
   298
            pre.addContent(NEW_LINE);
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5520
diff changeset
   299
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    /**
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   303
     * 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
   304
     *
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40508
diff changeset
   305
     * @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
   306
     * @param e the element to check.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
     * @return the name of the anchor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   309
    public static String getAnchorName(Utils utils, Element e) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 31297
diff changeset
   310
        return "line." + utils.getLineNumber(e);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
}