1 /*
   2  * Copyright (c) 1997, 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.*;
  29 
  30 import javax.lang.model.element.ModuleElement;
  31 import javax.lang.model.element.PackageElement;
  32 import javax.lang.model.element.TypeElement;
  33 
  34 import jdk.javadoc.doclet.Doclet;
  35 import jdk.javadoc.doclet.DocletEnvironment;
  36 import jdk.javadoc.doclet.Reporter;
  37 import jdk.javadoc.internal.doclets.toolkit.AbstractDoclet;
  38 import jdk.javadoc.internal.doclets.toolkit.DocletException;
  39 import jdk.javadoc.internal.doclets.toolkit.Messages;
  40 import jdk.javadoc.internal.doclets.toolkit.builders.AbstractBuilder;
  41 import jdk.javadoc.internal.doclets.toolkit.util.ClassTree;
  42 import jdk.javadoc.internal.doclets.toolkit.util.DocFile;
  43 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
  44 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
  45 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
  46 import jdk.javadoc.internal.doclets.toolkit.util.IndexBuilder;
  47 
  48 /**
  49  * The class with "start" method, calls individual Writers.
  50  *
  51  *  <p><b>This is NOT part of any supported API.
  52  *  If you write code that depends on this, you do so at your own risk.
  53  *  This code and its internal interfaces are subject to change or
  54  *  deletion without notice.</b>
  55  */
  56 public class HtmlDoclet extends AbstractDoclet {
  57 
  58     public HtmlDoclet(Doclet parent) {
  59         configuration = new HtmlConfiguration(parent);
  60     }
  61 
  62     @Override // defined by Doclet
  63     public String getName() {
  64         return "Html";
  65     }
  66 
  67     /**
  68      * The global configuration information for this run.
  69      */
  70     private final HtmlConfiguration configuration;
  71 
  72     private Messages messages;
  73 
  74 
  75     private static final DocPath DOCLET_RESOURCES = DocPath
  76             .create("/jdk/javadoc/internal/doclets/formats/html/resources");
  77 
  78     @Override // defined by Doclet
  79     public void init(Locale locale, Reporter reporter) {
  80         configuration.reporter = reporter;
  81         configuration.locale = locale;
  82         messages = configuration.getMessages();
  83     }
  84 
  85     /**
  86      * Create the configuration instance.
  87      * Override this method to use a different
  88      * configuration.
  89      *
  90      * @return the configuration
  91      */
  92     @Override // defined by AbstractDoclet
  93     public HtmlConfiguration getConfiguration() {
  94         return configuration;
  95     }
  96 
  97     /**
  98      * Start the generation of files. Call generate methods in the individual
  99      * writers, which will in turn generate the documentation files. Call the
 100      * TreeWriter generation first to ensure the Class Hierarchy is built
 101      * first and then can be used in the later generation.
 102      *
 103      * For new format.
 104      *
 105      * @throws DocletException if there is a problem while writing the other files
 106      */
 107     @Override // defined by AbstractDoclet
 108     protected void generateOtherFiles(DocletEnvironment docEnv, ClassTree classtree)
 109             throws DocletException {
 110         super.generateOtherFiles(docEnv, classtree);
 111         if (configuration.linksource) {
 112             SourceToHTMLConverter.convertRoot(configuration,
 113                 docEnv, DocPaths.SOURCE_OUTPUT);
 114         }
 115         // Modules with no documented classes may be specified on the
 116         // command line to specify a service provider, allow these.
 117         if (configuration.getSpecifiedModuleElements().isEmpty() &&
 118                 configuration.topFile.isEmpty()) {
 119             messages.error("doclet.No_Non_Deprecated_Classes_To_Document");
 120             return;
 121         }
 122         boolean nodeprecated = configuration.nodeprecated;
 123         performCopy(configuration.helpfile);
 124         performCopy(configuration.stylesheetfile);
 125         for (String stylesheet : configuration.additionalStylesheets) {
 126             performCopy(stylesheet);
 127         }
 128         // do early to reduce memory footprint
 129         if (configuration.classuse) {
 130             ClassUseWriter.generate(configuration, classtree);
 131         }
 132         IndexBuilder indexbuilder = new IndexBuilder(configuration, nodeprecated);
 133 
 134         if (configuration.createtree) {
 135             TreeWriter.generate(configuration, classtree);
 136         }
 137 
 138         if (!(configuration.nodeprecatedlist || nodeprecated)) {
 139             DeprecatedListWriter.generate(configuration);
 140         }
 141 
 142         if (configuration.createoverview) {
 143             if (configuration.showModules) {
 144                 ModuleIndexWriter.generate(configuration);
 145             } else {
 146                 PackageIndexWriter.generate(configuration);
 147             }
 148         }
 149 
 150         if (configuration.createindex) {
 151             configuration.buildSearchTagIndex();
 152             if (configuration.splitindex) {
 153                 SplitIndexWriter.generate(configuration, indexbuilder);
 154             } else {
 155                 SingleIndexWriter.generate(configuration, indexbuilder);
 156             }
 157             AllClassesIndexWriter.generate(configuration,
 158                     new IndexBuilder(configuration, nodeprecated, true));
 159             if (!configuration.packages.isEmpty()) {
 160                 AllPackagesIndexWriter.generate(configuration);
 161             }
 162             SystemPropertiesWriter.generate(configuration);
 163         }
 164 
 165         if (configuration.createoverview) {
 166             IndexRedirectWriter.generate(configuration, DocPaths.OVERVIEW_SUMMARY, DocPaths.INDEX);
 167         } else {
 168             IndexRedirectWriter.generate(configuration);
 169         }
 170 
 171         if (configuration.helpfile.isEmpty() && !configuration.nohelp) {
 172             HelpWriter.generate(configuration);
 173         }
 174         // If a stylesheet file is not specified, copy the default stylesheet
 175         // and replace newline with platform-specific newline.
 176         DocFile f;
 177         if (configuration.stylesheetfile.length() == 0) {
 178             f = DocFile.createFileForOutput(configuration, DocPaths.STYLESHEET);
 179             f.copyResource(DocPaths.RESOURCES.resolve(DocPaths.STYLESHEET), true, true);
 180         }
 181         f = DocFile.createFileForOutput(configuration, DocPaths.JAVASCRIPT);
 182         f.copyResource(DocPaths.RESOURCES.resolve(DocPaths.JAVASCRIPT), true, true);
 183         if (configuration.createindex) {
 184             f = DocFile.createFileForOutput(configuration, DocPaths.SEARCH_JS);
 185             f.copyResource(DOCLET_RESOURCES.resolve(DocPaths.SEARCH_JS), true, true);
 186 
 187             f = DocFile.createFileForOutput(configuration, DocPaths.RESOURCES.resolve(DocPaths.GLASS_IMG));
 188             f.copyResource(DOCLET_RESOURCES.resolve(DocPaths.GLASS_IMG), true, false);
 189 
 190             f = DocFile.createFileForOutput(configuration, DocPaths.RESOURCES.resolve(DocPaths.X_IMG));
 191             f.copyResource(DOCLET_RESOURCES.resolve(DocPaths.X_IMG), true, false);
 192             copyJqueryFiles();
 193         }
 194     }
 195 
 196     private void copyJqueryFiles() throws DocletException {
 197         List<String> files = Arrays.asList(
 198                 "jquery-3.4.1.js",
 199                 "jquery-ui.js",
 200                 "jquery-ui.css",
 201                 "jquery-ui.min.js",
 202                 "jquery-ui.min.css",
 203                 "jquery-ui.structure.min.css",
 204                 "jquery-ui.structure.css",
 205                 "external/jquery/jquery.js",
 206                 "jszip/dist/jszip.js",
 207                 "jszip/dist/jszip.min.js",
 208                 "jszip-utils/dist/jszip-utils.js",
 209                 "jszip-utils/dist/jszip-utils.min.js",
 210                 "jszip-utils/dist/jszip-utils-ie.js",
 211                 "jszip-utils/dist/jszip-utils-ie.min.js",
 212                 "images/ui-bg_glass_65_dadada_1x400.png",
 213                 "images/ui-icons_454545_256x240.png",
 214                 "images/ui-bg_glass_95_fef1ec_1x400.png",
 215                 "images/ui-bg_glass_75_dadada_1x400.png",
 216                 "images/ui-bg_highlight-soft_75_cccccc_1x100.png",
 217                 "images/ui-icons_888888_256x240.png",
 218                 "images/ui-icons_2e83ff_256x240.png",
 219                 "images/ui-icons_cd0a0a_256x240.png",
 220                 "images/ui-bg_glass_55_fbf9ee_1x400.png",
 221                 "images/ui-icons_222222_256x240.png",
 222                 "images/ui-bg_glass_75_e6e6e6_1x400.png");
 223         DocFile f;
 224         for (String file : files) {
 225             DocPath filePath = DocPaths.JQUERY_FILES.resolve(file);
 226             f = DocFile.createFileForOutput(configuration, filePath);
 227             f.copyResource(DOCLET_RESOURCES.resolve(filePath), true, false);
 228         }
 229     }
 230 
 231     /**
 232      * {@inheritDoc}
 233      */
 234     @Override // defined by AbstractDoclet
 235     protected void generateClassFiles(SortedSet<TypeElement> typeElems, ClassTree classTree)
 236             throws DocletException {
 237         for (TypeElement te : typeElems) {
 238             if (utils.hasHiddenTag(te) ||
 239                     !(configuration.isGeneratedDoc(te) && utils.isIncluded(te))) {
 240                 continue;
 241             }
 242             if (utils.isAnnotationType(te)) {
 243                 AbstractBuilder annotationTypeBuilder =
 244                     configuration.getBuilderFactory()
 245                         .getAnnotationTypeBuilder(te);
 246                 annotationTypeBuilder.build();
 247             } else {
 248                 AbstractBuilder classBuilder =
 249                     configuration.getBuilderFactory().getClassBuilder(te, classTree);
 250                 classBuilder.build();
 251             }
 252         }
 253     }
 254 
 255     /**
 256      * {@inheritDoc}
 257      */
 258     @Override // defined by AbstractDoclet
 259     protected void generateModuleFiles() throws DocletException {
 260         if (configuration.showModules) {
 261             List<ModuleElement> mdles = new ArrayList<>(configuration.modulePackages.keySet());
 262             for (ModuleElement mdle : mdles) {
 263                 AbstractBuilder moduleSummaryBuilder =
 264                         configuration.getBuilderFactory().getModuleSummaryBuilder(mdle);
 265                 moduleSummaryBuilder.build();
 266             }
 267         }
 268     }
 269 
 270     /**
 271      * {@inheritDoc}
 272      */
 273     @Override // defined by AbstractDoclet
 274     protected void generatePackageFiles(ClassTree classtree) throws DocletException {
 275         Set<PackageElement> packages = configuration.packages;
 276         List<PackageElement> pList = new ArrayList<>(packages);
 277         for (PackageElement pkg : pList) {
 278             // if -nodeprecated option is set and the package is marked as
 279             // deprecated, do not generate the package-summary.html, package-frame.html
 280             // and package-tree.html pages for that package.
 281             if (!(configuration.nodeprecated && utils.isDeprecated(pkg))) {
 282                 AbstractBuilder packageSummaryBuilder =
 283                         configuration.getBuilderFactory().getPackageSummaryBuilder(pkg);
 284                 packageSummaryBuilder.build();
 285                 if (configuration.createtree) {
 286                     PackageTreeWriter.generate(configuration, pkg, configuration.nodeprecated);
 287                 }
 288             }
 289         }
 290     }
 291 
 292     @Override // defined by Doclet
 293     public Set<Option> getSupportedOptions() {
 294         return configuration.getSupportedOptions();
 295     }
 296 
 297     private void performCopy(String filename) throws DocFileIOException {
 298         if (filename.isEmpty())
 299             return;
 300 
 301         DocFile fromfile = DocFile.createFileForInput(configuration, filename);
 302         DocPath path = DocPath.create(fromfile.getName());
 303         DocFile toFile = DocFile.createFileForOutput(configuration, path);
 304         if (toFile.isSameFile(fromfile))
 305             return;
 306 
 307         messages.notice("doclet.Copying_File_0_To_File_1",
 308                 fromfile.toString(), path.getPath());
 309         toFile.copyFile(fromfile);
 310     }
 311 }