1 /*
2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package jdk.javadoc.internal.doclets.formats.html;
27 
28 import java.util.Collections;
29 
30 import jdk.javadoc.internal.doclets.formats.html.markup.Head;
31 import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
32 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlAttr;
33 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlDocument;
34 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
35 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
36 import jdk.javadoc.internal.doclets.formats.html.markup.Script;
37 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
38 import jdk.javadoc.internal.doclets.toolkit.Content;
39 import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
40 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
41 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
42 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
43 
44 /**
45  * Writes a file that tries to redirect to an alternate page.
46  * The redirect uses JavaScript, if enabled, falling back on
47  * {@code <meta http-eqiv=refresh content="0,<uri>">}.
48  * If neither are supported/enabled in a browser, the page displays the
49  * standard "JavaScipt not enabled" message, and a link to the alternate page.
50  */
51 public class IndexRedirectWriter extends HtmlDocletWriter {
52 
generate(HtmlConfiguration configuration)53     public static void generate(HtmlConfiguration configuration)
54             throws DocFileIOException {
55         generate(configuration, DocPaths.INDEX, configuration.topFile);
56     }
57 
generate(HtmlConfiguration configuration, DocPath fileName, DocPath target)58     public static void generate(HtmlConfiguration configuration, DocPath fileName, DocPath target)
59             throws DocFileIOException {
60         IndexRedirectWriter indexRedirect = new IndexRedirectWriter(configuration, fileName, target);
61         indexRedirect.generateIndexFile();
62     }
63 
64     private DocPath target;
65 
IndexRedirectWriter(HtmlConfiguration configuration, DocPath filename, DocPath target)66     private IndexRedirectWriter(HtmlConfiguration configuration, DocPath filename, DocPath target) {
67         super(configuration, filename);
68         this.target = target;
69     }
70 
71     /**
72      * Generate an index file that redirects to an alternate file.
73      * @throws DocFileIOException if there is a problem generating the file
74      */
generateIndexFile()75     private void generateIndexFile() throws DocFileIOException {
76         Content htmlComment = contents.newPage;
77         Head head = new Head(path, configuration.docletVersion)
78                 .setTimestamp(!configuration.notimestamp)
79                 .setDescription("index redirect")
80                 .setGenerator(getGenerator(getClass()))
81                 .setStylesheets(configuration.getMainStylesheet(), Collections.emptyList()) // avoid reference to default stylesheet
82                 .addDefaultScript(false);
83 
84         String title = (configuration.windowtitle.length() > 0)
85                 ? configuration.windowtitle
86                 : resources.getText("doclet.Generated_Docs_Untitled");
87 
88         head.setTitle(title)
89                 .setCharset(configuration.charset)
90                 .setCanonicalLink(target);
91 
92         String targetPath = target.getPath();
93         Script script = new Script("window.location.replace(")
94                 .appendStringLiteral(targetPath, '\'')
95                 .append(")");
96         HtmlTree metaRefresh = new HtmlTree(HtmlTag.META)
97                 .put(HtmlAttr.HTTP_EQUIV, "Refresh")
98                 .put(HtmlAttr.CONTENT, "0;" + targetPath);
99         head.addContent(script.asContent(), HtmlTree.NOSCRIPT(metaRefresh));
100 
101         ContentBuilder bodyContent = new ContentBuilder();
102         bodyContent.add(HtmlTree.NOSCRIPT(
103                 HtmlTree.P(contents.getContent("doclet.No_Script_Message"))));
104 
105         bodyContent.add(HtmlTree.P(HtmlTree.A(targetPath, new StringContent(targetPath))));
106 
107         Content body = new HtmlTree(HtmlTag.BODY)
108                 .put(HtmlAttr.CLASS, "index-redirect");
109         HtmlTree main = HtmlTree.MAIN(bodyContent);
110         body.add(main);
111 
112         Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(), head.toContent(), body);
113         HtmlDocument htmlDocument = new HtmlDocument(htmlComment, htmlTree);
114         htmlDocument.write(DocFile.createFileForOutput(configuration, path));
115     }
116 }
117