1 /*
   2  * Copyright (c) 2016, 2017, 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 jdk.javadoc.internal.doclets.formats.html.markup.Comment;
  29 import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder;
  30 import jdk.javadoc.internal.doclets.formats.html.markup.DocType;
  31 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlAttr;
  32 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlDocument;
  33 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
  34 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  35 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
  36 import jdk.javadoc.internal.doclets.toolkit.Content;
  37 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
  38 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
  39 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
  40 
  41 /**
  42  * Writes an index.html file that tries to redirect to an alternate page.
  43  * The redirect uses JavaSCript, if enabled, falling back on
  44  * {@code <meta http-eqiv=refresh content="0,<uri>">}.
  45  * If neither are supported/enabled in a browser, the page displays the
  46  * standard "JavaScipt not enabled" message, and a link to the alternate page.
  47  */
  48 public class IndexRedirectWriter extends HtmlDocletWriter {
  49 
  50     public static void generate(HtmlConfiguration configuration)
  51             throws DocFileIOException {
  52         IndexRedirectWriter indexRedirect;
  53         DocPath filename = DocPaths.INDEX;
  54             indexRedirect = new IndexRedirectWriter(configuration, filename);
  55             indexRedirect.generateIndexFile();
  56     }
  57 
  58     IndexRedirectWriter(HtmlConfiguration configuration, DocPath filename) {
  59         super(configuration, filename);
  60     }
  61 
  62     /**
  63      * Generate an index file that redirects to an alternate file.
  64      * @throws DocFileIOException if there is a problem generating the file
  65      */
  66     void generateIndexFile() throws DocFileIOException {
  67         Content htmlDocType = configuration.isOutputHtml5()
  68                 ? DocType.HTML5
  69                 : DocType.TRANSITIONAL;
  70         Content htmlComment = new Comment(configuration.getText("doclet.New_Page"));
  71         Content head = new HtmlTree(HtmlTag.HEAD);
  72         head.addContent(getGeneratedBy(!configuration.notimestamp));
  73 
  74         String title = (configuration.windowtitle.length() > 0)
  75                 ? configuration.windowtitle
  76                 : configuration.getText("doclet.Generated_Docs_Untitled");
  77 
  78         Content windowTitle = HtmlTree.TITLE(new StringContent(title));
  79         head.addContent(windowTitle);
  80         Content metaContentType = HtmlTree.META("Content", CONTENT_TYPE, configuration.charset);
  81         head.addContent(metaContentType);
  82 
  83         String topFilePath = configuration.topFile.getPath();
  84         String javaScriptRefresh = "window.location.replace('" + topFilePath + "')";
  85         HtmlTree scriptTree = HtmlTree.SCRIPT();
  86         scriptTree.addContent(javaScriptRefresh);
  87         head.addContent(scriptTree);
  88         HtmlTree metaRefresh = new HtmlTree(HtmlTag.META);
  89         metaRefresh.addAttr(HtmlAttr.HTTP_EQUIV, "Refresh");
  90         metaRefresh.addAttr(HtmlAttr.CONTENT, "0;" + topFilePath);
  91         if (configuration.isOutputHtml5()) {
  92             head.addContent(HtmlTree.NOSCRIPT(metaRefresh));
  93         } else {
  94             head.addContent(metaRefresh);
  95         }
  96 
  97         head.addContent(getStyleSheetProperties(configuration));
  98 
  99         ContentBuilder bodyContent = new ContentBuilder();
 100         bodyContent.addContent(HtmlTree.NOSCRIPT(
 101                 HtmlTree.P(configuration.getContent("doclet.No_Script_Message"))));
 102 
 103         bodyContent.addContent(HtmlTree.P(HtmlTree.A(topFilePath, new StringContent(topFilePath))));
 104 
 105         Content body = new HtmlTree(HtmlTag.BODY);
 106         if (configuration.allowTag(HtmlTag.MAIN)) {
 107             HtmlTree main = HtmlTree.MAIN(bodyContent);
 108             body.addContent(main);
 109         } else {
 110             body.addContent(bodyContent);
 111         }
 112 
 113         Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(),
 114                 head, body);
 115         Content htmlDocument = new HtmlDocument(htmlDocType,
 116                 htmlComment, htmlTree);
 117         write(htmlDocument);
 118 
 119     }
 120 }