1 /*
   2  * Copyright (c) 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 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
  46     public Set<Location> getAllowedLocations() {
  47         return EnumSet.of(MODULE);
  48     }
  49 
  50     @Override
  51     public boolean isInlineTag() {
  52         return false;
  53     }
  54 
  55     @Override
  56     public String getName() {
  57         return "moduleGraph";
  58     }
  59 
  60     @Override
  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 = moduleName + "/module-graph.png";
  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>"
  77             + "<span class=\"simpleTagLabel\">Module Graph:</span>\n"
  78             + "</dt>"
  79             + "<dd>"
  80             + "<a class=moduleGraph href=\"" + imageFile + "\">"
  81             + getImage(moduleName, imageFile, thumbnailHeight, false)
  82             + hoverImage
  83             + "</a>"
  84             + "</dd>";
  85     }
  86 
  87     private static final String VERTICAL_ALIGN = "vertical-align:top";
  88     private static final String BORDER = "border: solid lightgray 1px;";
  89 
  90     private String getImage(String moduleName, String file, int height, boolean useBorder) {
  91         return String.format("<img style=\"%s\" alt=\"Module graph for %s\" src=\"%s\"%s>",
  92                              useBorder ? BORDER + " " + VERTICAL_ALIGN : VERTICAL_ALIGN,
  93                              moduleName,
  94                              file,
  95                              (height <= 0 ? "" : " height=\"" + height + "\""));
  96     }
  97 }