src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/IndexRedirectWriter.java
author jjg
Tue, 05 Mar 2019 10:35:29 -0800
changeset 54002 fb9541185457
parent 53863 d001808c57e8
child 54060 53a95878619f
permissions -rw-r--r--
8219946: Set class on body elements Reviewed-by: hannesw
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
     1
/*
53310
8ce4083fc831 8212233: javadoc fails on jdk12 with "The code being documented uses modules but the packages defined in $URL are in the unnamed module."
jjg
parents: 51468
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
     4
 *
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    10
 *
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    15
 * accompanied this code).
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    16
 *
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    20
 *
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    23
 * questions.
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    24
 */
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    25
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    26
package jdk.javadoc.internal.doclets.formats.html;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    27
53391
a99bd2570660 8217034: JavadocTester should check for missing files by default
jjg
parents: 53310
diff changeset
    28
import java.util.Collections;
a99bd2570660 8217034: JavadocTester should check for missing files by default
jjg
parents: 53310
diff changeset
    29
47849
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
    30
import jdk.javadoc.internal.doclets.formats.html.markup.Head;
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    31
import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    32
import jdk.javadoc.internal.doclets.formats.html.markup.DocType;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    33
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlAttr;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    34
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlDocument;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    35
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    36
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
47847
7640bc1eb94f 8190818: Introduce a new Script builder class
jjg
parents: 47748
diff changeset
    37
import jdk.javadoc.internal.doclets.formats.html.markup.Script;
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    38
import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    39
import jdk.javadoc.internal.doclets.toolkit.Content;
47848
6b1311fbbaba 8190819: Merge HtmlWriter into HtmlDocument
jjg
parents: 47847
diff changeset
    40
import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    41
import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    42
import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    43
import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    44
53310
8ce4083fc831 8212233: javadoc fails on jdk12 with "The code being documented uses modules but the packages defined in $URL are in the unnamed module."
jjg
parents: 51468
diff changeset
    45
