Tue, 14 May 2013 10:14:51 -0700
8010440: Replace int constants in LinkInfoImpl with enum
Reviewed-by: bpatel, darcy
1 /*
2 * Copyright (c) 2003, 2013, 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 */
26 package com.sun.tools.doclets.formats.html;
28 import java.util.List;
30 import com.sun.javadoc.*;
31 import com.sun.tools.doclets.internal.toolkit.*;
32 import com.sun.tools.doclets.internal.toolkit.util.*;
33 import com.sun.tools.doclets.internal.toolkit.util.links.*;
35 /**
36 * A factory that returns a link given the information about it.
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 * @since 1.5
45 */
46 public class LinkFactoryImpl extends LinkFactory {
48 private HtmlDocletWriter m_writer;
50 public LinkFactoryImpl(HtmlDocletWriter writer) {
51 m_writer = writer;
52 }
54 /**
55 * {@inheritDoc}
56 */
57 protected LinkOutput getOutputInstance() {
58 return new LinkOutputImpl();
59 }
61 /**
62 * {@inheritDoc}
63 */
64 protected LinkOutput getClassLink(LinkInfo linkInfo) {
65 LinkInfoImpl classLinkInfo = (LinkInfoImpl) linkInfo;
66 boolean noLabel = linkInfo.label == null || linkInfo.label.length() == 0;
67 ClassDoc classDoc = classLinkInfo.classDoc;
68 //Create a tool tip if we are linking to a class or interface. Don't
69 //create one if we are linking to a member.
70 String title =
71 (classLinkInfo.where == null || classLinkInfo.where.length() == 0) ?
72 getClassToolTip(classDoc,
73 classLinkInfo.type != null &&
74 !classDoc.qualifiedTypeName().equals(classLinkInfo.type.qualifiedTypeName())) :
75 "";
76 StringBuilder label = new StringBuilder(
77 classLinkInfo.getClassLinkLabel(m_writer.configuration));
78 classLinkInfo.displayLength += label.length();
79 Configuration configuration = m_writer.configuration;
80 LinkOutputImpl linkOutput = new LinkOutputImpl();
81 if (classDoc.isIncluded()) {
82 if (configuration.isGeneratedDoc(classDoc)) {
83 DocPath filename = getPath(classLinkInfo);
84 if (linkInfo.linkToSelf ||
85 !(DocPath.forName(classDoc)).equals(m_writer.filename)) {
86 linkOutput.append(m_writer.getHyperLinkString(
87 filename.fragment(classLinkInfo.where),
88 label.toString(),
89 classLinkInfo.isStrong, classLinkInfo.styleName,
90 title, classLinkInfo.target));
91 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
92 linkOutput.append(getTypeParameterLinks(linkInfo).toString());
93 }
94 return linkOutput;
95 }
96 }
97 } else {
98 String crossLink = m_writer.getCrossClassLink(
99 classDoc.qualifiedName(), classLinkInfo.where,
100 label.toString(), classLinkInfo.isStrong, classLinkInfo.styleName,
101 true);
102 if (crossLink != null) {
103 linkOutput.append(crossLink);
104 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
105 linkOutput.append(getTypeParameterLinks(linkInfo).toString());
106 }
107 return linkOutput;
108 }
109 }
110 // Can't link so just write label.
111 linkOutput.append(label.toString());
112 if (noLabel && !classLinkInfo.excludeTypeParameterLinks) {
113 linkOutput.append(getTypeParameterLinks(linkInfo).toString());
114 }
115 return linkOutput;
116 }
118 /**
119 * {@inheritDoc}
120 */
121 protected LinkOutput getTypeParameterLink(LinkInfo linkInfo,
122 Type typeParam) {
123 LinkInfoImpl typeLinkInfo = new LinkInfoImpl(m_writer.configuration,
124 ((LinkInfoImpl) linkInfo).getContext(), typeParam);
125 typeLinkInfo.excludeTypeBounds = linkInfo.excludeTypeBounds;
126 typeLinkInfo.excludeTypeParameterLinks = linkInfo.excludeTypeParameterLinks;
127 typeLinkInfo.linkToSelf = linkInfo.linkToSelf;
128 typeLinkInfo.isJava5DeclarationLocation = false;
129 LinkOutput output = getLinkOutput(typeLinkInfo);
130 ((LinkInfoImpl) linkInfo).displayLength += typeLinkInfo.displayLength;
131 return output;
132 }
134 protected LinkOutput getTypeAnnotationLink(LinkInfo linkInfo,
135 AnnotationDesc annotation) {
136 throw new RuntimeException("Not implemented yet!");
137 }
139 public LinkOutput getTypeAnnotationLinks(LinkInfo linkInfo) {
140 LinkOutput output = getOutputInstance();
141 AnnotationDesc[] annotations;
142 if (linkInfo.type instanceof AnnotatedType) {
143 annotations = linkInfo.type.asAnnotatedType().annotations();
144 } else if (linkInfo.type instanceof TypeVariable) {
145 annotations = linkInfo.type.asTypeVariable().annotations();
146 } else {
147 return output;
148 }
150 if (annotations.length == 0)
151 return output;
153 List<String> annos = m_writer.getAnnotations(0, annotations, false, linkInfo.isJava5DeclarationLocation);
155 boolean isFirst = true;
156 for (String anno : annos) {
157 if (!isFirst) {
158 linkInfo.displayLength += 1;
159 output.append(" ");
160 }
161 output.append(anno);
162 isFirst = false;
163 }
164 if (!annos.isEmpty()) {
165 linkInfo.displayLength += 1;
166 output.append(" ");
167 }
169 return output;
170 }
172 /**
173 * Given a class, return the appropriate tool tip.
174 *
175 * @param classDoc the class to get the tool tip for.
176 * @return the tool tip for the appropriate class.
177 */
178 private String getClassToolTip(ClassDoc classDoc, boolean isTypeLink) {
179 Configuration configuration = m_writer.configuration;
180 if (isTypeLink) {
181 return configuration.getText("doclet.Href_Type_Param_Title",
182 classDoc.name());
183 } else if (classDoc.isInterface()){
184 return configuration.getText("doclet.Href_Interface_Title",
185 Util.getPackageName(classDoc.containingPackage()));
186 } else if (classDoc.isAnnotationType()) {
187 return configuration.getText("doclet.Href_Annotation_Title",
188 Util.getPackageName(classDoc.containingPackage()));
189 } else if (classDoc.isEnum()) {
190 return configuration.getText("doclet.Href_Enum_Title",
191 Util.getPackageName(classDoc.containingPackage()));
192 } else {
193 return configuration.getText("doclet.Href_Class_Title",
194 Util.getPackageName(classDoc.containingPackage()));
195 }
196 }
198 /**
199 * Return path to the given file name in the given package. So if the name
200 * passed is "Object.html" and the name of the package is "java.lang", and
201 * if the relative path is "../.." then returned string will be
202 * "../../java/lang/Object.html"
203 *
204 * @param linkInfo the information about the link.
205 */
206 private DocPath getPath(LinkInfoImpl linkInfo) {
207 if (linkInfo.context == LinkInfoImpl.Kind.PACKAGE_FRAME) {
208 //Not really necessary to do this but we want to be consistent
209 //with 1.4.2 output.
210 return DocPath.forName(linkInfo.classDoc);
211 }
212 return m_writer.pathToRoot.resolve(DocPath.forClass(linkInfo.classDoc));
213 }
214 }