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