1 /*
   2  * Copyright (c) 2003, 2019, 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.toolkit.util.links;
  27 
  28 import javax.lang.model.element.ExecutableElement;
  29 import javax.lang.model.element.TypeElement;
  30 import javax.lang.model.type.TypeMirror;
  31 
  32 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
  33 import jdk.javadoc.internal.doclets.toolkit.Content;
  34 
  35 /**
  36  *  Encapsulates information about a link.
  37  *
  38  *  <p><b>This is NOT part of any supported API.
  39  *  If you write code that depends on this, you do so at your own risk.
  40  *  This code and its internal interfaces are subject to change or
  41  *  deletion without notice.</b>
  42  *
  43  *  @author Jamie Ho
  44  */
  45 public abstract class LinkInfo {
  46 
  47     /**
  48      * The class we want to link to.  Null if we are not linking
  49      * to a class.
  50      */
  51     public TypeElement typeElement;
  52 
  53     /**
  54      * The executable element we want to link to.  Null if we are not linking
  55      * to an executable element.
  56      */
  57     public ExecutableElement executableElement;
  58 
  59     /**
  60      * The Type we want to link to.  Null if we are not linking to a type.
  61      */
  62     public TypeMirror type;
  63 
  64     /**
  65      * True if this is a link to a VarArg.
  66      */
  67     public boolean isVarArg = false;
  68 
  69     /**
  70      * Set this to true to indicate that you are linking to a type parameter.
  71      */
  72     public boolean isTypeBound = false;
  73 
  74     /**
  75      * Whether the document element is in a Java 5 declaration
  76      * location or not.
  77      */
  78     public boolean isJava5DeclarationLocation = true;
  79 
  80     /**
  81      * The label for the link.
  82      */
  83     public Content label;
  84 
  85     /**
  86      * True if the link should be strong.
  87      */
  88     public boolean isStrong = false;
  89 
  90     /**
  91      * True if we should include the type in the link label.  False otherwise.
  92      */
  93     public boolean includeTypeInClassLinkLabel = true;
  94 
  95     /**
  96      * True if we should include the type as separate link.  False otherwise.
  97      */
  98     public boolean includeTypeAsSepLink = false;
  99 
 100     /**
 101      * True if we should exclude the type bounds for the type parameter.
 102      */
 103     public boolean excludeTypeBounds = false;
 104 
 105     /**
 106      * True if we should print the type parameters, but not link them.
 107      */
 108     public boolean excludeTypeParameterLinks = false;
 109 
 110     /**
 111      * True if we should print the type bounds, but not link them.
 112      */
 113     public boolean excludeTypeBoundsLinks = false;
 114 
 115     /**
 116      * By default, the link can be to the page it's already on.  However,
 117      * there are cases where we don't want this (e.g. heading of class page).
 118      */
 119     public boolean linkToSelf = true;
 120 
 121     /**
 122      * Return an empty instance of a content object.
 123      *
 124      * @return an empty instance of a content object.
 125      */
 126     protected abstract Content newContent();
 127 
 128     /**
 129      * Return true if this link is linkable and false if we can't link to the
 130      * desired place.
 131      *
 132      * @return true if this link is linkable and false if we can't link to the
 133      * desired place.
 134      */
 135     public abstract boolean isLinkable();
 136 
 137     /**
 138      * Return the label for this class link.
 139      *
 140      * @param configuration the current configuration of the doclet.
 141      * @return the label for this class link.
 142      */
 143     public Content getClassLinkLabel(BaseConfiguration configuration) {
 144         if (label != null && !label.isEmpty()) {
 145             return label;
 146         } else if (isLinkable()) {
 147             Content tlabel = newContent();
 148             tlabel.add(configuration.utils.getSimpleName(typeElement));
 149             return tlabel;
 150         } else {
 151             Content tlabel = newContent();
 152             tlabel.add(configuration.getClassName(typeElement));
 153             return tlabel;
 154         }
 155     }
 156 
 157     @Override
 158     public String toString() {
 159         return "LinkInfo{" + "typeElement=" + typeElement +
 160                 ", executableElement=" + executableElement +
 161                 ", type=" + type +
 162                 ", isVarArg=" + isVarArg +
 163                 ", isTypeBound=" + isTypeBound +
 164                 ", isJava5DeclarationLocation=" + isJava5DeclarationLocation +
 165                 ", label=" + label +
 166                 ", isStrong=" + isStrong +
 167                 ", includeTypeInClassLinkLabel=" + includeTypeInClassLinkLabel +
 168                 ", includeTypeAsSepLink=" + includeTypeAsSepLink +
 169                 ", excludeTypeBounds=" + excludeTypeBounds +
 170                 ", excludeTypeParameterLinks=" + excludeTypeParameterLinks +
 171                 ", excludeTypeBoundsLinks=" + excludeTypeBoundsLinks +
 172                 ", linkToSelf=" + linkToSelf + '}';
 173     }
 174 }