1 /*
   2  * Copyright (c) 1998, 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 java.util.*;
  29 
  30 import javax.lang.model.element.PackageElement;
  31 import javax.lang.model.element.TypeElement;
  32 
  33 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
  34 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  35 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
  36 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
  37 import jdk.javadoc.internal.doclets.formats.html.markup.StringContent;
  38 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
  39 import jdk.javadoc.internal.doclets.toolkit.Content;
  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  * Class to generate file for each package contents in the left-hand bottom
  46  * frame. This will list all the Class Kinds in the package. A click on any
  47  * class-kind will update the right-hand frame with the clicked class-kind page.
  48  *
  49  *  <p><b>This is NOT part of any supported API.
  50  *  If you write code that depends on this, you do so at your own risk.
  51  *  This code and its internal interfaces are subject to change or
  52  *  deletion without notice.</b>
  53  *
  54  * @author Atul M Dambalkar
  55  * @author Bhavesh Patel (Modified)
  56  */
  57 public class PackageFrameWriter extends HtmlDocletWriter {
  58 
  59     /**
  60      * The package being documented.
  61      */
  62     private final PackageElement packageElement;
  63 
  64     /**
  65      * The classes to be documented.  Use this to filter out classes
  66      * that will not be documented.
  67      */
  68     private SortedSet<TypeElement> documentedClasses;
  69 
  70     /**
  71      * Constructor to construct PackageFrameWriter object and to generate
  72      * "package-frame.html" file in the respective package directory.
  73      * For example for package "java.lang" this will generate file
  74      * "package-frame.html" file in the "java/lang" directory. It will also
  75      * create "java/lang" directory in the current or the destination directory
  76      * if it doesn't exist.
  77      *
  78      * @param configuration the configuration of the doclet.
  79      * @param packageElement PackageElement under consideration.
  80      */
  81     public PackageFrameWriter(HtmlConfiguration configuration, PackageElement packageElement) {
  82         super(configuration, DocPath.forPackage(packageElement).resolve(DocPaths.PACKAGE_FRAME));
  83         this.packageElement = packageElement;
  84         if (configuration.getSpecifiedPackageElements().isEmpty()) {
  85             documentedClasses = new TreeSet<>(utils.makeGeneralPurposeComparator());
  86             documentedClasses.addAll(configuration.getIncludedTypeElements());
  87         }
  88     }
  89 
  90     /**
  91      * Generate a package summary page for the left-hand bottom frame. Construct
  92      * the PackageFrameWriter object and then uses it generate the file.
  93      *
  94      * @param configuration the current configuration of the doclet.
  95      * @param packageElement The package for which "pacakge-frame.html" is to be generated.
  96      * @throws DocFileIOException if there is a problem generating the package summary page
  97      */
  98     public static void generate(HtmlConfiguration configuration, PackageElement packageElement)
  99             throws DocFileIOException {
 100         PackageFrameWriter packgen = new PackageFrameWriter(configuration, packageElement);
 101         String pkgName = configuration.utils.getPackageName(packageElement);
 102         HtmlTree body = packgen.getBody(false, packgen.getWindowTitle(pkgName));
 103         Content pkgNameContent = new StringContent(pkgName);
 104         HtmlTree htmlTree = (configuration.allowTag(HtmlTag.MAIN))
 105                 ? HtmlTree.MAIN()
 106                 : body;
 107         Content heading = HtmlTree.HEADING(HtmlConstants.TITLE_HEADING, HtmlStyle.bar,
 108                 packgen.getTargetPackageLink(packageElement, "classFrame", pkgNameContent));
 109         htmlTree.addContent(heading);
 110         HtmlTree div = new HtmlTree(HtmlTag.DIV);
 111         div.setStyle(HtmlStyle.indexContainer);
 112         packgen.addClassListing(div);
 113         htmlTree.addContent(div);
 114         if (configuration.allowTag(HtmlTag.MAIN)) {
 115             body.addContent(htmlTree);
 116         }
 117         packgen.printHtmlDocument(
 118                 configuration.metakeywords.getMetaKeywords(packageElement), false, body);
 119     }
 120 
 121     /**
 122      * Add class listing for all the classes in this package. Divide class
 123      * listing as per the class kind and generate separate listing for
 124      * Classes, Interfaces, Exceptions and Errors.
 125      *
 126      * @param contentTree the content tree to which the listing will be added
 127      */
 128     protected void addClassListing(HtmlTree contentTree) {
 129         BaseConfiguration config = configuration;
 130         if (utils.isSpecified(packageElement)) {
 131             addClassKindListing(utils.getInterfaces(packageElement),
 132                 contents.interfaces, contentTree);
 133             addClassKindListing(utils.getOrdinaryClasses(packageElement),
 134                 contents.classes, contentTree);
 135             addClassKindListing(utils.getEnums(packageElement),
 136                 contents.enums, contentTree);
 137             addClassKindListing(utils.getExceptions(packageElement),
 138                 contents.exceptions, contentTree);
 139             addClassKindListing(utils.getErrors(packageElement),
 140                 contents.errors, contentTree);
 141             addClassKindListing(utils.getAnnotationTypes(packageElement),
 142                 contents.annotationTypes, contentTree);
 143         } else {
 144             addClassKindListing(config.typeElementCatalog.interfaces(packageElement),
 145                 contents.interfaces, contentTree);
 146             addClassKindListing(config.typeElementCatalog.ordinaryClasses(packageElement),
 147                 contents.classes, contentTree);
 148             addClassKindListing(config.typeElementCatalog.enums(packageElement),
 149                 contents.enums, contentTree);
 150             addClassKindListing(config.typeElementCatalog.exceptions(packageElement),
 151                 contents.exceptions, contentTree);
 152             addClassKindListing(config.typeElementCatalog.errors(packageElement),
 153                 contents.errors, contentTree);
 154             addClassKindListing(config.typeElementCatalog.annotationTypes(packageElement),
 155                 contents.annotationTypes, contentTree);
 156         }
 157     }
 158 
 159     /**
 160      * Add specific class kind listing. Also add label to the listing.
 161      *
 162      * @param list list of specific class kinds, namely Class or Interface or Exception or Error
 163      * @param labelContent content tree of the label to be added
 164      * @param contentTree the content tree to which the class kind listing will be added
 165      */
 166     protected void addClassKindListing(Iterable<TypeElement> list, Content labelContent,
 167             HtmlTree contentTree) {
 168         SortedSet<TypeElement> tset = utils.filterOutPrivateClasses(list, configuration.javafx);
 169         if(!tset.isEmpty()) {
 170             boolean printedHeader = false;
 171             HtmlTree htmlTree = (configuration.allowTag(HtmlTag.SECTION))
 172                     ? HtmlTree.SECTION()
 173                     : contentTree;
 174             HtmlTree ul = new HtmlTree(HtmlTag.UL);
 175             ul.setTitle(labelContent);
 176             for (TypeElement typeElement : tset) {
 177                 if (documentedClasses != null && !documentedClasses.contains(typeElement)) {
 178                     continue;
 179                 }
 180                 if (!utils.isCoreClass(typeElement) || !configuration.isGeneratedDoc(typeElement)) {
 181                     continue;
 182                 }
 183                 if (!printedHeader) {
 184                     Content heading = HtmlTree.HEADING(HtmlConstants.CONTENT_HEADING,
 185                                                        true, labelContent);
 186                     htmlTree.addContent(heading);
 187                     printedHeader = true;
 188                 }
 189                 Content arr_i_name = new StringContent(utils.getSimpleName(typeElement));
 190                 if (utils.isInterface(typeElement))
 191                     arr_i_name = HtmlTree.SPAN(HtmlStyle.interfaceName, arr_i_name);
 192                 Content link = getLink(new LinkInfoImpl(configuration,
 193                                                         LinkInfoImpl.Kind.PACKAGE_FRAME, typeElement).label(arr_i_name).target("classFrame"));
 194                 Content li = HtmlTree.LI(link);
 195                 ul.addContent(li);
 196             }
 197             htmlTree.addContent(ul);
 198             if (configuration.allowTag(HtmlTag.SECTION)) {
 199                 contentTree.addContent(htmlTree);
 200             }
 201         }
 202     }
 203 }