1 /*
2  * Copyright (c) 2017, 2020, 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 build.tools.taglet;
27 
28 import java.util.EnumSet;
29 import java.util.List;
30 import java.util.Set;
31 import javax.lang.model.element.Element;
32 import javax.lang.model.element.ModuleElement;
33 import com.sun.source.doctree.DocTree;
34 import jdk.javadoc.doclet.Taglet;
35 import static jdk.javadoc.doclet.Taglet.Location.*;
36 
37 /**
38  * A block tag to optionally insert a reference to a module graph.
39  */
40 public class ModuleGraph implements Taglet {
41     private static final boolean enableModuleGraph =
42         Boolean.getBoolean("enableModuleGraph");
43 
44     /** Returns the set of locations in which a taglet may be used. */
45     @Override
getAllowedLocations()46     public Set<Location> getAllowedLocations() {
47         return EnumSet.of(MODULE);
48     }
49 
50     @Override
isInlineTag()51     public boolean isInlineTag() {
52         return false;
53     }
54 
55     @Override
getName()56     public String getName() {
57         return "moduleGraph";
58     }
59 
60     @Override
toString(List<? extends DocTree> tags, Element element)61     public String toString(List<? extends DocTree> tags, Element element) {
62         if (!enableModuleGraph) {
63             return "";
64         }
65 
66         String moduleName = ((ModuleElement) element).getQualifiedName().toString();
67         String imageFile = "module-graph.svg";
68         int thumbnailHeight = -1;
69         String hoverImage = "";
70         if (!moduleName.equals("java.base")) {
71             thumbnailHeight = 100; // also appears in the stylesheet
72             hoverImage = "<span>"
73                 + getImage(moduleName, imageFile, -1, true)
74                 + "</span>";
75         }
76         return "<dt>Module Graph:</dt>"
77             + "<dd>"
78             + "<a class=\"module-graph\" href=\"" + imageFile + "\">"
79             + getImage(moduleName, imageFile, thumbnailHeight, false)
80             + hoverImage
81             + "</a>"
82             + "</dd>";
83     }
84 
85     private static final String VERTICAL_ALIGN = "vertical-align:top";
86     private static final String BORDER = "border: solid lightgray 1px;";
87 
getImage(String moduleName, String file, int height, boolean useBorder)88     private String getImage(String moduleName, String file, int height, boolean useBorder) {
89         return String.format("<img style=\"%s\" alt=\"Module graph for %s\" src=\"%s\"%s>",
90                              useBorder ? BORDER + " " + VERTICAL_ALIGN : VERTICAL_ALIGN,
91                              moduleName,
92                              file,
93                              (height <= 0 ? "" : " height=\"" + height + "\""));
94     }
95 }
96