1 /*
   2  * Copyright (c) 1998, 2016, 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.io.*;
  29 
  30 import javax.lang.model.element.PackageElement;
  31 
  32 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
  33 import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
  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.toolkit.Content;
  37 import jdk.javadoc.internal.doclets.toolkit.util.ClassTree;
  38 import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
  39 import jdk.javadoc.internal.doclets.toolkit.util.DocPaths;
  40 import jdk.javadoc.internal.doclets.toolkit.util.DocletAbortException;
  41 
  42 
  43 /**
  44  * Class to generate Tree page for a package. The name of the file generated is
  45  * "package-tree.html" and it is generated in the respective package directory.
  46  *
  47  *  <p><b>This is NOT part of any supported API.
  48  *  If you write code that depends on this, you do so at your own risk.
  49  *  This code and its internal interfaces are subject to change or
  50  *  deletion without notice.</b>
  51  *
  52  * @author Atul M Dambalkar
  53  * @author Bhavesh Patel (Modified)
  54  */
  55 public class PackageTreeWriter extends AbstractTreeWriter {
  56 
  57     /**
  58      * Package for which tree is to be generated.
  59      */
  60     protected PackageElement packageElement;
  61 
  62     /**
  63      * The previous package name in the alpha-order list.
  64      */
  65     protected PackageElement prev;
  66 
  67     /**
  68      * The next package name in the alpha-order list.
  69      */
  70     protected PackageElement next;
  71 
  72     /**
  73      * Constructor.
  74      * @param configuration the configuration
  75      * @param path the docpath to generate files into
  76      * @param packageElement the current package
  77      * @param prev the previous package
  78      * @param next the next package
  79      * @throws IOException
  80      * @throws DocletAbortException
  81      */
  82     public PackageTreeWriter(ConfigurationImpl configuration,
  83                              DocPath path,
  84                              PackageElement packageElement,
  85                              PackageElement prev, PackageElement next)
  86                       throws IOException {
  87         super(configuration, path,
  88               new ClassTree(configuration.classDocCatalog.allClasses(packageElement), configuration));
  89         this.packageElement = packageElement;
  90         this.prev = prev;
  91         this.next = next;
  92     }
  93 
  94     /**
  95      * Construct a PackageTreeWriter object and then use it to generate the
  96      * package tree page.
  97      *
  98      * @param configuration the configuration for this run.
  99      * @param pkg      Package for which tree file is to be generated.
 100      * @param prev     Previous package in the alpha-ordered list.
 101      * @param next     Next package in the alpha-ordered list.
 102      * @param noDeprecated  If true, do not generate any information for
 103      * deprecated classe or interfaces.
 104      * @throws DocletAbortException
 105      */
 106     public static void generate(ConfigurationImpl configuration,
 107                                 PackageElement pkg, PackageElement prev,
 108                                 PackageElement next, boolean noDeprecated) {
 109         PackageTreeWriter packgen;
 110         DocPath path = DocPath.forPackage(pkg).resolve(DocPaths.PACKAGE_TREE);
 111         try {
 112             packgen = new PackageTreeWriter(configuration, path, pkg,
 113                 prev, next);
 114             packgen.generatePackageTreeFile();
 115             packgen.close();
 116         } catch (IOException exc) {
 117             configuration.standardmessage.error(
 118                         "doclet.exception_encountered",
 119                         exc.toString(), path.getPath());
 120             throw new DocletAbortException(exc);
 121         }
 122     }
 123 
 124     /**
 125      * Generate a separate tree file for each package.
 126      * @throws java.io.IOException
 127      */
 128     protected void generatePackageTreeFile() throws IOException {
 129         HtmlTree body = getPackageTreeHeader();
 130         HtmlTree htmlTree = (configuration.allowTag(HtmlTag.MAIN))
 131                 ? HtmlTree.MAIN()
 132                 : body;
 133         Content headContent = getResource("doclet.Hierarchy_For_Package",
 134                 utils.getPackageName(packageElement));
 135         Content heading = HtmlTree.HEADING(HtmlConstants.TITLE_HEADING, false,
 136                 HtmlStyle.title, headContent);
 137         Content div = HtmlTree.DIV(HtmlStyle.header, heading);
 138         if (configuration.packages.size() > 1) {
 139             addLinkToMainTree(div);
 140         }
 141         htmlTree.addContent(div);
 142         HtmlTree divTree = new HtmlTree(HtmlTag.DIV);
 143         divTree.addStyle(HtmlStyle.contentContainer);
 144         addTree(classtree.baseClasses(), "doclet.Class_Hierarchy", divTree);
 145         addTree(classtree.baseInterfaces(), "doclet.Interface_Hierarchy", divTree);
 146         addTree(classtree.baseAnnotationTypes(), "doclet.Annotation_Type_Hierarchy", divTree);
 147         addTree(classtree.baseEnums(), "doclet.Enum_Hierarchy", divTree, true);
 148         htmlTree.addContent(divTree);
 149         if (configuration.allowTag(HtmlTag.MAIN)) {
 150             body.addContent(htmlTree);
 151         }
 152         HtmlTree tree = (configuration.allowTag(HtmlTag.FOOTER))
 153                 ? HtmlTree.FOOTER()
 154                 : body;
 155         addNavLinks(false, tree);
 156         addBottom(tree);
 157         if (configuration.allowTag(HtmlTag.FOOTER)) {
 158             body.addContent(tree);
 159         }
 160         printHtmlDocument(null, true, body);
 161     }
 162 
 163     /**
 164      * Get the package tree header.
 165      *
 166      * @return a content tree for the header
 167      */
 168     protected HtmlTree getPackageTreeHeader() {
 169         String packageName = packageElement.isUnnamed() ? "" : utils.getPackageName(packageElement);
 170         String title = packageName + " " + configuration.getText("doclet.Window_Class_Hierarchy");
 171         HtmlTree bodyTree = getBody(true, getWindowTitle(title));
 172         HtmlTree htmlTree = (configuration.allowTag(HtmlTag.HEADER))
 173                 ? HtmlTree.HEADER()
 174                 : bodyTree;
 175         addTop(htmlTree);
 176         addNavLinks(true, htmlTree);
 177         if (configuration.allowTag(HtmlTag.HEADER)) {
 178             bodyTree.addContent(htmlTree);
 179         }
 180         return bodyTree;
 181     }
 182 
 183     /**
 184      * Add a link to the tree for all the packages.
 185      *
 186      * @param div the content tree to which the link will be added
 187      */
 188     protected void addLinkToMainTree(Content div) {
 189         Content span = HtmlTree.SPAN(HtmlStyle.packageHierarchyLabel,
 190                 getResource("doclet.Package_Hierarchies"));
 191         div.addContent(span);
 192         HtmlTree ul = new HtmlTree (HtmlTag.UL);
 193         ul.addStyle(HtmlStyle.horizontal);
 194         ul.addContent(getNavLinkMainTree(configuration.getText("doclet.All_Packages")));
 195         div.addContent(ul);
 196     }
 197 
 198     /**
 199      * Get link for the previous package tree file.
 200      *
 201      * @return a content tree for the link
 202      */
 203     protected Content getNavLinkPrevious() {
 204         if (prev == null) {
 205             return getNavLinkPrevious(null);
 206         } else {
 207             DocPath path = DocPath.relativePath(packageElement, prev);
 208             return getNavLinkPrevious(path.resolve(DocPaths.PACKAGE_TREE));
 209         }
 210     }
 211 
 212     /**
 213      * Get link for the next package tree file.
 214      *
 215      * @return a content tree for the link
 216      */
 217     protected Content getNavLinkNext() {
 218         if (next == null) {
 219             return getNavLinkNext(null);
 220         } else {
 221             DocPath path = DocPath.relativePath(packageElement, next);
 222             return getNavLinkNext(path.resolve(DocPaths.PACKAGE_TREE));
 223         }
 224     }
 225 
 226     /**
 227      * Get link to the package summary page for the package of this tree.
 228      *
 229      * @return a content tree for the package link
 230      */
 231     protected Content getNavLinkPackage() {
 232         Content linkContent = getHyperLink(DocPaths.PACKAGE_SUMMARY,
 233                 packageLabel);
 234         Content li = HtmlTree.LI(linkContent);
 235         return li;
 236     }
 237 }