langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/IndexRedirectWriter.java
author jjg
Mon, 22 Aug 2016 16:32:40 -0700
changeset 40587 1c355ea550ed
parent 40500 f293dbb81a53
child 45417 f7479ee8de69
permissions -rw-r--r--
8164130: Simplify doclet IOException handling Reviewed-by: bpatel, ksrini
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
/*
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
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
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    28
import jdk.javadoc.internal.doclets.formats.html.markup.Comment;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    29
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
    30
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
    31
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
    32
import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
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.HtmlDocument;
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.HtmlTag;
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.HtmlTree;
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.StringContent;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    37
import jdk.javadoc.internal.doclets.toolkit.Content;
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    38
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
    39
import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    40
import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    41
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    42
import static jdk.javadoc.internal.doclets.formats.html.markup.HtmlDocWriter.CONTENT_TYPE;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    43
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    44
/**
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    45
 * Writes an index.html file that tries to redirect to an alternate page.
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    46
 * The redirect uses JavaSCript, if enabled, falling back on
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    47
 * {@code <meta http-eqiv=refresh content="0,<uri>">}.
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    48
 * 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
    49
 * 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
    50
 */
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    51
public class IndexRedirectWriter extends HtmlDocletWriter {
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    52
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    53
    public static void generate(ConfigurationImpl configuration)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    54
            throws DocFileIOException {
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    55
        IndexRedirectWriter indexRedirect;
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    56
        DocPath filename = DocPaths.INDEX;
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    57
            indexRedirect = new IndexRedirectWriter(configuration, filename);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    58
            indexRedirect.generateIndexFile();
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
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    61
    IndexRedirectWriter(ConfigurationImpl configuration, DocPath filename) {
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    62
        super(configuration, filename);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    63
    }
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    64
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    65
    /**
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    66
     * Generate an index file that redirects to an alternate file.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    67
     * @throws DocFileIOException if there is a problem generating the file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    68
     */
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 40500
diff changeset
    69
    void generateIndexFile() throws DocFileIOException {
40500
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    70
        Content htmlDocType = configuration.isOutputHtml5()
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    71
                ? DocType.HTML5
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    72
                : DocType.TRANSITIONAL;
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    73
        Content htmlComment = new Comment(configuration.getText("doclet.New_Page"));
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    74
        Content head = new HtmlTree(HtmlTag.HEAD);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    75
        head.addContent(getGeneratedBy(!configuration.notimestamp));
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    76
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    77
        String title = (configuration.windowtitle.length() > 0)
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    78
                ? configuration.windowtitle
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    79
                : configuration.getText("doclet.Generated_Docs_Untitled");
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    80
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    81
        Content windowTitle = HtmlTree.TITLE(new StringContent(title));
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    82
        head.addContent(windowTitle);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    83
        Content metaContentType = HtmlTree.META("Content", CONTENT_TYPE,
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    84
                (configuration.charset.length() > 0) ?
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    85
                        configuration.charset : HtmlConstants.HTML_DEFAULT_CHARSET);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    86
        head.addContent(metaContentType);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    87
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    88
        String topFilePath = configuration.topFile.getPath();
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    89
        String javaScriptRefresh = "window.location.replace('" + topFilePath + "')";
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    90
        HtmlTree scriptTree = HtmlTree.SCRIPT();
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    91
        scriptTree.addContent(javaScriptRefresh);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    92
        head.addContent(scriptTree);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    93
        HtmlTree metaRefresh = new HtmlTree(HtmlTag.META);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    94
        metaRefresh.addAttr(HtmlAttr.HTTP_EQUIV, "Refresh");
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    95
        metaRefresh.addAttr(HtmlAttr.CONTENT, "0;" + topFilePath);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    96
        if (configuration.isOutputHtml5()) {
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    97
            head.addContent(HtmlTree.NOSCRIPT(metaRefresh));
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    98
        } else {
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
    99
            head.addContent(metaRefresh);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   100
        }
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   101
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   102
        head.addContent(getStyleSheetProperties(configuration));
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(
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   106
                HtmlTree.P(configuration.getContent("doclet.No_Script_Message"))));
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   107
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   108
        bodyContent.addContent(HtmlTree.P(HtmlTree.A(topFilePath, new StringContent(topFilePath))));
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   109
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   110
        Content body = new HtmlTree(HtmlTag.BODY);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   111
        if (configuration.allowTag(HtmlTag.MAIN)) {
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   112
            HtmlTree main = HtmlTree.MAIN(bodyContent);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   113
            body.addContent(main);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   114
        } else {
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   115
            body.addContent(bodyContent);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   116
        }
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   117
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   118
        Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   119
                head, body);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   120
        Content htmlDocument = new HtmlDocument(htmlDocType,
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   121
                htmlComment, htmlTree);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   122
        write(htmlDocument);
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   123
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   124
    }
f293dbb81a53 8162353: javadoc should provide a way to disable use of frames
jjg
parents:
diff changeset
   125
}