src/share/classes/com/sun/tools/doclets/internal/toolkit/util/links/LinkFactory.java

Tue, 14 May 2013 10:14:52 -0700

author
jjg
date
Tue, 14 May 2013 10:14:52 -0700
changeset 1737
7a9ef837e57f
parent 1736
74cd21f2c2fe
child 1741
4c43e51433ba
permissions
-rw-r--r--

8011650: reduce use of RawHtml nodes in doclet
Reviewed-by: darcy

duke@1 1 /*
jjg@1521 2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.doclets.internal.toolkit.util.links;
duke@1 27
duke@1 28 import com.sun.javadoc.*;
jjg@1736 29 import com.sun.tools.doclets.internal.toolkit.Content;
duke@1 30
duke@1 31 /**
duke@1 32 * A factory that constructs links from given link information.
duke@1 33 *
jjg@1359 34 * <p><b>This is NOT part of any supported API.
jjg@1359 35 * If you write code that depends on this, you do so at your own risk.
jjg@1359 36 * This code and its internal interfaces are subject to change or
jjg@1359 37 * deletion without notice.</b>
jjg@1359 38 *
duke@1 39 * @author Jamie Ho
duke@1 40 * @since 1.5
duke@1 41 */
duke@1 42 public abstract class LinkFactory {
duke@1 43
duke@1 44 /**
jjg@1736 45 * Return an empty instance of a content object.
duke@1 46 *
jjg@1736 47 * @return an empty instance of a content object.
duke@1 48 */
jjg@1736 49 protected abstract Content newContent();
duke@1 50
duke@1 51 /**
duke@1 52 * Constructs a link from the given link information.
duke@1 53 *
duke@1 54 * @param linkInfo the information about the link.
duke@1 55 * @return the output of the link.
duke@1 56 */
jjg@1736 57 public Content getLink(LinkInfo linkInfo) {
duke@1 58 if (linkInfo.type != null) {
duke@1 59 Type type = linkInfo.type;
jjg@1736 60 Content link = newContent();
duke@1 61 if (type.isPrimitive()) {
duke@1 62 //Just a primitive.
duke@1 63 linkInfo.displayLength += type.typeName().length();
jjg@1736 64 link.addContent(type.typeName());
bpatel@1691 65 } else if (type.asAnnotatedType() != null && type.dimension().length() == 0) {
jjg@1736 66 link.addContent(getTypeAnnotationLinks(linkInfo));
jjg@1521 67 linkInfo.type = type.asAnnotatedType().underlyingType();
jjg@1736 68 link.addContent(getLink(linkInfo));
jjg@1736 69 return link;
duke@1 70 } else if (type.asWildcardType() != null) {
duke@1 71 //Wildcard type.
duke@1 72 linkInfo.isTypeBound = true;
duke@1 73 linkInfo.displayLength += 1;
jjg@1736 74 link.addContent("?");
duke@1 75 WildcardType wildcardType = type.asWildcardType();
duke@1 76 Type[] extendsBounds = wildcardType.extendsBounds();
duke@1 77 for (int i = 0; i < extendsBounds.length; i++) {
duke@1 78 linkInfo.displayLength += i > 0 ? 2 : 9;
jjg@1736 79 link.addContent(i > 0 ? ", " : " extends ");
duke@1 80 setBoundsLinkInfo(linkInfo, extendsBounds[i]);
jjg@1736 81 link.addContent(getLink(linkInfo));
duke@1 82 }
duke@1 83 Type[] superBounds = wildcardType.superBounds();
duke@1 84 for (int i = 0; i < superBounds.length; i++) {
duke@1 85 linkInfo.displayLength += i > 0 ? 2 : 7;
jjg@1736 86 link.addContent(i > 0 ? ", " : " super ");
duke@1 87 setBoundsLinkInfo(linkInfo, superBounds[i]);
jjg@1736 88 link.addContent(getLink(linkInfo));
duke@1 89 }
duke@1 90 } else if (type.asTypeVariable()!= null) {
jjg@1736 91 link.addContent(getTypeAnnotationLinks(linkInfo));
duke@1 92 linkInfo.isTypeBound = true;
duke@1 93 //A type variable.
duke@1 94 Doc owner = type.asTypeVariable().owner();
duke@1 95 if ((! linkInfo.excludeTypeParameterLinks) &&
duke@1 96 owner instanceof ClassDoc) {
duke@1 97 linkInfo.classDoc = (ClassDoc) owner;
jjg@1737 98 Content label = newContent();
jjg@1737 99 label.addContent(type.typeName());
jjg@1737 100 linkInfo.label = label;
jjg@1736 101 link.addContent(getClassLink(linkInfo));
duke@1 102 } else {
duke@1 103 //No need to link method type parameters.
duke@1 104 linkInfo.displayLength += type.typeName().length();
jjg@1736 105 link.addContent(type.typeName());
duke@1 106 }
duke@1 107
duke@1 108 Type[] bounds = type.asTypeVariable().bounds();
duke@1 109 if (! linkInfo.excludeTypeBounds) {
duke@1 110 linkInfo.excludeTypeBounds = true;
duke@1 111 for (int i = 0; i < bounds.length; i++) {
duke@1 112 linkInfo.displayLength += i > 0 ? 2 : 9;
jjg@1736 113 link.addContent(i > 0 ? " & " : " extends ");
duke@1 114 setBoundsLinkInfo(linkInfo, bounds[i]);
jjg@1736 115 link.addContent(getLink(linkInfo));
duke@1 116 }
duke@1 117 }
duke@1 118 } else if (type.asClassDoc() != null) {
duke@1 119 //A class type.
duke@1 120 if (linkInfo.isTypeBound &&
duke@1 121 linkInfo.excludeTypeBoundsLinks) {
duke@1 122 //Since we are excluding type parameter links, we should not
duke@1 123 //be linking to the type bound.
duke@1 124 linkInfo.displayLength += type.typeName().length();
jjg@1736 125 link.addContent(type.typeName());
jjg@1736 126 link.addContent(getTypeParameterLinks(linkInfo));
jjg@1736 127 return link;
duke@1 128 } else {
duke@1 129 linkInfo.classDoc = type.asClassDoc();
jjg@1736 130 link = newContent();
jjg@1736 131 link.addContent(getClassLink(linkInfo));
duke@1 132 if (linkInfo.includeTypeAsSepLink) {
jjg@1736 133 link.addContent(getTypeParameterLinks(linkInfo, false));
duke@1 134 }
duke@1 135 }
duke@1 136 }
duke@1 137
duke@1 138 if (linkInfo.isVarArg) {
duke@1 139 if (type.dimension().length() > 2) {
duke@1 140 //Javadoc returns var args as array.
duke@1 141 //Strip out the first [] from the var arg.
duke@1 142 linkInfo.displayLength += type.dimension().length()-2;
jjg@1736 143 link.addContent(type.dimension().substring(2));
duke@1 144 }
duke@1 145 linkInfo.displayLength += 3;
jjg@1736 146 link.addContent("...");
duke@1 147 } else {
bpatel@1691 148 while (type != null && type.dimension().length() > 0) {
bpatel@1691 149 linkInfo.displayLength += type.dimension().length();
bpatel@1691 150 if (type.asAnnotatedType() != null) {
bpatel@1691 151 linkInfo.type = type;
jjg@1736 152 link.addContent(" ");
jjg@1736 153 link.addContent(getTypeAnnotationLinks(linkInfo));
jjg@1736 154 link.addContent("[]");
bpatel@1691 155 type = type.asAnnotatedType().underlyingType().getElementType();
bpatel@1691 156 } else {
jjg@1736 157 link.addContent("[]");
bpatel@1691 158 type = type.getElementType();
bpatel@1691 159 }
bpatel@1691 160 }
bpatel@1691 161 linkInfo.type = type;
jjg@1736 162 Content newLink = newContent();
jjg@1736 163 newLink.addContent(getTypeAnnotationLinks(linkInfo));
jjg@1736 164 newLink.addContent(link);
jjg@1736 165 link = newLink;
duke@1 166 }
jjg@1736 167 return link;
duke@1 168 } else if (linkInfo.classDoc != null) {
duke@1 169 //Just a class link
jjg@1736 170 Content link = newContent();
jjg@1736 171 link.addContent(getClassLink(linkInfo));
duke@1 172 if (linkInfo.includeTypeAsSepLink) {
jjg@1736 173 link.addContent(getTypeParameterLinks(linkInfo, false));
duke@1 174 }
jjg@1736 175 return link;
duke@1 176 } else {
duke@1 177 return null;
duke@1 178 }
duke@1 179 }
duke@1 180
duke@1 181 private void setBoundsLinkInfo(LinkInfo linkInfo, Type bound) {
duke@1 182 linkInfo.classDoc = null;
duke@1 183 linkInfo.label = null;
duke@1 184 linkInfo.type = bound;
duke@1 185 }
duke@1 186
duke@1 187 /**
duke@1 188 * Return the link to the given class.
duke@1 189 *
duke@1 190 * @param linkInfo the information about the link to construct.
duke@1 191 *
duke@1 192 * @return the link for the given class.
duke@1 193 */
jjg@1736 194 protected abstract Content getClassLink(LinkInfo linkInfo);
duke@1 195
duke@1 196 /**
duke@1 197 * Return the link to the given type parameter.
duke@1 198 *
duke@1 199 * @param linkInfo the information about the link to construct.
duke@1 200 * @param typeParam the type parameter to link to.
duke@1 201 */
jjg@1736 202 protected abstract Content getTypeParameterLink(LinkInfo linkInfo,
duke@1 203 Type typeParam);
duke@1 204
jjg@1736 205 protected abstract Content getTypeAnnotationLink(LinkInfo linkInfo,
jjg@1521 206 AnnotationDesc annotation);
jjg@1521 207
duke@1 208 /**
duke@1 209 * Return the links to the type parameters.
duke@1 210 *
duke@1 211 * @param linkInfo the information about the link to construct.
duke@1 212 * @return the links to the type parameters.
duke@1 213 */
jjg@1736 214 public Content getTypeParameterLinks(LinkInfo linkInfo) {
duke@1 215 return getTypeParameterLinks(linkInfo, true);
duke@1 216 }
duke@1 217
duke@1 218 /**
duke@1 219 * Return the links to the type parameters.
duke@1 220 *
duke@1 221 * @param linkInfo the information about the link to construct.
duke@1 222 * @param isClassLabel true if this is a class label. False if it is
duke@1 223 * the type parameters portion of the link.
duke@1 224 * @return the links to the type parameters.
duke@1 225 */
jjg@1736 226 public Content getTypeParameterLinks(LinkInfo linkInfo, boolean isClassLabel) {
jjg@1736 227 Content links = newContent();
duke@1 228 Type[] vars;
duke@1 229 if (linkInfo.executableMemberDoc != null) {
duke@1 230 vars = linkInfo.executableMemberDoc.typeParameters();
duke@1 231 } else if (linkInfo.type != null &&
duke@1 232 linkInfo.type.asParameterizedType() != null){
duke@1 233 vars = linkInfo.type.asParameterizedType().typeArguments();
duke@1 234 } else if (linkInfo.classDoc != null){
duke@1 235 vars = linkInfo.classDoc.typeParameters();
duke@1 236 } else {
duke@1 237 //Nothing to document.
jjg@1736 238 return links;
duke@1 239 }
duke@1 240 if (((linkInfo.includeTypeInClassLinkLabel && isClassLabel) ||
duke@1 241 (linkInfo.includeTypeAsSepLink && ! isClassLabel)
duke@1 242 )
duke@1 243 && vars.length > 0) {
duke@1 244 linkInfo.displayLength += 1;
jjg@1736 245 links.addContent("<");
duke@1 246 for (int i = 0; i < vars.length; i++) {
duke@1 247 if (i > 0) {
duke@1 248 linkInfo.displayLength += 1;
jjg@1736 249 links.addContent(",");
duke@1 250 }
jjg@1736 251 links.addContent(getTypeParameterLink(linkInfo, vars[i]));
duke@1 252 }
duke@1 253 linkInfo.displayLength += 1;
jjg@1736 254 links.addContent(">");
duke@1 255 }
jjg@1736 256 return links;
duke@1 257 }
duke@1 258
jjg@1736 259 public Content getTypeAnnotationLinks(LinkInfo linkInfo) {
jjg@1736 260 Content links = newContent();
jjg@1521 261 if (linkInfo.type.asAnnotatedType() == null)
jjg@1736 262 return links;
jjg@1521 263 AnnotationDesc[] annotations = linkInfo.type.asAnnotatedType().annotations();
jjg@1521 264 for (int i = 0; i < annotations.length; i++) {
jjg@1521 265 if (i > 0) {
jjg@1521 266 linkInfo.displayLength += 1;
jjg@1736 267 links.addContent(" ");
jjg@1521 268 }
jjg@1736 269 links.addContent(getTypeAnnotationLink(linkInfo, annotations[i]));
jjg@1521 270 }
jjg@1521 271
jjg@1521 272 linkInfo.displayLength += 1;
jjg@1736 273 links.addContent(" ");
jjg@1736 274 return links;
duke@1 275 }
duke@1 276 }

mercurial