1 /* 2 * Copyright (c) 2003, 2018, 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.formats.html; 27 28 import java.util.List; 29 30 import javax.lang.model.element.AnnotationMirror; 31 import javax.lang.model.element.Element; 32 import javax.lang.model.element.TypeElement; 33 import javax.lang.model.type.TypeMirror; 34 35 import jdk.javadoc.internal.doclets.formats.html.markup.ContentBuilder; 36 import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration; 37 import jdk.javadoc.internal.doclets.toolkit.Content; 38 import jdk.javadoc.internal.doclets.toolkit.util.DocPath; 39 import jdk.javadoc.internal.doclets.toolkit.util.Utils; 40 import jdk.javadoc.internal.doclets.toolkit.util.links.LinkFactory; 41 import jdk.javadoc.internal.doclets.toolkit.util.links.LinkInfo; 42 43 /** 44 * A factory that returns a link given the information about it. 45 * 46 * <p><b>This is NOT part of any supported API. 47 * If you write code that depends on this, you do so at your own risk. 48 * This code and its internal interfaces are subject to change or 49 * deletion without notice.</b> 50 * 51 * @author Jamie Ho 52 */ 53 public class LinkFactoryImpl extends LinkFactory { 54 55 private final HtmlDocletWriter m_writer; 56 57 public LinkFactoryImpl(HtmlDocletWriter writer) { 58 m_writer = writer; 59 } 60 61 /** 62 * {@inheritDoc} 63 */ 64 @Override 65 protected Content newContent() { 66 return new ContentBuilder(); 67 } 68 69 /** 70 * {@inheritDoc} 71 */ 72 @Override 73 protected Content getClassLink(LinkInfo linkInfo) { 74 BaseConfiguration configuration = m_writer.configuration; 75 Utils utils = configuration.utils; 76 LinkInfoImpl classLinkInfo = (LinkInfoImpl) linkInfo; 77 boolean noLabel = linkInfo.label == null || linkInfo.label.isEmpty(); 78 TypeElement typeElement = classLinkInfo.typeElement; 79 // Create a tool tip if we are linking to a class or interface. Don't 80 // create one if we are linking to a member. 81 String title = ""; 82 if (classLinkInfo.where == null || classLinkInfo.where.length() == 0) { 83 boolean isTypeLink = classLinkInfo.type != null && 84 utils.isTypeVariable(utils.getComponentType(classLinkInfo.type)); 85 title = getClassToolTip(typeElement, isTypeLink); 86 } 87 Content label = classLinkInfo.getClassLinkLabel(m_writer.configuration); 88 89 Content link = new ContentBuilder(); 90 if (utils.isIncluded(typeElement)) { 91 if (configuration.isGeneratedDoc(typeElement)) { 92 DocPath filename = getPath(classLinkInfo); 93 if (linkInfo.linkToSelf || 94 !(DocPath.forName(utils, typeElement)).equals(m_writer.filename)) { 95 link.addContent(m_writer.links.createLink( 96 filename.fragment(classLinkInfo.where), 97 label, 98 classLinkInfo.isStrong, 99 title, 100 classLinkInfo.target)); 101 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) { 102 link.addContent(getTypeParameterLinks(linkInfo)); 103 } 104 return link; 105 } 106 } 107 } else { 108 Content crossLink = m_writer.getCrossClassLink( 109 typeElement.getQualifiedName().toString(), classLinkInfo.where, 110 label, classLinkInfo.isStrong, true); 111 if (crossLink != null) { 112 link.addContent(crossLink); 113 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) { 114 link.addContent(getTypeParameterLinks(linkInfo)); 115 } 116 return link; 117 } 118 } 119 // Can't link so just write label. 120 link.addContent(label); 121 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) { 122 link.addContent(getTypeParameterLinks(linkInfo)); 123 } 124 return link; 125 } 126 127 /** 128 * {@inheritDoc} 129 */ 130 @Override 131 protected Content getTypeParameterLink(LinkInfo linkInfo, TypeMirror typeParam) { 132 LinkInfoImpl typeLinkInfo = new LinkInfoImpl(m_writer.configuration, 133 ((LinkInfoImpl) linkInfo).getContext(), typeParam); 134 typeLinkInfo.excludeTypeBounds = linkInfo.excludeTypeBounds; 135 typeLinkInfo.excludeTypeParameterLinks = linkInfo.excludeTypeParameterLinks; 136 typeLinkInfo.linkToSelf = linkInfo.linkToSelf; 137 typeLinkInfo.isJava5DeclarationLocation = false; 138 return getLink(typeLinkInfo); 139 } 140 141 @Override 142 public Content getTypeAnnotationLinks(LinkInfo linkInfo) { 143 Utils utils = ((LinkInfoImpl)linkInfo).utils; 144 ContentBuilder links = new ContentBuilder(); 145 List<? extends AnnotationMirror> annotations; 146 if (utils.isAnnotated(linkInfo.type)) { 147 annotations = linkInfo.type.getAnnotationMirrors(); 148 } else if (utils.isTypeVariable(linkInfo.type)) { 149 // TODO: use the context for now, and special case for Receiver_Types, 150 // which takes the default case. 151 switch (((LinkInfoImpl)linkInfo).context) { 152 case MEMBER_TYPE_PARAMS: 153 case EXECUTABLE_MEMBER_PARAM: 154 case CLASS_SIGNATURE: 155 Element element = utils.typeUtils.asElement(linkInfo.type); 156 annotations = element.getAnnotationMirrors(); 157 break; 158 default: 159 annotations = linkInfo.type.getAnnotationMirrors(); 160 break; 161 } 162 163 } else { 164 return links; 165 } 166 167 if (annotations.isEmpty()) 168 return links; 169 170 List<Content> annos = m_writer.getAnnotations(0, annotations, false, linkInfo.isJava5DeclarationLocation); 171 172 boolean isFirst = true; 173 for (Content anno : annos) { 174 if (!isFirst) { 175 links.addContent(" "); 176 } 177 links.addContent(anno); 178 isFirst = false; 179 } 180 if (!annos.isEmpty()) { 181 links.addContent(" "); 182 } 183 184 return links; 185 } 186 187 /** 188 * Given a class, return the appropriate tool tip. 189 * 190 * @param typeElement the class to get the tool tip for. 191 * @return the tool tip for the appropriate class. 192 */ 193 private String getClassToolTip(TypeElement typeElement, boolean isTypeLink) { 194 BaseConfiguration configuration = m_writer.configuration; 195 Utils utils = configuration.utils; 196 if (isTypeLink) { 197 return configuration.getText("doclet.Href_Type_Param_Title", 198 utils.getSimpleName(typeElement)); 199 } else if (utils.isInterface(typeElement)){ 200 return configuration.getText("doclet.Href_Interface_Title", 201 utils.getPackageName(utils.containingPackage(typeElement))); 202 } else if (utils.isAnnotationType(typeElement)) { 203 return configuration.getText("doclet.Href_Annotation_Title", 204 utils.getPackageName(utils.containingPackage(typeElement))); 205 } else if (utils.isEnum(typeElement)) { 206 return configuration.getText("doclet.Href_Enum_Title", 207 utils.getPackageName(utils.containingPackage(typeElement))); 208 } else { 209 return configuration.getText("doclet.Href_Class_Title", 210 utils.getPackageName(utils.containingPackage(typeElement))); 211 } 212 } 213 214 /** 215 * Return path to the given file name in the given package. So if the name 216 * passed is "Object.html" and the name of the package is "java.lang", and 217 * if the relative path is "../.." then returned string will be 218 * "../../java/lang/Object.html" 219 * 220 * @param linkInfo the information about the link. 221 */ 222 private DocPath getPath(LinkInfoImpl linkInfo) { 223 if (linkInfo.context == LinkInfoImpl.Kind.PACKAGE_FRAME) { 224 //Not really necessary to do this but we want to be consistent 225 //with 1.4.2 output. 226 return DocPath.forName(linkInfo.utils, linkInfo.typeElement); 227 } 228 return m_writer.pathToRoot.resolve(DocPath.forClass(linkInfo.utils, linkInfo.typeElement)); 229 } 230 }