src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/formats/html/HtmlDocletWriter.java
changeset 53863 d001808c57e8
parent 53562 0d9dee001667
child 53866 880baf6db17b
equal deleted inserted replaced
53862:6c13f5a67766 53863:d001808c57e8
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    39 import javax.lang.model.element.ElementKind;
    39 import javax.lang.model.element.ElementKind;
    40 import javax.lang.model.element.ExecutableElement;
    40 import javax.lang.model.element.ExecutableElement;
    41 import javax.lang.model.element.ModuleElement;
    41 import javax.lang.model.element.ModuleElement;
    42 import javax.lang.model.element.Name;
    42 import javax.lang.model.element.Name;
    43 import javax.lang.model.element.PackageElement;
    43 import javax.lang.model.element.PackageElement;
       
    44 import javax.lang.model.element.QualifiedNameable;
    44 import javax.lang.model.element.TypeElement;
    45 import javax.lang.model.element.TypeElement;
    45 import javax.lang.model.element.VariableElement;
    46 import javax.lang.model.element.VariableElement;
    46 import javax.lang.model.type.DeclaredType;
    47 import javax.lang.model.type.DeclaredType;
    47 import javax.lang.model.type.TypeMirror;
    48 import javax.lang.model.type.TypeMirror;
    48 import javax.lang.model.util.SimpleAnnotationValueVisitor9;
    49 import javax.lang.model.util.SimpleAnnotationValueVisitor9;
   419      * Generates the HTML document tree and prints it out.
   420      * Generates the HTML document tree and prints it out.
   420      *
   421      *
   421      * @param metakeywords Array of String keywords for META tag. Each element
   422      * @param metakeywords Array of String keywords for META tag. Each element
   422      *                     of the array is assigned to a separate META tag.
   423      *                     of the array is assigned to a separate META tag.
   423      *                     Pass in null for no array
   424      *                     Pass in null for no array
       
   425      * @param description the content for the description META tag.
   424      * @param includeScript true if printing windowtitle script
   426      * @param includeScript true if printing windowtitle script
   425      *                      false for files that appear in the left-hand frames
   427      *                      false for files that appear in the left-hand frames
   426      * @param body the body htmltree to be included in the document
   428      * @param body the body htmltree to be included in the document
   427      * @throws DocFileIOException if there is a problem writing the file
   429      * @throws DocFileIOException if there is a problem writing the file
   428      */
   430      */
   429     public void printHtmlDocument(List<String> metakeywords, boolean includeScript,
   431     public void printHtmlDocument(List<String> metakeywords,
   430                                   Content body) throws DocFileIOException {
   432                                   String description,
   431         printHtmlDocument(metakeywords, includeScript, new ContentBuilder(), body);
   433                                   boolean includeScript,
       
   434                                   Content body)
       
   435             throws DocFileIOException {
       
   436         printHtmlDocument(metakeywords, description, includeScript, new ContentBuilder(), body);
   432     }
   437     }
   433 
   438 
   434     /**
   439     /**
   435      * Generates the HTML document tree and prints it out.
   440      * Generates the HTML document tree and prints it out.
   436      *
   441      *
   437      * @param metakeywords Array of String keywords for META tag. Each element
   442      * @param metakeywords Array of String keywords for META tag. Each element
   438      *                     of the array is assigned to a separate META tag.
   443      *                     of the array is assigned to a separate META tag.
   439      *                     Pass in null for no array
   444      *                     Pass in null for no array
       
   445      * @param description the content for the description META tag.
   440      * @param includeScript true if printing windowtitle script
   446      * @param includeScript true if printing windowtitle script
   441      *                      false for files that appear in the left-hand frames
   447      *                      false for files that appear in the left-hand frames
   442      * @param extraContent any additional content to be included in the HEAD element
   448      * @param extraHeadContent any additional content to be included in the HEAD element
   443      * @param body the body htmltree to be included in the document
   449      * @param body the body htmltree to be included in the document
   444      * @throws DocFileIOException if there is a problem writing the file
   450      * @throws DocFileIOException if there is a problem writing the file
   445      */
   451      */
   446     public void printHtmlDocument(List<String> metakeywords, boolean includeScript, Content extraContent,
   452     public void printHtmlDocument(List<String> metakeywords,
   447                                   Content body) throws DocFileIOException {
   453                                   String description,
       
   454                                   boolean includeScript,
       
   455                                   Content extraHeadContent,
       
   456                                   Content body)
       
   457             throws DocFileIOException {
   448         Content htmlComment = contents.newPage;
   458         Content htmlComment = contents.newPage;
   449         Head head = new Head(path, configuration.docletVersion)
   459         Head head = new Head(path, configuration.docletVersion)
   450                 .setTimestamp(!configuration.notimestamp)
   460                 .setTimestamp(!configuration.notimestamp)
       
   461                 .setDescription(description)
       
   462                 .setGenerator(getGenerator(getClass()))
   451                 .setTitle(winTitle)
   463                 .setTitle(winTitle)
   452                 .setCharset(configuration.charset)
   464                 .setCharset(configuration.charset)
   453                 .addKeywords(metakeywords)
   465                 .addKeywords(metakeywords)
   454                 .setStylesheets(configuration.getMainStylesheet(), configuration.getAdditionalStylesheets())
   466                 .setStylesheets(configuration.getMainStylesheet(), configuration.getAdditionalStylesheets())
   455                 .setUseModuleDirectories(configuration.useModuleDirectories)
   467                 .setUseModuleDirectories(configuration.useModuleDirectories)
   456                 .setIndex(configuration.createindex, mainBodyScript)
   468                 .setIndex(configuration.createindex, mainBodyScript)
   457                 .addContent(extraContent);
   469                 .addContent(extraHeadContent);
   458 
   470 
   459         Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(), head.toContent(), body);
   471         Content htmlTree = HtmlTree.HTML(configuration.getLocale().getLanguage(), head.toContent(), body);
   460         HtmlDocument htmlDocument = new HtmlDocument(htmlComment, htmlTree);
   472         HtmlDocument htmlDocument = new HtmlDocument(htmlComment, htmlTree);
   461         htmlDocument.write(DocFile.createFileForOutput(configuration, path));
   473         htmlDocument.write(DocFile.createFileForOutput(configuration, path));
   462     }
   474     }
  2109     protected TableHeader getPackageTableHeader() {
  2121     protected TableHeader getPackageTableHeader() {
  2110         return new TableHeader(contents.packageLabel, contents.descriptionLabel);
  2122         return new TableHeader(contents.packageLabel, contents.descriptionLabel);
  2111     }
  2123     }
  2112 
  2124 
  2113     /**
  2125     /**
       
  2126      * Generates a string for use in a description meta element,
       
  2127      * based on an element and its enclosing elements
       
  2128      * @param prefix a prefix for the string
       
  2129      * @param elem the element
       
  2130      * @return the description
       
  2131      */
       
  2132     static String getDescription(String prefix, Element elem) {
       
  2133         LinkedList<Element> chain = new LinkedList<>();
       
  2134         for (Element e = elem; e != null; e = e.getEnclosingElement()) {
       
  2135             // ignore unnamed enclosing elements
       
  2136             if (e.getSimpleName().length() == 0 && e != elem) {
       
  2137                 break;
       
  2138             }
       
  2139             chain.addFirst(e);
       
  2140         }
       
  2141         StringBuilder sb = new StringBuilder();
       
  2142         for (Element e: chain) {
       
  2143             CharSequence name;
       
  2144             switch (e.getKind()) {
       
  2145                 case MODULE:
       
  2146                 case PACKAGE:
       
  2147                     name = ((QualifiedNameable) e).getQualifiedName();
       
  2148                     if (name.length() == 0) {
       
  2149                         name = "<unnamed>";
       
  2150                     }
       
  2151                     break;
       
  2152 
       
  2153                 default:
       
  2154                     name = e.getSimpleName();
       
  2155                     break;
       
  2156             }
       
  2157 
       
  2158             if (sb.length() == 0) {
       
  2159                 sb.append(prefix).append(": ");
       
  2160             } else {
       
  2161                 sb.append(", ");
       
  2162             }
       
  2163             sb.append(e.getKind().toString().toLowerCase(Locale.US).replace("_", " "))
       
  2164                     .append(": ")
       
  2165                     .append(name);
       
  2166         }
       
  2167         return sb.toString();
       
  2168     }
       
  2169 
       
  2170     static String getGenerator(Class<?> clazz) {
       
  2171         return "javadoc/" + clazz.getSimpleName();
       
  2172     }
       
  2173 
       
  2174     /**
  2114      * Returns an HtmlTree for the SCRIPT tag.
  2175      * Returns an HtmlTree for the SCRIPT tag.
  2115      *
  2176      *
  2116      * @return an HtmlTree for the SCRIPT tag
  2177      * @return an HtmlTree for the SCRIPT tag
  2117      */
  2178      */
  2118     protected Script getWinTitleScript() {
  2179     protected Script getWinTitleScript() {