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