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
26package jdk.javadoc.internal.doclets.formats.html;
27
28import java.util.*;
29
30import javax.lang.model.element.TypeElement;
31
32import jdk.javadoc.internal.doclets.formats.html.markup.HtmlConstants;
33import jdk.javadoc.internal.doclets.formats.html.markup.HtmlStyle;
34import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTag;
35import jdk.javadoc.internal.doclets.formats.html.markup.HtmlTree;
36import jdk.javadoc.internal.doclets.toolkit.Content;
37import jdk.javadoc.internal.doclets.toolkit.util.ClassTree;
38import jdk.javadoc.internal.doclets.toolkit.util.DocPath;
39
40
41/**
42 * Abstract class to print the class hierarchy page for all the Classes. This
43 * is sub-classed by {@link PackageTreeWriter} and {@link TreeWriter} to
44 * generate the Package Tree and global Tree(for all the classes and packages)
45 * pages.
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 */
54public abstract class AbstractTreeWriter extends HtmlDocletWriter {
55
56    /**
57     * The class and interface tree built by using {@link ClassTree}
58     */
59    protected final ClassTree classtree;
60
61    /**
62     * Constructor initializes classtree variable. This constructor will be used
63     * while generating global tree file "overview-tree.html".
64     *
65     * @param configuration  The current configuration
66     * @param filename   File to be generated.
67     * @param classtree  Tree built by {@link ClassTree}.
68     */
69    protected AbstractTreeWriter(ConfigurationImpl configuration,
70                                 DocPath filename, ClassTree classtree) {
71        super(configuration, filename);
72        this.classtree = classtree;
73    }
74
75    /**
76     * Add each level of the class tree. For each sub-class or
77     * sub-interface indents the next level information.
78     * Recurses itself to add sub-classes info.
79     *
80     * @param parent the superclass or superinterface of the sset
81     * @param collection  a collection of the sub-classes at this level
82     * @param isEnum true if we are generating a tree for enums
83     * @param contentTree the content tree to which the level information will be added
84     */
85    protected void addLevelInfo(TypeElement parent, Collection<TypeElement> collection,
86            boolean isEnum, Content contentTree) {
87        if (!collection.isEmpty()) {
88            Content ul = new HtmlTree(HtmlTag.UL);
89            for (TypeElement local : collection) {
90                HtmlTree li = new HtmlTree(HtmlTag.LI);
91                li.addStyle(HtmlStyle.circle);
92                addPartialInfo(local, li);
93                addExtendsImplements(parent, local, li);
94                addLevelInfo(local, classtree.directSubClasses(local, isEnum),
95                             isEnum, li);   // Recurse
96                ul.addContent(li);
97            }
98            contentTree.addContent(ul);
99        }
100    }
101
102    /**
103     * Add the heading for the tree depending upon tree type if it's a
104     * Class Tree or Interface tree.
105     *
106     * @param sset classes which are at the most base level, all the
107     * other classes in this run will derive from these classes
108     * @param heading heading for the tree
109     * @param div the content tree to which the tree will be added
110     */
111    protected void addTree(SortedSet<TypeElement> sset, String heading, HtmlTree div) {
112        addTree(sset, heading, div, false);
113    }
114
115    protected void addTree(SortedSet<TypeElement> sset, String heading,
116                           HtmlTree div, boolean isEnums) {
117        if (!sset.isEmpty()) {
118            TypeElement firstTypeElement = sset.first();
119            Content headingContent = contents.getContent(heading);
120            Content sectionHeading = HtmlTree.HEADING(HtmlConstants.CONTENT_HEADING, true,
121                    headingContent);
122            HtmlTree htmlTree;
123            if (configuration.allowTag(HtmlTag.SECTION)) {
124                htmlTree = HtmlTree.SECTION(sectionHeading);
125            } else {
126                div.addContent(sectionHeading);
127                htmlTree = div;
128            }
129            addLevelInfo(!utils.isInterface(firstTypeElement) ? firstTypeElement : null,
130                    sset, isEnums, htmlTree);
131            if (configuration.allowTag(HtmlTag.SECTION)) {
132                div.addContent(htmlTree);
133            }
134        }
135    }
136
137    /**
138     * Add information regarding the classes which this class extends or
139     * implements.
140     *
141     * @param parent the parent class of the class being documented
142     * @param typeElement the TypeElement under consideration
143     * @param contentTree the content tree to which the information will be added
144     */
145    protected void addExtendsImplements(TypeElement parent, TypeElement typeElement,
146            Content contentTree) {
147        SortedSet<TypeElement> interfaces = new TreeSet<>(utils.makeGeneralPurposeComparator());
148        typeElement.getInterfaces().stream().forEach((t) -> {
149            interfaces.add(utils.asTypeElement(t));
150        });
151        if (interfaces.size() > (utils.isInterface(typeElement) ? 1 : 0)) {
152            boolean isFirst = true;
153            for (TypeElement intf : interfaces) {
154                if (parent != intf) {
155                    if (utils.isPublic(intf) || utils.isLinkable(intf)) {
156                        if (isFirst) {
157                            isFirst = false;
158                            if (utils.isInterface(typeElement)) {
159                                contentTree.addContent(" (");
160                                contentTree.addContent(contents.also);
161                                contentTree.addContent(" extends ");
162                            } else {
163                                contentTree.addContent(" (implements ");
164                            }
165                        } else {
166                            contentTree.addContent(", ");
167                        }
168                        addPreQualifiedClassLink(LinkInfoImpl.Kind.TREE, intf, contentTree);
169                    }
170                }
171            }
172            if (!isFirst) {
173                contentTree.addContent(")");
174            }
175        }
176    }
177
178    /**
179     * Add information about the class kind, if it's a "class" or "interface".
180     *
181     * @param typeElement the class being documented
182     * @param contentTree the content tree to which the information will be added
183     */
184    protected void addPartialInfo(TypeElement typeElement, Content contentTree) {
185        addPreQualifiedStrongClassLink(LinkInfoImpl.Kind.TREE, typeElement, contentTree);
186    }
187
188    /**
189     * Get the tree label for the navigation bar.
190     *
191     * @return a content tree for the tree label
192     */
193    protected Content getNavLinkTree() {
194        Content li = HtmlTree.LI(HtmlStyle.navBarCell1Rev, contents.treeLabel);
195        return li;
196    }
197}
198