import java.util.Collections;
8ce4083fc831 8212233: javadoc fails on jdk12 with "The code being documented uses modules but the packages defined in $URL are in the unnamed module."
jjg
parents: 51468
diff changeset
    46
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    47
/**
50434
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    48
 * Writes a file that tries to redirect to an alternate page.
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    49
 * The redirect uses JavaScript, if enabled, falling back on
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    50
 * {@code <meta http-eqiv=refresh content="0,<uri>">}.
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    51
 * If neither are supported/enabled in a browser, the page displays the
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    52
 * standard "JavaScipt not enabled" message, and a link to the alternate page.
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    53
 */
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    54
public class IndexRedirectWriter extends HtmlDocletWriter {
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    55
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
    56
    public static void generate(HtmlConfiguration configuration)
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    57
            throws DocFileIOException {
50434
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    58
        generate(configuration, DocPaths.INDEX, configuration.topFile);
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    59
    }
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    60
50434
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    61
    public static void generate(HtmlConfiguration configuration, DocPath fileName, DocPath target)
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    62
            throws DocFileIOException {
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    63
        IndexRedirectWriter indexRedirect = new IndexRedirectWriter(configuration, fileName, target);
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    64
        indexRedirect.generateIndexFile();
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    65
    }
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    66
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    67
    private DocPath target;
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    68
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    69
    private IndexRedirectWriter(HtmlConfiguration configuration, DocPath filename, DocPath target) {
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    70
        super(configuration, filename);
50434
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    71
        this.target = target;
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    72
    }
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    73
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    74
    /**
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    75
     * Generate an index file that redirects to an alternate file.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    76
     * @throws DocFileIOException if there is a problem generating the file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    77
     */
50434
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    78
    private void generateIndexFile() throws DocFileIOException {
47848
6b1311fbbaba 8190819: Merge HtmlWriter into HtmlDocument
jjg
parents: 47847
diff changeset
    79
        Content htmlComment = contents.newPage;
53562
0d9dee001667 8215577: Remove javadoc support for HTML 4
pmuthuswamy
parents: 53391
diff changeset
    80
        Head head = new Head(path, configuration.docletVersion)
51468
d48a852983eb 8203792: Remove "compatibility" features from Head.java
pmuthuswamy
parents: 50434
diff changeset
    81
                .setTimestamp(true)
53863
d001808c57e8 8218998: Add metadata to generated API documentation files
jjg
parents: 53562
diff changeset
    82
                .setDescription("index redirect")
d001808c57e8 8218998: Add metadata to generated API documentation files
jjg
parents: 53562
diff changeset
    83
                .setGenerator(getGenerator(getClass()))
53391
a99bd2570660 8217034: JavadocTester should check for missing files by default
jjg
parents: 53310
diff changeset
    84
                .setStylesheets(configuration.getMainStylesheet(), Collections.emptyList()) // avoid reference to default stylesheet
47849
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
    85
                .addDefaultScript(false);
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    86
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    87
        String title = (configuration.windowtitle.length() > 0)
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    88
                ? configuration.windowtitle
47847
7640bc1eb94f 8190818: Introduce a new Script builder class
jjg
parents: 47748
diff changeset
    89
                : resources.getText("doclet.Generated_Docs_Untitled");
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    90
47849
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
    91
        head.setTitle(title)
50434
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    92
                .setCharset(configuration.charset)
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    93
                .setCanonicalLink(target);
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    94
50434
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    95
        String targetPath = target.getPath();
47847
7640bc1eb94f 8190818: Introduce a new Script builder class
jjg
parents: 47748
diff changeset
    96
        Script script = new Script("window.location.replace(")
50434
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
    97
                .appendStringLiteral(targetPath, '\'')
47847
7640bc1eb94f 8190818: Introduce a new Script builder class
jjg
parents: 47748
diff changeset
    98
                .append(")");
47849
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
    99
        HtmlTree metaRefresh = new HtmlTree(HtmlTag.META)
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
   100
                .addAttr(HtmlAttr.HTTP_EQUIV, "Refresh")
50434
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
   101
                .addAttr(HtmlAttr.CONTENT, "0;" + targetPath);
53562
0d9dee001667 8215577: Remove javadoc support for HTML 4
pmuthuswamy
parents: 53391
diff changeset
   102
        head.addContent(script.asContent(), HtmlTree.NOSCRIPT(metaRefresh));
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   103
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   104
        ContentBuilder bodyContent = new ContentBuilder();
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   105
        bodyContent.addContent(HtmlTree.NOSCRIPT(
47847
7640bc1eb94f 8190818: Introduce a new Script builder class
jjg
parents: 47748
diff changeset
   106
                HtmlTree.P(contents.getContent("doclet.No_Script_Message"))));
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   107
50434
505d944de3c5 8204303: Add redirect for overview-summary.html
jjg
parents: 47849
diff changeset
   108
        bodyContent.addContent(HtmlTree.P(HtmlTree.A(targetPath, new StringContent(targetPath))));
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   109
54002
fb9541185457 8219946: Set class on body elements
jjg
parents: 53863
diff changeset
   110
        Content body = new HtmlTree(HtmlTag.BODY)
fb9541185457 8219946: Set class on body elements
jjg
parents: 53863
diff changeset
   111
                .addAttr(HtmlAttr.CLASS, "index-redirect");
53562
0d9dee001667 8215577: Remove javadoc support for HTML 4
pmuthuswamy
parents: 53391
diff changeset
   112
        HtmlTree main = HtmlTree.MAIN(bodyContent);
0d9dee001667 8215577: Remove javadoc support for HTML 4
pmuthuswamy
parents: 53391
diff changeset
   113
        body.addContent(main);
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   114
47849
0e38db7cf1cc 8190820: Introduce a new Head builder class
jjg
parents: 47848
diff changeset
   115
        Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(), head.toContent(), body);
53562
0d9dee001667 8215577: Remove javadoc support for HTML 4
pmuthuswamy
parents: 53391
diff changeset
   116
        HtmlDocument htmlDocument = new HtmlDocument(htmlComment, htmlTree);
47848
6b1311fbbaba 8190819: Merge HtmlWriter into HtmlDocument
jjg
parents: 47847
diff changeset
   117
        htmlDocument.write(DocFile.createFileForOutput(configuration, path));
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   118
    }
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   119
}