src/share/classes/com/sun/tools/doclets/formats/html/LinkFactoryImpl.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.doclets.formats.html;
aoqi@0 27
aoqi@0 28 import java.util.List;
aoqi@0 29
aoqi@0 30 import com.sun.javadoc.*;
aoqi@0 31 import com.sun.tools.doclets.formats.html.markup.ContentBuilder;
aoqi@0 32 import com.sun.tools.doclets.formats.html.markup.RawHtml;
aoqi@0 33 import com.sun.tools.doclets.formats.html.markup.StringContent;
aoqi@0 34 import com.sun.tools.doclets.internal.toolkit.*;
aoqi@0 35 import com.sun.tools.doclets.internal.toolkit.util.*;
aoqi@0 36 import com.sun.tools.doclets.internal.toolkit.util.links.*;
aoqi@0 37
aoqi@0 38 /**
aoqi@0 39 * A factory that returns a link given the information about it.
aoqi@0 40 *
aoqi@0 41 * <p><b>This is NOT part of any supported API.
aoqi@0 42 * If you write code that depends on this, you do so at your own risk.
aoqi@0 43 * This code and its internal interfaces are subject to change or
aoqi@0 44 * deletion without notice.</b>
aoqi@0 45 *
aoqi@0 46 * @author Jamie Ho
aoqi@0 47 * @since 1.5
aoqi@0 48 */
aoqi@0 49 public class LinkFactoryImpl extends LinkFactory {
aoqi@0 50
aoqi@0 51 private HtmlDocletWriter m_writer;
aoqi@0 52
aoqi@0 53 public LinkFactoryImpl(HtmlDocletWriter writer) {
aoqi@0 54 m_writer = writer;
aoqi@0 55 }
aoqi@0 56
aoqi@0 57 /**
aoqi@0 58 * {@inheritDoc}
aoqi@0 59 */
aoqi@0 60 protected Content newContent() {
aoqi@0 61 return new ContentBuilder();
aoqi@0 62 }
aoqi@0 63
aoqi@0 64 /**
aoqi@0 65 * {@inheritDoc}
aoqi@0 66 */
aoqi@0 67 protected Content getClassLink(LinkInfo linkInfo) {
aoqi@0 68 LinkInfoImpl classLinkInfo = (LinkInfoImpl) linkInfo;
aoqi@0 69 boolean noLabel = linkInfo.label == null || linkInfo.label.isEmpty();
aoqi@0 70 ClassDoc classDoc = classLinkInfo.classDoc;
aoqi@0 71 //Create a tool tip if we are linking to a class or interface. Don't
aoqi@0 72 //create one if we are linking to a member.
aoqi@0 73 String title =
aoqi@0 74 (classLinkInfo.where == null || classLinkInfo.where.length() == 0) ?
aoqi@0 75 getClassToolTip(classDoc,
aoqi@0 76 classLinkInfo.type != null &&
aoqi@0 77 !classDoc.qualifiedTypeName().equals(classLinkInfo.type.qualifiedTypeName())) :
aoqi@0 78 "";
aoqi@0 79 Content label = classLinkInfo.getClassLinkLabel(m_writer.configuration);
aoqi@0 80 Configuration configuration = m_writer.configuration;
aoqi@0 81 Content link = new ContentBuilder();
aoqi@0 82 if (classDoc.isIncluded()) {
aoqi@0 83 if (configuration.isGeneratedDoc(classDoc)) {
aoqi@0 84 DocPath filename = getPath(classLinkInfo);
aoqi@0 85 if (linkInfo.linkToSelf ||
aoqi@0 86 !(DocPath.forName(classDoc)).equals(m_writer.filename)) {
aoqi@0 87 link.addContent(m_writer.getHyperLink(
aoqi@0 88 filename.fragment(classLinkInfo.where),
aoqi@0 89 label,
aoqi@0 90 classLinkInfo.isStrong, classLinkInfo.styleName,
aoqi@0 91 title, classLinkInfo.target));
aoqi@0 92 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
aoqi@0 93 link.addContent(getTypeParameterLinks(linkInfo));
aoqi@0 94 }
aoqi@0 95 return link;
aoqi@0 96 }
aoqi@0 97 }
aoqi@0 98 } else {
aoqi@0 99 Content crossLink = m_writer.getCrossClassLink(
aoqi@0 100 classDoc.qualifiedName(), classLinkInfo.where,
aoqi@0 101 label, classLinkInfo.isStrong, classLinkInfo.styleName,
aoqi@0 102 true);
aoqi@0 103 if (crossLink != null) {
aoqi@0 104 link.addContent(crossLink);
aoqi@0 105 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
aoqi@0 106 link.addContent(getTypeParameterLinks(linkInfo));
aoqi@0 107 }
aoqi@0 108 return link;
aoqi@0 109 }
aoqi@0 110 }
aoqi@0 111 // Can't link so just write label.
aoqi@0 112 link.addContent(label);
aoqi@0 113 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
aoqi@0 114 link.addContent(getTypeParameterLinks(linkInfo));
aoqi@0 115 }
aoqi@0 116 return link;
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 /**
aoqi@0 120 * {@inheritDoc}
aoqi@0 121 */
aoqi@0 122 protected Content getTypeParameterLink(LinkInfo linkInfo,
aoqi@0 123 Type typeParam) {
aoqi@0 124 LinkInfoImpl typeLinkInfo = new LinkInfoImpl(m_writer.configuration,
aoqi@0 125 ((LinkInfoImpl) linkInfo).getContext(), typeParam);
aoqi@0 126 typeLinkInfo.excludeTypeBounds = linkInfo.excludeTypeBounds;
aoqi@0 127 typeLinkInfo.excludeTypeParameterLinks = linkInfo.excludeTypeParameterLinks;
aoqi@0 128 typeLinkInfo.linkToSelf = linkInfo.linkToSelf;
aoqi@0 129 typeLinkInfo.isJava5DeclarationLocation = false;
aoqi@0 130 return getLink(typeLinkInfo);
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 protected Content getTypeAnnotationLink(LinkInfo linkInfo,
aoqi@0 134 AnnotationDesc annotation) {
aoqi@0 135 throw new RuntimeException("Not implemented yet!");
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 public Content getTypeAnnotationLinks(LinkInfo linkInfo) {
aoqi@0 139 ContentBuilder links = new ContentBuilder();
aoqi@0 140 AnnotationDesc[] annotations;
aoqi@0 141 if (linkInfo.type instanceof AnnotatedType) {
aoqi@0 142 annotations = linkInfo.type.asAnnotatedType().annotations();
aoqi@0 143 } else if (linkInfo.type instanceof TypeVariable) {
aoqi@0 144 annotations = linkInfo.type.asTypeVariable().annotations();
aoqi@0 145 } else {
aoqi@0 146 return links;
aoqi@0 147 }
aoqi@0 148
aoqi@0 149 if (annotations.length == 0)
aoqi@0 150 return links;
aoqi@0 151
aoqi@0 152 List<Content> annos = m_writer.getAnnotations(0, annotations, false, linkInfo.isJava5DeclarationLocation);
aoqi@0 153
aoqi@0 154 boolean isFirst = true;
aoqi@0 155 for (Content anno : annos) {
aoqi@0 156 if (!isFirst) {
aoqi@0 157 links.addContent(" ");
aoqi@0 158 }
aoqi@0 159 links.addContent(anno);
aoqi@0 160 isFirst = false;
aoqi@0 161 }
aoqi@0 162 if (!annos.isEmpty()) {
aoqi@0 163 links.addContent(" ");
aoqi@0 164 }
aoqi@0 165
aoqi@0 166 return links;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 /**
aoqi@0 170 * Given a class, return the appropriate tool tip.
aoqi@0 171 *
aoqi@0 172 * @param classDoc the class to get the tool tip for.
aoqi@0 173 * @return the tool tip for the appropriate class.
aoqi@0 174 */
aoqi@0 175 private String getClassToolTip(ClassDoc classDoc, boolean isTypeLink) {
aoqi@0 176 Configuration configuration = m_writer.configuration;
aoqi@0 177 if (isTypeLink) {
aoqi@0 178 return configuration.getText("doclet.Href_Type_Param_Title",
aoqi@0 179 classDoc.name());
aoqi@0 180 } else if (classDoc.isInterface()){
aoqi@0 181 return configuration.getText("doclet.Href_Interface_Title",
aoqi@0 182 Util.getPackageName(classDoc.containingPackage()));
aoqi@0 183 } else if (classDoc.isAnnotationType()) {
aoqi@0 184 return configuration.getText("doclet.Href_Annotation_Title",
aoqi@0 185 Util.getPackageName(classDoc.containingPackage()));
aoqi@0 186 } else if (classDoc.isEnum()) {
aoqi@0 187 return configuration.getText("doclet.Href_Enum_Title",
aoqi@0 188 Util.getPackageName(classDoc.containingPackage()));
aoqi@0 189 } else {
aoqi@0 190 return configuration.getText("doclet.Href_Class_Title",
aoqi@0 191 Util.getPackageName(classDoc.containingPackage()));
aoqi@0 192 }
aoqi@0 193 }
aoqi@0 194
aoqi@0 195 /**
aoqi@0 196 * Return path to the given file name in the given package. So if the name
aoqi@0 197 * passed is "Object.html" and the name of the package is "java.lang", and
aoqi@0 198 * if the relative path is "../.." then returned string will be
aoqi@0 199 * "../../java/lang/Object.html"
aoqi@0 200 *
aoqi@0 201 * @param linkInfo the information about the link.
aoqi@0 202 */
aoqi@0 203 private DocPath getPath(LinkInfoImpl linkInfo) {
aoqi@0 204 if (linkInfo.context == LinkInfoImpl.Kind.PACKAGE_FRAME) {
aoqi@0 205 //Not really necessary to do this but we want to be consistent
aoqi@0 206 //with 1.4.2 output.
aoqi@0 207 return DocPath.forName(linkInfo.classDoc);
aoqi@0 208 }
aoqi@0 209 return m_writer.pathToRoot.resolve(DocPath.forClass(linkInfo.classDoc));
aoqi@0 210 }
aoqi@0 211 }

mercurial