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